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