2 Copyright (C) 2000-2002 Paul Davis
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.
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.
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.
25 #include <cstdio> // so libraptor doesn't complain
33 #include <pbd/compose.h>
34 #include <pbd/error.h>
35 #include <pbd/pathscanner.h>
36 #include <pbd/xml++.h>
38 #include <midi++/manager.h>
40 #include <ardour/ardour.h>
41 #include <ardour/session.h>
42 #include <ardour/audioengine.h>
43 #include <ardour/plugin.h>
45 #include <pbd/stl_delete.h>
50 using namespace ARDOUR;
52 Plugin::Plugin (AudioEngine& e, Session& s)
53 : _engine (e), _session (s)
57 Plugin::Plugin (const Plugin& other)
58 : _engine (other._engine), _session (other._session), _info (other._info)
63 Plugin::setup_midi_controls ()
67 port_cnt = parameter_count();
69 /* set up a vector of null pointers for the MIDI controls.
70 we'll fill this in on an as-needed basis.
73 for (uint32_t i = 0; i < port_cnt; ++i) {
74 midi_controls.push_back (0);
80 for (vector<MIDIPortControl*>::iterator i = midi_controls.begin(); i != midi_controls.end(); ++i) {
88 Plugin::get_nth_midi_control (uint32_t n)
90 if (n >= parameter_count()) {
94 if (midi_controls[n] == 0) {
96 Plugin::ParameterDescriptor desc;
98 get_parameter_descriptor (n, desc);
100 midi_controls[n] = new MIDIPortControl (*this, n, _session.midi_port(), desc.lower, desc.upper, desc.toggled, desc.logarithmic);
103 return midi_controls[n];
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)
114 range = upper - lower;
115 last_written = 0; /* XXX need a good out-of-bound-value */
120 Plugin::MIDIPortControl::set_value (float value)
129 value = lower + (range * value);
137 plugin.set_parameter (absolute_port, value);
142 Plugin::MIDIPortControl::send_feedback (float value)
145 if (!setting && get_midi_feedback()) {
147 MIDI::channel_t ch = 0;
148 MIDI::eventType ev = MIDI::none;
149 MIDI::byte additional = 0;
150 MIDI::EventTwoBytes data;
153 val = (MIDI::byte) (value * 127.0f);
159 val = (MIDI::byte) (((value - lower) / range) * 127.0f);
162 if (get_control_info (ch, ev, additional)) {
163 data.controller_number = additional;
166 plugin.session().send_midi_message (get_port(), ev, ch, data);
173 Plugin::MIDIPortControl::write_feedback (MIDI::byte* buf, int32_t& bufsize, float value, bool force)
175 if (get_midi_feedback() && bufsize > 2) {
176 MIDI::channel_t ch = 0;
177 MIDI::eventType ev = MIDI::none;
178 MIDI::byte additional = 0;
180 if (get_control_info (ch, ev, additional)) {
186 val = (MIDI::byte) (value * 127.0f);
194 val = (MIDI::byte) (((value - lower) / range) * 127.0f);
197 if (val != last_written || force) {
198 *buf++ = MIDI::controller & ch;
199 *buf++ = additional; /* controller number */
212 Plugin::reset_midi_control (MIDI::Port* port, bool on)
218 for (vector<MIDIPortControl*>::iterator i = midi_controls.begin(); i != midi_controls.end(); ++i) {
221 (*i)->get_control_info (chn, ev, extra);
225 (*i)->midi_rebind (port, chn);
230 Plugin::send_all_midi_feedback ()
232 if (_session.get_midi_feedback()) {
236 for (vector<MIDIPortControl*>::iterator i = midi_controls.begin(); i != midi_controls.end(); ++i, ++n) {
241 val = (*i)->plugin.get_parameter (n);
242 (*i)->send_feedback (val);
249 Plugin::write_midi_feedback (MIDI::byte* buf, int32_t& bufsize)
251 if (_session.get_midi_feedback()) {
255 for (vector<MIDIPortControl*>::iterator i = midi_controls.begin(); i != midi_controls.end(); ++i, ++n) {
260 val = (*i)->plugin.get_parameter (n);
261 buf = (*i)->write_feedback (buf, bufsize, val);
269 Plugin::get_presets()
271 vector<string> labels;
272 lrdf_uris* set_uris = lrdf_get_setting_uris(unique_id());
275 for (uint32_t i = 0; i < set_uris->count; ++i) {
276 if (char* label = lrdf_get_label(set_uris->items[i])) {
277 labels.push_back(label);
278 presets[label] = set_uris->items[i];
281 lrdf_free_uris(set_uris);
284 // GTK2FIX find an equivalent way to do this with a vector (needed by GUI apis)
291 Plugin::load_preset(const string preset_label)
293 lrdf_defaults* defs = lrdf_get_setting_values(presets[preset_label].c_str());
296 for (uint32_t i = 0; i < defs->count; ++i) {
297 // The defs->items[i].pid < defs->count check is to work around
298 // a bug in liblrdf that saves invalid values into the presets file.
299 if (((uint32_t) defs->items[i].pid < defs->count) && parameter_is_input (defs->items[i].pid)) {
300 set_parameter(defs->items[i].pid, defs->items[i].value);
303 lrdf_free_setting_values(defs);
310 Plugin::save_preset (string name, string domain)
312 lrdf_portvalue portvalues[parameter_count()];
313 lrdf_defaults defaults;
314 defaults.count = parameter_count();
315 defaults.items = portvalues;
317 for (uint32_t i = 0; i < parameter_count(); ++i) {
318 if (parameter_is_input (i)) {
319 portvalues[i].pid = i;
320 portvalues[i].value = get_parameter(i);
325 if ((envvar = getenv ("HOME")) == 0) {
326 warning << _("Could not locate HOME. Preset not saved.") << endmsg;
330 string source(string_compose("file:%1/.%2/rdf/ardour-presets.n3", envvar, domain));
332 free(lrdf_add_preset(source.c_str(), name.c_str(), unique_id(), &defaults));
334 string path = string_compose("%1/.%2", envvar, domain);
335 if (mkdir(path.c_str(), 0775) && errno != EEXIST) {
336 warning << string_compose(_("Could not create %1. Preset not saved. (%2)"), path, strerror(errno)) << endmsg;
341 if (mkdir(path.c_str(), 0775) && errno != EEXIST) {
342 warning << string_compose(_("Could not create %1. Preset not saved. (%2)"), path, strerror(errno)) << endmsg;
346 if (lrdf_export_by_source(source.c_str(), source.substr(5).c_str())) {
347 warning << string_compose(_("Error saving presets file %1."), source) << endmsg;