API evolution
[ardour.git] / libs / ardour / plugin.cc
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"
50 #include "ardour/midi_state_tracker.h"
51 #include "ardour/plugin.h"
52 #include "ardour/plugin_manager.h"
53 #include "ardour/port.h"
54 #include "ardour/session.h"
55 #include "ardour/types.h"
56
57 #ifdef AUDIOUNIT_SUPPORT
58 #include "ardour/audio_unit.h"
59 #endif
60
61 #ifdef LV2_SUPPORT
62 #include "ardour/lv2_plugin.h"
63 #endif
64
65 #include "pbd/stl_delete.h"
66
67 #include "i18n.h"
68 #include <locale.h>
69
70 using namespace std;
71 using namespace ARDOUR;
72 using namespace PBD;
73
74 namespace ARDOUR { class AudioEngine; }
75
76 #ifdef NO_PLUGIN_STATE
77 static bool seen_get_state_message = false;
78 static bool seen_set_state_message = false;
79 #endif
80
81 PBD::Signal2<void, std::string, Plugin*> Plugin::PresetsChanged;
82
83 bool
84 PluginInfo::is_instrument () const
85 {
86         return (n_inputs.n_midi() != 0) && (n_outputs.n_audio() > 0);
87 }
88
89 Plugin::Plugin (AudioEngine& e, Session& s)
90         : _engine (e)
91         , _session (s)
92         , _cycles (0)
93         , _have_presets (false)
94         , _have_pending_stop_events (false)
95         , _parameter_changed_since_last_preset (false)
96 {
97         _pending_stop_events.ensure_buffers (DataType::MIDI, 1, 4096);
98         PresetsChanged.connect_same_thread (_preset_connection, boost::bind (&Plugin::update_presets, this, _1 ,_2));
99 }
100
101 Plugin::Plugin (const Plugin& other)
102         : StatefulDestructible()
103         , Latent()
104         , _engine (other._engine)
105         , _session (other._session)
106         , _info (other._info)
107         , _cycles (0)
108         , _have_presets (false)
109         , _have_pending_stop_events (false)
110         , _parameter_changed_since_last_preset (false)
111 {
112         _pending_stop_events.ensure_buffers (DataType::MIDI, 1, 4096);
113         PresetsChanged.connect_same_thread (_preset_connection, boost::bind (&Plugin::update_presets, this, _1 ,_2));
114 }
115
116 Plugin::~Plugin ()
117 {
118 }
119
120 void
121 Plugin::remove_preset (string name)
122 {
123         Plugin::PresetRecord const * p = preset_by_label (name);
124         if (!p->user) {
125                 PBD::error << _("Cannot remove plugin factory preset.") << endmsg;
126                 return;
127         }
128
129         do_remove_preset (name);
130         _presets.erase (preset_by_label (name)->uri);
131
132         _last_preset.uri = "";
133         _parameter_changed_since_last_preset = false;
134         PresetRemoved (); /* EMIT SIGNAL */
135         PresetsChanged (unique_id(), this); /* EMIT SIGNAL */
136 }
137
138 /** @return PresetRecord with empty URI on failure */
139 Plugin::PresetRecord
140 Plugin::save_preset (string name)
141 {
142         if (preset_by_label (name)) {
143                 PBD::error << _("Preset with given name already exists.") << endmsg;
144                 return Plugin::PresetRecord ();
145         }
146
147         string const uri = do_save_preset (name);
148
149         if (!uri.empty()) {
150                 _presets.insert (make_pair (uri, PresetRecord (uri, name)));
151                 PresetAdded (); /* EMIT SIGNAL */
152                 PresetsChanged (unique_id(), this); /* EMIT SIGNAL */
153         }
154
155         return PresetRecord (uri, name);
156 }
157
158 PluginPtr
159 ARDOUR::find_plugin(Session& session, string identifier, PluginType type)
160 {
161         PluginManager& mgr (PluginManager::instance());
162         PluginInfoList plugs;
163
164         switch (type) {
165         case ARDOUR::LADSPA:
166                 plugs = mgr.ladspa_plugin_info();
167                 break;
168
169 #ifdef LV2_SUPPORT
170         case ARDOUR::LV2:
171                 plugs = mgr.lv2_plugin_info();
172                 break;
173 #endif
174
175 #ifdef WINDOWS_VST_SUPPORT
176         case ARDOUR::Windows_VST:
177                 plugs = mgr.windows_vst_plugin_info();
178                 break;
179 #endif
180
181 #ifdef LXVST_SUPPORT
182         case ARDOUR::LXVST:
183                 plugs = mgr.lxvst_plugin_info();
184                 break;
185 #endif
186
187 #ifdef AUDIOUNIT_SUPPORT
188         case ARDOUR::AudioUnit:
189                 plugs = mgr.au_plugin_info();
190                 break;
191 #endif
192
193         default:
194                 return PluginPtr ((Plugin *) 0);
195         }
196
197         PluginInfoList::iterator i;
198
199         for (i = plugs.begin(); i != plugs.end(); ++i) {
200                 if (identifier == (*i)->unique_id){
201                         return (*i)->load (session);
202                 }
203         }
204
205 #ifdef WINDOWS_VST_SUPPORT
206         /* hmm, we didn't find it. could be because in older versions of Ardour.
207            we used to store the name of a VST plugin, not its unique ID. so try
208            again.
209         */
210
211         for (i = plugs.begin(); i != plugs.end(); ++i) {
212                 if (identifier == (*i)->name){
213                         return (*i)->load (session);
214                 }
215         }
216 #endif
217
218 #ifdef LXVST_SUPPORT
219         /* hmm, we didn't find it. could be because in older versions of Ardour.
220            we used to store the name of a VST plugin, not its unique ID. so try
221            again.
222         */
223
224         for (i = plugs.begin(); i != plugs.end(); ++i) {
225                 if (identifier == (*i)->name){
226                         return (*i)->load (session);
227                 }
228         }
229 #endif
230
231         return PluginPtr ((Plugin*) 0);
232 }
233
234 ChanCount
235 Plugin::output_streams () const
236 {
237         /* LADSPA & VST should not get here because they do not
238            return "infinite" i/o counts.
239         */
240         return ChanCount::ZERO;
241 }
242
243 ChanCount
244 Plugin::input_streams () const
245 {
246         /* LADSPA & VST should not get here because they do not
247            return "infinite" i/o counts.
248         */
249         return ChanCount::ZERO;
250 }
251
252 const Plugin::PresetRecord *
253 Plugin::preset_by_label (const string& label)
254 {
255         // FIXME: O(n)
256         for (map<string, PresetRecord>::const_iterator i = _presets.begin(); i != _presets.end(); ++i) {
257                 if (i->second.label == label) {
258                         return &i->second;
259                 }
260         }
261
262         return 0;
263 }
264
265 const Plugin::PresetRecord *
266 Plugin::preset_by_uri (const string& uri)
267 {
268         map<string, PresetRecord>::const_iterator pr = _presets.find (uri);
269         if (pr != _presets.end()) {
270                 return &pr->second;
271         } else {
272                 return 0;
273         }
274 }
275
276 int
277 Plugin::connect_and_run (BufferSet& bufs,
278                          ChanMapping /*in_map*/, ChanMapping /*out_map*/,
279                          pframes_t /* nframes */, framecnt_t /*offset*/)
280 {
281         if (bufs.count().n_midi() > 0) {
282
283                 /* Track notes that we are sending to the plugin */
284
285                 const MidiBuffer& b = bufs.get_midi (0);
286
287                 _tracker.track (b.begin(), b.end());
288
289                 if (_have_pending_stop_events) {
290                         /* Transmit note-offs that are pending from the last transport stop */
291                         bufs.merge_from (_pending_stop_events, 0);
292                         _have_pending_stop_events = false;
293                 }
294         }
295
296         return 0;
297 }
298
299 void
300 Plugin::realtime_handle_transport_stopped ()
301 {
302         resolve_midi ();
303 }
304
305 void
306 Plugin::realtime_locate ()
307 {
308         resolve_midi ();
309 }
310
311 void
312 Plugin::monitoring_changed ()
313 {
314         resolve_midi ();
315 }
316
317 void
318 Plugin::resolve_midi ()
319 {
320         /* Create note-offs for any active notes and put them in _pending_stop_events, to be picked
321            up on the next call to connect_and_run ().
322         */
323
324         _pending_stop_events.get_midi(0).clear ();
325         _tracker.resolve_notes (_pending_stop_events.get_midi (0), /* split cycle offset*/ Port::port_offset());
326         _have_pending_stop_events = true;
327 }
328
329 void
330 Plugin::update_presets (std::string src_unique_id, Plugin* src )
331 {
332         if (src == this || unique_id() != src_unique_id) {
333                 return;
334         }
335         _have_presets = false;
336         // TODO check if a preset was added/removed and emit the proper signal
337         // so far no subscriber distinguishes between PresetAdded and PresetRemoved
338         PresetAdded();
339 }
340
341 vector<Plugin::PresetRecord>
342 Plugin::get_presets ()
343 {
344         vector<PresetRecord> p;
345
346 #ifndef NO_PLUGIN_STATE
347         if (!_have_presets) {
348                 find_presets ();
349                 _have_presets = true;
350         }
351
352         for (map<string, PresetRecord>::const_iterator i = _presets.begin(); i != _presets.end(); ++i) {
353                 p.push_back (i->second);
354         }
355 #else
356         if (!seen_set_state_message) {
357                 info << string_compose (_("Plugin presets are not supported in this build of %1. Consider paying for a full version"),
358                                         PROGRAM_NAME)
359                      << endmsg;
360                 seen_set_state_message = true;
361         }
362 #endif
363
364         return p;
365 }
366
367 /** Set parameters using a preset */
368 bool
369 Plugin::load_preset (PresetRecord r)
370 {
371         _last_preset = r;
372         _parameter_changed_since_last_preset = false;
373
374         PresetLoaded (); /* EMIT SIGNAL */
375         return true;
376 }
377
378 void
379 Plugin::clear_preset ()
380 {
381         _last_preset.uri = "";
382         _last_preset.label = "";
383         _parameter_changed_since_last_preset = false;
384
385         PresetLoaded (); /* EMIT SIGNAL */
386 }
387
388 void
389 Plugin::set_parameter (uint32_t /* which */, float /* value */)
390 {
391         _parameter_changed_since_last_preset = true;
392         _session.set_dirty ();
393         PresetDirty (); /* EMIT SIGNAL */
394 }
395
396 void
397 Plugin::parameter_changed_externally (uint32_t which, float /* value */)
398 {
399         _parameter_changed_since_last_preset = true;
400         _session.set_dirty ();
401         ParameterChangedExternally (which, get_parameter (which)); /* EMIT SIGNAL */
402         PresetDirty (); /* EMIT SIGNAL */
403 }
404
405 int
406 Plugin::set_state (const XMLNode& node, int /*version*/)
407 {
408         XMLProperty const * p = node.property (X_("last-preset-uri"));
409         if (p) {
410                 _last_preset.uri = p->value ();
411         }
412
413         p = node.property (X_("last-preset-label"));
414         if (p) {
415                 _last_preset.label = p->value ();
416         }
417
418         p = node.property (X_("parameter-changed-since-last-preset"));
419         if (p) {
420                 _parameter_changed_since_last_preset = string_is_affirmative (p->value ());
421         }
422
423         return 0;
424 }
425
426 XMLNode &
427 Plugin::get_state ()
428 {
429         XMLNode* root = new XMLNode (state_node_name ());
430         LocaleGuard lg (X_("C"));
431
432         root->add_property (X_("last-preset-uri"), _last_preset.uri);
433         root->add_property (X_("last-preset-label"), _last_preset.label);
434         root->add_property (X_("parameter-changed-since-last-preset"), _parameter_changed_since_last_preset ? X_("yes") : X_("no"));
435
436 #ifndef NO_PLUGIN_STATE
437         add_state (root);
438 #else
439         if (!seen_get_state_message) {
440                 info << string_compose (_("Saving plugin settings is not supported in this build of %1. Consider paying for the full version"),
441                                         PROGRAM_NAME)
442                      << endmsg;
443                 seen_get_state_message = true;
444         }
445 #endif
446
447         return *root;
448 }
449
450 void
451 Plugin::set_info (PluginInfoPtr info)
452 {
453         _info = info;
454 }
455
456