This commit is contained in:
gauthiier 2022-03-20 12:13:36 +01:00
parent 64ba0a83e3
commit 755a963fa3
2 changed files with 259 additions and 12 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.DS_Store

View File

@ -1,34 +1,280 @@
#include "cinder/app/App.h" #include "cinder/app/App.h"
#include "cinder/app/RendererGl.h" #include "cinder/app/RendererGl.h"
#include "cinder/gl/gl.h" #include "cinder/gl/gl.h"
#include "cinder/osc/Osc.h"
#include "cinder/Json.h"
#include "cinder/Log.h"
#include "cinder/CinderImGui.h"
using namespace ci; using namespace ci;
using namespace ci::app; using namespace ci::app;
using namespace std; using namespace std;
class voicemachine_configApp : public App { int WINDOW_H = 1500;
public: float RATIO = 0.5625;
void setup() override;
void mouseDown( MouseEvent event ) override; float TEMP_MIN = 0.2;
void update() override; float TEMP_MAX = 3.0;
void draw() override;
/*- NET -*/
using Sender = osc::SenderUdp;
using protocol = asio::ip::udp;
struct VoiceData {
VoiceData(std::string name, std::string channel, std::string font, int size, ColorA color, ColorA background, float temp=0)
: _name(name),
_channel(channel),
_font_name(font),
_font_size(size),
_font_size_drag(size),
_color_sel(color),
_color(color),
_background_sel(background),
_background(background),
_temp_sel(temp),
_temp(temp)
{}
void setup(std::shared_ptr<Sender> sender_voice, std::shared_ptr<Sender> sender_machine, std::function<void( asio::error_code )> on_error, std::atomic_bool* connected, std::vector<const char*>* fonts_char)
{
_sender_voice = sender_voice;
_sender_machine = sender_machine;
_error = on_error;
_connected = connected;
_fonts_char = fonts_char;
int indx = 0;
for(auto& fn: *_fonts_char){
if(strcmp(fn, _font_name.c_str()) == 0){
_font_name_index = indx;
_font_name_sel_index = _font_name_index;
break;
}
indx++;
}
}
void update() {
ImGui::ScopedWindow window(_name.c_str());
ImGui::ColorEdit4( "Background Color", &_background_sel[0]);
ImGui::ColorEdit4( "Text Color", &_color_sel[0]);
ImGui::SliderInt("Text Size", &_font_size_drag, 10, 200 );
ImGui::Combo("Font", &_font_name_sel_index, _fonts_char->data(), _fonts_char->size());
ImGui::SliderFloat("Temperature", &_temp_sel, TEMP_MIN, TEMP_MAX );
if (ImGui::Button("Save")){
osc::Message m("/command");
m.append("save");
_sender_voice->send(m, _error);
}
if(_background_sel != _background && _connected){
_background = _background_sel;
osc::Message m(_channel + "/background");
m.append(_background.a);
m.append(_background.r);
m.append(_background.g);
m.append(_background.b);
_sender_voice->send(m, _error);
console() << _name << " change background" << endl;
console() << _color.r << ", " << _color.g << ", " << _color.b << ", " << _color.a << endl;
}
if(_color_sel != _color && _connected){
_color = _color_sel;
osc::Message m(_channel + "/font/color");
m.append(_color.a);
m.append(_color.r);
m.append(_color.g);
m.append(_color.b);
_sender_voice->send(m, _error);
console() << _name << " change color" << endl;
console() << _color.r << ", " << _color.g << ", " << _color.b << ", " << _color.a << endl;
}
if(_font_size_drag != _font_size){
_font_size = _font_size_drag;
osc::Message m(_channel + "/font/size");
m.append(_font_size);
_sender_voice->send(m, _error);
}
if(_font_name_sel_index != _font_name_index){
_font_name_index = _font_name_sel_index;
osc::Message m(_channel + "/font/type");
m.append(_fonts_char->at(_font_name_index));
_sender_voice->send(m, _error);
}
if(_temp_sel != _temp){
_temp = _temp_sel;
osc::Message m("/temperature");
m.append(_temp);
m.append(_name);
_sender_machine->send(m, _error);
}
}
std::string _name;
std::string _channel;
// FONT
std::string _font_name;
int _font_name_index;
int _font_name_sel_index;
// FONT SIZE
int _font_size;
int _font_size_drag;
// COLOR
ColorA _color_sel;
ColorA _color;
// background
ColorA _background_sel;
ColorA _background;
//voice temperature
float _temp_sel;
float _temp;
std::atomic_bool* _connected;
std::shared_ptr<Sender> _sender_voice;
std::shared_ptr<Sender> _sender_machine;
std::vector<const char*>* _fonts_char;
std::function<void( asio::error_code )> _error;
}; };
void voicemachine_configApp::setup()
class VoiceMachineClientApp : public App {
public:
void setup() override;
void update() override;
void draw() override;
void stage_config();
void onSendError( asio::error_code error );
std::vector<std::string> _fonts_string;
std::vector<const char*> _fonts_char;
std::shared_ptr<Sender> _sender_voicemachine;
std::shared_ptr<Sender> _sender_machinespeak;
std::atomic_bool _connected;
std::map<std::string, VoiceData*> _map_voices;
};
void VoiceMachineClientApp::stage_config()
{ {
try {
const JsonTree json(loadAsset("voice.config.json"));
// RECEIVER PORT
int port = json["port_voicemachine"].getValue<int>();
std::string host = json["host_voicemachine"].getValue();
_sender_voicemachine = std::shared_ptr<Sender>(new Sender(port - 1, host, port));
port = json["port_machinespeak"].getValue<int>();
host = json["host_machinespeak"].getValue();
_sender_machinespeak = std::shared_ptr<Sender>(new Sender(port + 1, host, port));
//VOICES
for(auto &voice: json["voices"].getChildren()){
std::string name = voice["name"].getValue();
std::string channel = voice["osc_channel"]["root"].getValue();
std::string font = voice["font"].getValue();
int size = voice["size"].getValue<int>();
std::string hexcolor = voice["color"].getValue();
ColorA color = ColorA::hexA(std::stoul(hexcolor, nullptr, 16));
std::string hexbackground = voice["background"].getValue();
ColorA background = ColorA::hexA(std::stoul(hexbackground, nullptr, 16));
float temp = voice["model"]["temperature"].getValue<float>();
VoiceData* v = new VoiceData(name, channel, font, size, color, background, temp);
// VoiceData* v = new VoiceData(name, channel, font, size, color, background);
_map_voices[name] = v;
}
} catch (ci::Exception &exc) {
CI_LOG_D("Failed to stage config: " << exc.what());
}
} }
void voicemachine_configApp::mouseDown( MouseEvent event ) void VoiceMachineClientApp::onSendError( asio::error_code e )
{ {
if(e){
CI_LOG_E("Error sending: " << e.message() << " - " << e.value());
_connected = false;
try {
_sender_voicemachine->close();
_sender_machinespeak->close();
} catch (const osc::Exception &e) {
CI_LOG_EXCEPTION("Error sending, close: " << e.value(), e)
}
quit();
}
} }
void voicemachine_configApp::update()
void VoiceMachineClientApp::setup()
{ {
stage_config();
try {
_sender_voicemachine->bind();
_sender_machinespeak->bind();
} catch (const osc::Exception &e) {
CI_LOG_E("Error sender bind: " << e.what() << " - " << e.value());
quit();
}
_connected = true;
_fonts_string = Font::getNames();
std::transform(std::begin(_fonts_string), std::end(_fonts_string), std::back_inserter(_fonts_char), [](std::string &str) { return str.c_str(); });
for (auto& f : _fonts_char)
{
std::cout << ">" << f << "<" << std::endl;
}
for(const auto& [n, v]: _map_voices)
v->setup(_sender_voicemachine,_sender_machinespeak, std::bind(&VoiceMachineClientApp::onSendError, this, std::placeholders::_1), &_connected, &_fonts_char);
ImGui::Initialize();
} }
void voicemachine_configApp::draw() void VoiceMachineClientApp::update()
{
for(const auto& [n, v]: _map_voices)
v->update();
}
void VoiceMachineClientApp::draw()
{ {
gl::clear( Color( 0, 0, 0 ) ); gl::clear( Color( 0, 0, 0 ) );
} }
CINDER_APP( voicemachine_configApp, RendererGl ) auto settingsFunc = [](App::Settings *settings) {
settings->setMultiTouchEnabled( false );
settings->setFrameRate(25);
settings->setWindowSize(int(WINDOW_H * RATIO), WINDOW_H );
// settings->setFullScreen();
//FullScreenOptions& exclusive( bool enable = true )
};
CINDER_APP( VoiceMachineClientApp, RendererGl, settingsFunc )