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"
29 #include "pbd/compose.h"
31 #include "midi++/types.h" // Added by JE - 06-01-2009. All instances of 'byte' changed to 'MIDI::byte' (for clarification)
32 #include "midi++/port.h"
33 #include "midi++/channel.h"
35 #include "ardour/async_midi_port.h"
36 #include "ardour/automation_control.h"
37 #include "ardour/midi_ui.h"
38 #include "ardour/utils.h"
39 #include "ardour/debug.h"
41 #include "midicontrollable.h"
42 #include "generic_midi_control_protocol.h"
47 using namespace ARDOUR;
49 MIDIControllable::MIDIControllable (GenericMidiControlProtocol* s, MIDI::Parser& p, bool m)
56 _learned = false; /* from URI */
58 last_value = 0; // got a better idea ?
59 last_controllable_value = 0.0f;
61 _control_description = "MIDI Control: none";
62 control_additional = (MIDI::byte) -1;
63 feedback = true; // for now
66 MIDIControllable::MIDIControllable (GenericMidiControlProtocol* s, MIDI::Parser& p, Controllable& c, bool m)
72 set_controllable (&c);
74 _learned = true; /* from controllable */
76 last_value = 0; // got a better idea ?
77 last_controllable_value = 0.0f;
79 _control_description = "MIDI Control: none";
80 control_additional = (MIDI::byte) -1;
81 feedback = true; // for now
84 MIDIControllable::~MIDIControllable ()
86 drop_external_control ();
90 MIDIControllable::init (const std::string& s)
94 _descriptor = new ControllableDescriptor;
95 return _descriptor->set (s);
99 MIDIControllable::midi_forget ()
101 /* stop listening for incoming messages, but retain
102 our existing event + type information.
105 midi_sense_connection[0].disconnect ();
106 midi_sense_connection[1].disconnect ();
107 midi_learn_connection.disconnect ();
111 MIDIControllable::drop_external_control ()
115 control_additional = (MIDI::byte) -1;
119 MIDIControllable::set_controllable (Controllable* c)
121 if (c == controllable) {
125 controllable_death_connection.disconnect ();
130 last_controllable_value = controllable->get_value();
132 last_controllable_value = 0.0f; // is there a better value?
136 controllable->Destroyed.connect (controllable_death_connection, MISSING_INVALIDATOR,
137 boost::bind (&MIDIControllable::drop_controllable, this),
138 MidiControlUI::instance());
143 MIDIControllable::midi_rebind (channel_t c)
146 bind_midi (c, control_type, control_additional);
153 MIDIControllable::learn_about_external_control ()
155 drop_external_control ();
156 _parser.any.connect_same_thread (midi_learn_connection, boost::bind (&MIDIControllable::midi_receiver, this, _1, _2, _3));
160 MIDIControllable::stop_learning ()
162 midi_learn_connection.disconnect ();
166 MIDIControllable::control_to_midi (float val)
168 if (controllable->is_gain_like()) {
169 return gain_to_slider_position (val) * max_value_for_type ();
172 float control_min = controllable->lower ();
173 float control_max = controllable->upper ();
174 float control_range = control_max - control_min;
176 if (controllable->is_toggle()) {
177 if (val >= (control_min + (control_range/2.0f))) {
178 return max_value_for_type();
183 AutomationControl *actl = dynamic_cast<AutomationControl*> (controllable);
185 control_min = actl->internal_to_interface(control_min);
186 control_max = actl->internal_to_interface(control_max);
187 control_range = control_max - control_min;
188 val = actl->internal_to_interface(val);
192 return (val - control_min) / control_range * max_value_for_type ();
196 MIDIControllable::midi_to_control (int val)
198 /* fiddle with MIDI value so that we get an odd number of integer steps
199 and can thus represent "middle" precisely as 0.5. this maps to
203 float fv = (val == 0 ? 0 : float (val - 1) / (max_value_for_type() - 1));
205 if (controllable->is_gain_like()) {
206 return slider_position_to_gain (fv);
209 float control_min = controllable->lower ();
210 float control_max = controllable->upper ();
211 float control_range = control_max - control_min;
213 AutomationControl *actl = dynamic_cast<AutomationControl*> (controllable);
215 if (fv == 0.f) return control_min;
216 if (fv == 1.f) return control_max;
217 control_min = actl->internal_to_interface(control_min);
218 control_max = actl->internal_to_interface(control_max);
219 control_range = control_max - control_min;
220 return actl->interface_to_internal((fv * control_range) + control_min);
222 return (fv * control_range) + control_min;
226 MIDIControllable::midi_sense_note_on (Parser &p, EventTwoBytes *tb)
228 midi_sense_note (p, tb, true);
232 MIDIControllable::midi_sense_note_off (Parser &p, EventTwoBytes *tb)
234 midi_sense_note (p, tb, false);
238 MIDIControllable::lookup_controllable()
244 boost::shared_ptr<Controllable> c = _surface->lookup_controllable (*_descriptor);
250 set_controllable (c.get ());
256 MIDIControllable::drop_controllable ()
258 set_controllable (0);
262 MIDIControllable::midi_sense_note (Parser &, EventTwoBytes *msg, bool /*is_on*/)
265 if (lookup_controllable()) {
270 if (!controllable->is_toggle()) {
271 if (control_additional == msg->note_number) {
272 controllable->set_value (midi_to_control (msg->velocity));
273 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Note %1 value %2 %3\n", (int) msg->note_number, (float) midi_to_control (msg->velocity), current_uri() ));
276 if (control_additional == msg->note_number) {
277 float new_value = controllable->get_value() > 0.5f ? 0.0f : 1.0f;
278 controllable->set_value (new_value);
279 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Note %1 Value %2 %3\n", (int) msg->note_number, (float) new_value, current_uri()));
283 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
287 MIDIControllable::midi_sense_controller (Parser &, EventTwoBytes *msg)
290 if (lookup_controllable ()) {
295 assert (controllable);
297 if (controllable->touching()) {
298 return; // to prevent feedback fights when e.g. dragging a UI slider
301 if (control_additional == msg->controller_number) {
303 if (!controllable->is_toggle()) {
305 float new_value = msg->value;
306 float max_value = max(last_controllable_value, new_value);
307 float min_value = min(last_controllable_value, new_value);
308 float range = max_value - min_value;
309 float threshold = (float) _surface->threshold ();
311 bool const in_sync = (
313 controllable->get_value() <= midi_to_control(max_value) &&
314 controllable->get_value() >= midi_to_control(min_value)
317 /* If the surface is not motorised, we try to prevent jumps when
318 the MIDI controller and controllable are out of sync.
319 There might be a better way of doing this.
322 if (in_sync || _surface->motorised ()) {
323 controllable->set_value (midi_to_control (new_value));
325 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI CC %1 value %2 %3\n", (int) msg->controller_number, (float) midi_to_control(new_value), current_uri() ));
327 last_controllable_value = new_value;
329 if (msg->value > 64.0f) {
330 controllable->set_value (1);
331 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Midi CC %1 value 1 %2\n", (int) msg->controller_number, current_uri()));
333 controllable->set_value (0);
334 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Midi CC %1 value 0 %2\n", (int) msg->controller_number, current_uri()));
338 last_value = (MIDI::byte) (control_to_midi(controllable->get_value())); // to prevent feedback fights
343 MIDIControllable::midi_sense_program_change (Parser &, MIDI::byte msg)
346 if (lookup_controllable ()) {
351 if (!controllable->is_toggle()) {
352 controllable->set_value (midi_to_control (msg));
353 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI program %1 value %2 %3\n", (int) msg, (float) midi_to_control (msg), current_uri() ));
354 } else if (msg == control_additional) {
355 float new_value = controllable->get_value() > 0.5f ? 0.0f : 1.0f;
356 controllable->set_value (new_value);
357 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI program %1 value %2 %3\n", (int) msg, (float) new_value, current_uri()));
360 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
364 MIDIControllable::midi_sense_pitchbend (Parser &, pitchbend_t pb)
367 if (lookup_controllable ()) {
372 if (!controllable->is_toggle()) {
373 controllable->set_value (midi_to_control (pb));
374 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI pitchbend %1 value %2 %3\n", (int) control_channel, (float) midi_to_control (pb), current_uri() ));
377 controllable->set_value (1);
378 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Midi pitchbend %1 value 1 %2\n", (int) control_channel, current_uri()));
380 controllable->set_value (0);
381 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Midi pitchbend %1 value 0 %2\n", (int) control_channel, current_uri()));
385 last_value = control_to_midi (controllable->get_value ());
389 MIDIControllable::midi_receiver (Parser &, MIDI::byte *msg, size_t /*len*/)
391 /* we only respond to channel messages */
393 if ((msg[0] & 0xF0) < 0x80 || (msg[0] & 0xF0) > 0xE0) {
397 bind_midi ((channel_t) (msg[0] & 0xf), eventType (msg[0] & 0xF0), msg[1]);
400 controllable->LearningFinished ();
405 MIDIControllable::bind_midi (channel_t chn, eventType ev, MIDI::byte additional)
409 drop_external_control ();
412 control_channel = chn;
413 control_additional = additional;
418 _parser.channel_note_off[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_note_off, this, _1, _2));
420 /* if this is a togglee, connect to noteOn as well,
421 and we'll toggle back and forth between the two.
425 _parser.channel_note_on[chn_i].connect_same_thread (midi_sense_connection[1], boost::bind (&MIDIControllable::midi_sense_note_on, this, _1, _2));
428 _control_description = "MIDI control: NoteOff";
432 _parser.channel_note_on[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_note_on, this, _1, _2));
434 _parser.channel_note_off[chn_i].connect_same_thread (midi_sense_connection[1], boost::bind (&MIDIControllable::midi_sense_note_off, this, _1, _2));
436 _control_description = "MIDI control: NoteOn";
439 case MIDI::controller:
440 _parser.channel_controller[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_controller, this, _1, _2));
441 snprintf (buf, sizeof (buf), "MIDI control: Controller %d", control_additional);
442 _control_description = buf;
446 _parser.channel_program_change[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_program_change, this, _1, _2));
447 _control_description = "MIDI control: ProgramChange";
450 case MIDI::pitchbend:
451 _parser.channel_pitchbend[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_pitchbend, this, _1, _2));
452 _control_description = "MIDI control: Pitchbend";
458 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Controlable: bind_midi: %1 on Channel %2 value %3 \n", _control_description, chn_i + 1, (int) additional));
462 MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool /*force*/)
464 if (!controllable || control_type == none || !feedback || bufsize <= 2) {
468 int const gm = control_to_midi (controllable->get_value());
470 if (gm == last_value) {
474 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Feedback: %1 %2\n", control_description(), current_uri()));
476 *buf++ = (0xF0 & control_type) | (0xF & control_channel);
478 switch (control_type) {
479 case MIDI::pitchbend:
480 *buf++ = int (gm) & 127;
481 *buf++ = (int (gm) >> 7) & 127;
484 *buf++ = control_additional; /* program number */
488 *buf++ = control_additional; /* controller number */
492 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI out: Type %1 Channel %2 Bytes %3 %4\n", (int) control_type, (int) control_channel , (int) *(buf - 2), (int) *(buf - 1)));
501 MIDIControllable::set_state (const XMLNode& node, int /*version*/)
503 const XMLProperty* prop;
506 if ((prop = node.property ("event")) != 0) {
507 sscanf (prop->value().c_str(), "0x%x", &xx);
508 control_type = (MIDI::eventType) xx;
513 if ((prop = node.property ("channel")) != 0) {
514 sscanf (prop->value().c_str(), "%d", &xx);
515 control_channel = (MIDI::channel_t) xx;
520 if ((prop = node.property ("additional")) != 0) {
521 sscanf (prop->value().c_str(), "0x%x", &xx);
522 control_additional = (MIDI::byte) xx;
527 if ((prop = node.property ("feedback")) != 0) {
528 feedback = (prop->value() == "yes");
530 feedback = true; // default
533 bind_midi (control_channel, control_type, control_additional);
539 MIDIControllable::get_state ()
543 XMLNode* node = new XMLNode ("MIDIControllable");
545 if (_current_uri.empty()) {
546 node->add_property ("id", controllable->id().to_s());
548 node->add_property ("uri", _current_uri);
552 snprintf (buf, sizeof(buf), "0x%x", (int) control_type);
553 node->add_property ("event", buf);
554 snprintf (buf, sizeof(buf), "%d", (int) control_channel);
555 node->add_property ("channel", buf);
556 snprintf (buf, sizeof(buf), "0x%x", (int) control_additional);
557 node->add_property ("additional", buf);
558 node->add_property ("feedback", (feedback ? "yes" : "no"));
564 /** @return the maximum value for a control value transmitted
565 * using a given MIDI::eventType.
568 MIDIControllable::max_value_for_type () const
570 /* XXX: this is not complete */
572 if (control_type == MIDI::pitchbend) {