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 */
59 last_value = 0; // got a better idea ?
60 last_controllable_value = 0.0f;
62 _control_description = "MIDI Control: none";
63 control_additional = (MIDI::byte) -1;
64 feedback = true; // for now
67 MIDIControllable::MIDIControllable (GenericMidiControlProtocol* s, MIDI::Parser& p, Controllable& c, bool m)
73 set_controllable (&c);
75 _learned = true; /* from controllable */
78 last_value = 0; // got a better idea ?
79 last_controllable_value = 0.0f;
81 _control_description = "MIDI Control: none";
82 control_additional = (MIDI::byte) -1;
83 feedback = true; // for now
86 MIDIControllable::~MIDIControllable ()
88 drop_external_control ();
92 MIDIControllable::init (const std::string& s)
96 _descriptor = new ControllableDescriptor;
97 return _descriptor->set (s);
101 MIDIControllable::midi_forget ()
103 /* stop listening for incoming messages, but retain
104 our existing event + type information.
107 midi_sense_connection[0].disconnect ();
108 midi_sense_connection[1].disconnect ();
109 midi_learn_connection.disconnect ();
113 MIDIControllable::drop_external_control ()
117 control_additional = (MIDI::byte) -1;
121 MIDIControllable::set_controllable (Controllable* c)
123 if (c == controllable) {
127 controllable_death_connection.disconnect ();
132 last_controllable_value = controllable->get_value();
134 last_controllable_value = 0.0f; // is there a better value?
138 controllable->Destroyed.connect (controllable_death_connection, MISSING_INVALIDATOR,
139 boost::bind (&MIDIControllable::drop_controllable, this),
140 MidiControlUI::instance());
145 MIDIControllable::midi_rebind (channel_t c)
148 bind_midi (c, control_type, control_additional);
155 MIDIControllable::learn_about_external_control ()
157 drop_external_control ();
158 _parser.any.connect_same_thread (midi_learn_connection, boost::bind (&MIDIControllable::midi_receiver, this, _1, _2, _3));
162 MIDIControllable::stop_learning ()
164 midi_learn_connection.disconnect ();
168 MIDIControllable::control_to_midi (float val)
170 if (controllable->is_gain_like()) {
171 return gain_to_slider_position (val) * max_value_for_type ();
174 float control_min = controllable->lower ();
175 float control_max = controllable->upper ();
176 float control_range = control_max - control_min;
178 if (controllable->is_toggle()) {
179 if (val >= (control_min + (control_range/2.0f))) {
180 return max_value_for_type();
185 AutomationControl *actl = dynamic_cast<AutomationControl*> (controllable);
187 control_min = actl->internal_to_interface(control_min);
188 control_max = actl->internal_to_interface(control_max);
189 control_range = control_max - control_min;
190 val = actl->internal_to_interface(val);
193 // fiddle value of max so value doesn't jump from 125 to 127 for 1.0
194 // otherwise decrement won't work.
195 return (val - control_min) / control_range * (max_value_for_type () - 1);
199 MIDIControllable::midi_to_control (int val)
201 /* fiddle with MIDI value so that we get an odd number of integer steps
202 and can thus represent "middle" precisely as 0.5. this maps to
203 the range 0..+1.0 (0 to 126)
206 float fv = (val == 0 ? 0 : float (val - 1) / (max_value_for_type() - 1));
208 if (controllable->is_gain_like()) {
209 return slider_position_to_gain (fv);
212 float control_min = controllable->lower ();
213 float control_max = controllable->upper ();
214 float control_range = control_max - control_min;
216 AutomationControl *actl = dynamic_cast<AutomationControl*> (controllable);
218 if (fv == 0.f) return control_min;
219 if (fv == 1.f) return control_max;
220 control_min = actl->internal_to_interface(control_min);
221 control_max = actl->internal_to_interface(control_max);
222 control_range = control_max - control_min;
223 return actl->interface_to_internal((fv * control_range) + control_min);
225 return (fv * control_range) + control_min;
229 MIDIControllable::midi_sense_note_on (Parser &p, EventTwoBytes *tb)
231 midi_sense_note (p, tb, true);
235 MIDIControllable::midi_sense_note_off (Parser &p, EventTwoBytes *tb)
237 midi_sense_note (p, tb, false);
241 MIDIControllable::lookup_controllable()
247 boost::shared_ptr<Controllable> c = _surface->lookup_controllable (*_descriptor);
253 set_controllable (c.get ());
259 MIDIControllable::drop_controllable ()
261 set_controllable (0);
265 MIDIControllable::midi_sense_note (Parser &, EventTwoBytes *msg, bool /*is_on*/)
268 if (lookup_controllable()) {
273 if (!controllable->is_toggle()) {
274 if (control_additional == msg->note_number) {
275 controllable->set_value (midi_to_control (msg->velocity));
276 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Note %1 value %2 %3\n", (int) msg->note_number, (float) midi_to_control (msg->velocity), current_uri() ));
279 if (control_additional == msg->note_number) {
280 float new_value = controllable->get_value() > 0.5f ? 0.0f : 1.0f;
281 controllable->set_value (new_value);
282 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Note %1 Value %2 %3\n", (int) msg->note_number, (float) new_value, current_uri()));
286 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
290 MIDIControllable::midi_sense_controller (Parser &, EventTwoBytes *msg)
293 if (lookup_controllable ()) {
298 assert (controllable);
300 if (controllable->touching()) {
301 return; // to prevent feedback fights when e.g. dragging a UI slider
304 if (control_additional == msg->controller_number) {
306 if (!controllable->is_toggle()) {
307 if (get_encoder() == No_enc) {
308 float new_value = msg->value;
309 float max_value = max(last_controllable_value, new_value);
310 float min_value = min(last_controllable_value, new_value);
311 float range = max_value - min_value;
312 float threshold = (float) _surface->threshold ();
314 bool const in_sync = (
316 controllable->get_value() <= midi_to_control(max_value) &&
317 controllable->get_value() >= midi_to_control(min_value)
320 /* If the surface is not motorised, we try to prevent jumps when
321 the MIDI controller and controllable are out of sync.
322 There might be a better way of doing this.
325 if (in_sync || _surface->motorised ()) {
326 controllable->set_value (midi_to_control (new_value));
328 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() ));
330 last_controllable_value = new_value;
332 int offset = (msg->value & 0x3f);
333 switch (get_encoder()) {
335 if (msg->value > 0x40) {
336 controllable->set_value (midi_to_control (last_value - offset + 1));
338 controllable->set_value (midi_to_control (last_value + offset + 1));
342 if (msg->value > 0x40) {
343 controllable->set_value (midi_to_control (last_value + offset + 1));
345 controllable->set_value (midi_to_control (last_value - offset + 1));
349 if (msg->value > 0x40) {
350 controllable->set_value (midi_to_control (last_value - (0x7f - msg->value) + 1));
352 controllable->set_value (midi_to_control (last_value + offset + 1));
356 if (msg->value > 0x40) {
357 controllable->set_value (midi_to_control (last_value + offset + 1));
359 controllable->set_value (midi_to_control (last_value - (0x40 - offset)));
365 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI CC %1 value %2 %3\n", (int) msg->controller_number, (int) last_value, current_uri() ));
369 if (msg->value > 64.0f) {
370 controllable->set_value (1);
371 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Midi CC %1 value 1 %2\n", (int) msg->controller_number, current_uri()));
373 controllable->set_value (0);
374 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Midi CC %1 value 0 %2\n", (int) msg->controller_number, current_uri()));
378 last_value = (MIDI::byte) (control_to_midi(controllable->get_value())); // to prevent feedback fights
383 MIDIControllable::midi_sense_program_change (Parser &, MIDI::byte msg)
386 if (lookup_controllable ()) {
390 if (msg == control_additional) {
392 if (!controllable->is_toggle()) {
393 controllable->set_value (1.0);
394 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI program %1 value 1.0 %3\n", (int) msg, current_uri() ));
396 float new_value = controllable->get_value() > 0.5f ? 0.0f : 1.0f;
397 controllable->set_value (new_value);
398 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI program %1 value %2 %3\n", (int) msg, (float) new_value, current_uri()));
402 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
406 MIDIControllable::midi_sense_pitchbend (Parser &, pitchbend_t pb)
409 if (lookup_controllable ()) {
414 if (!controllable->is_toggle()) {
415 controllable->set_value (midi_to_control (pb));
416 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI pitchbend %1 value %2 %3\n", (int) control_channel, (float) midi_to_control (pb), current_uri() ));
419 controllable->set_value (1);
420 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Midi pitchbend %1 value 1 %2\n", (int) control_channel, current_uri()));
422 controllable->set_value (0);
423 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Midi pitchbend %1 value 0 %2\n", (int) control_channel, current_uri()));
427 last_value = control_to_midi (controllable->get_value ());
431 MIDIControllable::midi_receiver (Parser &, MIDI::byte *msg, size_t /*len*/)
433 /* we only respond to channel messages */
435 if ((msg[0] & 0xF0) < 0x80 || (msg[0] & 0xF0) > 0xE0) {
439 _surface->check_used_event(msg[0], msg[1]);
440 bind_midi ((channel_t) (msg[0] & 0xf), eventType (msg[0] & 0xF0), msg[1]);
443 controllable->LearningFinished ();
448 MIDIControllable::bind_midi (channel_t chn, eventType ev, MIDI::byte additional)
452 drop_external_control ();
455 control_channel = chn;
456 control_additional = additional;
461 _parser.channel_note_off[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_note_off, this, _1, _2));
463 /* if this is a togglee, connect to noteOn as well,
464 and we'll toggle back and forth between the two.
468 _parser.channel_note_on[chn_i].connect_same_thread (midi_sense_connection[1], boost::bind (&MIDIControllable::midi_sense_note_on, this, _1, _2));
471 _control_description = "MIDI control: NoteOff";
475 _parser.channel_note_on[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_note_on, this, _1, _2));
477 _parser.channel_note_off[chn_i].connect_same_thread (midi_sense_connection[1], boost::bind (&MIDIControllable::midi_sense_note_off, this, _1, _2));
479 _control_description = "MIDI control: NoteOn";
482 case MIDI::controller:
483 _parser.channel_controller[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_controller, this, _1, _2));
484 snprintf (buf, sizeof (buf), "MIDI control: Controller %d", control_additional);
485 _control_description = buf;
489 _parser.channel_program_change[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_program_change, this, _1, _2));
490 _control_description = "MIDI control: ProgramChange";
493 case MIDI::pitchbend:
494 _parser.channel_pitchbend[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_pitchbend, this, _1, _2));
495 _control_description = "MIDI control: Pitchbend";
501 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Controlable: bind_midi: %1 on Channel %2 value %3 \n", _control_description, chn_i + 1, (int) additional));
505 MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool /*force*/)
507 if (!controllable || control_type == none || !feedback || bufsize <= 2) {
511 int const gm = control_to_midi (controllable->get_value());
513 if (gm == last_value) {
517 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Feedback: %1 %2\n", control_description(), current_uri()));
519 *buf++ = (0xF0 & control_type) | (0xF & control_channel);
521 switch (control_type) {
522 case MIDI::pitchbend:
523 *buf++ = int (gm) & 127;
524 *buf++ = (int (gm) >> 7) & 127;
527 *buf++ = control_additional; /* program number */
531 *buf++ = control_additional; /* controller number */
535 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)));
544 MIDIControllable::set_state (const XMLNode& node, int /*version*/)
546 const XMLProperty* prop;
549 if ((prop = node.property ("event")) != 0) {
550 sscanf (prop->value().c_str(), "0x%x", &xx);
551 control_type = (MIDI::eventType) xx;
556 if ((prop = node.property ("channel")) != 0) {
557 sscanf (prop->value().c_str(), "%d", &xx);
558 control_channel = (MIDI::channel_t) xx;
563 if ((prop = node.property ("additional")) != 0) {
564 sscanf (prop->value().c_str(), "0x%x", &xx);
565 control_additional = (MIDI::byte) xx;
570 if ((prop = node.property ("feedback")) != 0) {
571 feedback = (prop->value() == "yes");
573 feedback = true; // default
576 bind_midi (control_channel, control_type, control_additional);
582 MIDIControllable::get_state ()
586 XMLNode* node = new XMLNode ("MIDIControllable");
588 if (_current_uri.empty()) {
589 node->add_property ("id", controllable->id().to_s());
591 node->add_property ("uri", _current_uri);
595 snprintf (buf, sizeof(buf), "0x%x", (int) control_type);
596 node->add_property ("event", buf);
597 snprintf (buf, sizeof(buf), "%d", (int) control_channel);
598 node->add_property ("channel", buf);
599 snprintf (buf, sizeof(buf), "0x%x", (int) control_additional);
600 node->add_property ("additional", buf);
601 node->add_property ("feedback", (feedback ? "yes" : "no"));
607 /** @return the maximum value for a control value transmitted
608 * using a given MIDI::eventType.
611 MIDIControllable::max_value_for_type () const
613 /* XXX: this is not complete */
615 if (control_type == MIDI::pitchbend) {