Skip to content

Commit

Permalink
Merge pull request #67 from emilio1234/emilw/vst3_preset_loader
Browse files Browse the repository at this point in the history
  • Loading branch information
psobot authored Jan 28, 2022
2 parents edca8a7 + dd2bb2f commit 2ab83f9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
33 changes: 33 additions & 0 deletions pedalboard/ExternalPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,35 @@ template <typename ExternalPluginType> class ExternalPlugin : public Plugin {
}
}

struct PresetVisitor : public juce::ExtensionsVisitor {
const std::string presetFilePath;

PresetVisitor(const std::string presetFilePath)
: presetFilePath(presetFilePath) {}

void visitVST3Client(
const juce::ExtensionsVisitor::VST3Client &client) override {
juce::File presetFile(presetFilePath);
juce::MemoryBlock presetData;

if (!presetFile.loadFileAsData(presetData)) {
throw std::runtime_error("Failed to read preset file: " +
presetFilePath);
}

if (!client.setPreset(presetData)) {
throw std::runtime_error(
"Plugin returned an error when loading data from preset file: " +
presetFilePath);
}
}
};

void loadPresetData(std::string presetFilePath) {
PresetVisitor visitor{presetFilePath};
pluginInstance->getExtensions(visitor);
}

void reinstantiatePlugin() {
// If we have an existing plugin, save its state and reload its state later:
juce::MemoryBlock savedState;
Expand Down Expand Up @@ -793,6 +822,10 @@ inline void init_external_plugins(py::module &m) {
ss << ">";
return ss.str();
})
.def("load_preset",
&ExternalPlugin<juce::VST3PluginFormat>::loadPresetData,
"Load a VST3 preset file in .vstpreset format.",
py::arg("preset_file_path"))
.def_property_readonly_static(
"installed_plugins",
[](py::object /* cls */) { return findInstalledVSTPluginPaths(); },
Expand Down
Binary file added tests/presets/CHOWTapeModel.vst3.vstpreset
Binary file not shown.
27 changes: 27 additions & 0 deletions tests/test_external_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@


TEST_PLUGIN_BASE_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)), "plugins")
TEST_PRESET_BASE_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)), "presets")

AVAILABLE_PLUGINS_IN_TEST_ENVIRONMENT = [
os.path.basename(filename)
Expand Down Expand Up @@ -189,6 +190,32 @@ def test_at_least_one_parameter(plugin_filename: str):
assert get_parameters(plugin_filename)


@pytest.mark.parametrize(
"plugin_filename,plugin_preset",
[
(plugin, os.path.join(TEST_PRESET_BASE_PATH, plugin + ".vstpreset"))
for plugin in AVAILABLE_PLUGINS_IN_TEST_ENVIRONMENT
if os.path.isfile(os.path.join(TEST_PRESET_BASE_PATH, plugin + ".vstpreset"))
],
)
def test_preset_parameters(plugin_filename: str, plugin_preset: str):
# plugin with default params.
plugin = load_test_plugin(plugin_filename)

default_params = {k: v.raw_value for k, v in plugin.parameters.items() if v.type == float}

# load preset file
plugin.load_preset(plugin_preset)

for name, default in default_params.items():
actual = getattr(plugin, name)
if math.isnan(actual):
continue
assert (
actual != default
), f"Expected attribute {name} to be different from default ({default}), but was {actual}"


@pytest.mark.parametrize("plugin_filename", AVAILABLE_PLUGINS_IN_TEST_ENVIRONMENT)
def test_initial_parameters(plugin_filename: str):
parameters = {
Expand Down

0 comments on commit 2ab83f9

Please sign in to comment.