Oops, fix build.
[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 HAVE_AUDIOUNITS
49 #include "ardour/audio_unit.h"
50 #endif
51
52 #ifdef HAVE_SLV2
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 PBD::Signal0<bool> Plugin::PresetFileExists;
66
67 Plugin::Plugin (AudioEngine& e, Session& s)
68         : _engine (e)
69         , _session (s)
70         , _cycles (0)
71         , _have_pending_stop_events (false)
72 {
73 }
74
75 Plugin::Plugin (const Plugin& other)
76         : StatefulDestructible()
77         , Latent()
78         , _engine (other._engine)
79         , _session (other._session)
80         , _info (other._info)
81         , _cycles (0)
82         , _have_pending_stop_events (false)
83 {
84         
85 }
86
87 Plugin::~Plugin ()
88 {
89         
90 }
91
92 void
93 Plugin::remove_preset (string name)
94 {
95         do_remove_preset (name);
96         _presets.erase (preset_by_label (name)->uri);
97         PresetRemoved (); /* EMIT SIGNAL */
98 }
99
100 bool
101 Plugin::save_preset (string name)
102 {
103         string const uri = do_save_preset (name);
104
105         if (!uri.empty()) {
106                 _presets.insert (make_pair (uri, PresetRecord (uri, name)));
107                 PresetAdded (); /* EMIT SIGNAL */
108         }
109         
110         return !uri.empty ();
111 }
112
113 PluginPtr
114 ARDOUR::find_plugin(Session& session, string identifier, PluginType type)
115 {
116         PluginManager *mgr = PluginManager::the_manager();
117         PluginInfoList plugs;
118
119         switch (type) {
120         case ARDOUR::LADSPA:
121                 plugs = mgr->ladspa_plugin_info();
122                 break;
123
124 #ifdef HAVE_SLV2
125         case ARDOUR::LV2:
126                 plugs = mgr->lv2_plugin_info();
127                 break;
128 #endif
129
130 #ifdef VST_SUPPORT
131         case ARDOUR::VST:
132                 plugs = mgr->vst_plugin_info();
133                 break;
134 #endif
135
136 #ifdef HAVE_AUDIOUNITS
137         case ARDOUR::AudioUnit:
138                 plugs = mgr->au_plugin_info();
139                 break;
140 #endif
141
142         default:
143                 return PluginPtr ((Plugin *) 0);
144         }
145
146         PluginInfoList::iterator i;
147
148         for (i = plugs.begin(); i != plugs.end(); ++i) {
149                 if (identifier == (*i)->unique_id){
150                         return (*i)->load (session);
151                 }
152         }
153
154 #ifdef VST_SUPPORT
155         /* hmm, we didn't find it. could be because in older versions of Ardour.
156            we used to store the name of a VST plugin, not its unique ID. so try
157            again.
158         */
159
160         for (i = plugs.begin(); i != plugs.end(); ++i) {
161                 if (identifier == (*i)->name){
162                         return (*i)->load (session);
163                 }
164         }
165 #endif
166
167         return PluginPtr ((Plugin*) 0);
168 }
169
170 ChanCount
171 Plugin::output_streams () const
172 {
173         /* LADSPA & VST should not get here because they do not
174            return "infinite" i/o counts.
175         */
176         return ChanCount::ZERO;
177 }
178
179 ChanCount
180 Plugin::input_streams () const
181 {
182         /* LADSPA & VST should not get here because they do not
183            return "infinite" i/o counts.
184         */
185         return ChanCount::ZERO;
186 }
187
188 const Plugin::PresetRecord *
189 Plugin::preset_by_label (const string& label)
190 {
191         // FIXME: O(n)
192         for (map<string, PresetRecord>::const_iterator i = _presets.begin(); i != _presets.end(); ++i) {
193                 if (i->second.label == label) {
194                         return &i->second;
195                 }
196         }
197         
198         return 0;
199 }
200
201 const Plugin::PresetRecord *
202 Plugin::preset_by_uri (const string& uri)
203 {
204         map<string, PresetRecord>::const_iterator pr = _presets.find (uri);
205         if (pr != _presets.end()) {
206                 return &pr->second;
207         } else {
208                 return 0;
209         }
210 }
211
212 int
213 Plugin::connect_and_run (BufferSet& bufs,
214                          ChanMapping in_map, ChanMapping out_map,
215                          pframes_t nframes, framecnt_t offset)
216 {
217         if (bufs.count().n_midi() > 0) {
218
219                 /* Track notes that we are sending to the plugin */
220                 MidiBuffer& b = bufs.get_midi (0);
221                 bool looped;
222                 _tracker.track (b.begin(), b.end(), looped);
223                 
224                 if (_have_pending_stop_events) {
225                         /* Transmit note-offs that are pending from the last transport stop */
226                         bufs.merge_from (_pending_stop_events, 0);
227                         _have_pending_stop_events = false;
228                 }
229         }
230
231         return 0;
232 }
233
234 void
235 Plugin::realtime_handle_transport_stopped ()
236 {
237         /* Create note-offs for any active notes and put them in _pending_stop_events, to be picked
238            up on the next call to connect_and_run ().
239         */
240         
241         _pending_stop_events.ensure_buffers (DataType::MIDI, 1, 4096);
242         _pending_stop_events.get_midi(0).clear ();
243         _tracker.resolve_notes (_pending_stop_events.get_midi (0), 0);
244         _have_pending_stop_events = true;
245 }