ardour
plugin.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2000-2002 Paul Davis
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 
18 */
19 
20 #ifdef WAF_BUILD
21 #include "libardour-config.h"
22 #endif
23 
24 #include <vector>
25 #include <string>
26 
27 #include <cstdlib>
28 #include <cstdio> // so libraptor doesn't complain
29 #include <cmath>
30 #ifndef COMPILER_MSVC
31 #include <dirent.h>
32 #endif
33 #include <sys/stat.h>
34 #include <cerrno>
35 #include <utility>
36 
37 #ifdef HAVE_LRDF
38 #include <lrdf.h>
39 #endif
40 
41 #include "pbd/compose.h"
42 #include "pbd/error.h"
43 #include "pbd/xml++.h"
44 
45 #include "ardour/buffer_set.h"
46 #include "ardour/chan_count.h"
47 #include "ardour/chan_mapping.h"
48 #include "ardour/data_type.h"
49 #include "ardour/midi_buffer.h"
51 #include "ardour/plugin.h"
52 #include "ardour/plugin_manager.h"
53 #include "ardour/session.h"
54 #include "ardour/types.h"
55 
56 #ifdef AUDIOUNIT_SUPPORT
57 #include "ardour/audio_unit.h"
58 #endif
59 
60 #ifdef LV2_SUPPORT
61 #include "ardour/lv2_plugin.h"
62 #endif
63 
64 #include "pbd/stl_delete.h"
65 
66 #include "i18n.h"
67 #include <locale.h>
68 
69 using namespace std;
70 using namespace ARDOUR;
71 using namespace PBD;
72 
73 namespace ARDOUR { class AudioEngine; }
74 
75 #ifdef NO_PLUGIN_STATE
76 static bool seen_get_state_message = false;
77 static bool seen_set_state_message = false;
78 #endif
79 
80 bool
81 PluginInfo::is_instrument () const
82 {
83  return (n_inputs.n_midi() != 0) && (n_outputs.n_audio() > 0);
84 }
85 
86 Plugin::Plugin (AudioEngine& e, Session& s)
87  : _engine (e)
88  , _session (s)
89  , _cycles (0)
90  , _have_presets (false)
91  , _have_pending_stop_events (false)
92  , _parameter_changed_since_last_preset (false)
93 {
95 }
96 
97 Plugin::Plugin (const Plugin& other)
99  , Latent()
100  , _engine (other._engine)
101  , _session (other._session)
102  , _info (other._info)
103  , _cycles (0)
104  , _have_presets (false)
105  , _have_pending_stop_events (false)
106  , _parameter_changed_since_last_preset (false)
107 {
109 }
110 
112 {
113 }
114 
115 void
117 {
118  do_remove_preset (name);
119  _presets.erase (preset_by_label (name)->uri);
120 
121  _last_preset.uri = "";
123  PresetRemoved (); /* EMIT SIGNAL */
124 }
125 
129 {
130  string const uri = do_save_preset (name);
131 
132  if (!uri.empty()) {
133  _presets.insert (make_pair (uri, PresetRecord (uri, name)));
134  PresetAdded (); /* EMIT SIGNAL */
135  }
136 
137  return PresetRecord (uri, name);
138 }
139 
140 PluginPtr
141 ARDOUR::find_plugin(Session& session, string identifier, PluginType type)
142 {
144  PluginInfoList plugs;
145 
146  switch (type) {
147  case ARDOUR::LADSPA:
148  plugs = mgr.ladspa_plugin_info();
149  break;
150 
151 #ifdef LV2_SUPPORT
152  case ARDOUR::LV2:
153  plugs = mgr.lv2_plugin_info();
154  break;
155 #endif
156 
157 #ifdef WINDOWS_VST_SUPPORT
158  case ARDOUR::Windows_VST:
159  plugs = mgr.windows_vst_plugin_info();
160  break;
161 #endif
162 
163 #ifdef LXVST_SUPPORT
164  case ARDOUR::LXVST:
165  plugs = mgr.lxvst_plugin_info();
166  break;
167 #endif
168 
169 #ifdef AUDIOUNIT_SUPPORT
170  case ARDOUR::AudioUnit:
171  plugs = mgr.au_plugin_info();
172  break;
173 #endif
174 
175  default:
176  return PluginPtr ((Plugin *) 0);
177  }
178 
179  PluginInfoList::iterator i;
180 
181  for (i = plugs.begin(); i != plugs.end(); ++i) {
182  if (identifier == (*i)->unique_id){
183  return (*i)->load (session);
184  }
185  }
186 
187 #ifdef WINDOWS_VST_SUPPORT
188  /* hmm, we didn't find it. could be because in older versions of Ardour.
189  we used to store the name of a VST plugin, not its unique ID. so try
190  again.
191  */
192 
193  for (i = plugs.begin(); i != plugs.end(); ++i) {
194  if (identifier == (*i)->name){
195  return (*i)->load (session);
196  }
197  }
198 #endif
199 
200 #ifdef LXVST_SUPPORT
201  /* hmm, we didn't find it. could be because in older versions of Ardour.
202  we used to store the name of a VST plugin, not its unique ID. so try
203  again.
204  */
205 
206  for (i = plugs.begin(); i != plugs.end(); ++i) {
207  if (identifier == (*i)->name){
208  return (*i)->load (session);
209  }
210  }
211 #endif
212 
213  return PluginPtr ((Plugin*) 0);
214 }
215 
216 ChanCount
218 {
219  /* LADSPA & VST should not get here because they do not
220  return "infinite" i/o counts.
221  */
222  return ChanCount::ZERO;
223 }
224 
225 ChanCount
227 {
228  /* LADSPA & VST should not get here because they do not
229  return "infinite" i/o counts.
230  */
231  return ChanCount::ZERO;
232 }
233 
234 const Plugin::PresetRecord *
235 Plugin::preset_by_label (const string& label)
236 {
237  // FIXME: O(n)
238  for (map<string, PresetRecord>::const_iterator i = _presets.begin(); i != _presets.end(); ++i) {
239  if (i->second.label == label) {
240  return &i->second;
241  }
242  }
243 
244  return 0;
245 }
246 
247 const Plugin::PresetRecord *
248 Plugin::preset_by_uri (const string& uri)
249 {
250  map<string, PresetRecord>::const_iterator pr = _presets.find (uri);
251  if (pr != _presets.end()) {
252  return &pr->second;
253  } else {
254  return 0;
255  }
256 }
257 
258 int
260  ChanMapping /*in_map*/, ChanMapping /*out_map*/,
261  pframes_t /* nframes */, framecnt_t /*offset*/)
262 {
263  if (bufs.count().n_midi() > 0) {
264 
265  /* Track notes that we are sending to the plugin */
266 
267  const MidiBuffer& b = bufs.get_midi (0);
268 
269  _tracker.track (b.begin(), b.end());
270 
272  /* Transmit note-offs that are pending from the last transport stop */
275  }
276  }
277 
278  return 0;
279 }
280 
281 void
283 {
284  resolve_midi ();
285 }
286 
287 void
289 {
290  resolve_midi ();
291 }
292 
293 void
295 {
296  resolve_midi ();
297 }
298 
299 void
301 {
302  /* Create note-offs for any active notes and put them in _pending_stop_events, to be picked
303  up on the next call to connect_and_run ().
304  */
305 
309 }
310 
311 
312 vector<Plugin::PresetRecord>
314 {
315  vector<PresetRecord> p;
316 
317 #ifndef NO_PLUGIN_STATE
318  if (!_have_presets) {
319  find_presets ();
320  _have_presets = true;
321  }
322 
323  for (map<string, PresetRecord>::const_iterator i = _presets.begin(); i != _presets.end(); ++i) {
324  p.push_back (i->second);
325  }
326 #else
327  if (!seen_set_state_message) {
328  info << string_compose (_("Plugin presets are not supported in this build of %1. Consider paying for a full version"),
329  PROGRAM_NAME)
330  << endmsg;
331  seen_set_state_message = true;
332  }
333 #endif
334 
335  return p;
336 }
337 
339 bool
341 {
342  _last_preset = r;
344 
345  PresetLoaded (); /* EMIT SIGNAL */
346  return true;
347 }
348 
349 void
351 {
352  _last_preset.uri = "";
353  _last_preset.label = "";
355 
356  PresetLoaded (); /* EMIT SIGNAL */
357 }
358 
360 void
361 Plugin::set_parameter (uint32_t which, float)
362 {
364  _session.set_dirty ();
365  ParameterChanged (which, get_parameter (which)); /* EMIT SIGNAL */
366 }
367 
368 int
369 Plugin::set_state (const XMLNode& node, int /*version*/)
370 {
371  XMLProperty const * p = node.property (X_("last-preset-uri"));
372  if (p) {
373  _last_preset.uri = p->value ();
374  }
375 
376  p = node.property (X_("last-preset-label"));
377  if (p) {
378  _last_preset.label = p->value ();
379  }
380 
381  p = node.property (X_("parameter-changed-since-last-preset"));
382  if (p) {
384  }
385 
386  return 0;
387 }
388 
389 XMLNode &
391 {
392  XMLNode* root = new XMLNode (state_node_name ());
393  LocaleGuard lg (X_("C"));
394 
395  root->add_property (X_("last-preset-uri"), _last_preset.uri);
396  root->add_property (X_("last-preset-label"), _last_preset.label);
397  root->add_property (X_("parameter-changed-since-last-preset"), _parameter_changed_since_last_preset ? X_("yes") : X_("no"));
398 
399 #ifndef NO_PLUGIN_STATE
400  add_state (root);
401 #else
402  if (!seen_get_state_message) {
403  info << string_compose (_("Saving plugin settings is not supported in this build of %1. Consider paying for the full version"),
404  PROGRAM_NAME)
405  << endmsg;
406  seen_get_state_message = true;
407  }
408 #endif
409 
410  return *root;
411 }
412 
413 void
415 {
416  _info = info;
417 }
418 
419 
virtual ChanCount output_streams() const
Definition: plugin.cc:217
void ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity)
Definition: buffer_set.cc:149
Plugin(ARDOUR::AudioEngine &, ARDOUR::Session &)
Definition: plugin.cc:86
virtual int set_state(const XMLNode &, int version)
Definition: plugin.cc:369
const PresetRecord * preset_by_uri(const std::string &)
Definition: plugin.cc:248
MidiBuffer & get_midi(size_t i)
Definition: buffer_set.h:107
const std::string & value() const
Definition: xml++.h:159
virtual ChanCount input_streams() const
Definition: plugin.cc:226
PresetRecord _last_preset
Definition: plugin.h:303
virtual void set_parameter(uint32_t which, float val)
Definition: plugin.cc:361
PresetRecord save_preset(std::string)
Definition: plugin.cc:128
virtual float get_parameter(uint32_t which) const =0
BufferSet _pending_stop_events
Definition: plugin.h:301
uint32_t pframes_t
Definition: types.h:61
void track(const MidiBuffer::const_iterator &from, const MidiBuffer::const_iterator &to)
Definition: Beats.hpp:239
PluginInfoPtr _info
Definition: plugin.h:287
std::ostream & endmsg(std::ostream &ostr)
Definition: transmitter.h:71
virtual ~Plugin()
Definition: plugin.cc:111
PBD::Signal2< void, uint32_t, float > ParameterChanged
Definition: plugin.h:214
uint32_t n_midi() const
Definition: chan_count.h:66
virtual int connect_and_run(BufferSet &bufs, ChanMapping in, ChanMapping out, pframes_t nframes, framecnt_t offset)
Definition: plugin.cc:259
virtual std::string do_save_preset(std::string)=0
#define _(Text)
Definition: i18n.h:11
const PresetRecord * preset_by_label(const std::string &)
Definition: plugin.cc:235
void resolve_notes(MidiBuffer &buffer, framepos_t time)
boost::shared_ptr< Plugin > PluginPtr
Definition: plugin.h:50
#define X_(Text)
Definition: i18n.h:13
int64_t framecnt_t
Definition: types.h:76
XMLProperty * property(const char *)
Definition: xml++.cc:413
PBD::Signal0< void > PresetLoaded
Definition: plugin.h:209
bool string_is_affirmative(const std::string &str)
Definition: convert.cc:282
Definition: amp.h:29
std::map< std::string, PresetRecord > _presets
Definition: plugin.h:289
virtual void add_state(XMLNode *) const =0
MidiStateTracker _tracker
Definition: plugin.h:300
PBD::Signal0< void > PresetAdded
Definition: plugin.h:205
void realtime_locate()
Definition: plugin.cc:288
bool _have_pending_stop_events
Definition: plugin.h:302
void resolve_midi()
Definition: plugin.cc:300
bool _parameter_changed_since_last_preset
Definition: plugin.h:304
LIBPBD_API Transmitter info
void monitoring_changed()
Definition: plugin.cc:294
XMLProperty * add_property(const char *name, const std::string &value)
virtual void find_presets()=0
static PluginManager & instance()
void merge_from(const BufferSet &in, framecnt_t nframes)
Definition: buffer_set.cc:445
const char * name
Definition: xml++.h:95
const ChanCount & count() const
Definition: buffer_set.h:87
PluginPtr find_plugin(ARDOUR::Session &, std::string unique_id, ARDOUR::PluginType)
virtual void clear()
Definition: buffer.h:70
ARDOUR::Session & _session
Definition: plugin.h:286
Definition: debug.h:30
iterator begin()
Definition: midi_buffer.h:127
std::list< PluginInfoPtr > PluginInfoList
Definition: plugin.h:90
void realtime_handle_transport_stopped()
Definition: plugin.cc:282
bool _have_presets
Definition: plugin.h:299
virtual std::string state_node_name() const =0
static const ChanCount ZERO
Definition: chan_count.h:149
PBD::Signal0< void > PresetRemoved
Definition: plugin.h:206
std::vector< PresetRecord > get_presets()
Definition: plugin.cc:313
virtual bool load_preset(PresetRecord)
Definition: plugin.cc:340
virtual void set_info(const PluginInfoPtr inf)
Definition: plugin.cc:414
void remove_preset(std::string)
Definition: plugin.cc:116
std::string string_compose(const std::string &fmt, const T1 &o1)
Definition: compose.h:208
virtual void do_remove_preset(std::string)=0
XMLNode & get_state()
Definition: plugin.cc:390
void clear_preset()
Definition: plugin.cc:350