Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load a default SFZ if path empty #283

Merged
merged 2 commits into from
Jun 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions lv2/sfizz.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@
#define LV2_DEBUG(...)
#endif

static const char default_sfz_text[] =
"<region>sample=*sine" "\n"
"ampeg_attack=0.02 ampeg_release=0.1" "\n";

typedef struct
{
// Features
Expand Down Expand Up @@ -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;
}

Expand All @@ -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
Expand Down Expand Up @@ -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)
{
Expand All @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
19 changes: 16 additions & 3 deletions vst/SfizzVstProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ constexpr int fastRound(T x)
return static_cast<int>(x + T{ 0.5 }); // NOLINT
}

static const char defaultSfzText[] =
"<region>sample=*sine" "\n"
"ampeg_attack=0.02 ampeg_release=0.1" "\n";

SfizzVstProcessor::SfizzVstProcessor()
: _fifoToWorker(64 * 1024)
{
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -325,7 +330,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 +341,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", defaultSfzText);
}

void SfizzVstProcessor::doBackgroundWork()
{
for (;;) {
Expand Down Expand Up @@ -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);
}
}
}
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