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 const 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();
182 return (val - control_min) / control_range * max_value_for_type ();
186 MIDIControllable::midi_to_control (int val)
188 /* fiddle with MIDI value so that we get an odd number of integer steps
189 and can thus represent "middle" precisely as 0.5. this maps to
193 float fv = (val == 0 ? 0 : float (val - 1) / (max_value_for_type() - 1));
195 if (controllable->is_gain_like()) {
196 return slider_position_to_gain (fv);
199 float control_min = controllable->lower ();
200 float control_max = controllable->upper ();
201 const float control_range = control_max - control_min;
203 return (fv * control_range) + control_min;
207 MIDIControllable::midi_sense_note_on (Parser &p, EventTwoBytes *tb)
209 midi_sense_note (p, tb, true);
213 MIDIControllable::midi_sense_note_off (Parser &p, EventTwoBytes *tb)
215 midi_sense_note (p, tb, false);
219 MIDIControllable::lookup_controllable()
225 boost::shared_ptr<Controllable> c = _surface->lookup_controllable (*_descriptor);
231 set_controllable (c.get ());
237 MIDIControllable::drop_controllable ()
239 set_controllable (0);
243 MIDIControllable::midi_sense_note (Parser &, EventTwoBytes *msg, bool /*is_on*/)
246 if (lookup_controllable()) {
251 if (!controllable->is_toggle()) {
252 if (control_additional == msg->note_number) {
253 controllable->set_value (midi_to_control (msg->velocity));
256 if (control_additional == msg->note_number) {
257 controllable->set_value (controllable->get_value() > 0.5f ? 0.0f : 1.0f);
261 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
265 MIDIControllable::midi_sense_controller (Parser &, EventTwoBytes *msg)
268 if (lookup_controllable ()) {
273 assert (controllable);
275 if (controllable->touching()) {
276 return; // to prevent feedback fights when e.g. dragging a UI slider
279 if (control_additional == msg->controller_number) {
281 if (!controllable->is_toggle()) {
283 float new_value = msg->value;
284 float max_value = max(last_controllable_value, new_value);
285 float min_value = min(last_controllable_value, new_value);
286 float range = max_value - min_value;
287 float threshold = (float) _surface->threshold ();
289 bool const in_sync = (
291 controllable->get_value() <= midi_to_control(max_value) &&
292 controllable->get_value() >= midi_to_control(min_value)
295 /* If the surface is not motorised, we try to prevent jumps when
296 the MIDI controller and controllable are out of sync.
297 There might be a better way of doing this.
300 if (in_sync || _surface->motorised ()) {
301 controllable->set_value (midi_to_control (new_value));
304 last_controllable_value = new_value;
306 if (msg->value > 64.0f) {
307 controllable->set_value (1);
309 controllable->set_value (0);
313 last_value = (MIDI::byte) (control_to_midi(controllable->get_value())); // to prevent feedback fights
318 MIDIControllable::midi_sense_program_change (Parser &, MIDI::byte msg)
321 if (lookup_controllable ()) {
326 if (!controllable->is_toggle()) {
327 controllable->set_value (midi_to_control (msg));
328 } else if (msg == control_additional) {
329 controllable->set_value (controllable->get_value() > 0.5f ? 0.0f : 1.0f);
332 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
336 MIDIControllable::midi_sense_pitchbend (Parser &, pitchbend_t pb)
339 if (lookup_controllable ()) {
344 if (!controllable->is_toggle()) {
345 controllable->set_value (midi_to_control (pb));
347 controllable->set_value (controllable->get_value() > 0.5f ? 0.0f : 1.0f);
350 last_value = control_to_midi (controllable->get_value ());
354 MIDIControllable::midi_receiver (Parser &, MIDI::byte *msg, size_t /*len*/)
356 /* we only respond to channel messages */
358 if ((msg[0] & 0xF0) < 0x80 || (msg[0] & 0xF0) > 0xE0) {
362 bind_midi ((channel_t) (msg[0] & 0xf), eventType (msg[0] & 0xF0), msg[1]);
365 controllable->LearningFinished ();
370 MIDIControllable::bind_midi (channel_t chn, eventType ev, MIDI::byte additional)
374 drop_external_control ();
377 control_channel = chn;
378 control_additional = additional;
383 _parser.channel_note_off[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_note_off, this, _1, _2));
385 /* if this is a togglee, connect to noteOn as well,
386 and we'll toggle back and forth between the two.
390 _parser.channel_note_on[chn_i].connect_same_thread (midi_sense_connection[1], boost::bind (&MIDIControllable::midi_sense_note_on, this, _1, _2));
393 _control_description = "MIDI control: NoteOff";
397 _parser.channel_note_on[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_note_on, this, _1, _2));
399 _parser.channel_note_off[chn_i].connect_same_thread (midi_sense_connection[1], boost::bind (&MIDIControllable::midi_sense_note_off, this, _1, _2));
401 _control_description = "MIDI control: NoteOn";
404 case MIDI::controller:
405 _parser.channel_controller[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_controller, this, _1, _2));
406 snprintf (buf, sizeof (buf), "MIDI control: Controller %d", control_additional);
407 _control_description = buf;
411 _parser.channel_program_change[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_program_change, this, _1, _2));
412 _control_description = "MIDI control: ProgramChange";
415 case MIDI::pitchbend:
416 _parser.channel_pitchbend[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_pitchbend, this, _1, _2));
417 _control_description = "MIDI control: Pitchbend";
426 MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool /*force*/)
428 if (!controllable || control_type == none || !feedback || bufsize <= 2) {
432 int const gm = control_to_midi (controllable->get_value());
434 if (gm == last_value) {
438 *buf++ = (0xF0 & control_type) | (0xF & control_channel);
440 switch (control_type) {
441 case MIDI::pitchbend:
442 *buf++ = int (gm) & 127;
443 *buf++ = (int (gm) >> 7) & 127;
446 *buf++ = control_additional; /* controller number */
458 MIDIControllable::set_state (const XMLNode& node, int /*version*/)
460 const XMLProperty* prop;
463 if ((prop = node.property ("event")) != 0) {
464 sscanf (prop->value().c_str(), "0x%x", &xx);
465 control_type = (MIDI::eventType) xx;
470 if ((prop = node.property ("channel")) != 0) {
471 sscanf (prop->value().c_str(), "%d", &xx);
472 control_channel = (MIDI::channel_t) xx;
477 if ((prop = node.property ("additional")) != 0) {
478 sscanf (prop->value().c_str(), "0x%x", &xx);
479 control_additional = (MIDI::byte) xx;
484 if ((prop = node.property ("feedback")) != 0) {
485 feedback = (prop->value() == "yes");
487 feedback = true; // default
490 bind_midi (control_channel, control_type, control_additional);
496 MIDIControllable::get_state ()
500 XMLNode* node = new XMLNode ("MIDIControllable");
502 if (_current_uri.empty()) {
503 node->add_property ("id", controllable->id().to_s());
505 node->add_property ("uri", _current_uri);
509 snprintf (buf, sizeof(buf), "0x%x", (int) control_type);
510 node->add_property ("event", buf);
511 snprintf (buf, sizeof(buf), "%d", (int) control_channel);
512 node->add_property ("channel", buf);
513 snprintf (buf, sizeof(buf), "0x%x", (int) control_additional);
514 node->add_property ("additional", buf);
515 node->add_property ("feedback", (feedback ? "yes" : "no"));
521 /** @return the maximum value for a control value transmitted
522 * using a given MIDI::eventType.
525 MIDIControllable::max_value_for_type () const
527 /* XXX: this is not complete */
529 if (control_type == MIDI::pitchbend) {