Deduplicate load-or-import code from plugins

This commit is contained in:
Jean Pierre Cimalando 2021-06-22 21:18:16 +02:00
parent ee477ee58a
commit 23a1ab268d
5 changed files with 70 additions and 26 deletions

View file

@ -35,7 +35,7 @@
#include "sfizz_lv2.h"
#include "sfizz_lv2_plugin.h"
#include "sfizz/import/ForeignInstrument.h"
#include "sfizz/import/sfizz_import.h"
#include "plugin/InstrumentDescription.h"
#include <math.h>
@ -1231,8 +1231,6 @@ sfizz_lv2_update_sfz_info(sfizz_plugin_t *self)
static bool
sfizz_lv2_load_file(sfizz_plugin_t *self, const char *file_path)
{
bool status;
char buf[MAX_PATH_SIZE];
if (file_path[0] == '\0')
{
@ -1241,18 +1239,7 @@ sfizz_lv2_load_file(sfizz_plugin_t *self, const char *file_path)
}
///
const sfz::InstrumentFormatRegistry& formatRegistry = sfz::InstrumentFormatRegistry::getInstance();
const sfz::InstrumentFormat* format = formatRegistry.getMatchingFormat(file_path);
if (!format)
status = sfizz_load_file(self->synth, file_path);
else {
auto importer = format->createImporter();
std::string virtual_path = std::string(file_path) + ".sfz";
std::string sfz_text = importer->convertToSfz(file_path);
status = sfizz_load_string(self->synth, virtual_path.c_str(), sfz_text.c_str());
}
bool status = sfizz_load_or_import_file(self->synth, file_path, nullptr);
sfizz_lv2_update_sfz_info(self);
sfizz_lv2_update_file_info(self, file_path);
return status;

View file

@ -9,7 +9,7 @@
#include "SfizzVstState.h"
#include "SfizzVstParameters.h"
#include "SfizzVstIDs.h"
#include "sfizz/import/ForeignInstrument.h"
#include "sfizz/import/sfizz_import.h"
#include "plugin/SfizzFileScan.h"
#include "plugin/InstrumentDescription.h"
#include "base/source/fstreamer.h"
@ -682,16 +682,7 @@ void SfizzVstProcessor::loadSfzFileOrDefault(const std::string& filePath, bool i
sfz::Sfizz& synth = *_synth;
if (!filePath.empty()) {
const sfz::InstrumentFormatRegistry& formatRegistry = sfz::InstrumentFormatRegistry::getInstance();
const sfz::InstrumentFormat* format = formatRegistry.getMatchingFormat(filePath);
if (!format)
synth.loadSfzFile(filePath);
else {
auto importer = format->createImporter();
std::string virtualPath = filePath + ".sfz";
std::string sfzText = importer->convertToSfz(filePath);
synth.loadSfzString(virtualPath, sfzText);
}
sfizz_load_or_import_file(synth.handle(), filePath.c_str(), nullptr);
}
else {
synth.loadSfzString("default.sfz", defaultSfzText);

View file

@ -251,11 +251,13 @@ endif()
# Import library
set(SFIZZ_IMPORT_HEADERS
sfizz/import/sfizz_import.h
sfizz/import/ForeignInstrument.h
sfizz/import/foreign_instruments/AudioFile.h
sfizz/import/foreign_instruments/DecentSampler.h)
set(SFIZZ_IMPORT_SOURCES
sfizz/import/sfizz_import.cpp
sfizz/import/ForeignInstrument.cpp
sfizz/import/foreign_instruments/AudioFile.cpp
sfizz/import/foreign_instruments/DecentSampler.cpp)

View file

@ -0,0 +1,33 @@
// SPDX-License-Identifier: BSD-2-Clause
// This code is part of the sfizz library and is licensed under a BSD 2-clause
// license. You should have receive a LICENSE.md file along with the code.
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz
#include "sfizz.h"
#include "sfizz_import.h"
#include "ForeignInstrument.h"
bool sfizz_load_or_import_file(sfizz_synth_t* synth, const char* path, const char** format)
{
const sfz::InstrumentFormatRegistry& ireg = sfz::InstrumentFormatRegistry::getInstance();
const sfz::InstrumentFormat* ifmt = ireg.getMatchingFormat(path);
if (!ifmt) {
if (!sfizz_load_file(synth, path))
return false;
if (format)
*format = nullptr;
}
else {
auto importer = ifmt->createImporter();
std::string virtualPath = std::string(path) + ".sfz";
std::string sfzText = importer->convertToSfz(path);
if (!sfizz_load_string(synth, virtualPath.c_str(), sfzText.c_str()))
return false;
if (format)
*format = ifmt->name();
}
return true;
}

View file

@ -0,0 +1,31 @@
// SPDX-License-Identifier: BSD-2-Clause
// This code is part of the sfizz library and is licensed under a BSD 2-clause
// license. You should have receive a LICENSE.md file along with the code.
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz
#pragma once
typedef struct sfizz_synth_t sfizz_synth_t;
/**
* @brief Loads or imports an instrument file.
*
* The file path can be absolute or relative.
* @since 1.0.1
*
* @param synth The synth.
* @param path A null-terminated string representing a path to an instrument
* in SFZ format, or another format which can be imported.
* @param format An optional pointer to a string pointer, which receives the
* null-terminated name of the format if the file was imported,
* or null if the file was loaded directly as SFZ.
*
* @return @true when file loading went OK,
* @false if some error occured while loading.
*
* @par Thread-safety constraints
* - @b CT: the function must be invoked from the Control thread
* - @b OFF: the function cannot be invoked while a thread is calling @b RT functions
*/
bool sfizz_load_or_import_file(sfizz_synth_t* synth, const char* path, const char** format);