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++/types.h" // Added by JE - 06-01-2009. All instances of 'byte' changed to 'MIDI::byte' (for clarification)
31 #include "midi++/port.h"
32 #include "midi++/channel.h"
34 #include "ardour/async_midi_port.h"
35 #include "ardour/automation_control.h"
36 #include "ardour/midi_ui.h"
37 #include "ardour/utils.h"
39 #include "midicontrollable.h"
40 #include "generic_midi_control_protocol.h"
45 using namespace ARDOUR;
47 MIDIControllable::MIDIControllable (GenericMidiControlProtocol* s, MIDI::Parser& p, bool m)
54 _learned = false; /* from URI */
56 last_value = 0; // got a better idea ?
57 last_controllable_value = 0.0f;
59 _control_description = "MIDI Control: none";
60 control_additional = (MIDI::byte) -1;
61 feedback = true; // for now
64 MIDIControllable::MIDIControllable (GenericMidiControlProtocol* s, MIDI::Parser& p, Controllable& c, bool m)
70 set_controllable (&c);
72 _learned = true; /* from controllable */
74 last_value = 0; // got a better idea ?
75 last_controllable_value = 0.0f;
77 _control_description = "MIDI Control: none";
78 control_additional = (MIDI::byte) -1;
79 feedback = true; // for now
82 MIDIControllable::~MIDIControllable ()
84 drop_external_control ();
88 MIDIControllable::init (const std::string& s)
92 _descriptor = new ControllableDescriptor;
93 return _descriptor->set (s);
97 MIDIControllable::midi_forget ()
99 /* stop listening for incoming messages, but retain
100 our existing event + type information.
103 midi_sense_connection[0].disconnect ();
104 midi_sense_connection[1].disconnect ();
105 midi_learn_connection.disconnect ();
109 MIDIControllable::drop_external_control ()
113 control_additional = (MIDI::byte) -1;
117 MIDIControllable::set_controllable (Controllable* c)
119 if (c == controllable) {
123 controllable_death_connection.disconnect ();
128 last_controllable_value = controllable->get_value();
130 last_controllable_value = 0.0f; // is there a better value?
134 controllable->Destroyed.connect (controllable_death_connection, MISSING_INVALIDATOR,
135 boost::bind (&MIDIControllable::drop_controllable, this),
136 MidiControlUI::instance());
141 MIDIControllable::midi_rebind (channel_t c)
144 bind_midi (c, control_type, control_additional);
151 MIDIControllable::learn_about_external_control ()
153 drop_external_control ();
154 _parser.any.connect_same_thread (midi_learn_connection, boost::bind (&MIDIControllable::midi_receiver, this, _1, _2, _3));
158 MIDIControllable::stop_learning ()
160 midi_learn_connection.disconnect ();
164 MIDIControllable::control_to_midi (float val)
166 if (controllable->is_gain_like()) {
167 return gain_to_slider_position (val) * max_value_for_type ();
170 float control_min = controllable->lower ();
171 float control_max = controllable->upper ();
172 float control_range = control_max - control_min;
174 if (controllable->is_toggle()) {
175 if (val >= (control_min + (control_range/2.0f))) {
176 return max_value_for_type();
181 AutomationControl *actl = dynamic_cast<AutomationControl*> (controllable);
183 control_min = actl->internal_to_interface(control_min);
184 control_max = actl->internal_to_interface(control_max);
185 control_range = control_max - control_min;
186 val = actl->internal_to_interface(val);
190 return (val - control_min) / control_range * max_value_for_type ();
194 MIDIControllable::midi_to_control (int val)
196 /* fiddle with MIDI value so that we get an odd number of integer steps
197 and can thus represent "middle" precisely as 0.5. this maps to
201 float fv = (val == 0 ? 0 : float (val - 1) / (max_value_for_type() - 1));
203 if (controllable->is_gain_like()) {
204 return slider_position_to_gain (fv);
207 float control_min = controllable->lower ();
208 float control_max = controllable->upper ();
209 float control_range = control_max - control_min;
211 AutomationControl *actl = dynamic_cast<AutomationControl*> (controllable);
213 if (fv == 0.f) return control_min;
214 if (fv == 1.f) return control_max;
215 control_min = actl->internal_to_interface(control_min);
216 control_max = actl->internal_to_interface(control_max);
217 control_range = control_max - control_min;
218 return actl->interface_to_internal((fv * control_range) + control_min);
220 return (fv * control_range) + control_min;
224 MIDIControllable::midi_sense_note_on (Parser &p, EventTwoBytes *tb)
226 midi_sense_note (p, tb, true);
230 MIDIControllable::midi_sense_note_off (Parser &p, EventTwoBytes *tb)
232 midi_sense_note (p, tb, false);
236 MIDIControllable::lookup_controllable()
242 boost::shared_ptr<Controllable> c = _surface->lookup_controllable (*_descriptor);
248 set_controllable (c.get ());
254 MIDIControllable::drop_controllable ()
256 set_controllable (0);
260 MIDIControllable::midi_sense_note (Parser &, EventTwoBytes *msg, bool /*is_on*/)
263 if (lookup_controllable()) {
268 if (!controllable->is_toggle()) {
269 if (control_additional == msg->note_number) {
270 controllable->set_value (midi_to_control (msg->velocity));
273 if (control_additional == msg->note_number) {
274 controllable->set_value (controllable->get_value() > 0.5f ? 0.0f : 1.0f);
278 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
282 MIDIControllable::midi_sense_controller (Parser &, EventTwoBytes *msg)
285 if (lookup_controllable ()) {
290 assert (controllable);
292 if (controllable->touching()) {
293 return; // to prevent feedback fights when e.g. dragging a UI slider
296 if (control_additional == msg->controller_number) {
298 if (!controllable->is_toggle()) {
300 float new_value = msg->value;
301 float max_value = max(last_controllable_value, new_value);
302 float min_value = min(last_controllable_value, new_value);
303 float range = max_value - min_value;
304 float threshold = (float) _surface->threshold ();
306 bool const in_sync = (
308 controllable->get_value() <= midi_to_control(max_value) &&
309 controllable->get_value() >= midi_to_control(min_value)
312 /* If the surface is not motorised, we try to prevent jumps when
313 the MIDI controller and controllable are out of sync.
314 There might be a better way of doing this.
317 if (in_sync || _surface->motorised ()) {
318 controllable->set_value (midi_to_control (new_value));
321 last_controllable_value = new_value;
323 if (msg->value > 64.0f) {
324 controllable->set_value (1);
326 controllable->set_value (0);
330 last_value = (MIDI::byte) (control_to_midi(controllable->get_value())); // to prevent feedback fights
335 MIDIControllable::midi_sense_program_change (Parser &, MIDI::byte msg)
338 if (lookup_controllable ()) {
343 if (!controllable->is_toggle()) {
344 controllable->set_value (midi_to_control (msg));
345 } else if (msg == control_additional) {
346 controllable->set_value (controllable->get_value() > 0.5f ? 0.0f : 1.0f);
349 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
353 MIDIControllable::midi_sense_pitchbend (Parser &, pitchbend_t pb)
356 if (lookup_controllable ()) {
361 if (!controllable->is_toggle()) {
362 controllable->set_value (midi_to_control (pb));
364 controllable->set_value (controllable->get_value() > 0.5f ? 0.0f : 1.0f);
367 last_value = control_to_midi (controllable->get_value ());
371 MIDIControllable::midi_receiver (Parser &, MIDI::byte *msg, size_t /*len*/)
373 /* we only respond to channel messages */
375 if ((msg[0] & 0xF0) < 0x80 || (msg[0] & 0xF0) > 0xE0) {
379 bind_midi ((channel_t) (msg[0] & 0xf), eventType (msg[0] & 0xF0), msg[1]);
382 controllable->LearningFinished ();
387 MIDIControllable::bind_midi (channel_t chn, eventType ev, MIDI::byte additional)
391 drop_external_control ();
394 control_channel = chn;
395 control_additional = additional;
400 _parser.channel_note_off[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_note_off, this, _1, _2));
402 /* if this is a togglee, connect to noteOn as well,
403 and we'll toggle back and forth between the two.
407 _parser.channel_note_on[chn_i].connect_same_thread (midi_sense_connection[1], boost::bind (&MIDIControllable::midi_sense_note_on, this, _1, _2));
410 _control_description = "MIDI control: NoteOff";
414 _parser.channel_note_on[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_note_on, this, _1, _2));
416 _parser.channel_note_off[chn_i].connect_same_thread (midi_sense_connection[1], boost::bind (&MIDIControllable::midi_sense_note_off, this, _1, _2));
418 _control_description = "MIDI control: NoteOn";
421 case MIDI::controller:
422 _parser.channel_controller[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_controller, this, _1, _2));
423 snprintf (buf, sizeof (buf), "MIDI control: Controller %d", control_additional);
424 _control_description = buf;
428 _parser.channel_program_change[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_program_change, this, _1, _2));
429 _control_description = "MIDI control: ProgramChange";
432 case MIDI::pitchbend:
433 _parser.channel_pitchbend[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_pitchbend, this, _1, _2));
434 _control_description = "MIDI control: Pitchbend";
443 MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool /*force*/)
445 if (!controllable || control_type == none || !feedback || bufsize <= 2) {
449 int const gm = control_to_midi (controllable->get_value());
451 if (gm == last_value) {
455 *buf++ = (0xF0 & control_type) | (0xF & control_channel);
457 switch (control_type) {
458 case MIDI::pitchbend:
459 *buf++ = int (gm) & 127;
460 *buf++ = (int (gm) >> 7) & 127;
463 *buf++ = control_additional; /* controller number */
475 MIDIControllable::set_state (const XMLNode& node, int /*version*/)
477 const XMLProperty* prop;
480 if ((prop = node.property ("event")) != 0) {
481 sscanf (prop->value().c_str(), "0x%x", &xx);
482 control_type = (MIDI::eventType) xx;
487 if ((prop = node.property ("channel")) != 0) {
488 sscanf (prop->value().c_str(), "%d", &xx);
489 control_channel = (MIDI::channel_t) xx;
494 if ((prop = node.property ("additional")) != 0) {
495 sscanf (prop->value().c_str(), "0x%x", &xx);
496 control_additional = (MIDI::byte) xx;
501 if ((prop = node.property ("feedback")) != 0) {
502 feedback = (prop->value() == "yes");
504 feedback = true; // default
507 bind_midi (control_channel, control_type, control_additional);
513 MIDIControllable::get_state ()
517 XMLNode* node = new XMLNode ("MIDIControllable");
519 if (_current_uri.empty()) {
520 node->add_property ("id", controllable->id().to_s());
522 node->add_property ("uri", _current_uri);
526 snprintf (buf, sizeof(buf), "0x%x", (int) control_type);
527 node->add_property ("event", buf);
528 snprintf (buf, sizeof(buf), "%d", (int) control_channel);
529 node->add_property ("channel", buf);
530 snprintf (buf, sizeof(buf), "0x%x", (int) control_additional);
531 node->add_property ("additional", buf);
532 node->add_property ("feedback", (feedback ? "yes" : "no"));
538 /** @return the maximum value for a control value transmitted
539 * using a given MIDI::eventType.
542 MIDIControllable::max_value_for_type () const
544 /* XXX: this is not complete */
546 if (control_type == MIDI::pitchbend) {