Skip to content

Commit

Permalink
LV2 - Parameter fetching by ID could sometimes return the incorrect p…
Browse files Browse the repository at this point in the history
…arameter, as the assumption that parameter ID's started from 0 is not guaranteed. They are now stored in a map by ID, meaning this can no longer happen.
  • Loading branch information
IliasBergstrom committed Apr 2, 2020
1 parent e1749dc commit 26aacaa
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/library/lv2/lv2_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ void LV2_Wrapper::configure(float /*sample_rate*/)
SUSHI_LOG_WARNING("LV2 does not support altering the sample rate after initialization.");
}

const ParameterDescriptor* LV2_Wrapper::parameter_from_id(ObjectId id) const
{
auto descriptor = _parameters_by_lv2_id.find(id);
if (descriptor != _parameters_by_lv2_id.end())
{
return descriptor->second;
}
return nullptr;
}

std::pair<ProcessorReturnCode, float> LV2_Wrapper::parameter_value(ObjectId parameter_id) const
{
auto parameter = parameter_from_id(parameter_id);
Expand Down Expand Up @@ -263,6 +273,9 @@ bool LV2_Wrapper::_register_parameters()
{
// Here I need to get the name of the port.
auto nameNode = lilv_port_get_name(_model->plugin_class(), currentPort->lilv_port());
int portIndex = lilv_port_get_index(_model->plugin_class(), currentPort->lilv_port());

assert(portIndex == _pi); // This should only fail is the plugin's .ttl file is incorrect.

const std::string name_as_string = lilv_node_as_string(nameNode);
const std::string param_unit = "";
Expand All @@ -273,7 +286,7 @@ bool LV2_Wrapper::_register_parameters()
currentPort->min(), // range min
currentPort->max(), // range max
nullptr), // ParameterPreProcessor
static_cast<ObjectId>(_pi)); // Registering the ObjectID as the index in LV2 plugin's ports list.
static_cast<ObjectId>(portIndex)); // Registering the ObjectID as the LV2 Port index.

if (param_inserted_ok)
{
Expand All @@ -288,6 +301,17 @@ bool LV2_Wrapper::_register_parameters()
}
}

/* Create a "backwards map" from LV2 parameter ids to parameter indices.
* LV2 parameter ports have an integer ID, assigned in the ttl file.
* While often it starts from 0 and goes up to n-1 parameters, there is no
* guarantee. Very often this is not true, when in the ttl, the parameter ports,
* are preceded by other types of ports in the list (i.e. audio/midi i/o).
*/
for (auto param : this->all_parameters())
{
_parameters_by_lv2_id[param->id()] = param;
}

return param_inserted_ok;
}

Expand Down
4 changes: 4 additions & 0 deletions src/library/lv2/lv2_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class LV2_Wrapper : public Processor

bool bypassed() const override;

const ParameterDescriptor* parameter_from_id(ObjectId id) const override;

std::pair<ProcessorReturnCode, float> parameter_value(ObjectId parameter_id) const override;

std::pair<ProcessorReturnCode, float> parameter_value_in_domain(ObjectId parameter_id) const override;
Expand Down Expand Up @@ -200,6 +202,8 @@ class LV2_Wrapper : public Processor

static constexpr float _min_normalized{0.0f};
static constexpr float _max_normalized{1.0f};

std::map<ObjectId, const ParameterDescriptor*> _parameters_by_lv2_id;
};

} // end namespace lv2
Expand Down
9 changes: 9 additions & 0 deletions test/unittests/library/lv2_wrapper_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ TEST_F(TestLv2Wrapper, TestLV2PluginInterraction)
auto ret = SetUp("http://lv2plug.in/plugins/eg-amp");
ASSERT_EQ(ProcessorReturnCode::OK, ret);

int pCount = _module_under_test->parameter_count();
EXPECT_EQ(1, pCount);
auto param0 = _module_under_test->parameter_from_id(0);
EXPECT_TRUE(param0);
EXPECT_EQ(0u, param0->id());

auto paramNull = _module_under_test->parameter_from_id(1);
EXPECT_EQ(paramNull, nullptr);

// TestSetName
EXPECT_EQ("http://lv2plug.in/plugins/eg-amp", _module_under_test->name());
EXPECT_EQ("Simple Amplifier", _module_under_test->label());
Expand Down

0 comments on commit 26aacaa

Please sign in to comment.