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