NO-OP: whitespace/comments
[ardour.git] / libs / ardour / plugin.cc
1 /*
2  * Copyright (C) 2000-2017 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2005-2006 Taybin Rutkin <taybin@taybin.com>
4  * Copyright (C) 2006 Jesse Chappell <jesse@essej.net>
5  * Copyright (C) 2007-2014 David Robillard <d@drobilla.net>
6  * Copyright (C) 2007-2017 Tim Mayberry <mojofunk@gmail.com>
7  * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
8  * Copyright (C) 2013-2015 John Emmas <john@creativepost.co.uk>
9  * Copyright (C) 2015-2019 Robin Gareus <robin@gareus.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #ifdef WAF_BUILD
27 #include "libardour-config.h"
28 #endif
29
30 #include <vector>
31 #include <string>
32
33 #include <cstdlib>
34 #include <cstdio> // so libraptor doesn't complain
35 #include <cmath>
36 #ifndef COMPILER_MSVC
37 #include <dirent.h>
38 #endif
39 #include <sys/stat.h>
40 #include <cerrno>
41 #include <utility>
42
43 #ifdef HAVE_LRDF
44 #include <lrdf.h>
45 #endif
46
47 #include "pbd/compose.h"
48 #include "pbd/error.h"
49 #include "pbd/xml++.h"
50
51 #include "ardour/buffer_set.h"
52 #include "ardour/chan_count.h"
53 #include "ardour/chan_mapping.h"
54 #include "ardour/data_type.h"
55 #include "ardour/luaproc.h"
56 #include "ardour/midi_buffer.h"
57 #include "ardour/midi_state_tracker.h"
58 #include "ardour/plugin.h"
59 #include "ardour/plugin_manager.h"
60 #include "ardour/port.h"
61 #include "ardour/session.h"
62 #include "ardour/types.h"
63
64 #ifdef AUDIOUNIT_SUPPORT
65 #include "ardour/audio_unit.h"
66 #endif
67
68 #ifdef LV2_SUPPORT
69 #include "ardour/lv2_plugin.h"
70 #endif
71
72 #include "pbd/stl_delete.h"
73
74 #include "pbd/i18n.h"
75 #include <locale.h>
76
77 using namespace std;
78 using namespace ARDOUR;
79 using namespace PBD;
80
81 namespace ARDOUR { class AudioEngine; }
82
83 PBD::Signal2<void, std::string, Plugin*> Plugin::PresetsChanged;
84
85 bool
86 PluginInfo::needs_midi_input () const
87 {
88         return (n_inputs.n_midi() != 0);
89 }
90
91 Plugin::Plugin (AudioEngine& e, Session& s)
92         : _engine (e)
93         , _session (s)
94         , _cycles (0)
95         , _owner (0)
96         , _for_impulse_analysis (false)
97         , _have_presets (false)
98         , _have_pending_stop_events (false)
99         , _parameter_changed_since_last_preset (false)
100         , _immediate_events(6096) // FIXME: size?
101 {
102         _pending_stop_events.ensure_buffers (DataType::MIDI, 1, 4096);
103 }
104
105 Plugin::Plugin (const Plugin& other)
106         : StatefulDestructible()
107         , HasLatency()
108         , _engine (other._engine)
109         , _session (other._session)
110         , _info (other._info)
111         , _cycles (0)
112         , _owner (other._owner)
113         , _for_impulse_analysis (false)
114         , _have_presets (false)
115         , _have_pending_stop_events (false)
116         , _last_preset (other._last_preset)
117         , _parameter_changed_since_last_preset (false)
118         , _immediate_events(6096) // FIXME: size?
119 {
120         _pending_stop_events.ensure_buffers (DataType::MIDI, 1, 4096);
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) {
132                 PBD::error << _("Trying to remove nonexistent preset.") << endmsg;
133                 return;
134         }
135         if (!p->user) {
136                 PBD::error << _("Cannot remove plugin factory preset.") << endmsg;
137                 return;
138         }
139
140         do_remove_preset (name);
141         _presets.erase (p->uri);
142
143         _last_preset.uri = "";
144         _parameter_changed_since_last_preset = false;
145         _have_presets = false;
146         PresetsChanged (unique_id(), this); /* EMIT SIGNAL */
147         PresetRemoved (); /* EMIT SIGNAL */
148 }
149
150 Plugin::PresetRecord
151 Plugin::save_preset (string name)
152 {
153         if (preset_by_label (name)) {
154                 PBD::error << _("Preset with given name already exists.") << endmsg;
155                 return Plugin::PresetRecord ();
156         }
157
158         string const uri = do_save_preset (name);
159
160         if (!uri.empty()) {
161                 _presets.insert (make_pair (uri, PresetRecord (uri, name)));
162                 _have_presets = false;
163                 PresetsChanged (unique_id(), this); /* EMIT SIGNAL */
164                 PresetAdded (); /* EMIT SIGNAL */
165         }
166
167         return PresetRecord (uri, name);
168 }
169
170 PluginPtr
171 ARDOUR::find_plugin(Session& session, string identifier, PluginType type)
172 {
173         PluginManager& mgr (PluginManager::instance());
174         PluginInfoList plugs;
175
176         switch (type) {
177         case ARDOUR::Lua:
178                 plugs = mgr.lua_plugin_info();
179                 break;
180
181         case ARDOUR::LADSPA:
182                 plugs = mgr.ladspa_plugin_info();
183                 break;
184
185 #ifdef LV2_SUPPORT
186         case ARDOUR::LV2:
187                 plugs = mgr.lv2_plugin_info();
188                 break;
189 #endif
190
191 #ifdef WINDOWS_VST_SUPPORT
192         case ARDOUR::Windows_VST:
193                 plugs = mgr.windows_vst_plugin_info();
194                 break;
195 #endif
196
197 #ifdef LXVST_SUPPORT
198         case ARDOUR::LXVST:
199                 plugs = mgr.lxvst_plugin_info();
200                 break;
201 #endif
202
203 #ifdef MACVST_SUPPORT
204         case ARDOUR::MacVST:
205                 plugs = mgr.mac_vst_plugin_info();
206                 break;
207 #endif
208
209 #ifdef AUDIOUNIT_SUPPORT
210         case ARDOUR::AudioUnit:
211                 plugs = mgr.au_plugin_info();
212                 break;
213 #endif
214
215         default:
216                 return PluginPtr ((Plugin *) 0);
217         }
218
219         PluginInfoList::iterator i;
220
221         for (i = plugs.begin(); i != plugs.end(); ++i) {
222                 if (identifier == (*i)->unique_id){
223                         return (*i)->load (session);
224                 }
225         }
226
227 #ifdef WINDOWS_VST_SUPPORT
228         /* hmm, we didn't find it. could be because in older versions of Ardour.
229            we used to store the name of a VST plugin, not its unique ID. so try
230            again.
231         */
232
233         for (i = plugs.begin(); i != plugs.end(); ++i) {
234                 if (identifier == (*i)->name){
235                         return (*i)->load (session);
236                 }
237         }
238 #endif
239
240 #ifdef LXVST_SUPPORT
241         /* hmm, we didn't find it. could be because in older versions of Ardour.
242            we used to store the name of a VST plugin, not its unique ID. so try
243            again.
244         */
245
246         for (i = plugs.begin(); i != plugs.end(); ++i) {
247                 if (identifier == (*i)->name){
248                         return (*i)->load (session);
249                 }
250         }
251 #endif
252
253         return PluginPtr ((Plugin*) 0);
254 }
255
256 ChanCount
257 Plugin::output_streams () const
258 {
259         /* LADSPA & VST should not get here because they do not
260            return "infinite" i/o counts.
261         */
262         return ChanCount::ZERO;
263 }
264
265 ChanCount
266 Plugin::input_streams () const
267 {
268         /* LADSPA & VST should not get here because they do not
269            return "infinite" i/o counts.
270         */
271         return ChanCount::ZERO;
272 }
273
274 Plugin::IOPortDescription
275 Plugin::describe_io_port (ARDOUR::DataType dt, bool input, uint32_t id) const
276 {
277         std::stringstream ss;
278         switch (dt) {
279                 case DataType::AUDIO:
280                         ss << _("Audio") << " ";
281                         break;
282                 case DataType::MIDI:
283                         ss << _("Midi") << " ";
284                         break;
285                 default:
286                         ss << _("?") << " ";
287                         break;
288         }
289         if (input) {
290                 ss << _("In") << " ";
291         } else {
292                 ss << _("Out") << " ";
293         }
294
295         std::stringstream gn;
296         gn << ss.str();
297
298         ss << (id + 1);
299         gn << (id / 2 + 1) << " L/R";
300
301         Plugin::IOPortDescription iod (ss.str());
302         iod.group_name = gn.str();
303         iod.group_channel = id % 2;
304         return iod;
305 }
306
307 PluginOutputConfiguration
308 Plugin::possible_output () const
309 {
310         PluginOutputConfiguration oc;
311         if (_info) {
312                 oc.insert (_info->n_outputs.n_audio ());
313         }
314         return oc;
315 }
316
317 const Plugin::PresetRecord *
318 Plugin::preset_by_label (const string& label)
319 {
320         if (!_have_presets) {
321                 find_presets ();
322                 _have_presets = true;
323         }
324
325         // FIXME: O(n)
326         for (map<string, PresetRecord>::const_iterator i = _presets.begin(); i != _presets.end(); ++i) {
327                 if (i->second.label == label) {
328                         return &i->second;
329                 }
330         }
331
332         return 0;
333 }
334
335 const Plugin::PresetRecord *
336 Plugin::preset_by_uri (const string& uri)
337 {
338         if (!_have_presets) {
339                 find_presets ();
340                 _have_presets = true;
341         }
342
343         map<string, PresetRecord>::const_iterator pr = _presets.find (uri);
344         if (pr != _presets.end()) {
345                 return &pr->second;
346         } else {
347                 return 0;
348         }
349 }
350
351 bool
352 Plugin::write_immediate_event (size_t size, const uint8_t* buf)
353 {
354         if (!Evoral::midi_event_is_valid (buf, size)) {
355                 return false;
356         }
357         return (_immediate_events.write (0, Evoral::MIDI_EVENT, size, buf) == size);
358 }
359
360 int
361 Plugin::connect_and_run (BufferSet& bufs,
362                 samplepos_t /*start*/, samplepos_t /*end*/, double /*speed*/,
363                 ChanMapping const& /*in_map*/, ChanMapping const& /*out_map*/,
364                 pframes_t nframes, samplecnt_t /*offset*/)
365 {
366         if (bufs.count().n_midi() > 0) {
367
368                 if (_immediate_events.read_space() && nframes > 0) {
369                         _immediate_events.read (bufs.get_midi (0), 0, 1, nframes - 1, true);
370                 }
371
372                 /* Track notes that we are sending to the plugin */
373                 const MidiBuffer& b = bufs.get_midi (0);
374
375                 _tracker.track (b.begin(), b.end());
376
377                 if (_have_pending_stop_events) {
378                         /* Transmit note-offs that are pending from the last transport stop */
379                         bufs.merge_from (_pending_stop_events, 0);
380                         _have_pending_stop_events = false;
381                 }
382         }
383
384         return 0;
385 }
386
387 void
388 Plugin::realtime_handle_transport_stopped ()
389 {
390         resolve_midi ();
391 }
392
393 void
394 Plugin::realtime_locate ()
395 {
396         resolve_midi ();
397 }
398
399 void
400 Plugin::monitoring_changed ()
401 {
402         resolve_midi ();
403 }
404
405 void
406 Plugin::resolve_midi ()
407 {
408         /* Create note-offs for any active notes and put them in _pending_stop_events, to be picked
409            up on the next call to connect_and_run ().
410         */
411
412         _pending_stop_events.get_midi(0).clear ();
413         _tracker.resolve_notes (_pending_stop_events.get_midi (0), 0);
414         _have_pending_stop_events = true;
415 }
416
417 vector<Plugin::PresetRecord>
418 Plugin::get_presets ()
419 {
420         vector<PresetRecord> p;
421
422         if (!_have_presets) {
423                 find_presets ();
424                 _have_presets = true;
425         }
426
427         for (map<string, PresetRecord>::const_iterator i = _presets.begin(); i != _presets.end(); ++i) {
428                 p.push_back (i->second);
429         }
430
431         return p;
432 }
433
434 bool
435 Plugin::load_preset (PresetRecord r)
436 {
437         _last_preset = r;
438         _parameter_changed_since_last_preset = false;
439
440         _session.set_dirty ();
441         PresetLoaded (); /* EMIT SIGNAL */
442         return true;
443 }
444
445 void
446 Plugin::clear_preset ()
447 {
448         _last_preset.uri = "";
449         _last_preset.label = "";
450         _parameter_changed_since_last_preset = false;
451
452         _session.set_dirty ();
453         PresetLoaded (); /* EMIT SIGNAL */
454 }
455
456 void
457 Plugin::set_parameter (uint32_t /* which */, float /* value */)
458 {
459         _parameter_changed_since_last_preset = true;
460         PresetDirty (); /* EMIT SIGNAL */
461 }
462
463 void
464 Plugin::parameter_changed_externally (uint32_t which, float /* value */)
465 {
466         _parameter_changed_since_last_preset = true;
467         _session.set_dirty ();
468         ParameterChangedExternally (which, get_parameter (which)); /* EMIT SIGNAL */
469         PresetDirty (); /* EMIT SIGNAL */
470 }
471
472 int
473 Plugin::set_state (const XMLNode& node, int /*version*/)
474 {
475         std::string preset_uri;
476         const Plugin::PresetRecord* r = 0;
477         if (node.get_property (X_("last-preset-uri"), preset_uri)) {
478                 r = preset_by_uri (preset_uri);
479         }
480         if (r) {
481                 _last_preset = *r;
482                 node.get_property (X_("parameter-changed-since-last-preset"), _parameter_changed_since_last_preset); // XXX
483         } else {
484                 _last_preset.uri = "";
485                 _last_preset.valid = false;
486         }
487         return 0;
488 }
489
490 XMLNode &
491 Plugin::get_state ()
492 {
493         XMLNode* root = new XMLNode (state_node_name ());
494
495         root->set_property (X_("last-preset-uri"), _last_preset.uri);
496         root->set_property (X_("last-preset-label"), _last_preset.label);
497         root->set_property (X_("parameter-changed-since-last-preset"), _parameter_changed_since_last_preset);
498
499         add_state (root);
500
501         return *root;
502 }
503
504 std::string
505 Plugin::parameter_label (uint32_t which) const
506 {
507         if (which >= parameter_count ()) {
508                 return "";
509         }
510         ParameterDescriptor pd;
511         get_parameter_descriptor (which, pd);
512         return pd.label;
513 }
514
515 bool
516 PluginInfo::is_effect () const
517 {
518         return (!is_instrument () && !is_utility ()  && !is_analyzer ());
519 }
520
521 bool
522 PluginInfo::is_instrument () const
523 {
524         if (category == "Instrument") {
525                 return true;
526         }
527
528         // second check: if we have  midi input and audio output, we're likely an instrument
529         return (n_inputs.n_midi() != 0) && (n_outputs.n_audio() > 0) && (n_inputs.n_audio() == 0);
530 }
531
532 bool
533 PluginInfo::is_utility () const
534 {
535         /* XXX beware of translations, e.g. LV2 categories */
536         return (category == "Utility" || category == "MIDI" || category == "Generator");
537 }
538
539 bool
540 PluginInfo::is_analyzer () const
541 {
542         return (category == "Analyser" || category == "Anaylsis" || category == "Analyzer");
543 }