diff --git a/lv2/sfizz.c b/lv2/sfizz.c index 825519c9..2271a466 100644 --- a/lv2/sfizz.c +++ b/lv2/sfizz.c @@ -88,6 +88,10 @@ #define LV2_DEBUG(...) #endif +static const char default_sfz_text[] = + "sample=*sine" "\n" + "ampeg_attack=0.02 ampeg_release=0.1" "\n"; + typedef struct { // Features @@ -416,6 +420,8 @@ instantiate(const LV2_Descriptor *descriptor, } self->synth = sfizz_create_synth(); + sfizz_load_string(self->synth, "default.sfz", default_sfz_text); + return (LV2_Handle)self; } @@ -433,6 +439,7 @@ activate(LV2_Handle instance) sfizz_plugin_t *self = (sfizz_plugin_t *)instance; sfizz_set_samples_per_block(self->synth, self->max_block_size); sfizz_set_sample_rate(self->synth, self->sample_rate); + atomic_store(&self->must_update_midnam, 1); } static void @@ -856,11 +863,13 @@ lv2_set_options(LV2_Handle instance, const LV2_Options_Option *options) } static void -sfizz_lv2_update_file_info(sfizz_plugin_t* self, const char* file_path) +sfizz_lv2_update_file_info(sfizz_plugin_t* self, const char *file_path) { strcpy(self->sfz_file_path, file_path); - lv2_log_note(&self->logger, "[sfizz] File changed to: %s\n", self->sfz_file_path); + const char *display_path = (file_path[0] != '\0') ? file_path : "(default)"; + lv2_log_note(&self->logger, "[sfizz] File changed to: %s\n", display_path); + char *unknown_opcodes = sfizz_get_unknown_opcodes(self->synth); if (unknown_opcodes) { @@ -874,6 +883,26 @@ sfizz_lv2_update_file_info(sfizz_plugin_t* self, const char* file_path) atomic_store(&self->must_update_midnam, 1); } +static bool +sfizz_lv2_load_file(LV2_Handle instance, const char *file_path) +{ + sfizz_plugin_t *self = (sfizz_plugin_t *)instance; + + if (file_path[0] == '\0') + { + if (!sfizz_load_string(self->synth, "default.sfz", default_sfz_text)) + return false; + } + else + { + if (!sfizz_load_file(self->synth, file_path)) + return false; + } + + sfizz_lv2_update_file_info(self, file_path); + return true; +} + static LV2_State_Status restore(LV2_Handle instance, LV2_State_Retrieve_Function retrieve, @@ -894,10 +923,7 @@ restore(LV2_Handle instance, if (value) { lv2_log_note(&self->logger, "[sfizz] Restoring the file %s\n", (const char *)value); - if (sfizz_load_file(self->synth, (const char *)value)) - { - sfizz_lv2_update_file_info(self, (const char *)value); - } + sfizz_lv2_load_file(instance, (const char *)value); } value = retrieve(handle, self->sfizz_scala_file_uri, &size, &type, &val_flags); @@ -1038,9 +1064,7 @@ work(LV2_Handle instance, if (atom->type == self->sfizz_sfz_file_uri) { const char *sfz_file_path = LV2_ATOM_BODY_CONST(atom); - if (sfizz_load_file(self->synth, sfz_file_path)) { - sfizz_lv2_update_file_info(self, sfz_file_path); - } else { + if (!sfizz_lv2_load_file(self, sfz_file_path)) { lv2_log_error(&self->logger, "[sfizz] Error with %s; no file should be loaded\n", sfz_file_path); } @@ -1104,9 +1128,7 @@ work(LV2_Handle instance, lv2_log_note(&self->logger, "[sfizz] File %s seems to have been updated, reloading\n", self->sfz_file_path); - if (sfizz_load_file(self->synth, self->sfz_file_path)) { - sfizz_lv2_update_file_info(self, self->sfz_file_path); - } else { + if (!sfizz_lv2_load_file(self, self->sfz_file_path)) { lv2_log_error(&self->logger, "[sfizz] Error with %s; no file should be loaded\n", self->sfz_file_path); } diff --git a/vst/SfizzVstProcessor.cpp b/vst/SfizzVstProcessor.cpp index 56aef12a..944a07c3 100644 --- a/vst/SfizzVstProcessor.cpp +++ b/vst/SfizzVstProcessor.cpp @@ -20,6 +20,10 @@ constexpr int fastRound(T x) return static_cast(x + T{ 0.5 }); // NOLINT } +static const char defaultSfzText[] = + "sample=*sine" "\n" + "ampeg_attack=0.02 ampeg_release=0.1" "\n"; + SfizzVstProcessor::SfizzVstProcessor() : _fifoToWorker(64 * 1024) { @@ -48,6 +52,7 @@ tresult PLUGIN_API SfizzVstProcessor::initialize(FUnknown* context) fprintf(stderr, "[sfizz] new synth\n"); _synth.reset(new sfz::Sfizz); + loadSfzFileOrDefault(*_synth, {}); return result; } @@ -91,7 +96,7 @@ void SfizzVstProcessor::syncStateToSynth() if (!synth) return; - synth->loadSfzFile(_state.sfzFile); + loadSfzFileOrDefault(*synth, _state.sfzFile); synth->setVolume(_state.volume); synth->setNumVoices(_state.numVoices); synth->setOversamplingFactor(1 << _state.oversamplingLog2); @@ -325,7 +330,7 @@ tresult PLUGIN_API SfizzVstProcessor::notify(Vst::IMessage* message) std::lock_guard lock(_processMutex); _state.sfzFile.assign(static_cast(data), size); - _synth->loadSfzFile(_state.sfzFile); + loadSfzFileOrDefault(*_synth, _state.sfzFile); } return result; @@ -336,6 +341,14 @@ FUnknown* SfizzVstProcessor::createInstance(void*) return static_cast(new SfizzVstProcessor); } +void SfizzVstProcessor::loadSfzFileOrDefault(sfz::Sfizz& synth, const std::string& filePath) +{ + if (!filePath.empty()) + synth.loadSfzFile(filePath); + else + synth.loadSfzString("default.sfz", defaultSfzText); +} + void SfizzVstProcessor::doBackgroundWork() { for (;;) { @@ -367,7 +380,7 @@ void SfizzVstProcessor::doBackgroundWork() else if (!std::strcmp(id, "CheckShouldReload")) { if (_synth->shouldReloadFile()) { fprintf(stderr, "[Sfizz] file has changed, reloading\n"); - _synth->loadSfzFile(_state.sfzFile); + loadSfzFileOrDefault(*_synth, _state.sfzFile); } } } diff --git a/vst/SfizzVstProcessor.h b/vst/SfizzVstProcessor.h index 1ed7fb37..946e6772 100644 --- a/vst/SfizzVstProcessor.h +++ b/vst/SfizzVstProcessor.h @@ -49,6 +49,9 @@ private: std::unique_ptr _synth; SfizzVstState _state; + // misc + static void loadSfzFileOrDefault(sfz::Sfizz& synth, const std::string& filePath); + // worker and thread sync std::thread _worker; volatile bool _workRunning = false;