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