Skip to content

Commit

Permalink
Load a default SFZ if path empty
Browse files Browse the repository at this point in the history
  • Loading branch information
jpcima committed Jun 20, 2020
1 parent 90f6039 commit 846dd6d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
42 changes: 30 additions & 12 deletions lv2/sfizz.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,8 @@ instantiate(const LV2_Descriptor *descriptor,
}

self->synth = sfizz_create_synth();
sfizz_load_string(self->synth, "default.sfz", "<region>sample=*sine");

return (LV2_Handle)self;
}

Expand All @@ -433,6 +435,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
Expand Down Expand Up @@ -856,11 +859,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)
{
Expand All @@ -874,6 +879,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", "<region>sample=*sine"))
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,
Expand All @@ -894,10 +919,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);
Expand Down Expand Up @@ -1038,9 +1060,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);
}
Expand Down Expand Up @@ -1104,9 +1124,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);
}
Expand Down
15 changes: 12 additions & 3 deletions vst/SfizzVstProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ tresult PLUGIN_API SfizzVstProcessor::initialize(FUnknown* context)

fprintf(stderr, "[sfizz] new synth\n");
_synth.reset(new sfz::Sfizz);
loadSfzFileOrDefault(*_synth, {});

return result;
}
Expand Down Expand Up @@ -91,7 +92,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);
Expand Down Expand Up @@ -325,7 +326,7 @@ tresult PLUGIN_API SfizzVstProcessor::notify(Vst::IMessage* message)

std::lock_guard<std::mutex> lock(_processMutex);
_state.sfzFile.assign(static_cast<const char *>(data), size);
_synth->loadSfzFile(_state.sfzFile);
loadSfzFileOrDefault(*_synth, _state.sfzFile);
}

return result;
Expand All @@ -336,6 +337,14 @@ FUnknown* SfizzVstProcessor::createInstance(void*)
return static_cast<Vst::IAudioProcessor*>(new SfizzVstProcessor);
}

void SfizzVstProcessor::loadSfzFileOrDefault(sfz::Sfizz& synth, const std::string& filePath)
{
if (!filePath.empty())
synth.loadSfzFile(filePath);
else
synth.loadSfzString("default.sfz", "<region>sample=*sine");
}

void SfizzVstProcessor::doBackgroundWork()
{
for (;;) {
Expand Down Expand Up @@ -367,7 +376,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);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions vst/SfizzVstProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class SfizzVstProcessor : public Vst::AudioEffect {
std::unique_ptr<sfz::Sfizz> _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;
Expand Down

0 comments on commit 846dd6d

Please sign in to comment.