From 25af38a3c240117a2e347d1abdb7e5fb28f63ca4 Mon Sep 17 00:00:00 2001 From: dreamer Date: Fri, 4 Mar 2022 04:00:51 +0100 Subject: [PATCH] it's something, but it segfaults --- src/jack-audio-module.cc | 38 ++++++++++++++++++++++++++++++++++++++ src/jack-audio-module.hh | 4 ++++ 2 files changed, 42 insertions(+) diff --git a/src/jack-audio-module.cc b/src/jack-audio-module.cc index 1e584ee..ccd7392 100644 --- a/src/jack-audio-module.cc +++ b/src/jack-audio-module.cc @@ -60,6 +60,44 @@ void JackAudioModule::process(const ProcessArgs &args) { } } +json_t* jack_audio_module_base::dataToJson() { + auto map = Module::toJson(); + auto port_names = json_array(); + + for (int i = 0; i < JACK_PORTS; i++) { + auto str = json_string(this->port_names[i]->text.c_str()); + json_array_append_new(port_names, str); + } + + json_object_set_new(map, "port_names", port_names); + return map; +} + +void jack_audio_module_base::dataFromJson(json_t* json) { + auto module = reinterpret_cast(this); + auto port_names = json_object_get(json, "port_names"); + if (json_is_array(port_names)) { + for (size_t i = 0; i < std::min(json_array_size(port_names), (size_t)8); i++) { + auto item = json_array_get(port_names, i); + if (json_is_string(item)) { + if (module->jport[i].rename(json_string_value(item))) { + this->port_names[i]->text = std::string(json_string_value(item)); + } else { + static const size_t buffer_size = 128; + char port_name[buffer_size]; + hashidsxx::Hashids hash(g_hashid_salt); + std::string id = hash.encode(reinterpret_cast(module)); + + snprintf(reinterpret_cast(&port_name), + buffer_size, + "%s:%d", id.c_str(), (int)i); + this->port_names[i]->setText(std::string(port_name)); + } + } + } + } +} + void jack_audio_module_base::report_backlogged() { // we're over half capacity, so set our output latch if (output_latch.try_set()) { diff --git a/src/jack-audio-module.hh b/src/jack-audio-module.hh index d4856fb..7db022c 100644 --- a/src/jack-audio-module.hh +++ b/src/jack-audio-module.hh @@ -22,6 +22,7 @@ struct jack_audio_module_base: public Module { int lastSampleRate = 0; int lastNumOutputs = -1; int lastNumInputs = -1; + TextField* port_names[8]; dsp::SampleRateConverter inputSrc; dsp::SampleRateConverter outputSrc; @@ -42,6 +43,9 @@ struct jack_audio_module_base: public Module { void report_backlogged(); + virtual json_t* dataToJson() override; + virtual void dataFromJson(json_t* json) override; + jack_audio_module_base(size_t params, size_t inputs, size_t outputs, size_t lights); virtual ~jack_audio_module_base();