globally change all use of "frame" to refer to audio into "sample".
[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         , _owner (0)
101         , _have_presets (false)
102         , _have_pending_stop_events (false)
103         , _parameter_changed_since_last_preset (false)
104         , _immediate_events(6096) // FIXME: size?
105 {
106         _pending_stop_events.ensure_buffers (DataType::MIDI, 1, 4096);
107 }
108
109 Plugin::Plugin (const Plugin& other)
110         : StatefulDestructible()
111         , Latent()
112         , _engine (other._engine)
113         , _session (other._session)
114         , _info (other._info)
115         , _cycles (0)
116         , _owner (other._owner)
117         , _have_presets (false)
118         , _have_pending_stop_events (false)
119         , _parameter_changed_since_last_preset (false)
120         , _immediate_events(6096) // FIXME: size?
121 {
122         _pending_stop_events.ensure_buffers (DataType::MIDI, 1, 4096);
123 }
124
125 Plugin::~Plugin ()
126 {
127 }
128
129 void
130 Plugin::remove_preset (string name)
131 {
132         Plugin::PresetRecord const * p = preset_by_label (name);
133         if (!p) {
134                 PBD::error << _("Trying to remove nonexistent preset.") << endmsg;
135                 return;
136         }
137         if (!p->user) {
138                 PBD::error << _("Cannot remove plugin factory preset.") << endmsg;
139                 return;
140         }
141
142         do_remove_preset (name);
143         _presets.erase (p->uri);
144
145         _last_preset.uri = "";
146         _parameter_changed_since_last_preset = false;
147         _have_presets = false;
148         PresetsChanged (unique_id(), this); /* EMIT SIGNAL */
149         PresetRemoved (); /* EMIT SIGNAL */
150 }
151
152 /** @return PresetRecord with empty URI on failure */
153 Plugin::PresetRecord
154 Plugin::save_preset (string name)
155 {
156         if (preset_by_label (name)) {
157                 PBD::error << _("Preset with given name already exists.") << endmsg;
158                 return Plugin::PresetRecord ();
159         }
160
161         string const uri = do_save_preset (name);
162
163         if (!uri.empty()) {
164                 _presets.insert (make_pair (uri, PresetRecord (uri, name)));
165                 _have_presets = false;
166                 PresetsChanged (unique_id(), this); /* EMIT SIGNAL */
167                 PresetAdded (); /* EMIT SIGNAL */
168         }
169
170         return PresetRecord (uri, name);
171 }
172
173 PluginPtr
174 ARDOUR::find_plugin(Session& session, string identifier, PluginType type)
175 {
176         PluginManager& mgr (PluginManager::instance());
177         PluginInfoList plugs;
178
179         switch (type) {
180         case ARDOUR::Lua:
181                 plugs = mgr.lua_plugin_info();
182                 break;
183
184         case ARDOUR::LADSPA:
185                 plugs = mgr.ladspa_plugin_info();
186                 break;
187
188 #ifdef LV2_SUPPORT
189         case ARDOUR::LV2:
190                 plugs = mgr.lv2_plugin_info();
191                 break;
192 #endif
193
194 #ifdef WINDOWS_VST_SUPPORT
195         case ARDOUR::Windows_VST:
196                 plugs = mgr.windows_vst_plugin_info();
197                 break;
198 #endif
199
200 #ifdef LXVST_SUPPORT
201         case ARDOUR::LXVST:
202                 plugs = mgr.lxvst_plugin_info();
203                 break;
204 #endif
205
206 #ifdef MACVST_SUPPORT
207         case ARDOUR::MacVST:
208                 plugs = mgr.mac_vst_plugin_info();
209                 break;
210 #endif
211
212 #ifdef AUDIOUNIT_SUPPORT
213         case ARDOUR::AudioUnit:
214                 plugs = mgr.au_plugin_info();
215                 break;
216 #endif
217
218         default:
219                 return PluginPtr ((Plugin *) 0);
220         }
221
222         PluginInfoList::iterator i;
223
224         for (i = plugs.begin(); i != plugs.end(); ++i) {
225                 if (identifier == (*i)->unique_id){
226                         return (*i)->load (session);
227                 }
228         }
229
230 #ifdef WINDOWS_VST_SUPPORT
231         /* hmm, we didn't find it. could be because in older versions of Ardour.
232            we used to store the name of a VST plugin, not its unique ID. so try
233            again.
234         */
235
236         for (i = plugs.begin(); i != plugs.end(); ++i) {
237                 if (identifier == (*i)->name){
238                         return (*i)->load (session);
239                 }
240         }
241 #endif
242
243 #ifdef LXVST_SUPPORT
244         /* hmm, we didn't find it. could be because in older versions of Ardour.
245            we used to store the name of a VST plugin, not its unique ID. so try
246            again.
247         */
248
249         for (i = plugs.begin(); i != plugs.end(); ++i) {
250                 if (identifier == (*i)->name){
251                         return (*i)->load (session);
252                 }
253         }
254 #endif
255
256         return PluginPtr ((Plugin*) 0);
257 }
258
259 ChanCount
260 Plugin::output_streams () const
261 {
262         /* LADSPA & VST should not get here because they do not
263            return "infinite" i/o counts.
264         */
265         return ChanCount::ZERO;
266 }
267
268 ChanCount
269 Plugin::input_streams () const
270 {
271         /* LADSPA & VST should not get here because they do not
272            return "infinite" i/o counts.
273         */
274         return ChanCount::ZERO;
275 }
276
277 Plugin::IOPortDescription
278 Plugin::describe_io_port (ARDOUR::DataType dt, bool input, uint32_t id) const
279 {
280         std::stringstream ss;
281         switch (dt) {
282                 case DataType::AUDIO:
283                         ss << _("Audio") << " ";
284                         break;
285                 case DataType::MIDI:
286                         ss << _("Midi") << " ";
287                         break;
288                 default:
289                         ss << _("?") << " ";
290                         break;
291         }
292         if (input) {
293                 ss << _("In") << " ";
294         } else {
295                 ss << _("Out") << " ";
296         }
297
298         ss << (id + 1);
299
300         Plugin::IOPortDescription iod (ss.str());
301         return iod;
302 }
303
304 PluginOutputConfiguration
305 Plugin::possible_output () const
306 {
307         PluginOutputConfiguration oc;
308         if (_info) {
309                 oc.insert (_info->n_outputs.n_audio ());
310         }
311         return oc;
312 }
313
314 const Plugin::PresetRecord *
315 Plugin::preset_by_label (const string& label)
316 {
317 #ifndef NO_PLUGIN_STATE
318         if (!_have_presets) {
319                 find_presets ();
320                 _have_presets = true;
321         }
322 #endif
323         // FIXME: O(n)
324         for (map<string, PresetRecord>::const_iterator i = _presets.begin(); i != _presets.end(); ++i) {
325                 if (i->second.label == label) {
326                         return &i->second;
327                 }
328         }
329
330         return 0;
331 }
332
333 const Plugin::PresetRecord *
334 Plugin::preset_by_uri (const string& uri)
335 {
336 #ifndef NO_PLUGIN_STATE
337         if (!_have_presets) {
338                 find_presets ();
339                 _have_presets = true;
340         }
341 #endif
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 /*in_map*/, ChanMapping /*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 #ifndef NO_PLUGIN_STATE
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 #else
431         if (!seen_set_state_message) {
432                 info << string_compose (_("Plugin presets are not supported in this build of %1. Consider paying for a full version"),
433                                         PROGRAM_NAME)
434                      << endmsg;
435                 seen_set_state_message = true;
436         }
437 #endif
438
439         return p;
440 }
441
442 /** Set parameters using a preset */
443 bool
444 Plugin::load_preset (PresetRecord r)
445 {
446         _last_preset = r;
447         _parameter_changed_since_last_preset = false;
448
449         _session.set_dirty ();
450         PresetLoaded (); /* EMIT SIGNAL */
451         return true;
452 }
453
454 void
455 Plugin::clear_preset ()
456 {
457         _last_preset.uri = "";
458         _last_preset.label = "";
459         _parameter_changed_since_last_preset = false;
460
461         _session.set_dirty ();
462         PresetLoaded (); /* EMIT SIGNAL */
463 }
464
465 void
466 Plugin::set_parameter (uint32_t /* which */, float /* value */)
467 {
468         _parameter_changed_since_last_preset = true;
469         PresetDirty (); /* EMIT SIGNAL */
470 }
471
472 void
473 Plugin::parameter_changed_externally (uint32_t which, float /* value */)
474 {
475         _parameter_changed_since_last_preset = true;
476         _session.set_dirty ();
477         ParameterChangedExternally (which, get_parameter (which)); /* EMIT SIGNAL */
478         PresetDirty (); /* EMIT SIGNAL */
479 }
480
481 int
482 Plugin::set_state (const XMLNode& node, int /*version*/)
483 {
484         node.get_property (X_("last-preset-uri"), _last_preset.uri);
485         node.get_property (X_("last-preset-label"), _last_preset.label);
486         node.get_property (X_("parameter-changed-since-last-preset"), _parameter_changed_since_last_preset);
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 #ifndef NO_PLUGIN_STATE
500         add_state (root);
501 #else
502         if (!seen_get_state_message) {
503                 info << string_compose (_("Saving plugin settings is not supported in this build of %1. Consider paying for the full version"),
504                                         PROGRAM_NAME)
505                      << endmsg;
506                 seen_get_state_message = true;
507         }
508 #endif
509
510         return *root;
511 }
512
513 void
514 Plugin::set_info (PluginInfoPtr info)
515 {
516         _info = info;
517 }
518
519 std::string
520 Plugin::parameter_label (uint32_t which) const
521 {
522         if (which >= parameter_count ()) {
523                 return "";
524         }
525         ParameterDescriptor pd;
526         get_parameter_descriptor (which, pd);
527         return pd.label;
528 }