calculate, temp, etc.

This commit is contained in:
NATURESPEAK 2022-04-04 09:14:43 +02:00
parent 554b10a8f7
commit b4c923408e
3 changed files with 121 additions and 65 deletions

View File

@ -4,6 +4,7 @@ void Voice::setup(Receiver &receiver)
{
_font = Font(_font_name, _size);
_tex = gl::TextureFont::create(_font);
_tex_name = gl::TextureFont::create(_font);
/*- NET -*/
receiver.setListener(_channel + "/utterance",
@ -16,6 +17,30 @@ void Voice::setup(Receiver &receiver)
}
});
receiver.setListener(_channel + "/calculate",
[&](const osc::Message &m){
std::lock_guard<std::mutex> lock(_utterance_mutex);
std::string u = m[0].string();
vec2 v = _tex->measureStringWrapped(u, _bounds);
int h = _bounds.getHeight();
if(v.y > h) {
console() << "Texture too large for screen..." << endl;
} else {
_offset.y = (h - v.y) / 2;
console() << "Texture offset = " << _offset.y << endl;
}
});
receiver.setListener(_channel + "/temperature",
[&](const osc::Message &m){
std::lock_guard<std::mutex> lock(_temperatue_mutex);
float t = m[0].flt();
if(_temperature != t) _temperature = t;
});
receiver.setListener(_channel + "/font/color",
[&](const osc::Message &m){
std::lock_guard<std::mutex> lock(_color_mutex);
@ -80,39 +105,52 @@ void Voice::update()
// need to check if the font name is valide... bof...
_font = Font(_font_name, _size);
_tex = gl::TextureFont::create(_font);
_tex_name = gl::TextureFont::create(_font);
_update_font = false;
}
}
void Voice::draw(Rectf &bounds)
void Voice::draw(Rectf &bounds, bool name, bool debug)
{
// gl::color(_color);
vec2 offset = vec2(0);
_tex->drawStringWrapped(_utterance, bounds + offset);
if(name && debug) {
_tex_name->drawStringWrapped(_name + " " + std::to_string(_temperature), bounds);
}
else if(name) _tex_name->drawStringWrapped(_name, bounds);
_tex->drawStringWrapped(_utterance, bounds + _offset);
if(debug) {
gl::color(Color(0, 0, 1));
Rectf b = _bounds;
b.y1 += _offset.y;
b.y2 -= _offset.y;
gl::drawStrokedRect(b);
}
}
void VoiceAloud::setup()
{
auto ctx = audio::master();
mFilter = ctx->makeNode( new audio::FilterLowPassNode );
mGen = ctx->makeNode( new audio::GenSineNode );
mGain = ctx->makeNode( new audio::GainNode );
mGen->setFreq( _freq );
mFilter->setCutoffFreq(_freq / 2);
mFilter->setResonance(0.5f);
mGain->setValue( _amp );
mGen >> mFilter >> mGain >> ctx->getOutput();
mGen->enable();
ctx->enable();
}
void VoiceAloud::update()
{
mGen->setFreq( _freq );
mGain->setValue( _amp );
}
//void VoiceAloud::setup()
//{
// auto ctx = audio::master();
//
// mFilter = ctx->makeNode( new audio::FilterLowPassNode );
// mGen = ctx->makeNode( new audio::GenSineNode );
// mGain = ctx->makeNode( new audio::GainNode );
//
// mGen->setFreq( _freq );
// mFilter->setCutoffFreq(_freq / 2);
// mFilter->setResonance(0.5f);
// mGain->setValue( _amp );
//
// mGen >> mFilter >> mGain >> ctx->getOutput();
// mGen->enable();
//
// ctx->enable();
//
//}
//
//void VoiceAloud::update()
//{
// mGen->setFreq( _freq );
// mGain->setValue( _amp );
//}

View File

@ -25,7 +25,7 @@ using Receiver = osc::ReceiverUdp;
/*- Voice -*/
class Voice {
public:
Voice(std::string name, std::string channel, std::string font, int size, ColorA8u color, ColorA8u background, std::function<void(Voice*)> utterance_cb, std::function<void(Voice*)> change_cb)
Voice(std::string name, std::string channel, std::string font, int size, ColorA8u color, ColorA8u background, Rectf bounds, std::function<void(Voice*)> utterance_cb, std::function<void(Voice*)> change_cb)
: _name(name),
_channel(channel),
_utterance(name),
@ -33,46 +33,54 @@ public:
_size(size),
_color(color),
_background(background),
_bounds(bounds),
_offset(vec2(0)),
_utterance_cb(utterance_cb),
_change_cb(change_cb)
{};
void setup(Receiver &receiver);
void update();
void draw(Rectf &bounds);
void draw(Rectf &bounds, bool name=false, bool debug=false);
void bounds(int W, int H);
std::string _name;
std::string _channel;
std::string _utterance;
Font _font;
std::string _font_name;
ColorA8u _color;
ColorA8u _background;
Rectf _bounds;
vec2 _offset;
int _size;
bool _update_font = false;
float _temperature;
std::string _name;
std::string _channel;
std::string _utterance;
Font _font;
std::string _font_name;
ColorA8u _color;
ColorA8u _background;
int _size;
bool _update_font = false;
gl::TextureFontRef _tex;
gl::TextureFontRef _tex_name;
gl::GlslProgRef _glsl;
std::mutex _color_mutex, _font_mutex, _utterance_mutex;
std::mutex _color_mutex, _font_mutex, _utterance_mutex, _temperatue_mutex;
std::function<void(Voice*)> _utterance_cb, _change_cb;
audio::VoiceRef _sound;
// audio::VoiceRef _sound;
};
class VoiceAloud {
public:
VoiceAloud(float freq, float amp) : _freq(freq), _amp(amp) {};
void setup();
void update();
audio::GenNodeRef mGen; // Gen's generate audio signals
audio::FilterLowPassNodeRef mFilter;
audio::GainNodeRef mGain; // Gain modifies the volume of the signal
Anim<float> _freq;
Anim<float> _amp;
};
//class VoiceAloud {
//public:
// VoiceAloud(float freq, float amp) : _freq(freq), _amp(amp) {};
// void setup();
// void update();
//
// audio::GenNodeRef mGen; // Gen's generate audio signals
// audio::FilterLowPassNodeRef mFilter;
// audio::GainNodeRef mGain; // Gain modifies the volume of the signal
//
// Anim<float> _freq;
// Anim<float> _amp;
//};
#endif /* Voice_hpp */

View File

@ -89,11 +89,23 @@ void VoiceMachineApp::stage_config()
try {
const JsonTree config(loadAsset("voice.config.json"));
bool fullscreen = config["fullscreen"].getValue<bool>();
if (fullscreen) {
// FullScreenOptions& exclusive( bool enable = true );
// setFullScreen(exclusive);
setFullScreen(true);
}
// RECEIVER PORT
int port = config["port_voicemachine"].getValue<int>();
_receiver = std::shared_ptr<Receiver>(new Receiver(port, protocol::v4(), *_io_service));
//VOICES
Rectf bounds( 40, 40, getWindowWidth() - 40, getWindowHeight() - 40 );
for(auto &voice: config["voices"].getChildren()){
std::string name = voice["name"].getValue();
std::string channel = voice["osc_channel"]["root"].getValue();
@ -112,12 +124,9 @@ void VoiceMachineApp::stage_config()
this->change(v);
};
Voice* v = new Voice(name, channel, font, size, color, background, utterance_cb, change_cb);
Voice* v = new Voice(name, channel, font, size, color, background, bounds, utterance_cb, change_cb);
_map_voices[name] = v;
// //garbage
// _current_voice = v;
}
std::string command_channel = config["command_osc_channel"].getValue();
@ -230,7 +239,6 @@ void VoiceMachineApp::update()
{
for(const auto& [n, v]: _map_voices)
v->update();
// audio.update();
}
@ -244,7 +252,7 @@ void VoiceMachineApp::draw()
if(_current_voice){
Rectf bounds( 40, _current_voice->_tex->getAscent() + 40, getWindowWidth() - 40, getWindowHeight() - 40 );
_current_voice->draw(bounds);
_current_voice->draw(bounds, _debug, _debug);
if(_debug){
gl::color(Color(1, 0, 0));
gl::drawStrokedRect(bounds);
@ -268,10 +276,12 @@ void VoiceMachineApp::keyDown( KeyEvent event )
save_config();
break;
}
// case '-':
// mFont = Font( mFont.getName(), mFont.getSize() - 1 );
// mTextureFont = gl::TextureFont::create( mFont );
// break;
case 'd':
{
CI_LOG_D("debug mode...");
_debug = !_debug;
break;
}
}
}