From 99d907e5c7090580a99ca130ae9651cb848dc733 Mon Sep 17 00:00:00 2001 From: Paul Fd Date: Sat, 24 Jul 2021 16:59:22 +0200 Subject: [PATCH] Remember the last keyswitch used in the states --- plugins/lv2/sfizz.cpp | 31 +++++++++++++++++++++++++++++++ plugins/lv2/sfizz_lv2.h | 1 + plugins/lv2/sfizz_lv2_plugin.h | 2 ++ plugins/vst/SfizzVstProcessor.cpp | 12 ++++++++++++ plugins/vst/SfizzVstState.cpp | 11 +++++++++++ plugins/vst/SfizzVstState.h | 3 ++- 6 files changed, 59 insertions(+), 1 deletion(-) diff --git a/plugins/lv2/sfizz.cpp b/plugins/lv2/sfizz.cpp index 9bc3c4a16..407ed8042 100644 --- a/plugins/lv2/sfizz.cpp +++ b/plugins/lv2/sfizz.cpp @@ -120,6 +120,7 @@ sfizz_lv2_map_required_uris(sfizz_plugin_t *self) self->sfizz_num_voices_uri = map->map(map->handle, SFIZZ__numVoices); self->sfizz_preload_size_uri = map->map(map->handle, SFIZZ__preloadSize); self->sfizz_oversampling_uri = map->map(map->handle, SFIZZ__oversampling); + self->sfizz_last_keyswitch_uri = map->map(map->handle, SFIZZ__lastKeyswitch); self->sfizz_log_status_uri = map->map(map->handle, SFIZZ__logStatus); self->sfizz_check_modification_uri = map->map(map->handle, SFIZZ__checkModification); self->sfizz_osc_blob_uri = map->map(map->handle, SFIZZ__OSCBlob); @@ -323,6 +324,14 @@ sfizz_lv2_receive_message(void* data, int delay, const char* path, const char* s sfizz_plugin_t *self = (sfizz_plugin_t *)data; + if (!strcmp(path, "/sw/last/current") && sig) + { + if (sig[0] == 'i') + self->last_keyswitch = args[0].i; + else if (sig[0] == 'N') + self->last_keyswitch = -1; + } + // transmit to UI as OSC blob uint8_t *osc_temp = self->osc_temp; uint32_t osc_size = sfizz_prepare_message(osc_temp, OSC_TEMP_SIZE, path, sig, args); @@ -1301,6 +1310,7 @@ restore(LV2_Handle instance, } // Set default values + self->last_keyswitch = -1; sfizz_lv2_get_default_sfz_path(self, self->sfz_file_path, MAX_PATH_SIZE); sfizz_lv2_get_default_scala_path(self, self->scala_file_path, MAX_PATH_SIZE); @@ -1351,6 +1361,13 @@ restore(LV2_Handle instance, } } + value = retrieve(handle, self->sfizz_last_keyswitch_uri, &size, &type, &val_flags); + if (value) + { + int last_keyswitch = *(const int*)value; + self->last_keyswitch = last_keyswitch; + } + // Collect all CC values present in the state std::unique_ptr[]> cc_values( new absl::optional[sfz::config::numCCs]); @@ -1405,6 +1422,11 @@ restore(LV2_Handle instance, } } + if (self->last_keyswitch >= 0 && self->last_keyswitch <= 127) { + sfizz_send_hd_note_on(self->synth, 0, self->last_keyswitch, 1.0f); + sfizz_send_hd_note_off(self->synth, 1, self->last_keyswitch, 0.0f); + } + spin_mutex_unlock(self->synth_mutex); return status; @@ -1474,6 +1496,15 @@ save(LV2_Handle instance, absl::string_view((const char*)self->sfz_blob_data, self->sfz_blob_size)); self->sfz_blob_mutex->unlock(); + if (self->last_keyswitch >= 0 && self->last_keyswitch <= 127) { + store(handle, + self->sfizz_last_keyswitch_uri, + &self->last_keyswitch, + sizeof(int), + self->atom_int_uri, + LV2_STATE_IS_POD); + } + for (unsigned cc = 0; cc < sfz::config::numCCs; ++cc) { if (desc.ccUsed.test(cc) && !desc.sustainOrSostenuto.test(cc)) { LV2_URID urid = sfizz_lv2_ccmap_map(self->ccmap, int(cc)); diff --git a/plugins/lv2/sfizz_lv2.h b/plugins/lv2/sfizz_lv2.h index f48f2606f..c9935ec08 100644 --- a/plugins/lv2/sfizz_lv2.h +++ b/plugins/lv2/sfizz_lv2.h @@ -39,6 +39,7 @@ #define SFIZZ__numVoices SFIZZ_URI ":" "numvoices" #define SFIZZ__preloadSize SFIZZ_URI ":" "preload_size" #define SFIZZ__oversampling SFIZZ_URI ":" "oversampling" +#define SFIZZ__lastKeyswitch SFIZZ_URI ":" "last_keyswitch" // These ones are just for the worker #define SFIZZ__logStatus SFIZZ_URI ":" "log_status" #define SFIZZ__checkModification SFIZZ_URI ":" "check_modification" diff --git a/plugins/lv2/sfizz_lv2_plugin.h b/plugins/lv2/sfizz_lv2_plugin.h index 9c1918f96..fad8342ac 100644 --- a/plugins/lv2/sfizz_lv2_plugin.h +++ b/plugins/lv2/sfizz_lv2_plugin.h @@ -86,6 +86,7 @@ struct sfizz_plugin_t LV2_URID sfizz_num_voices_uri {}; LV2_URID sfizz_preload_size_uri {}; LV2_URID sfizz_oversampling_uri {}; + LV2_URID sfizz_last_keyswitch_uri {}; LV2_URID sfizz_log_status_uri {}; LV2_URID sfizz_check_modification_uri {}; LV2_URID sfizz_active_voices_uri {}; @@ -120,6 +121,7 @@ struct sfizz_plugin_t float sample_rate {}; std::atomic must_update_midnam {}; volatile bool must_automate_cc {}; + int last_keyswitch { -1 }; // Current instrument description std::mutex *sfz_blob_mutex {}; diff --git a/plugins/vst/SfizzVstProcessor.cpp b/plugins/vst/SfizzVstProcessor.cpp index 1e0a71346..e50801faa 100644 --- a/plugins/vst/SfizzVstProcessor.cpp +++ b/plugins/vst/SfizzVstProcessor.cpp @@ -227,6 +227,10 @@ void SfizzVstProcessor::syncStateToSynth() synth->setScalaRootKey(_state.scalaRootKey); synth->setTuningFrequency(_state.tuningFrequency); synth->loadStretchTuningByRatio(_state.stretchedTuning); + if (_state.lastKeyswitch >= 0 && _state.lastKeyswitch <= 127) { + synth->hdNoteOn(0, _state.lastKeyswitch, 1.0f); + synth->hdNoteOff(1, _state.lastKeyswitch, 0.0f); + } } tresult PLUGIN_API SfizzVstProcessor::canProcessSampleSize(int32 symbolicSampleSize) @@ -669,6 +673,14 @@ bool SfizzVstProcessor::processUpdate(FUnknown* changedUnknown, int32 message) void SfizzVstProcessor::receiveOSC(int delay, const char* path, const char* sig, const sfizz_arg_t* args) { + if (!strcmp(path, "/sw/last/current") && sig) + { + if (sig[0] == 'i') + _state.lastKeyswitch = args[0].i; + else if (sig[0] == 'N') + _state.lastKeyswitch = -1; + } + uint8_t* oscTemp = _oscTemp.get(); uint32 oscSize = sfizz_prepare_message(oscTemp, kOscTempSize, path, sig, args); if (oscSize <= kOscTempSize) { diff --git a/plugins/vst/SfizzVstState.cpp b/plugins/vst/SfizzVstState.cpp index 1783f817f..160d7abbe 100644 --- a/plugins/vst/SfizzVstState.cpp +++ b/plugins/vst/SfizzVstState.cpp @@ -73,6 +73,14 @@ tresult SfizzVstState::load(IBStream* state) oscillatorQuality = defaults.oscillatorQuality; } + if (version >= 4) { + if (!s.readInt32(lastKeyswitch)) + return kResultFalse; + } + else { + lastKeyswitch = -1; + } + controllers.clear(); if (version >= 2) { uint32 count; @@ -135,6 +143,9 @@ tresult SfizzVstState::store(IBStream* state) const if (!s.writeInt32(oscillatorQuality)) return kResultFalse; + if (!s.writeInt32(lastKeyswitch)) + return kResultFalse; + { uint32 ccCount = 0; uint32 ccLimit = uint32(std::min(controllers.size(), size_t(0x10000))); diff --git a/plugins/vst/SfizzVstState.h b/plugins/vst/SfizzVstState.h index 7f50f4c57..93a7a4ec4 100644 --- a/plugins/vst/SfizzVstState.h +++ b/plugins/vst/SfizzVstState.h @@ -27,9 +27,10 @@ class SfizzVstState { float stretchedTuning = 0.0; int32 sampleQuality = 2; int32 oscillatorQuality = 1; + int32 lastKeyswitch = -1; std::vector> controllers; - static constexpr uint64 currentStateVersion = 3; + static constexpr uint64 currentStateVersion = 4; tresult load(IBStream* state); tresult store(IBStream* state) const;