059cf133a5ccb59a5d9fae8b864971c044a90d1c
[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     $Id$
19 */
20
21 #include <vector>
22 #include <string>
23
24 #include <cstdlib>
25 #include <cstdio> // so libraptor doesn't complain
26 #include <cmath>
27 #include <dirent.h>
28 #include <sys/stat.h>
29 #include <cerrno>
30
31 #include <lrdf.h>
32
33 #include <pbd/compose.h>
34 #include <pbd/error.h>
35 #include <pbd/pathscanner.h>
36 #include <pbd/xml++.h>
37
38 #include <midi++/manager.h>
39
40 #include <ardour/ardour.h>
41 #include <ardour/session.h>
42 #include <ardour/audioengine.h>
43 #include <ardour/plugin.h>
44
45 #include <pbd/stl_delete.h>
46
47 #include "i18n.h"
48 #include <locale.h>
49
50 using namespace ARDOUR;
51
52 Plugin::Plugin (AudioEngine& e, Session& s)
53         : _engine (e), _session (s)
54 {
55 }
56
57 Plugin::Plugin (const Plugin& other)
58         : _engine (other._engine), _session (other._session), _info (other._info)
59 {
60 }
61
62 void
63 Plugin::setup_midi_controls ()
64 {
65         uint32_t port_cnt;
66
67         port_cnt = parameter_count();
68
69         /* set up a vector of null pointers for the MIDI controls.
70            we'll fill this in on an as-needed basis.
71         */
72
73         for (uint32_t i = 0; i < port_cnt; ++i) {
74                 midi_controls.push_back (0);
75         }
76 }
77
78 Plugin::~Plugin ()
79 {
80         for (vector<MIDIPortControl*>::iterator i = midi_controls.begin(); i != midi_controls.end(); ++i) {
81                 if (*i) {
82                         delete *i;
83                 }
84         }
85 }
86
87 MIDI::Controllable *
88 Plugin::get_nth_midi_control (uint32_t n)
89 {
90         if (n >= parameter_count()) {
91                 return 0;
92         }
93
94         if (midi_controls[n] == 0) {
95
96                 Plugin::ParameterDescriptor desc;
97
98                 get_parameter_descriptor (n, desc);
99
100                 midi_controls[n] = new MIDIPortControl (*this, n, _session.midi_port(), desc.lower, desc.upper, desc.toggled, desc.logarithmic);
101         } 
102
103         return midi_controls[n];
104 }
105
106 Plugin::MIDIPortControl::MIDIPortControl (Plugin& p, uint32_t port_id, MIDI::Port *port,
107                                           float low, float up, bool t, bool loga)
108         : MIDI::Controllable (port, 0), plugin (p), absolute_port (port_id)
109 {
110         toggled = t;
111         logarithmic = loga;
112         lower = low;
113         upper = up;
114         range = upper - lower;
115         last_written = 0; /* XXX need a good out-of-bound-value */
116         setting = false;
117 }
118
119 void
120 Plugin::MIDIPortControl::set_value (float value)
121 {
122         if (toggled) {
123                 if (value > 0.5) {
124                         value = 1.0;
125                 } else {
126                         value = 0.0;
127                 }
128         } else {
129                 value = lower + (range * value);
130                 
131                 if (logarithmic) {
132                         value = exp(value);
133                 }
134         }
135
136         setting = true;
137         plugin.set_parameter (absolute_port, value);
138         setting = false;
139 }
140
141 void
142 Plugin::MIDIPortControl::send_feedback (float value)
143 {
144
145         if (!setting && get_midi_feedback()) {
146                 MIDI::byte val;
147                 MIDI::channel_t ch = 0;
148                 MIDI::eventType ev = MIDI::none;
149                 MIDI::byte additional = 0;
150                 MIDI::EventTwoBytes data;
151
152                 if (toggled) {
153                         val = (MIDI::byte) (value * 127.0f);
154                 } else {
155                         if (logarithmic) {
156                                 value = log(value);
157                         }
158
159                         val = (MIDI::byte) (((value - lower) / range) * 127.0f);
160                 }
161                 
162                 if (get_control_info (ch, ev, additional)) {
163                         data.controller_number = additional;
164                         data.value = val;
165                         last_written = val;
166                         
167                         plugin.session().send_midi_message (get_port(), ev, ch, data);
168                 }
169         }
170         
171 }
172
173 MIDI::byte*
174 Plugin::MIDIPortControl::write_feedback (MIDI::byte* buf, int32_t& bufsize, float value, bool force)
175 {
176         if (get_midi_feedback() && bufsize > 2) {
177                 MIDI::channel_t ch = 0;
178                 MIDI::eventType ev = MIDI::none;
179                 MIDI::byte additional = 0;
180
181                 if (get_control_info (ch, ev, additional)) {
182
183                         MIDI::byte val;
184
185                         if (toggled) {
186
187                                 val = (MIDI::byte) (value * 127.0f);
188
189                         } else {
190
191                                 if (logarithmic) {
192                                         value = log(value);
193                                 }
194                                 
195                                 val = (MIDI::byte) (((value - lower) / range) * 127.0f);
196                         }
197
198                         if (val != last_written || force)  {
199                                 *buf++ = MIDI::controller & ch;
200                                 *buf++ = additional; /* controller number */
201                                 *buf++ = val;
202                                 last_written = val;
203                                 bufsize -= 3;
204                         }
205                 }
206         }
207
208         return buf;
209 }
210
211
212 void
213 Plugin::reset_midi_control (MIDI::Port* port, bool on)
214 {
215         MIDI::channel_t chn;
216         MIDI::eventType ev;
217         MIDI::byte extra;
218         
219         for (vector<MIDIPortControl*>::iterator i = midi_controls.begin(); i != midi_controls.end(); ++i) {
220                 if (*i == 0)
221                         continue;
222                 (*i)->get_control_info (chn, ev, extra);
223                 if (!on) {
224                         chn = -1;
225                 }
226                 (*i)->midi_rebind (port, chn);
227         }
228 }
229
230 void
231 Plugin::send_all_midi_feedback ()
232 {
233         if (_session.get_midi_feedback()) {
234                 float val = 0.0;
235                 uint32_t n = 0;
236                 
237                 for (vector<MIDIPortControl*>::iterator i = midi_controls.begin(); i != midi_controls.end(); ++i, ++n) {
238                         if (*i == 0) {
239                                 continue;
240                         }
241
242                         val = (*i)->plugin.get_parameter (n);
243                         (*i)->send_feedback (val);
244                 }
245                 
246         }
247 }
248
249 MIDI::byte*
250 Plugin::write_midi_feedback (MIDI::byte* buf, int32_t& bufsize)
251 {
252         if (_session.get_midi_feedback()) {
253                 float val = 0.0;
254                 uint32_t n = 0;
255                 
256                 for (vector<MIDIPortControl*>::iterator i = midi_controls.begin(); i != midi_controls.end(); ++i, ++n) {
257                         if (*i == 0) {
258                                 continue;
259                         }
260
261                         val = (*i)->plugin.get_parameter (n);
262                         buf = (*i)->write_feedback (buf, bufsize, val);
263                 }
264         }
265
266         return buf;
267 }
268
269 vector<string>
270 Plugin::get_presets()
271 {
272         vector<string> labels;
273         lrdf_uris* set_uris = lrdf_get_setting_uris(unique_id());
274
275         if (set_uris) {
276                 for (uint32_t i = 0; i < set_uris->count; ++i) {
277                         if (char* label = lrdf_get_label(set_uris->items[i])) {
278                                 labels.push_back(label);
279                                 presets[label] = set_uris->items[i];
280                         }
281                 }
282                 lrdf_free_uris(set_uris);
283         }
284
285         // GTK2FIX find an equivalent way to do this with a vector (needed by GUI apis)
286         // labels.unique();
287
288         return labels;
289 }
290
291 bool
292 Plugin::load_preset(const string preset_label)
293 {
294         lrdf_defaults* defs = lrdf_get_setting_values(presets[preset_label].c_str());
295
296         if (defs) {
297                 for (uint32_t i = 0; i < defs->count; ++i) {
298                         // The defs->items[i].pid < defs->count check is to work around 
299                         // a bug in liblrdf that saves invalid values into the presets file.
300                         if (((uint32_t) defs->items[i].pid < defs->count) && parameter_is_input (defs->items[i].pid)) {
301                                 set_parameter(defs->items[i].pid, defs->items[i].value);
302                         }
303                 }
304                 lrdf_free_setting_values(defs);
305         }
306
307         return true;
308 }
309
310 bool
311 Plugin::save_preset (string name, string domain)
312 {
313         lrdf_portvalue portvalues[parameter_count()];
314         lrdf_defaults defaults;
315         defaults.count = parameter_count();
316         defaults.items = portvalues;
317
318         for (uint32_t i = 0; i < parameter_count(); ++i) {
319                 if (parameter_is_input (i)) {
320                         portvalues[i].pid = i;
321                         portvalues[i].value = get_parameter(i);
322                 }
323         }
324
325         char* envvar;
326         if ((envvar = getenv ("HOME")) == 0) {
327                 warning << _("Could not locate HOME.  Preset not saved.") << endmsg;
328                 return false;
329         }
330         
331         string source(string_compose("file:%1/.%2/rdf/ardour-presets.n3", envvar, domain));
332
333         free(lrdf_add_preset(source.c_str(), name.c_str(), unique_id(), &defaults));
334
335         string path = string_compose("%1/.%2", envvar, domain);
336         if (mkdir(path.c_str(), 0775) && errno != EEXIST) {
337                 warning << string_compose(_("Could not create %1.  Preset not saved. (%2)"), path, strerror(errno)) << endmsg;
338                 return false;
339         }
340         
341         path += "/rdf";
342         if (mkdir(path.c_str(), 0775) && errno != EEXIST) {
343                 warning << string_compose(_("Could not create %1.  Preset not saved. (%2)"), path, strerror(errno)) << endmsg;
344                 return false;
345         }
346         
347         if (lrdf_export_by_source(source.c_str(), source.substr(5).c_str())) {
348                 warning << string_compose(_("Error saving presets file %1."), source) << endmsg;
349                 return false;
350         }
351
352         return true;
353 }