2 Copyright (C) 1998-2006 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 "pbd/error.h"
26 #include "pbd/controllable_descriptor.h"
27 #include "pbd/xml++.h"
28 #include "pbd/stacktrace.h"
30 #include "midi++/port.h"
31 #include "midi++/channel.h"
33 #include "ardour/automation_control.h"
34 #include "ardour/midi_ui.h"
35 #include "ardour/utils.h"
37 #include "midicontrollable.h"
38 #include "generic_midi_control_protocol.h"
43 using namespace ARDOUR;
45 MIDIControllable::MIDIControllable (GenericMidiControlProtocol* s, Port& p, bool m)
52 _learned = false; /* from URI */
54 last_value = 0; // got a better idea ?
55 last_controllable_value = 0.0f;
57 _control_description = "MIDI Control: none";
58 control_additional = (byte) -1;
59 feedback = true; // for now
62 MIDIControllable::MIDIControllable (GenericMidiControlProtocol* s, Port& p, Controllable& c, bool m)
69 _learned = true; /* from controllable */
71 last_value = 0; // got a better idea ?
72 last_controllable_value = 0.0f;
74 _control_description = "MIDI Control: none";
75 control_additional = (byte) -1;
76 feedback = true; // for now
79 MIDIControllable::~MIDIControllable ()
81 drop_external_control ();
85 MIDIControllable::init (const std::string& s)
89 _descriptor = new ControllableDescriptor;
90 return _descriptor->set (s);
94 MIDIControllable::midi_forget ()
96 /* stop listening for incoming messages, but retain
97 our existing event + type information.
100 midi_sense_connection[0].disconnect ();
101 midi_sense_connection[1].disconnect ();
102 midi_learn_connection.disconnect ();
106 MIDIControllable::drop_external_control ()
110 control_additional = (byte) -1;
114 MIDIControllable::set_controllable (Controllable* c)
120 MIDIControllable::midi_rebind (channel_t c)
123 bind_midi (c, control_type, control_additional);
130 MIDIControllable::learn_about_external_control ()
132 drop_external_control ();
133 _port.parser()->any.connect_same_thread (midi_learn_connection, boost::bind (&MIDIControllable::midi_receiver, this, _1, _2, _3));
137 MIDIControllable::stop_learning ()
139 midi_learn_connection.disconnect ();
143 MIDIControllable::control_to_midi (float val)
145 if (controllable->is_gain_like()) {
146 return gain_to_slider_position (val) * max_value_for_type ();
149 float control_min = controllable->lower ();
150 float control_max = controllable->upper ();
151 const float control_range = control_max - control_min;
153 return (val - control_min) / control_range * max_value_for_type ();
157 MIDIControllable::midi_to_control (int val)
159 /* fiddle with MIDI value so that we get an odd number of integer steps
160 and can thus represent "middle" precisely as 0.5. this maps to
164 float fv = (val == 0 ? 0 : float (val - 1) / (max_value_for_type() - 1));
166 if (controllable->is_gain_like()) {
167 return slider_position_to_gain (fv);
170 float control_min = controllable->lower ();
171 float control_max = controllable->upper ();
172 const float control_range = control_max - control_min;
174 return (fv * control_range) + control_min;
178 MIDIControllable::midi_sense_note_on (Parser &p, EventTwoBytes *tb)
180 midi_sense_note (p, tb, true);
184 MIDIControllable::midi_sense_note_off (Parser &p, EventTwoBytes *tb)
186 midi_sense_note (p, tb, false);
190 MIDIControllable::lookup_controllable()
196 boost::shared_ptr<Controllable> c = _surface->lookup_controllable (*_descriptor);
202 controllable = c.get();
203 controllable->Destroyed.connect (controllable_death_connection, MISSING_INVALIDATOR,
204 boost::bind (&MIDIControllable::drop_controllable, this),
205 MidiControlUI::instance());
211 MIDIControllable::drop_controllable ()
213 controllable_death_connection.disconnect ();
218 MIDIControllable::midi_sense_note (Parser &, EventTwoBytes *msg, bool /*is_on*/)
221 if (lookup_controllable()) {
226 if (!controllable->is_toggle()) {
227 controllable->set_value (midi_to_control (msg->note_number));
229 if (control_additional == msg->note_number) {
230 controllable->set_value (controllable->get_value() > 0.5f ? 0.0f : 1.0f);
234 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
238 MIDIControllable::midi_sense_controller (Parser &, EventTwoBytes *msg)
241 if (lookup_controllable ()) {
246 if (controllable->touching()) {
247 return; // to prevent feedback fights when e.g. dragging a UI slider
250 if (control_additional == msg->controller_number) {
252 if (!controllable->is_toggle()) {
253 float new_value = msg->value;
254 float max_value = max(last_controllable_value, new_value);
255 float min_value = min(last_controllable_value, new_value);
256 float range = max_value - min_value;
257 float threshold = (float) _surface->threshold ();
259 bool const in_sync = (
261 controllable->get_value() <= midi_to_control(max_value) &&
262 controllable->get_value() >= midi_to_control(min_value)
265 /* If the surface is not motorised, we try to prevent jumps when
266 the MIDI controller and controllable are out of sync.
267 There might be a better way of doing this.
270 if (in_sync || _surface->motorised ()) {
271 controllable->set_value (midi_to_control (new_value));
274 last_controllable_value = new_value;
276 if (msg->value > 64.0f) {
277 controllable->set_value (1);
279 controllable->set_value (0);
283 last_value = (MIDI::byte) (control_to_midi(controllable->get_value())); // to prevent feedback fights
288 MIDIControllable::midi_sense_program_change (Parser &, byte msg)
291 if (lookup_controllable ()) {
296 if (!controllable->is_toggle()) {
297 controllable->set_value (midi_to_control (msg));
298 } else if (msg == control_additional) {
299 controllable->set_value (controllable->get_value() > 0.5f ? 0.0f : 1.0f);
302 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
306 MIDIControllable::midi_sense_pitchbend (Parser &, pitchbend_t pb)
309 if (lookup_controllable ()) {
314 if (!controllable->is_toggle()) {
315 controllable->set_value (midi_to_control (pb));
317 controllable->set_value (controllable->get_value() > 0.5f ? 0.0f : 1.0f);
320 last_value = control_to_midi (controllable->get_value ());
324 MIDIControllable::midi_receiver (Parser &, byte *msg, size_t /*len*/)
326 /* we only respond to channel messages */
328 if ((msg[0] & 0xF0) < 0x80 || (msg[0] & 0xF0) > 0xE0) {
332 /* if the our port doesn't do input anymore, forget it ... */
334 if (!_port.parser()) {
338 bind_midi ((channel_t) (msg[0] & 0xf), eventType (msg[0] & 0xF0), msg[1]);
341 controllable->LearningFinished ();
346 MIDIControllable::bind_midi (channel_t chn, eventType ev, MIDI::byte additional)
350 drop_external_control ();
353 control_channel = chn;
354 control_additional = additional;
356 if (_port.parser() == 0) {
360 Parser& p = *_port.parser();
365 p.channel_note_off[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_note_off, this, _1, _2));
367 /* if this is a togglee, connect to noteOn as well,
368 and we'll toggle back and forth between the two.
372 p.channel_note_on[chn_i].connect_same_thread (midi_sense_connection[1], boost::bind (&MIDIControllable::midi_sense_note_on, this, _1, _2));
375 _control_description = "MIDI control: NoteOff";
379 p.channel_note_on[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_note_on, this, _1, _2));
381 p.channel_note_off[chn_i].connect_same_thread (midi_sense_connection[1], boost::bind (&MIDIControllable::midi_sense_note_off, this, _1, _2));
383 _control_description = "MIDI control: NoteOn";
386 case MIDI::controller:
387 p.channel_controller[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_controller, this, _1, _2));
388 snprintf (buf, sizeof (buf), "MIDI control: Controller %d", control_additional);
389 _control_description = buf;
393 p.channel_program_change[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_program_change, this, _1, _2));
394 _control_description = "MIDI control: ProgramChange";
397 case MIDI::pitchbend:
398 p.channel_pitchbend[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_pitchbend, this, _1, _2));
399 _control_description = "MIDI control: Pitchbend";
408 MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool /*force*/)
410 if (!controllable || control_type == none || !feedback || bufsize <= 2) {
414 int const gm = control_to_midi (controllable->get_value());
416 if (gm == last_value) {
420 *buf++ = (0xF0 & control_type) | (0xF & control_channel);
422 switch (control_type) {
423 case MIDI::pitchbend:
424 *buf++ = int (gm) & 127;
425 *buf++ = (int (gm) >> 7) & 127;
428 *buf++ = control_additional; /* controller number */
440 MIDIControllable::set_state (const XMLNode& node, int /*version*/)
442 const XMLProperty* prop;
445 if ((prop = node.property ("event")) != 0) {
446 sscanf (prop->value().c_str(), "0x%x", &xx);
447 control_type = (MIDI::eventType) xx;
452 if ((prop = node.property ("channel")) != 0) {
453 sscanf (prop->value().c_str(), "%d", &xx);
454 control_channel = (MIDI::channel_t) xx;
459 if ((prop = node.property ("additional")) != 0) {
460 sscanf (prop->value().c_str(), "0x%x", &xx);
461 control_additional = (MIDI::byte) xx;
466 if ((prop = node.property ("feedback")) != 0) {
467 feedback = (prop->value() == "yes");
469 feedback = true; // default
472 bind_midi (control_channel, control_type, control_additional);
478 MIDIControllable::get_state ()
482 XMLNode* node = new XMLNode ("MIDIControllable");
484 if (_current_uri.empty()) {
485 node->add_property ("id", controllable->id().to_s());
487 node->add_property ("uri", _current_uri);
491 snprintf (buf, sizeof(buf), "0x%x", (int) control_type);
492 node->add_property ("event", buf);
493 snprintf (buf, sizeof(buf), "%d", (int) control_channel);
494 node->add_property ("channel", buf);
495 snprintf (buf, sizeof(buf), "0x%x", (int) control_additional);
496 node->add_property ("additional", buf);
497 node->add_property ("feedback", (feedback ? "yes" : "no"));
503 /** @return the maximum value for a control value transmitted
504 * using a given MIDI::eventType.
507 MIDIControllable::max_value_for_type () const
509 /* XXX: this is not complete */
511 if (control_type == MIDI::pitchbend) {