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