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