Merge pull request #283 from jpcima/default-file

Load a default SFZ if path empty
This commit is contained in:
JP Cimalando 2020-06-21 13:30:15 +02:00 committed by GitHub
commit 3b5f215a10
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 15 deletions

View file

@ -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
@ -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);
}

View file

@ -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)
{
@ -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<std::mutex> lock(_processMutex);
_state.sfzFile.assign(static_cast<const char *>(data), size);
_synth->loadSfzFile(_state.sfzFile);
loadSfzFileOrDefault(*_synth, _state.sfzFile);
}
return result;
@ -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 (;;) {
@ -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);
}
}
}

View file

@ -49,6 +49,9 @@ private:
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;