diff --git a/src/nam_lv2.cpp b/src/nam_lv2.cpp index 5e73bc7..06299bb 100644 --- a/src/nam_lv2.cpp +++ b/src/nam_lv2.cpp @@ -7,8 +7,6 @@ // LV2 #include #include -#include -#include #include #include "nam_plugin.h" @@ -20,48 +18,40 @@ static LV2_Handle instantiate( const char*, const LV2_Feature* const* features ) { - LV2_URID_Map* map = nullptr; - LV2_Log_Logger logger = {}; + try + { + auto nam = std::make_unique(static_cast(rate)); - for (size_t i = 0; features[i]; ++i) { - if (std::string(features[i]->URI) == std::string(LV2_URID__map)) - map = static_cast(features[i]->data); - else if (std::string(features[i]->URI) == std::string(LV2_LOG__log)) - logger.log = static_cast(features[i]->data); - } + if (nam->initialize(rate, features)) + { + return static_cast(nam.release()); + } - lv2_log_logger_set_map(&logger, map); - - if (!map) { - lv2_log_error(&logger, "Missing required feature: `%s`", LV2_URID__map); return nullptr; } - - try { - auto nam = std::make_unique(static_cast(rate)); - nam->map_uris(map); - return static_cast(nam.release()); - } catch(const std::exception& e) { - lv2_log_error(&logger, "Failed to instantiate plugin: %s", e.what()); + catch(const std::exception& e) + { return nullptr; } } -static void connect_port(LV2_Handle instance, uint32_t port, void* data) { +static void connect_port(LV2_Handle instance, uint32_t port, void* data) +{ auto nam = static_cast(instance); *(reinterpret_cast(&nam->ports)+port) = data; } static void activate(LV2_Handle) {} -static void run(LV2_Handle instance, uint32_t n_samples) { - +static void run(LV2_Handle instance, uint32_t n_samples) +{ static_cast(instance)->process(n_samples); } static void deactivate(LV2_Handle) {} -static void cleanup(LV2_Handle instance) { +static void cleanup(LV2_Handle instance) +{ delete static_cast(instance); } @@ -89,14 +79,14 @@ extension_data(const char* uri) { static const LV2_Worker_Interface worker = { work, work_response, NULL }; - if (!strcmp(uri, LV2_WORKER__interface)) { + if (!strcmp(uri, LV2_WORKER__interface)) return &worker; - } return NULL; } -static const LV2_Descriptor descriptor = { +static const LV2_Descriptor descriptor = +{ "http://github.com/mikeoliphant/neural-amp-modeler-lv2", instantiate, connect_port, @@ -107,6 +97,7 @@ static const LV2_Descriptor descriptor = { extension_data }; -LV2_SYMBOL_EXPORT const LV2_Descriptor* lv2_descriptor(uint32_t index) { +LV2_SYMBOL_EXPORT const LV2_Descriptor* lv2_descriptor(uint32_t index) +{ return index == 0 ? &descriptor : nullptr; } diff --git a/src/nam_plugin.cpp b/src/nam_plugin.cpp index 013cf22..8ed19f9 100644 --- a/src/nam_plugin.cpp +++ b/src/nam_plugin.cpp @@ -2,10 +2,6 @@ #include #include -// Lv2 -#include -#include - #include "nam_plugin.h" namespace NAM { @@ -13,7 +9,33 @@ namespace NAM { { } - void Plugin::map_uris(LV2_URID_Map* map) noexcept { + bool Plugin::initialize(double rate, const LV2_Feature* const* features) noexcept + { + for (size_t i = 0; features[i]; ++i) { + if (std::string(features[i]->URI) == std::string(LV2_URID__map)) + map = static_cast(features[i]->data); + else if (!strcmp(features[i]->URI, LV2_WORKER__schedule)) + schedule = (LV2_Worker_Schedule*)features[i]->data; + else if (std::string(features[i]->URI) == std::string(LV2_LOG__log)) + logger.log = static_cast(features[i]->data); + } + + lv2_log_logger_set_map(&logger, map); + + if (!map) + { + lv2_log_error(&logger, "Missing required feature: `%s`", LV2_URID__map); + + return false; + } + + if (!schedule) + { + lv2_log_error(&logger, "Missing required feature: `%s`", LV2_WORKER__schedule); + + return false; + } + lv2_atom_forge_init(&atom_forge, map); uris.atom_Object = map->map(map->handle, LV2_ATOM__Object); @@ -26,6 +48,8 @@ namespace NAM { uris.patch_value = map->map(map->handle, LV2_PATCH__value); uris.model_Path = map->map(map->handle, MODEL_URI); + + return true; } void Plugin::process(uint32_t n_samples) noexcept { @@ -66,7 +90,7 @@ namespace NAM { if (namModel == nullptr) { - for (int i = 0; i < n_samples; i++) + for (unsigned int i = 0; i < n_samples; i++) { ports.audio_out[i] = ports.audio_in[i]; } diff --git a/src/nam_plugin.h b/src/nam_plugin.h index ea48ecb..7eaad3f 100644 --- a/src/nam_plugin.h +++ b/src/nam_plugin.h @@ -7,9 +7,14 @@ #include // LV2 +#include #include +#include +#include #include #include +#include +#include #include "dsp.h" @@ -29,6 +34,10 @@ namespace NAM { Ports ports = {}; + LV2_URID_Map* map; + LV2_Log_Logger logger; + LV2_Worker_Schedule* schedule; + std::unique_ptr<::DSP> namModel; std::unordered_map mNAMParams = @@ -37,15 +46,11 @@ namespace NAM { {"Output", 0.0} }; - /* - Member Functions - */ Plugin(float rate); ~Plugin() = default; - void map_uris(LV2_URID_Map* map) noexcept; - + bool initialize(double rate, const LV2_Feature* const* features) noexcept; void process(uint32_t n_samples) noexcept; private: