From 755a963fa399219a77bb0d91c97239e5769f2b16 Mon Sep 17 00:00:00 2001 From: gauthiier Date: Sun, 20 Mar 2022 12:13:36 +0100 Subject: [PATCH] haha --- .gitignore | 1 + src/voicemachine_configApp.cpp | 270 +++++++++++++++++++++++++++++++-- 2 files changed, 259 insertions(+), 12 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/src/voicemachine_configApp.cpp b/src/voicemachine_configApp.cpp index 8c20d91..87f7c74 100644 --- a/src/voicemachine_configApp.cpp +++ b/src/voicemachine_configApp.cpp @@ -1,34 +1,280 @@ #include "cinder/app/App.h" #include "cinder/app/RendererGl.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::app; using namespace std; -class voicemachine_configApp : public App { - public: - void setup() override; - void mouseDown( MouseEvent event ) override; - void update() override; - void draw() override; +int WINDOW_H = 1500; +float RATIO = 0.5625; + +float TEMP_MIN = 0.2; +float TEMP_MAX = 3.0; + + +/*- 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_voice, std::shared_ptr sender_machine, std::function on_error, std::atomic_bool* connected, std::vector* 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_voice; + std::shared_ptr _sender_machine; + std::vector* _fonts_char; + std::function _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 _fonts_string; + std::vector _fonts_char; + + std::shared_ptr _sender_voicemachine; + std::shared_ptr _sender_machinespeak; + std::atomic_bool _connected; + + std::map _map_voices; +}; + +void VoiceMachineClientApp::stage_config() { + try { + const JsonTree json(loadAsset("voice.config.json")); + + // RECEIVER PORT + int port = json["port_voicemachine"].getValue(); + std::string host = json["host_voicemachine"].getValue(); + _sender_voicemachine = std::shared_ptr(new Sender(port - 1, host, port)); + + port = json["port_machinespeak"].getValue(); + host = json["host_machinespeak"].getValue(); + _sender_machinespeak = std::shared_ptr(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(); + 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(); + 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() { - gl::clear( Color( 0, 0, 0 ) ); + for(const auto& [n, v]: _map_voices) + v->update(); } -CINDER_APP( voicemachine_configApp, RendererGl ) +void VoiceMachineClientApp::draw() +{ + gl::clear( Color( 0, 0, 0 ) ); +} + +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 )