Audacity  2.2.2
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 #include "../../Audacity.h"
12 
13 #if defined(USE_VAMP)
14 
15 #include <wx/filename.h>
16 
17 #include "../EffectManager.h"
18 #include "VampEffect.h"
19 #include "LoadVamp.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(moduleManager, path);
42 }
43 
44 // ============================================================================
45 // Register this as a builtin module
46 // ============================================================================
47 DECLARE_BUILTIN_MODULE(VampsEffectBuiltin);
48 
50 //
51 // VampEffectsModule
52 //
54 
55 VampEffectsModule::VampEffectsModule(ModuleManagerInterface *moduleManager,
56  const wxString *path)
57 {
58  mModMan = moduleManager;
59  if (path)
60  {
61  mPath = *path;
62  }
63 }
64 
65 VampEffectsModule::~VampEffectsModule()
66 {
67 }
68 
69 // ============================================================================
70 // IdentInterface implementation
71 // ============================================================================
72 
73 wxString VampEffectsModule::GetPath()
74 {
75  return mPath;
76 }
77 
78 wxString VampEffectsModule::GetSymbol()
79 {
80  return wxT("Vamp Effects");
81 }
82 
83 wxString VampEffectsModule::GetName()
84 {
85  return XO("Vamp Effects");
86 }
87 
88 wxString VampEffectsModule::GetVendor()
89 {
90  return XO("The Audacity Team");
91 }
92 
93 wxString VampEffectsModule::GetVersion()
94 {
95  // This "may" be different if this were to be maintained as a separate DLL
96  return VAMPEFFECTS_VERSION;
97 }
98 
99 wxString VampEffectsModule::GetDescription()
100 {
101  return _("Provides Vamp Effects support to Audacity");
102 }
103 
104 // ============================================================================
105 // ModuleInterface implementation
106 // ============================================================================
107 
108 bool VampEffectsModule::Initialize()
109 {
110  // Nothing to do here
111  return true;
112 }
113 
114 void VampEffectsModule::Terminate()
115 {
116  // Nothing to do here
117  return;
118 }
119 
120 bool VampEffectsModule::AutoRegisterPlugins(PluginManagerInterface & WXUNUSED(pm))
121 {
122  return false;
123 }
124 
125 wxArrayString VampEffectsModule::FindPluginPaths(PluginManagerInterface & WXUNUSED(pm))
126 {
127  wxArrayString names;
128 
129  PluginLoader *loader = PluginLoader::getInstance();
130 
131  PluginLoader::PluginKeyList keys = loader->listPlugins();
132 
133  for (PluginLoader::PluginKeyList::iterator i = keys.begin(); i != keys.end(); ++i)
134  {
135  std::unique_ptr<Plugin> vp{ PluginLoader::getInstance()->loadPlugin(*i, 48000) }; // rate doesn't matter here
136  if (!vp)
137  {
138  continue;
139  }
140 
141  // We limit the listed plugin outputs to those whose results can
142  // readily be displayed in an Audacity label track.
143  //
144  // - Any output whose features have no values (time instants only),
145  // with or without duration, is fine
146  //
147  // - Any output whose features have more than one value, or an
148  // unknown or variable number of values, is right out
149  //
150  // - Any output whose features have exactly one value, with
151  // variable sample rate or with duration, should be OK --
152  // this implies a sparse feature, of which the time and/or
153  // duration are significant aspects worth displaying
154  //
155  // - An output whose features have exactly one value, with
156  // fixed sample rate and no duration, cannot be usefully
157  // displayed -- the value is the only significant piece of
158  // data there and we have no good value plot
159 
160  Plugin::OutputList outputs = vp->getOutputDescriptors();
161 
162  int output = 0;
163 
164  for (Plugin::OutputList::iterator j = outputs.begin(); j != outputs.end(); ++j)
165  {
166  if (j->sampleType == Plugin::OutputDescriptor::FixedSampleRate ||
167  j->sampleType == Plugin::OutputDescriptor::OneSamplePerStep ||
168  !j->hasFixedBinCount ||
169  j->binCount > 1)
170  {
171  // All of these qualities disqualify (see notes above)
172 
173  ++output;
174  continue;
175  }
176 
177  wxString name = wxString::FromUTF8(vp->getName().c_str());
178 
179  if (outputs.size() > 1)
180  {
181  // This is not the plugin's only output.
182  // Use "plugin name: output name" as the effect name,
183  // unless the output name is the same as the plugin name
184  wxString outputName = wxString::FromUTF8(j->name.c_str());
185  if (outputName != name)
186  {
187  name = wxString::Format(wxT("%s: %s"),
188  name, outputName);
189  }
190  }
191 
192  wxString path = wxString::FromUTF8(i->c_str()) + wxT("/") + name;
193  names.Add(path);
194 
195  ++output;
196  }
197  }
198 
199  return names;
200 }
201 
202 unsigned VampEffectsModule::DiscoverPluginsAtPath(
203  const wxString & path, wxString &errMsg,
204  const RegistrationCallback &callback)
205 {
206  errMsg.clear();
207  int output;
208  bool hasParameters;
209 
210  auto vp = FindPlugin(path, output, hasParameters);
211  if (vp)
212  {
213  VampEffect effect(std::move(vp), path, output, hasParameters);
214  if (callback)
215  callback( this, &effect );
216 
217  return 1;
218  }
219 
220  errMsg = _("Could not load the library");
221  return 0;
222 }
223 
224 bool VampEffectsModule::IsPluginValid(const wxString & path, bool bFast)
225 {
226  int output;
227  bool hasParameters;
228  if( bFast )
229  return true;
230 
231  auto vp = FindPlugin(path, output, hasParameters);
232  return bool(vp);
233 }
234 
235 IdentInterface *VampEffectsModule::CreateInstance(const wxString & path)
236 {
237  // Acquires a resource for the application.
238  int output;
239  bool hasParameters;
240 
241  auto vp = FindPlugin(path, output, hasParameters);
242  if (vp)
243  {
244  // Safety of this depends on complementary calls to DeleteInstance on the module manager side.
245  return safenew VampEffect(std::move(vp), path, output, hasParameters);
246  }
247 
248  return NULL;
249 }
250 
251 void VampEffectsModule::DeleteInstance(IdentInterface *instance)
252 {
253  std::unique_ptr < VampEffect > {
254  dynamic_cast<VampEffect *>(instance)
255  };
256 }
257 
258 // VampEffectsModule implementation
259 
260 std::unique_ptr<Vamp::Plugin> VampEffectsModule::FindPlugin(const wxString & path,
261  int & output,
262  bool & hasParameters)
263 {
264  PluginLoader::PluginKey key = path.BeforeLast(wxT('/')).ToUTF8().data();
265 
266  std::unique_ptr<Plugin> vp{ PluginLoader::getInstance()->loadPlugin(key, 48000) }; // rate doesn't matter here
267  if (!vp)
268  {
269  return nullptr;
270  }
271 
272  // We limit the listed plugin outputs to those whose results can
273  // readily be displayed in an Audacity label track.
274  //
275  // - Any output whose features have no values (time instants only),
276  // with or without duration, is fine
277  //
278  // - Any output whose features have more than one value, or an
279  // unknown or variable number of values, is right out
280  //
281  // - Any output whose features have exactly one value, with
282  // variable sample rate or with duration, should be OK --
283  // this implies a sparse feature, of which the time and/or
284  // duration are significant aspects worth displaying
285  //
286  // - An output whose features have exactly one value, with
287  // fixed sample rate and no duration, cannot be usefully
288  // displayed -- the value is the only significant piece of
289  // data there and we have no good value plot
290 
291  Plugin::OutputList outputs = vp->getOutputDescriptors();
292 
293  output = 0;
294 
295  hasParameters = !vp->getParameterDescriptors().empty();
296 
297  for (Plugin::OutputList::iterator j = outputs.begin(); j != outputs.end(); ++j)
298  {
299  if (j->sampleType == Plugin::OutputDescriptor::FixedSampleRate ||
300  j->sampleType == Plugin::OutputDescriptor::OneSamplePerStep ||
301  !j->hasFixedBinCount ||
302  j->binCount > 1)
303  {
304  // All of these qualities disqualify (see notes above)
305 
306  ++output;
307  continue;
308  }
309 
310  wxString name = wxString::FromUTF8(vp->getName().c_str());
311 
312  if (outputs.size() > 1)
313  {
314  // This is not the plugin's only output.
315  // Use "plugin name: output name" as the effect name,
316  // unless the output name is the same as the plugin name
317  wxString outputName = wxString::FromUTF8(j->name.c_str());
318  if (outputName != name)
319  {
320  name = wxString::Format(wxT("%s: %s"),
321  name, outputName);
322  }
323  }
324 
325  if (wxString::FromUTF8(key.c_str()) + wxT("/") + name == path)
326  {
327  return vp;
328  }
329 
330  ++output;
331  }
332 
333  return {};
334 }
335 
336 #endif
static wxArrayString names()
Definition: Tags.cpp:697
DECLARE_BUILTIN_MODULE(LadspaBuiltin)
#define XO(s)
Definition: Internat.h:30
#define safenew
Definition: Audacity.h:223
DECLARE_MODULE_ENTRY(AudacityModule)
_("Move Track &Down")+wxT("\t")+(GetActiveProject() -> GetCommandManager() ->GetKeyFromName(wxT("TrackMoveDown"))), OnMoveTrack) POPUP_MENU_ITEM(OnMoveTopID, _("Move Track to &Top")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveTop"))), OnMoveTrack) POPUP_MENU_ITEM(OnMoveBottomID, _("Move Track to &Bottom")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveBottom"))), OnMoveTrack) void TrackMenuTable::OnSetName(wxCommandEvent &)
const wxChar * name
Definition: Distortion.cpp:94