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"
29 #include "midi++/port.h"
30 #include "midi++/channel.h"
32 #include "ardour/automation_control.h"
33 #include "ardour/utils.h"
35 #include "midicontrollable.h"
36 #include "generic_midi_control_protocol.h"
41 using namespace ARDOUR;
43 MIDIControllable::MIDIControllable (GenericMidiControlProtocol* s, Port& p, bool m)
50 _learned = false; /* from URI */
52 last_value = 0; // got a better idea ?
53 last_controllable_value = 0.0f;
55 _control_description = "MIDI Control: none";
56 control_additional = (byte) -1;
57 feedback = true; // for now
60 MIDIControllable::MIDIControllable (GenericMidiControlProtocol* s, Port& p, Controllable& c, bool m)
67 _learned = true; /* from controllable */
69 last_value = 0; // got a better idea ?
70 last_controllable_value = 0.0f;
72 _control_description = "MIDI Control: none";
73 control_additional = (byte) -1;
74 feedback = true; // for now
77 MIDIControllable::~MIDIControllable ()
79 drop_external_control ();
83 MIDIControllable::init (const std::string& s)
87 _descriptor = new ControllableDescriptor;
88 return _descriptor->set (s);
92 MIDIControllable::midi_forget ()
94 /* stop listening for incoming messages, but retain
95 our existing event + type information.
98 midi_sense_connection[0].disconnect ();
99 midi_sense_connection[1].disconnect ();
100 midi_learn_connection.disconnect ();
104 MIDIControllable::drop_external_control ()
108 control_additional = (byte) -1;
112 MIDIControllable::set_controllable (Controllable* c)
118 MIDIControllable::midi_rebind (channel_t c)
121 bind_midi (c, control_type, control_additional);
128 MIDIControllable::learn_about_external_control ()
130 drop_external_control ();
131 _port.parser()->any.connect_same_thread (midi_learn_connection, boost::bind (&MIDIControllable::midi_receiver, this, _1, _2, _3));
135 MIDIControllable::stop_learning ()
137 midi_learn_connection.disconnect ();
141 MIDIControllable::control_to_midi (float val)
143 if (controllable->is_gain_like()) {
144 return gain_to_slider_position (val) * max_value_for_type ();
147 float control_min = controllable->lower ();
148 float control_max = controllable->upper ();
149 const float control_range = control_max - control_min;
151 return (val - control_min) / control_range * max_value_for_type ();
155 MIDIControllable::midi_to_control (int val)
157 /* fiddle with MIDI value so that we get an odd number of integer steps
158 and can thus represent "middle" precisely as 0.5. this maps to
162 float fv = (val == 0 ? 0 : float (val - 1) / (max_value_for_type() - 1));
164 if (controllable->is_gain_like()) {
165 return slider_position_to_gain (fv);
168 float control_min = controllable->lower ();
169 float control_max = controllable->upper ();
170 const float control_range = control_max - control_min;
172 return (fv * control_range) + control_min;
176 MIDIControllable::midi_sense_note_on (Parser &p, EventTwoBytes *tb)
178 midi_sense_note (p, tb, true);
182 MIDIControllable::midi_sense_note_off (Parser &p, EventTwoBytes *tb)
184 midi_sense_note (p, tb, false);
188 MIDIControllable::lookup_controllable()
194 boost::shared_ptr<Controllable> c = _surface->lookup_controllable (*_descriptor);
200 controllable = c.get();
206 MIDIControllable::midi_sense_note (Parser &, EventTwoBytes *msg, bool /*is_on*/)
209 if (lookup_controllable()) {
214 if (!controllable->is_toggle()) {
215 controllable->set_value (midi_to_control (msg->note_number));
217 if (control_additional == msg->note_number) {
218 controllable->set_value (controllable->get_value() > 0.5f ? 0.0f : 1.0f);
222 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
226 MIDIControllable::midi_sense_controller (Parser &, EventTwoBytes *msg)
229 if (lookup_controllable ()) {
234 if (controllable->touching()) {
235 return; // to prevent feedback fights when e.g. dragging a UI slider
238 if (control_additional == msg->controller_number) {
240 if (!controllable->is_toggle()) {
241 float new_value = msg->value;
242 float max_value = max(last_controllable_value, new_value);
243 float min_value = min(last_controllable_value, new_value);
244 float range = max_value - min_value;
245 float threshold = (float) _surface->threshold ();
247 bool const in_sync = (
249 controllable->get_value() <= midi_to_control(max_value) &&
250 controllable->get_value() >= midi_to_control(min_value)
253 /* If the surface is not motorised, we try to prevent jumps when
254 the MIDI controller and controllable are out of sync.
255 There might be a better way of doing this.
258 if (in_sync || _surface->motorised ()) {
259 controllable->set_value (midi_to_control (new_value));
262 last_controllable_value = new_value;
264 if (msg->value > 64.0f) {
265 controllable->set_value (1);
267 controllable->set_value (0);
271 last_value = (MIDI::byte) (control_to_midi(controllable->get_value())); // to prevent feedback fights
276 MIDIControllable::midi_sense_program_change (Parser &, byte msg)
279 if (lookup_controllable ()) {
284 if (!controllable->is_toggle()) {
285 controllable->set_value (midi_to_control (msg));
286 } else if (msg == control_additional) {
287 controllable->set_value (controllable->get_value() > 0.5f ? 0.0f : 1.0f);
290 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
294 MIDIControllable::midi_sense_pitchbend (Parser &, pitchbend_t pb)
297 if (lookup_controllable ()) {
302 if (!controllable->is_toggle()) {
303 controllable->set_value (midi_to_control (pb));
305 controllable->set_value (controllable->get_value() > 0.5f ? 0.0f : 1.0f);
308 last_value = control_to_midi (controllable->get_value ());
312 MIDIControllable::midi_receiver (Parser &, byte *msg, size_t /*len*/)
314 /* we only respond to channel messages */
316 if ((msg[0] & 0xF0) < 0x80 || (msg[0] & 0xF0) > 0xE0) {
320 /* if the our port doesn't do input anymore, forget it ... */
322 if (!_port.parser()) {
326 bind_midi ((channel_t) (msg[0] & 0xf), eventType (msg[0] & 0xF0), msg[1]);
329 controllable->LearningFinished ();
334 MIDIControllable::bind_midi (channel_t chn, eventType ev, MIDI::byte additional)
338 drop_external_control ();
341 control_channel = chn;
342 control_additional = additional;
344 if (_port.parser() == 0) {
348 Parser& p = *_port.parser();
353 p.channel_note_off[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_note_off, this, _1, _2));
355 /* if this is a togglee, connect to noteOn as well,
356 and we'll toggle back and forth between the two.
360 p.channel_note_on[chn_i].connect_same_thread (midi_sense_connection[1], boost::bind (&MIDIControllable::midi_sense_note_on, this, _1, _2));
363 _control_description = "MIDI control: NoteOff";
367 p.channel_note_on[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_note_on, this, _1, _2));
369 p.channel_note_off[chn_i].connect_same_thread (midi_sense_connection[1], boost::bind (&MIDIControllable::midi_sense_note_off, this, _1, _2));
371 _control_description = "MIDI control: NoteOn";
374 case MIDI::controller:
375 p.channel_controller[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_controller, this, _1, _2));
376 snprintf (buf, sizeof (buf), "MIDI control: Controller %d", control_additional);
377 _control_description = buf;
381 p.channel_program_change[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_program_change, this, _1, _2));
382 _control_description = "MIDI control: ProgramChange";
385 case MIDI::pitchbend:
386 p.channel_pitchbend[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_pitchbend, this, _1, _2));
387 _control_description = "MIDI control: Pitchbend";
396 MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool /*force*/)
398 if (!controllable || control_type == none || !feedback || bufsize <= 2) {
402 int const gm = control_to_midi (controllable->get_value());
404 if (gm == last_value) {
408 *buf++ = (0xF0 & control_type) | (0xF & control_channel);
410 switch (control_type) {
411 case MIDI::pitchbend:
412 *buf++ = int (gm) & 127;
413 *buf++ = (int (gm) >> 7) & 127;
416 *buf++ = control_additional; /* controller number */
428 MIDIControllable::set_state (const XMLNode& node, int /*version*/)
430 const XMLProperty* prop;
433 if ((prop = node.property ("event")) != 0) {
434 sscanf (prop->value().c_str(), "0x%x", &xx);
435 control_type = (MIDI::eventType) xx;
440 if ((prop = node.property ("channel")) != 0) {
441 sscanf (prop->value().c_str(), "%d", &xx);
442 control_channel = (MIDI::channel_t) xx;
447 if ((prop = node.property ("additional")) != 0) {
448 sscanf (prop->value().c_str(), "0x%x", &xx);
449 control_additional = (MIDI::byte) xx;
454 if ((prop = node.property ("feedback")) != 0) {
455 feedback = (prop->value() == "yes");
457 feedback = true; // default
460 bind_midi (control_channel, control_type, control_additional);
466 MIDIControllable::get_state ()
470 XMLNode* node = new XMLNode ("MIDIControllable");
472 if (_current_uri.empty()) {
473 node->add_property ("id", controllable->id().to_s());
475 node->add_property ("uri", _current_uri);
479 snprintf (buf, sizeof(buf), "0x%x", (int) control_type);
480 node->add_property ("event", buf);
481 snprintf (buf, sizeof(buf), "%d", (int) control_channel);
482 node->add_property ("channel", buf);
483 snprintf (buf, sizeof(buf), "0x%x", (int) control_additional);
484 node->add_property ("additional", buf);
485 node->add_property ("feedback", (feedback ? "yes" : "no"));
491 /** @return the maximum value for a control value transmitted
492 * using a given MIDI::eventType.
495 MIDIControllable::max_value_for_type () const
497 /* XXX: this is not complete */
499 if (control_type == MIDI::pitchbend) {