plugin cleanup

This commit is contained in:
Mike Oliphant
2023-03-13 09:52:15 -07:00
parent 079bd4dbfd
commit 99c709d70d
3 changed files with 60 additions and 40 deletions
+20 -29
View File
@@ -7,8 +7,6 @@
// LV2
#include <lv2/core/lv2.h>
#include <lv2/urid/urid.h>
#include <lv2/log/log.h>
#include <lv2/log/logger.h>
#include <lv2/worker/worker.h>
#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<NAM::Plugin>(static_cast<float>(rate));
for (size_t i = 0; features[i]; ++i) {
if (std::string(features[i]->URI) == std::string(LV2_URID__map))
map = static_cast<LV2_URID_Map*>(features[i]->data);
else if (std::string(features[i]->URI) == std::string(LV2_LOG__log))
logger.log = static_cast<LV2_Log_Log*>(features[i]->data);
}
if (nam->initialize(rate, features))
{
return static_cast<LV2_Handle>(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<NAM::Plugin>(static_cast<float>(rate));
nam->map_uris(map);
return static_cast<LV2_Handle>(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<NAM::Plugin*>(instance);
*(reinterpret_cast<void**>(&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<NAM::Plugin*>(instance)->process(n_samples);
}
static void deactivate(LV2_Handle) {}
static void cleanup(LV2_Handle instance) {
static void cleanup(LV2_Handle instance)
{
delete static_cast<NAM::Plugin*>(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;
}
+30 -6
View File
@@ -2,10 +2,6 @@
#include <cmath>
#include <utility>
// Lv2
#include <lv2/atom/util.h>
#include <lv2/patch/patch.h>
#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<LV2_URID_Map*>(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<LV2_Log_Log*>(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];
}
+10 -5
View File
@@ -7,9 +7,14 @@
#include <string_view>
// LV2
#include <lv2/core/lv2.h>
#include <lv2/atom/atom.h>
#include <lv2/log/log.h>
#include <lv2/log/logger.h>
#include <lv2/urid/urid.h>
#include <lv2/atom/forge.h>
#include <lv2/patch/patch.h>
#include <lv2/worker/worker.h>
#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<std::string, double> 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: