annotate test/vamp-client/tst_PluginStub.cpp @ 296:50a0b4fea7f1 tip master

Merge pull request #8 from michel-slm/gcc15 Include headers needed to compile with GCC 15's -std=gnu23 default
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 27 Jan 2025 08:53:58 +0000
parents 09753ad777db
children
rev   line source
dev@190 1 #include "catch/catch.hpp"
dev@191 2 #include "vamp-client/Loader.h"
dev@191 3 #include "vamp-client/PluginClient.h"
cannam@213 4 #include "vamp-client/PiperVampPlugin.h"
dev@191 5 #include "vamp-support/RequestResponse.h"
dev@191 6 #include <vector>
dev@190 7
dev@191 8 using namespace piper_vamp;
dev@191 9 using namespace piper_vamp::client;
dev@191 10 using AudioBuffer = std::vector<std::vector<float>>;
dev@190 11
dev@196 12 // This stub fakes the interaction with a Piper server
dev@191 13 // Here we only need to implement the configure method
cannam@213 14 // due to testing only the initialise implemention of PiperVampPlugin
dev@191 15 class StubClient : public PluginClient
dev@191 16 {
dev@191 17 public:
dev@191 18 StubClient(PluginStaticData staticData) : m_staticData(staticData) {}
dev@191 19
dev@191 20 ConfigurationResponse
cannam@213 21 configure(PiperVampPlugin* plugin,
dev@191 22 PluginConfiguration config) override
dev@191 23 {
dev@191 24 const float scale = plugin->getParameter("framing-scale");
dev@191 25 ConfigurationResponse cr;
dev@191 26 cr.plugin = plugin;
dev@191 27
dev@194 28 // we want to return different framing sizes than config provides
dev@191 29 // there isn't really any need to be doing this with a plugin param
dev@194 30 cr.framing.blockSize = config.framing.blockSize * scale;
dev@194 31 cr.framing.stepSize = config.framing.stepSize * scale;
dev@191 32
dev@191 33 // just return some outputs anyway
dev@191 34 // to avoid a failure case we are not testing here.
dev@191 35 Vamp::Plugin::OutputDescriptor output;
dev@191 36 const auto basic = m_staticData.basicOutputInfo[0];
dev@191 37 output.identifier = basic.identifier;
dev@191 38 output.name = basic.name;
dev@191 39 output.description = basic.description;
dev@191 40 cr.outputs = {output};
dev@191 41 return cr;
dev@191 42 }
dev@191 43
dev@191 44 Vamp::Plugin::FeatureSet
cannam@213 45 process(PiperVampPlugin* /*plugin*/,
dev@191 46 AudioBuffer /*channels*/,
dev@191 47 Vamp::RealTime /*timestamp*/) override
dev@191 48 {
dev@191 49 return {};
dev@191 50 }
dev@191 51
dev@191 52 Vamp::Plugin::FeatureSet
cannam@213 53 finish(PiperVampPlugin* /*plugin*/) override
dev@191 54 {
dev@191 55 return {};
dev@191 56 }
dev@191 57
dev@191 58 void
cannam@213 59 reset(PiperVampPlugin* /*plugin*/, PluginConfiguration /*config*/) override
dev@191 60 {}
dev@191 61 private:
dev@191 62 PluginStaticData m_staticData;
dev@191 63 };
dev@191 64
dev@191 65
dev@191 66 TEST_CASE("Init plugin with parameter dependent preferred framing sizes") {
dev@194 67 const std::size_t initialBlockSize = 1024;
dev@194 68 const std::size_t initialStepSize = 512;
dev@191 69 PluginConfiguration defaultConfig;
dev@191 70 defaultConfig.channelCount = 1;
dev@194 71 defaultConfig.framing.blockSize = initialBlockSize;
dev@194 72 defaultConfig.framing.stepSize = initialStepSize;
dev@191 73 defaultConfig.parameterValues = {{"framing-scale", 1.0}};
dev@191 74
dev@191 75 Vamp::PluginBase::ParameterDescriptor stubParam;
dev@191 76 stubParam.identifier = "framing-scale";
dev@191 77 stubParam.name = "Framing Scale Factor";
dev@191 78 stubParam.description = "Scales the preferred framing sizes";
dev@191 79 stubParam.maxValue = 2.0;
dev@191 80
dev@191 81 PluginStaticData staticData;
dev@191 82 staticData.pluginKey = "stub";
dev@202 83 staticData.basic = {"param-init", "Stub", "Testing init"};
dev@191 84 staticData.maker = "Lucas Thompson";
dev@191 85 staticData.copyright = "GPL";
dev@191 86 staticData.pluginVersion = 1;
dev@191 87 staticData.category = {"Test"};
dev@191 88 staticData.minChannelCount = 1;
dev@191 89 staticData.maxChannelCount = 1;
dev@191 90 staticData.parameters = {stubParam};
dev@191 91 staticData.inputDomain = Vamp::Plugin::InputDomain::TimeDomain;
dev@191 92 staticData.basicOutputInfo = {{"output", "NA", "Not real"}};
dev@191 93
cannam@287 94 PluginProgramParameters programParameters;
cannam@287 95 // none
cannam@287 96
dev@191 97 StubClient stub {staticData};
dev@191 98
cannam@213 99 PiperVampPlugin vampPiperAdapter {
dev@191 100 &stub,
dev@191 101 "stub", // plugin key
dev@191 102 44100.0, // sample rate
dev@191 103 0, // adapter flags, don't care here
dev@191 104 staticData,
cannam@287 105 defaultConfig,
cannam@287 106 programParameters
dev@191 107 };
dev@191 108
dev@194 109 const auto initWithPreferredFraming = [&]() -> bool {
dev@194 110 return vampPiperAdapter.initialise(
dev@191 111 1,
dev@191 112 vampPiperAdapter.getPreferredStepSize(),
dev@191 113 vampPiperAdapter.getPreferredBlockSize()
dev@194 114 );
dev@194 115 };
dev@194 116
dev@202 117 const AudioBuffer monoAudio {
dev@202 118 std::vector<float>(vampPiperAdapter.getPreferredBlockSize())
dev@202 119 };
dev@202 120 const std::vector<const float*> channelPtrs {
dev@202 121 monoAudio[0].data()
dev@202 122 };
dev@202 123
dev@194 124 SECTION("Initialises with default parameters")
dev@194 125 {
dev@194 126 REQUIRE( initWithPreferredFraming() );
dev@194 127 }
dev@194 128
dev@194 129 SECTION("Fails to init when changing framing influencing parameter")
dev@194 130 {
dev@194 131 const float scalingFactor = 2.0;
dev@194 132 vampPiperAdapter.setParameter("framing-scale", scalingFactor);
dev@194 133 REQUIRE( initWithPreferredFraming() == false );
dev@194 134 const float configuredStepSize = vampPiperAdapter.getPreferredStepSize();
dev@194 135 const float configuredBlockSize = vampPiperAdapter.getPreferredBlockSize();
dev@194 136 REQUIRE( configuredStepSize == initialStepSize * scalingFactor );
dev@194 137 REQUIRE( configuredBlockSize == initialBlockSize * scalingFactor );
dev@194 138 }
dev@194 139
dev@194 140 SECTION("Cannot process after a failed init call (due to framing)")
dev@194 141 {
dev@194 142 const float scalingFactor = 2.0;
dev@194 143 vampPiperAdapter.setParameter("framing-scale", scalingFactor);
dev@194 144 REQUIRE( initWithPreferredFraming() == false );
dev@202 145 REQUIRE_THROWS( vampPiperAdapter.process(channelPtrs.data(), {}) );
dev@199 146 REQUIRE_THROWS( initWithPreferredFraming() );
dev@194 147 }
dev@194 148
dev@194 149 SECTION("Can process after correctly initialising framing")
dev@194 150 {
dev@194 151 const float scalingFactor = 2.0;
dev@194 152 vampPiperAdapter.setParameter("framing-scale", scalingFactor);
dev@194 153 REQUIRE( initWithPreferredFraming() == false );
dev@194 154 REQUIRE( initWithPreferredFraming() );
dev@194 155 REQUIRE( vampPiperAdapter.process(channelPtrs.data(), {}).empty() );
dev@194 156 }
dev@190 157 }