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