Skip to content

Commit

Permalink
Setting up CLAP preset discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
jatinchowdhury18 committed Dec 30, 2023
1 parent 168b876 commit 9336043
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 22 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ juce_add_plugin(ChowMultiTool
IPAD_SCREEN_ORIENTATIONS UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight
)

set(CHOWMULTITOOL_CLAP_ID "org.chowdsp.ChowMultiTool")
if(CHOWMULTITOOL_BUILD_CLAP)
clap_juce_extensions_plugin(
TARGET ChowMultiTool
CLAP_ID "org.chowdsp.ChowMultiTool"
CLAP_FEATURES audio-effect win32-dpi-aware
CLAP_PROCESS_EVENTS_RESOLUTION_SAMPLES 64
CLAP_USE_JUCE_PARAMETER_RANGES DISCRETE
CLAP_SUPPORTS_CUSTOM_FACTORY 1
)
endif()

Expand All @@ -82,6 +84,7 @@ add_subdirectory(res)

target_compile_definitions(ChowMultiTool PUBLIC
JUCE_VST3_CAN_REPLACE_VST2=0
CHOWMULTITOOL_CLAP_ID="${CHOWMULTITOOL_CLAP_ID}"
)

target_link_libraries(ChowMultiTool
Expand Down
4 changes: 3 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ target_sources(ChowMultiTool PRIVATE
ChowMultiTool.cpp

state/PluginState.cpp
state/PresetManager.cpp
state/PluginRemoteControls.cpp

state/presets/PresetManager.cpp
state/presets/PresetDiscovery.cpp

dsp/MultiToolProcessor.cpp

dsp/AnalogEQ/PultecEQWDF.cpp
Expand Down
27 changes: 25 additions & 2 deletions src/ChowMultiTool.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include "ChowMultiTool.h"
#include "gui/PluginEditor.h"
#include "state/PresetManager.h"
#include "state/presets/PresetManager.h"

namespace
{
const juce::String settingsFilePath = "ChowdhuryDSP/ChowMultiTool/.plugin_settings.json";
const juce::String logFileSubDir = "ChowdhuryDSP/ChowMultiTool/Logs";
const juce::String logFileNameRoot = "ChowMultiTool_Log_";
} // namespace
Expand Down Expand Up @@ -82,3 +81,27 @@ juce::AudioProcessor* JUCE_CALLTYPE createPluginFilter()
{
return new ChowMultiTool();
}

#if HAS_CLAP_JUCE_EXTENSIONS
#include "state/presets/PresetDiscovery.h"

// bool BYOD::presetLoadFromLocation (uint32_t location_kind, const char* location, const char* load_key) noexcept
// {
// return preset_discovery::presetLoadFromLocation (*presetManager, location_kind, location, load_key);
// }

static const clap_preset_discovery_factory chowmultitool_preset_discovery_factory {
.count = state::presets::discovery::count,
.get_descriptor = state::presets::discovery::get_descriptor,
.create = state::presets::discovery::create,
};

const void* JUCE_CALLTYPE clapJuceExtensionCustomFactory (const char* factory_id)
{
if (strcmp (factory_id, CLAP_PRESET_DISCOVERY_FACTORY_ID) == 0)
{
return &chowmultitool_preset_discovery_factory;
}
return nullptr;
}
#endif
2 changes: 1 addition & 1 deletion src/gui/Toolbar/Toolbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "ChowMultiTool.h"
#include "gui/Shared/Colours.h"
#include "gui/Shared/LookAndFeels.h"
#include "state/PresetManager.h"
#include "state/presets/PresetManager.h"

namespace gui
{
Expand Down
3 changes: 3 additions & 0 deletions src/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@ CMRC_DECLARE (presets);

// Any other widely used headers that don't change...
#include "state/ParameterVersionHints.h"

// constants
const juce::String settingsFilePath = "ChowdhuryDSP/ChowMultiTool/.plugin_settings.json";
130 changes: 130 additions & 0 deletions src/state/presets/PresetDiscovery.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#if HAS_CLAP_JUCE_EXTENSIONS

#include "PresetDiscovery.h"
#include "PresetManager.h"

namespace state::presets::discovery
{
static constexpr clap_plugin_id plugin_id {
.abi = "clap",
.id = CHOWMULTITOOL_CLAP_ID,
};

static auto getUserPresetsFolder()
{
chowdsp::SharedPluginSettings pluginSettings;
pluginSettings->initialise (settingsFilePath, 0);

static constexpr auto propertyID = chowdsp::presets::frontend::SettingsInterface::userPresetsDirID;
if (! pluginSettings->hasProperty (propertyID))
return juce::File {};

const auto userPresetsPath = pluginSettings->getProperty<juce::String> (propertyID);
return juce::File { userPresetsPath };
}

//==============================================================================
struct FactoryPresetsProvider : chowdsp::presets::discovery::EmbeddedPresetsProvider
{
static constexpr clap_preset_discovery_provider_descriptor descriptor {
.clap_version = CLAP_VERSION_INIT,
.id = "org.chowdsp.ChowMultiTool.factory-presets",
.name = "ChowMultiTool Factory Presets Provider",
.vendor = "ChowDSP"
};

static constexpr clap_preset_discovery_location factoryPresetsLocation {
.flags = CLAP_PRESET_DISCOVERY_IS_FACTORY_CONTENT,
.name = "ChowMultiTool Factory Presets Location",
.kind = CLAP_PRESET_DISCOVERY_LOCATION_PLUGIN,
.location = nullptr,
};

explicit FactoryPresetsProvider (const clap_preset_discovery_indexer* indexer)
: EmbeddedPresetsProvider (plugin_id, descriptor, factoryPresetsLocation, indexer)
{
}

std::vector<chowdsp::presets::Preset> getPresets() override
{
return PresetManager::getFactoryPresets();
}
};

//==============================================================================
struct UserPresetsProvider : chowdsp::presets::discovery::FilePresetsProvider
{
static constexpr clap_preset_discovery_provider_descriptor descriptor {
.clap_version = CLAP_VERSION_INIT,
.id = "org.chowdsp.ChowMultiTool.user-presets",
.name = "ChowMultiTool User Presets Provider",
.vendor = "User"
};


static constexpr clap_preset_discovery_filetype filetype {
.name = "User Preset Filetype",
.description = "User preset filetype for ChowMultiTool",
.file_extension = "chowpreset",
};

explicit UserPresetsProvider (const clap_preset_discovery_indexer* indexer)
: FilePresetsProvider (plugin_id, descriptor, filetype, indexer)
{
}

bool fillInLocation(clap_preset_discovery_location& location) override
{
const auto userPresetsFolder = getUserPresetsFolder();
if (userPresetsFolder == juce::File {} || ! userPresetsFolder.isDirectory())
return false;

location.name = "ChowMultiTool User Presets Location";
location.location = userPresetsFolder.getFullPathName().toRawUTF8();
return true;
}
};

//==============================================================================
uint32_t count (const clap_preset_discovery_factory*)
{
const auto userPresetsFolder = getUserPresetsFolder();
if (userPresetsFolder == juce::File {} || ! userPresetsFolder.isDirectory())
return 1;
return 2;
}

const clap_preset_discovery_provider_descriptor_t* get_descriptor (
const clap_preset_discovery_factory*,
uint32_t index)
{
if (index == 0)
return &FactoryPresetsProvider::descriptor;

if (index == 1)
return &UserPresetsProvider::descriptor;

return nullptr;
}

const clap_preset_discovery_provider_t* create (
const clap_preset_discovery_factory*,
const clap_preset_discovery_indexer_t* indexer,
const char* provider_id)
{
if (strcmp (provider_id, FactoryPresetsProvider::descriptor.id) == 0)
{
auto* provider = new FactoryPresetsProvider { indexer };
return provider->provider();
}

if (strcmp (provider_id, UserPresetsProvider::descriptor.id) == 0)
{
auto* provider = new UserPresetsProvider { indexer };
return provider->provider();
}

return nullptr;
}
} // namespace state::presets::discovery
#endif
17 changes: 17 additions & 0 deletions src/state/presets/PresetDiscovery.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <pch.h>

namespace state::presets::discovery
{
uint32_t count (const clap_preset_discovery_factory* factory);

const clap_preset_discovery_provider_descriptor_t* get_descriptor (
const clap_preset_discovery_factory* factory,
uint32_t index);

const clap_preset_discovery_provider_t* create (
const clap_preset_discovery_factory* factory,
const clap_preset_discovery_indexer_t* indexer,
const char* provider_id);
} // namespace state::presets::discovery
42 changes: 26 additions & 16 deletions src/state/PresetManager.cpp → src/state/presets/PresetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,29 @@ using namespace chowdsp::presets;
constexpr std::string_view presetParamsTag { "preset_params" };
constexpr std::string_view presetWaveshaperStateTag { "ws_state" };

const auto createPresetFromEmbeddedFile = [] (const cmrc::embedded_filesystem& fs,
const std::string& path) -> Preset
{
const auto presetFile = fs.open (path);
return { presetFile.begin(), presetFile.size() };
};

std::vector<Preset> PresetManager::getFactoryPresets()
{
const auto fs = cmrc::presets::get_filesystem();

std::vector<Preset> factoryPresets;
factoryPresets.reserve (32);
for (auto&& entry : fs.iterate_directory (""))
{
jassert (entry.is_file());
jassert (fs.exists (entry.filename()));
factoryPresets.emplace_back (createPresetFromEmbeddedFile (fs, entry.filename()));
}

return factoryPresets;
}

PresetManager::PresetManager (ChowMultiTool& plugin)
: chowdsp::presets::PresetManager (plugin.getState(), &plugin, ".chowpreset"),
toolParam (*plugin.getState().params.toolParam)
Expand Down Expand Up @@ -70,22 +93,9 @@ PresetManager::PresetManager (ChowMultiTool& plugin)
}
};

const auto fs = cmrc::presets::get_filesystem();
const auto createPresetFromEmbeddedFile = [&fs] (const std::string& path) -> Preset
{
const auto presetFile = fs.open (path);
return { presetFile.begin(), presetFile.size() };
};

std::vector<Preset> factoryPresets;
for (auto&& entry : fs.iterate_directory (""))
{
jassert (entry.is_file());
jassert (fs.exists (entry.filename()));
factoryPresets.emplace_back (createPresetFromEmbeddedFile (entry.filename()));
}
auto factoryPresets = getFactoryPresets();
addPresets (std::move (factoryPresets));
setDefaultPreset (createPresetFromEmbeddedFile ("Init.chowpreset"));
setDefaultPreset (createPresetFromEmbeddedFile (cmrc::presets::get_filesystem(), "Init.chowpreset"));

presetsSettings.emplace (*this,
*pluginSettings,
Expand All @@ -97,7 +107,7 @@ PresetManager::PresetManager (ChowMultiTool& plugin)

Preset PresetManager::getUserPresetForState (const juce::String& presetName, nlohmann::json&& presetState) const
{
std::cout << presetState << std::endl;
DBG (presetState.dump());
const auto toolParamIndex = presetState[presetParamsTag][toolParam.paramID.toStdString()].get<int>();
if (toolParamIndex == 0)
return chowdsp::presets::PresetManager::getUserPresetForState (presetName, std::move (presetState));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class PresetManager : public chowdsp::presets::PresetManager
[[nodiscard]] chowdsp::presets::Preset getUserPresetForState (const juce::String& presetName, nlohmann::json&& presetState) const override;
auto* getPresetSettings() { return &(*presetsSettings); }

static std::vector<chowdsp::presets::Preset> getFactoryPresets();

private:
chowdsp::SharedPluginSettings pluginSettings;
std::optional<chowdsp::presets::frontend::SettingsInterface> presetsSettings;
Expand Down

0 comments on commit 9336043

Please sign in to comment.