Audacity  3.0.3
LoadVamp.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  LoadVamp.cpp
6 
7  Chris Cannam
8 
9 **********************************************************************/
10 
11 
12 
13 #if defined(USE_VAMP)
14 #include "LoadVamp.h"
15 #include "../../ModuleManager.h"
16 
17 #include <wx/filename.h>
18 
19 #include "VampEffect.h"
20 
21 #include <iostream>
22 #include <map>
23 
24 using namespace Vamp;
25 using namespace Vamp::HostExt;
26 using namespace Vamp::HostExt;
27 
28 // ============================================================================
29 // Module registration entry point
30 //
31 // This is the symbol that Audacity looks for when the module is built as a
32 // dynamic library.
33 //
34 // When the module is builtin to Audacity, we use the same function, but it is
35 // declared static so as not to clash with other builtin modules.
36 // ============================================================================
37 DECLARE_MODULE_ENTRY(AudacityModule)
38 {
39  // Create and register the importer
40  // Trust the module manager not to leak this
41  return safenew VampEffectsModule();
42 }
43 
44 // ============================================================================
45 // Register this as a builtin module
46 // ============================================================================
47 DECLARE_BUILTIN_MODULE(VampsEffectBuiltin);
48 
50 //
51 // VampEffectsModule
52 //
54 
56 {
57 }
58 
60 {
61 }
62 
63 // ============================================================================
64 // ComponentInterface implementation
65 // ============================================================================
66 
68 {
69  return {};
70 }
71 
73 {
74  return XO("Vamp Effects");
75 }
76 
78 {
79  return XO("The Audacity Team");
80 }
81 
83 {
84  // This "may" be different if this were to be maintained as a separate DLL
85  return VAMPEFFECTS_VERSION;
86 }
87 
89 {
90  return XO("Provides Vamp Effects support to Audacity");
91 }
92 
93 // ============================================================================
94 // ModuleInterface implementation
95 // ============================================================================
96 
98 {
99  // Nothing to do here
100  return true;
101 }
102 
104 {
105  // Nothing to do here
106  return;
107 }
108 
110 {
111 #if USE_VAMP
112  return VAMPEFFECTS_FAMILY;
113 #else
114  return {};
115 #endif
116 }
117 
119 {
120  static FileExtensions empty;
121  return empty;
122 }
123 
125 {
126  return false;
127 }
128 
130 {
132 
133  PluginLoader *loader = PluginLoader::getInstance();
134 
135  PluginLoader::PluginKeyList keys = loader->listPlugins();
136 
137  for (PluginLoader::PluginKeyList::iterator i = keys.begin(); i != keys.end(); ++i)
138  {
139  std::unique_ptr<Plugin> vp{ PluginLoader::getInstance()->loadPlugin(*i, 48000) }; // rate doesn't matter here
140  if (!vp)
141  {
142  continue;
143  }
144 
145  // We limit the listed plugin outputs to those whose results can
146  // readily be displayed in an Audacity label track.
147  //
148  // - Any output whose features have no values (time instants only),
149  // with or without duration, is fine
150  //
151  // - Any output whose features have more than one value, or an
152  // unknown or variable number of values, is right out
153  //
154  // - Any output whose features have exactly one value, with
155  // variable sample rate or with duration, should be OK --
156  // this implies a sparse feature, of which the time and/or
157  // duration are significant aspects worth displaying
158  //
159  // - An output whose features have exactly one value, with
160  // fixed sample rate and no duration, cannot be usefully
161  // displayed -- the value is the only significant piece of
162  // data there and we have no good value plot
163 
164  Plugin::OutputList outputs = vp->getOutputDescriptors();
165 
166  int output = 0;
167 
168  for (Plugin::OutputList::iterator j = outputs.begin(); j != outputs.end(); ++j)
169  {
170  if (j->sampleType == Plugin::OutputDescriptor::FixedSampleRate ||
171  j->sampleType == Plugin::OutputDescriptor::OneSamplePerStep ||
172  !j->hasFixedBinCount ||
173  j->binCount > 1)
174  {
175  // All of these qualities disqualify (see notes above)
176 
177  ++output;
178  continue;
179  }
180 
181  wxString name = wxString::FromUTF8(vp->getName().c_str());
182 
183  if (outputs.size() > 1)
184  {
185  // This is not the plugin's only output.
186  // Use "plugin name: output name" as the effect name,
187  // unless the output name is the same as the plugin name
188  wxString outputName = wxString::FromUTF8(j->name.c_str());
189  if (outputName != name)
190  {
191  name = wxString::Format(wxT("%s: %s"),
192  name, outputName);
193  }
194  }
195 
196  wxString path = wxString::FromUTF8(i->c_str()) + wxT("/") + name;
197  names.push_back(path);
198 
199  ++output;
200  }
201  }
202 
203  return names;
204 }
205 
207  const PluginPath & path, TranslatableString &errMsg,
208  const RegistrationCallback &callback)
209 {
210  errMsg = {};
211  int output;
212  bool hasParameters;
213 
214  auto vp = FindPlugin(path, output, hasParameters);
215  if (vp)
216  {
217  VampEffect effect(std::move(vp), path, output, hasParameters);
218  if (callback)
219  callback( this, &effect );
220 
221  return 1;
222  }
223 
224  errMsg = XO("Could not load the library");
225  return 0;
226 }
227 
228 bool VampEffectsModule::IsPluginValid(const PluginPath & path, bool bFast)
229 {
230  int output;
231  bool hasParameters;
232  if( bFast )
233  return true;
234 
235  auto vp = FindPlugin(path, output, hasParameters);
236  return bool(vp);
237 }
238 
239 std::unique_ptr<ComponentInterface>
241 {
242  // Acquires a resource for the application.
243  int output;
244  bool hasParameters;
245 
246  if (auto vp = FindPlugin(path, output, hasParameters))
247  return std::make_unique<VampEffect>(std::move(vp), path, output, hasParameters);
248  return nullptr;
249 }
250 
251 // VampEffectsModule implementation
252 
253 std::unique_ptr<Vamp::Plugin> VampEffectsModule::FindPlugin(const PluginPath & path,
254  int & output,
255  bool & hasParameters)
256 {
257  PluginLoader::PluginKey key = path.BeforeLast(wxT('/')).ToUTF8().data();
258 
259  std::unique_ptr<Plugin> vp{ PluginLoader::getInstance()->loadPlugin(key, 48000) }; // rate doesn't matter here
260  if (!vp)
261  {
262  return nullptr;
263  }
264 
265  // We limit the listed plugin outputs to those whose results can
266  // readily be displayed in an Audacity label track.
267  //
268  // - Any output whose features have no values (time instants only),
269  // with or without duration, is fine
270  //
271  // - Any output whose features have more than one value, or an
272  // unknown or variable number of values, is right out
273  //
274  // - Any output whose features have exactly one value, with
275  // variable sample rate or with duration, should be OK --
276  // this implies a sparse feature, of which the time and/or
277  // duration are significant aspects worth displaying
278  //
279  // - An output whose features have exactly one value, with
280  // fixed sample rate and no duration, cannot be usefully
281  // displayed -- the value is the only significant piece of
282  // data there and we have no good value plot
283 
284  Plugin::OutputList outputs = vp->getOutputDescriptors();
285 
286  output = 0;
287 
288  hasParameters = !vp->getParameterDescriptors().empty();
289 
290  for (Plugin::OutputList::iterator j = outputs.begin(); j != outputs.end(); ++j)
291  {
292  if (j->sampleType == Plugin::OutputDescriptor::FixedSampleRate ||
293  j->sampleType == Plugin::OutputDescriptor::OneSamplePerStep ||
294  !j->hasFixedBinCount ||
295  j->binCount > 1)
296  {
297  // All of these qualities disqualify (see notes above)
298 
299  ++output;
300  continue;
301  }
302 
303  wxString name = wxString::FromUTF8(vp->getName().c_str());
304 
305  if (outputs.size() > 1)
306  {
307  // This is not the plugin's only output.
308  // Use "plugin name: output name" as the effect name,
309  // unless the output name is the same as the plugin name
310  wxString outputName = wxString::FromUTF8(j->name.c_str());
311  if (outputName != name)
312  {
313  name = wxString::Format(wxT("%s: %s"),
314  name, outputName);
315  }
316  }
317 
318  if (wxString::FromUTF8(key.c_str()) + wxT("/") + name == path)
319  {
320  return vp;
321  }
322 
323  ++output;
324  }
325 
326  return {};
327 }
328 
329 #endif
TranslatableString
Holds a msgid for the translation catalog; may also bind format arguments.
Definition: TranslatableString.h:32
VampEffectsModule::GetOptionalFamilySymbol
EffectFamilySymbol GetOptionalFamilySymbol() override
Definition: LoadVamp.cpp:109
VampEffectsModule::VampEffectsModule
VampEffectsModule()
Definition: LoadVamp.cpp:55
DECLARE_MODULE_ENTRY
DECLARE_MODULE_ENTRY(AudacityModule)
Definition: LoadVamp.cpp:37
PluginPath
wxString PluginPath
type alias for identifying a Plugin supplied by a module, each module defining its own interpretation...
Definition: Identifier.h:214
XO
#define XO(s)
Definition: Internat.h:31
VampEffectsModule::Terminate
void Terminate() override
Definition: LoadVamp.cpp:103
VampEffectsModule::Initialize
bool Initialize() override
Definition: LoadVamp.cpp:97
VampEffectsModule::GetVersion
wxString GetVersion() override
Definition: LoadVamp.cpp:82
wxArrayStringEx
Extend wxArrayString with move operations and construction and insertion fromstd::initializer_list.
Definition: wxArrayStringEx.h:18
VampEffectsModule::IsPluginValid
bool IsPluginValid(const PluginPath &path, bool bFast) override
Definition: LoadVamp.cpp:228
ComponentInterfaceSymbol
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
Definition: ComponentInterfaceSymbol.h:27
LoadVamp.h
VampEffectsModule::~VampEffectsModule
virtual ~VampEffectsModule()
Definition: LoadVamp.cpp:59
VampEffectsModule
Definition: LoadVamp.h:30
PluginManagerInterface
Definition: PluginInterface.h:55
VAMPEFFECTS_VERSION
#define VAMPEFFECTS_VERSION
Definition: VampEffect.h:29
VampEffectsModule::AutoRegisterPlugins
bool AutoRegisterPlugins(PluginManagerInterface &pm) override
Definition: LoadVamp.cpp:124
VampEffectsModule::CreateInstance
std::unique_ptr< ComponentInterface > CreateInstance(const PluginPath &path) override
Definition: LoadVamp.cpp:240
name
const TranslatableString name
Definition: Distortion.cpp:98
VampEffectsModule::GetDescription
TranslatableString GetDescription() override
Definition: LoadVamp.cpp:88
VAMPEFFECTS_FAMILY
#define VAMPEFFECTS_FAMILY
Definition: VampEffect.h:32
VampEffectsModule::GetFileExtensions
const FileExtensions & GetFileExtensions() override
Definition: LoadVamp.cpp:118
VampEffect.h
DECLARE_BUILTIN_MODULE
DECLARE_BUILTIN_MODULE(VampsEffectBuiltin)
VampEffectsModule::DiscoverPluginsAtPath
unsigned DiscoverPluginsAtPath(const PluginPath &path, TranslatableString &errMsg, const RegistrationCallback &callback) override
Definition: LoadVamp.cpp:206
PluginPaths
std::vector< PluginPath > PluginPaths
Definition: Identifier.h:215
VampEffectsModule::FindPluginPaths
PluginPaths FindPluginPaths(PluginManagerInterface &pm) override
Definition: LoadVamp.cpp:129
VampEffectsModule::FindPlugin
std::unique_ptr< Vamp::Plugin > FindPlugin(const PluginPath &wpath, int &output, bool &hasParameters)
Definition: LoadVamp.cpp:253
key
static const AudacityProject::AttachedObjects::RegisteredFactory key
Definition: CommandManager.cpp:201
names
static TranslatableStrings names
Definition: Tags.cpp:744
VampEffectsModule::GetSymbol
ComponentInterfaceSymbol GetSymbol() override
Definition: LoadVamp.cpp:72
VampEffectsModule::GetVendor
VendorSymbol GetVendor() override
Definition: LoadVamp.cpp:77
ModuleInterface::RegistrationCallback
std::function< const PluginID &(ModuleInterface *, ComponentInterface *) > RegistrationCallback
Definition: ModuleInterface.h:123
safenew
#define safenew
Definition: MemoryX.h:10
VampEffectsModule::GetPath
PluginPath GetPath() override
Definition: LoadVamp.cpp:67
VampEffect
Definition: VampEffect.h:35