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