make BusProfile argument to new Session constructor be const (and in associated call...
[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         return iod;
304 }
305
306 PluginOutputConfiguration
307 Plugin::possible_output () const
308 {
309         PluginOutputConfiguration oc;
310         if (_info) {
311                 oc.insert (_info->n_outputs.n_audio ());
312         }
313         return oc;
314 }
315
316 const Plugin::PresetRecord *
317 Plugin::preset_by_label (const string& label)
318 {
319         if (!_have_presets) {
320                 find_presets ();
321                 _have_presets = true;
322         }
323
324         // FIXME: O(n)
325         for (map<string, PresetRecord>::const_iterator i = _presets.begin(); i != _presets.end(); ++i) {
326                 if (i->second.label == label) {
327                         return &i->second;
328                 }
329         }
330
331         return 0;
332 }
333
334 const Plugin::PresetRecord *
335 Plugin::preset_by_uri (const string& uri)
336 {
337         if (!_have_presets) {
338                 find_presets ();
339                 _have_presets = true;
340         }
341
342         map<string, PresetRecord>::const_iterator pr = _presets.find (uri);
343         if (pr != _presets.end()) {
344                 return &pr->second;
345         } else {
346                 return 0;
347         }
348 }
349
350 bool
351 Plugin::write_immediate_event (size_t size, const uint8_t* buf)
352 {
353         if (!Evoral::midi_event_is_valid (buf, size)) {
354                 return false;
355         }
356         return (_immediate_events.write (0, Evoral::MIDI_EVENT, size, buf) == size);
357 }
358
359 int
360 Plugin::connect_and_run (BufferSet& bufs,
361                 samplepos_t /*start*/, samplepos_t /*end*/, double /*speed*/,
362                 ChanMapping const& /*in_map*/, ChanMapping const& /*out_map*/,
363                 pframes_t nframes, samplecnt_t /*offset*/)
364 {
365         if (bufs.count().n_midi() > 0) {
366
367                 if (_immediate_events.read_space() && nframes > 0) {
368                         _immediate_events.read (bufs.get_midi (0), 0, 1, nframes - 1, true);
369                 }
370
371                 /* Track notes that we are sending to the plugin */
372                 const MidiBuffer& b = bufs.get_midi (0);
373
374                 _tracker.track (b.begin(), b.end());
375
376                 if (_have_pending_stop_events) {
377                         /* Transmit note-offs that are pending from the last transport stop */
378                         bufs.merge_from (_pending_stop_events, 0);
379                         _have_pending_stop_events = false;
380                 }
381         }
382
383         return 0;
384 }
385
386 void
387 Plugin::realtime_handle_transport_stopped ()
388 {
389         resolve_midi ();
390 }
391
392 void
393 Plugin::realtime_locate ()
394 {
395         resolve_midi ();
396 }
397
398 void
399 Plugin::monitoring_changed ()
400 {
401         resolve_midi ();
402 }
403
404 void
405 Plugin::resolve_midi ()
406 {
407         /* Create note-offs for any active notes and put them in _pending_stop_events, to be picked
408            up on the next call to connect_and_run ().
409         */
410
411         _pending_stop_events.get_midi(0).clear ();
412         _tracker.resolve_notes (_pending_stop_events.get_midi (0), 0);
413         _have_pending_stop_events = true;
414 }
415
416 vector<Plugin::PresetRecord>
417 Plugin::get_presets ()
418 {
419         vector<PresetRecord> p;
420
421         if (!_have_presets) {
422                 find_presets ();
423                 _have_presets = true;
424         }
425
426         for (map<string, PresetRecord>::const_iterator i = _presets.begin(); i != _presets.end(); ++i) {
427                 p.push_back (i->second);
428         }
429
430         return p;
431 }
432
433 bool
434 Plugin::load_preset (PresetRecord r)
435 {
436         _last_preset = r;
437         _parameter_changed_since_last_preset = false;
438
439         _session.set_dirty ();
440         PresetLoaded (); /* EMIT SIGNAL */
441         return true;
442 }
443
444 void
445 Plugin::clear_preset ()
446 {
447         _last_preset.uri = "";
448         _last_preset.label = "";
449         _parameter_changed_since_last_preset = false;
450
451         _session.set_dirty ();
452         PresetLoaded (); /* EMIT SIGNAL */
453 }
454
455 void
456 Plugin::set_parameter (uint32_t /* which */, float /* value */)
457 {
458         _parameter_changed_since_last_preset = true;
459         PresetDirty (); /* EMIT SIGNAL */
460 }
461
462 void
463 Plugin::parameter_changed_externally (uint32_t which, float /* value */)
464 {
465         _parameter_changed_since_last_preset = true;
466         _session.set_dirty ();
467         ParameterChangedExternally (which, get_parameter (which)); /* EMIT SIGNAL */
468         PresetDirty (); /* EMIT SIGNAL */
469 }
470
471 int
472 Plugin::set_state (const XMLNode& node, int /*version*/)
473 {
474         std::string preset_uri;
475         const Plugin::PresetRecord* r = 0;
476         if (node.get_property (X_("last-preset-uri"), preset_uri)) {
477                 r = preset_by_uri (preset_uri);
478         }
479         if (r) {
480                 _last_preset = *r;
481                 node.get_property (X_("parameter-changed-since-last-preset"), _parameter_changed_since_last_preset); // XXX
482         } else {
483                 _last_preset.uri = "";
484                 _last_preset.valid = false;
485         }
486         return 0;
487 }
488
489 XMLNode &
490 Plugin::get_state ()
491 {
492         XMLNode* root = new XMLNode (state_node_name ());
493
494         root->set_property (X_("last-preset-uri"), _last_preset.uri);
495         root->set_property (X_("last-preset-label"), _last_preset.label);
496         root->set_property (X_("parameter-changed-since-last-preset"), _parameter_changed_since_last_preset);
497
498         add_state (root);
499
500         return *root;
501 }
502
503 void
504 Plugin::set_info (PluginInfoPtr info)
505 {
506         _info = info;
507 }
508
509 std::string
510 Plugin::parameter_label (uint32_t which) const
511 {
512         if (which >= parameter_count ()) {
513                 return "";
514         }
515         ParameterDescriptor pd;
516         get_parameter_descriptor (which, pd);
517         return pd.label;
518 }
519
520 bool
521 PluginInfo::is_effect () const
522 {
523         return (!is_instrument () && !is_utility ()  && !is_analyzer ());
524 }
525
526 bool
527 PluginInfo::is_instrument () const
528 {
529         if (category == "Instrument") {
530                 return true;
531         }
532
533         // second check: if we have  midi input and audio output, we're likely an instrument
534         return (n_inputs.n_midi() != 0) && (n_outputs.n_audio() > 0) && (n_inputs.n_audio() == 0);
535 }
536
537 bool
538 PluginInfo::is_utility () const
539 {
540         /* XXX beware of translations, e.g. LV2 categories */
541         return (category == "Utility" || category == "MIDI" || category == "Generator");
542 }
543
544 bool
545 PluginInfo::is_analyzer () const
546 {
547         return (category == "Analyser" || category == "Anaylsis" || category == "Analyzer");
548 }