Only show user-presets in favorite sidebar
[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 Plugin::Plugin (AudioEngine& e, Session& s)
91         : _engine (e)
92         , _session (s)
93         , _cycles (0)
94         , _owner (0)
95         , _have_presets (false)
96         , _have_pending_stop_events (false)
97         , _parameter_changed_since_last_preset (false)
98         , _immediate_events(6096) // FIXME: size?
99 {
100         _pending_stop_events.ensure_buffers (DataType::MIDI, 1, 4096);
101 }
102
103 Plugin::Plugin (const Plugin& other)
104         : StatefulDestructible()
105         , Latent()
106         , _engine (other._engine)
107         , _session (other._session)
108         , _info (other._info)
109         , _cycles (0)
110         , _owner (other._owner)
111         , _have_presets (false)
112         , _have_pending_stop_events (false)
113         , _parameter_changed_since_last_preset (false)
114         , _immediate_events(6096) // FIXME: size?
115 {
116         _pending_stop_events.ensure_buffers (DataType::MIDI, 1, 4096);
117 }
118
119 Plugin::~Plugin ()
120 {
121 }
122
123 void
124 Plugin::remove_preset (string name)
125 {
126         Plugin::PresetRecord const * p = preset_by_label (name);
127         if (!p) {
128                 PBD::error << _("Trying to remove nonexistent preset.") << endmsg;
129                 return;
130         }
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 (p->uri);
138
139         _last_preset.uri = "";
140         _parameter_changed_since_last_preset = false;
141         _have_presets = false;
142         PresetsChanged (unique_id(), this); /* EMIT SIGNAL */
143         PresetRemoved (); /* EMIT SIGNAL */
144 }
145
146 /** @return PresetRecord with empty URI on failure */
147 Plugin::PresetRecord
148 Plugin::save_preset (string name)
149 {
150         if (preset_by_label (name)) {
151                 PBD::error << _("Preset with given name already exists.") << endmsg;
152                 return Plugin::PresetRecord ();
153         }
154
155         string const uri = do_save_preset (name);
156
157         if (!uri.empty()) {
158                 _presets.insert (make_pair (uri, PresetRecord (uri, name)));
159                 _have_presets = false;
160                 PresetsChanged (unique_id(), this); /* EMIT SIGNAL */
161                 PresetAdded (); /* EMIT SIGNAL */
162         }
163
164         return PresetRecord (uri, name);
165 }
166
167 PluginPtr
168 ARDOUR::find_plugin(Session& session, string identifier, PluginType type)
169 {
170         PluginManager& mgr (PluginManager::instance());
171         PluginInfoList plugs;
172
173         switch (type) {
174         case ARDOUR::Lua:
175                 plugs = mgr.lua_plugin_info();
176                 break;
177
178         case ARDOUR::LADSPA:
179                 plugs = mgr.ladspa_plugin_info();
180                 break;
181
182 #ifdef LV2_SUPPORT
183         case ARDOUR::LV2:
184                 plugs = mgr.lv2_plugin_info();
185                 break;
186 #endif
187
188 #ifdef WINDOWS_VST_SUPPORT
189         case ARDOUR::Windows_VST:
190                 plugs = mgr.windows_vst_plugin_info();
191                 break;
192 #endif
193
194 #ifdef LXVST_SUPPORT
195         case ARDOUR::LXVST:
196                 plugs = mgr.lxvst_plugin_info();
197                 break;
198 #endif
199
200 #ifdef MACVST_SUPPORT
201         case ARDOUR::MacVST:
202                 plugs = mgr.mac_vst_plugin_info();
203                 break;
204 #endif
205
206 #ifdef AUDIOUNIT_SUPPORT
207         case ARDOUR::AudioUnit:
208                 plugs = mgr.au_plugin_info();
209                 break;
210 #endif
211
212         default:
213                 return PluginPtr ((Plugin *) 0);
214         }
215
216         PluginInfoList::iterator i;
217
218         for (i = plugs.begin(); i != plugs.end(); ++i) {
219                 if (identifier == (*i)->unique_id){
220                         return (*i)->load (session);
221                 }
222         }
223
224 #ifdef WINDOWS_VST_SUPPORT
225         /* hmm, we didn't find it. could be because in older versions of Ardour.
226            we used to store the name of a VST plugin, not its unique ID. so try
227            again.
228         */
229
230         for (i = plugs.begin(); i != plugs.end(); ++i) {
231                 if (identifier == (*i)->name){
232                         return (*i)->load (session);
233                 }
234         }
235 #endif
236
237 #ifdef LXVST_SUPPORT
238         /* hmm, we didn't find it. could be because in older versions of Ardour.
239            we used to store the name of a VST plugin, not its unique ID. so try
240            again.
241         */
242
243         for (i = plugs.begin(); i != plugs.end(); ++i) {
244                 if (identifier == (*i)->name){
245                         return (*i)->load (session);
246                 }
247         }
248 #endif
249
250         return PluginPtr ((Plugin*) 0);
251 }
252
253 ChanCount
254 Plugin::output_streams () const
255 {
256         /* LADSPA & VST should not get here because they do not
257            return "infinite" i/o counts.
258         */
259         return ChanCount::ZERO;
260 }
261
262 ChanCount
263 Plugin::input_streams () const
264 {
265         /* LADSPA & VST should not get here because they do not
266            return "infinite" i/o counts.
267         */
268         return ChanCount::ZERO;
269 }
270
271 Plugin::IOPortDescription
272 Plugin::describe_io_port (ARDOUR::DataType dt, bool input, uint32_t id) const
273 {
274         std::stringstream ss;
275         switch (dt) {
276                 case DataType::AUDIO:
277                         ss << _("Audio") << " ";
278                         break;
279                 case DataType::MIDI:
280                         ss << _("Midi") << " ";
281                         break;
282                 default:
283                         ss << _("?") << " ";
284                         break;
285         }
286         if (input) {
287                 ss << _("In") << " ";
288         } else {
289                 ss << _("Out") << " ";
290         }
291
292         ss << (id + 1);
293
294         Plugin::IOPortDescription iod (ss.str());
295         return iod;
296 }
297
298 PluginOutputConfiguration
299 Plugin::possible_output () const
300 {
301         PluginOutputConfiguration oc;
302         if (_info) {
303                 oc.insert (_info->n_outputs.n_audio ());
304         }
305         return oc;
306 }
307
308 const Plugin::PresetRecord *
309 Plugin::preset_by_label (const string& label)
310 {
311 #ifndef NO_PLUGIN_STATE
312         if (!_have_presets) {
313                 find_presets ();
314                 _have_presets = true;
315         }
316 #endif
317         // FIXME: O(n)
318         for (map<string, PresetRecord>::const_iterator i = _presets.begin(); i != _presets.end(); ++i) {
319                 if (i->second.label == label) {
320                         return &i->second;
321                 }
322         }
323
324         return 0;
325 }
326
327 const Plugin::PresetRecord *
328 Plugin::preset_by_uri (const string& uri)
329 {
330 #ifndef NO_PLUGIN_STATE
331         if (!_have_presets) {
332                 find_presets ();
333                 _have_presets = true;
334         }
335 #endif
336         map<string, PresetRecord>::const_iterator pr = _presets.find (uri);
337         if (pr != _presets.end()) {
338                 return &pr->second;
339         } else {
340                 return 0;
341         }
342 }
343
344 bool
345 Plugin::write_immediate_event (size_t size, const uint8_t* buf)
346 {
347         if (!Evoral::midi_event_is_valid (buf, size)) {
348                 return false;
349         }
350         return (_immediate_events.write (0, Evoral::MIDI_EVENT, size, buf) == size);
351 }
352
353 int
354 Plugin::connect_and_run (BufferSet& bufs,
355                 samplepos_t /*start*/, samplepos_t /*end*/, double /*speed*/,
356                 ChanMapping const& /*in_map*/, ChanMapping const& /*out_map*/,
357                 pframes_t nframes, samplecnt_t /*offset*/)
358 {
359         if (bufs.count().n_midi() > 0) {
360
361                 if (_immediate_events.read_space() && nframes > 0) {
362                         _immediate_events.read (bufs.get_midi (0), 0, 1, nframes - 1, true);
363                 }
364
365                 /* Track notes that we are sending to the plugin */
366                 const MidiBuffer& b = bufs.get_midi (0);
367
368                 _tracker.track (b.begin(), b.end());
369
370                 if (_have_pending_stop_events) {
371                         /* Transmit note-offs that are pending from the last transport stop */
372                         bufs.merge_from (_pending_stop_events, 0);
373                         _have_pending_stop_events = false;
374                 }
375         }
376
377         return 0;
378 }
379
380 void
381 Plugin::realtime_handle_transport_stopped ()
382 {
383         resolve_midi ();
384 }
385
386 void
387 Plugin::realtime_locate ()
388 {
389         resolve_midi ();
390 }
391
392 void
393 Plugin::monitoring_changed ()
394 {
395         resolve_midi ();
396 }
397
398 void
399 Plugin::resolve_midi ()
400 {
401         /* Create note-offs for any active notes and put them in _pending_stop_events, to be picked
402            up on the next call to connect_and_run ().
403         */
404
405         _pending_stop_events.get_midi(0).clear ();
406         _tracker.resolve_notes (_pending_stop_events.get_midi (0), 0);
407         _have_pending_stop_events = true;
408 }
409
410 vector<Plugin::PresetRecord>
411 Plugin::get_presets ()
412 {
413         vector<PresetRecord> p;
414
415 #ifndef NO_PLUGIN_STATE
416         if (!_have_presets) {
417                 find_presets ();
418                 _have_presets = true;
419         }
420
421         for (map<string, PresetRecord>::const_iterator i = _presets.begin(); i != _presets.end(); ++i) {
422                 p.push_back (i->second);
423         }
424 #else
425         if (!seen_set_state_message) {
426                 info << string_compose (_("Plugin presets are not supported in this build of %1. Consider paying for a full version"),
427                                         PROGRAM_NAME)
428                      << endmsg;
429                 seen_set_state_message = true;
430         }
431 #endif
432
433         return p;
434 }
435
436 /** Set parameters using a preset */
437 bool
438 Plugin::load_preset (PresetRecord r)
439 {
440         _last_preset = r;
441         _parameter_changed_since_last_preset = false;
442
443         _session.set_dirty ();
444         PresetLoaded (); /* EMIT SIGNAL */
445         return true;
446 }
447
448 void
449 Plugin::clear_preset ()
450 {
451         _last_preset.uri = "";
452         _last_preset.label = "";
453         _parameter_changed_since_last_preset = false;
454
455         _session.set_dirty ();
456         PresetLoaded (); /* EMIT SIGNAL */
457 }
458
459 void
460 Plugin::set_parameter (uint32_t /* which */, float /* value */)
461 {
462         _parameter_changed_since_last_preset = true;
463         PresetDirty (); /* EMIT SIGNAL */
464 }
465
466 void
467 Plugin::parameter_changed_externally (uint32_t which, float /* value */)
468 {
469         _parameter_changed_since_last_preset = true;
470         _session.set_dirty ();
471         ParameterChangedExternally (which, get_parameter (which)); /* EMIT SIGNAL */
472         PresetDirty (); /* EMIT SIGNAL */
473 }
474
475 int
476 Plugin::set_state (const XMLNode& node, int /*version*/)
477 {
478         std::string preset_uri;
479         const Plugin::PresetRecord* r = 0;
480         if (node.get_property (X_("last-preset-uri"), preset_uri)) {
481                 r = preset_by_uri (preset_uri);
482         }
483         if (r) {
484                 _last_preset = *r;
485                 node.get_property (X_("parameter-changed-since-last-preset"), _parameter_changed_since_last_preset); // XXX
486         } else {
487                 _last_preset.uri = "";
488                 _last_preset.valid = false;
489         }
490         return 0;
491 }
492
493 XMLNode &
494 Plugin::get_state ()
495 {
496         XMLNode* root = new XMLNode (state_node_name ());
497
498         root->set_property (X_("last-preset-uri"), _last_preset.uri);
499         root->set_property (X_("last-preset-label"), _last_preset.label);
500         root->set_property (X_("parameter-changed-since-last-preset"), _parameter_changed_since_last_preset);
501
502 #ifndef NO_PLUGIN_STATE
503         add_state (root);
504 #else
505         if (!seen_get_state_message) {
506                 info << string_compose (_("Saving plugin settings is not supported in this build of %1. Consider paying for the full version"),
507                                         PROGRAM_NAME)
508                      << endmsg;
509                 seen_get_state_message = true;
510         }
511 #endif
512
513         return *root;
514 }
515
516 void
517 Plugin::set_info (PluginInfoPtr info)
518 {
519         _info = info;
520 }
521
522 std::string
523 Plugin::parameter_label (uint32_t which) const
524 {
525         if (which >= parameter_count ()) {
526                 return "";
527         }
528         ParameterDescriptor pd;
529         get_parameter_descriptor (which, pd);
530         return pd.label;
531 }
532
533 bool
534 PluginInfo::is_effect () const
535 {
536         return (!is_instrument () && !is_utility ()  && !is_analyzer ());
537 }
538
539 bool
540 PluginInfo::is_instrument () const
541 {
542         if (category == "Instrument") {
543                 return true;
544         }
545
546         // second check: if we have  midi input and audio output, we're likely an instrument
547         return (n_inputs.n_midi() != 0) && (n_outputs.n_audio() > 0) && (n_inputs.n_audio() == 0);
548 }
549
550 bool
551 PluginInfo::is_utility () const
552 {
553         /* XXX beware of translations, e.g. LV2 categories */
554         return (category == "Utility" || category == "MIDI" || category == "Generator");
555 }
556
557 bool
558 PluginInfo::is_analyzer () const
559 {
560         return (category == "Analyser" || category == "Anaylsis" || category == "Analyzer");
561 }