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