Skip to content

Commit

Permalink
Remember the last keyswitch used in the states
Browse files Browse the repository at this point in the history
  • Loading branch information
paulfd authored and jpcima committed Sep 20, 2021
1 parent 2bb268a commit 99d907e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 1 deletion.
31 changes: 31 additions & 0 deletions plugins/lv2/sfizz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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<absl::optional<float>[]> cc_values(
new absl::optional<float>[sfz::config::numCCs]);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
1 change: 1 addition & 0 deletions plugins/lv2/sfizz_lv2.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions plugins/lv2/sfizz_lv2_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
Expand Down Expand Up @@ -120,6 +121,7 @@ struct sfizz_plugin_t
float sample_rate {};
std::atomic<int> must_update_midnam {};
volatile bool must_automate_cc {};
int last_keyswitch { -1 };

// Current instrument description
std::mutex *sfz_blob_mutex {};
Expand Down
12 changes: 12 additions & 0 deletions plugins/vst/SfizzVstProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions plugins/vst/SfizzVstState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)));
Expand Down
3 changes: 2 additions & 1 deletion plugins/vst/SfizzVstState.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ class SfizzVstState {
float stretchedTuning = 0.0;
int32 sampleQuality = 2;
int32 oscillatorQuality = 1;
int32 lastKeyswitch = -1;
std::vector<absl::optional<float>> controllers;

static constexpr uint64 currentStateVersion = 3;
static constexpr uint64 currentStateVersion = 4;

tresult load(IBStream* state);
tresult store(IBStream* state) const;
Expand Down

0 comments on commit 99d907e

Please sign in to comment.