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"
40 using namespace ARDOUR;
42 MIDIControllable::MIDIControllable (Port& p, bool m)
48 _learned = false; /* from URI */
50 last_value = 0; // got a better idea ?
52 _control_description = "MIDI Control: none";
53 control_additional = (byte) -1;
54 feedback = true; // for now
57 MIDIControllable::MIDIControllable (Port& p, Controllable& c, bool m)
63 _learned = true; /* from controllable */
65 last_value = 0; // got a better idea ?
67 _control_description = "MIDI Control: none";
68 control_additional = (byte) -1;
69 feedback = true; // for now
72 MIDIControllable::~MIDIControllable ()
74 drop_external_control ();
78 MIDIControllable::init (const std::string& s)
82 _descriptor = new ControllableDescriptor;
83 return _descriptor->set (s);
87 MIDIControllable::midi_forget ()
89 /* stop listening for incoming messages, but retain
90 our existing event + type information.
93 midi_sense_connection[0].disconnect ();
94 midi_sense_connection[1].disconnect ();
95 midi_learn_connection.disconnect ();
99 MIDIControllable::drop_external_control ()
103 control_additional = (byte) -1;
107 MIDIControllable::set_controllable (Controllable* c)
113 MIDIControllable::midi_rebind (channel_t c)
116 bind_midi (c, control_type, control_additional);
123 MIDIControllable::learn_about_external_control ()
125 drop_external_control ();
126 _port.parser()->any.connect_same_thread (midi_learn_connection, boost::bind (&MIDIControllable::midi_receiver, this, _1, _2, _3));
130 MIDIControllable::stop_learning ()
132 midi_learn_connection.disconnect ();
136 MIDIControllable::control_to_midi (float val)
138 const float midi_range = 127.0f; // TODO: NRPN etc.
140 if (controllable->is_gain_like()) {
141 return gain_to_slider_position (val/midi_range);
144 float control_min = controllable->lower ();
145 float control_max = controllable->upper ();
146 const float control_range = control_max - control_min;
148 return (val - control_min) / control_range * midi_range;
152 MIDIControllable::midi_to_control(float val)
154 /* fiddle with MIDI value so that we get an odd number of integer steps
155 and can thus represent "middle" precisely as 0.5. this maps to
161 val = (val == 0.0f ? 0.0f : (val-1.0f) / 126.0f);
163 if (controllable->is_gain_like()) {
164 return slider_position_to_gain (val);
167 float control_min = controllable->lower ();
168 float control_max = controllable->upper ();
169 const float control_range = control_max - control_min;
171 return (val * control_range) + control_min;
175 MIDIControllable::midi_sense_note_on (Parser &p, EventTwoBytes *tb)
177 midi_sense_note (p, tb, true);
181 MIDIControllable::midi_sense_note_off (Parser &p, EventTwoBytes *tb)
183 midi_sense_note (p, tb, false);
187 MIDIControllable::midi_sense_note (Parser &, EventTwoBytes *msg, bool /*is_on*/)
194 if (!controllable->is_toggle()) {
195 controllable->set_value (msg->note_number/127.0);
198 if (control_additional == msg->note_number) {
199 controllable->set_value (controllable->get_value() > 0.5f ? 0.0f : 1.0f);
203 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
207 MIDIControllable::midi_sense_controller (Parser &, EventTwoBytes *msg)
213 if (controllable->touching()) {
214 return; // to prevent feedback fights when e.g. dragging a UI slider
217 if (control_additional == msg->controller_number) {
219 if (!controllable->is_toggle()) {
220 controllable->set_value (midi_to_control (msg->value));
222 if (msg->value > 64.0f) {
223 controllable->set_value (1);
225 controllable->set_value (0);
229 last_value = (MIDI::byte) (control_to_midi(controllable->get_value())); // to prevent feedback fights
234 MIDIControllable::midi_sense_program_change (Parser &, byte msg)
240 if (!controllable->is_toggle()) {
241 controllable->set_value (msg/127.0);
243 controllable->set_value (controllable->get_value() > 0.5f ? 0.0f : 1.0f);
246 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
250 MIDIControllable::midi_sense_pitchbend (Parser &, pitchbend_t pb)
256 if (!controllable->is_toggle()) {
257 /* XXX gack - get rid of assumption about typeof pitchbend_t */
258 controllable->set_value ((pb/(float) SHRT_MAX));
260 controllable->set_value (controllable->get_value() > 0.5f ? 0.0f : 1.0f);
263 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
267 MIDIControllable::midi_receiver (Parser &, byte *msg, size_t /*len*/)
269 /* we only respond to channel messages */
271 if ((msg[0] & 0xF0) < 0x80 || (msg[0] & 0xF0) > 0xE0) {
275 /* if the our port doesn't do input anymore, forget it ... */
277 if (!_port.parser()) {
281 bind_midi ((channel_t) (msg[0] & 0xf), eventType (msg[0] & 0xF0), msg[1]);
283 controllable->LearningFinished ();
287 MIDIControllable::bind_midi (channel_t chn, eventType ev, MIDI::byte additional)
291 drop_external_control ();
294 control_channel = chn;
295 control_additional = additional;
297 if (_port.parser() == 0) {
301 Parser& p = *_port.parser();
306 p.channel_note_off[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_note_off, this, _1, _2));
308 /* if this is a togglee, connect to noteOn as well,
309 and we'll toggle back and forth between the two.
313 p.channel_note_on[chn_i].connect_same_thread (midi_sense_connection[1], boost::bind (&MIDIControllable::midi_sense_note_on, this, _1, _2));
316 _control_description = "MIDI control: NoteOff";
320 p.channel_note_on[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_note_on, this, _1, _2));
322 p.channel_note_off[chn_i].connect_same_thread (midi_sense_connection[1], boost::bind (&MIDIControllable::midi_sense_note_off, this, _1, _2));
324 _control_description = "MIDI control: NoteOn";
327 case MIDI::controller:
328 p.channel_controller[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_controller, this, _1, _2));
329 snprintf (buf, sizeof (buf), "MIDI control: Controller %d", control_additional);
330 _control_description = buf;
334 p.channel_program_change[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_program_change, this, _1, _2));
335 _control_description = "MIDI control: ProgramChange";
338 case MIDI::pitchbend:
339 p.channel_pitchbend[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_pitchbend, this, _1, _2));
340 _control_description = "MIDI control: Pitchbend";
349 MIDIControllable::send_feedback ()
353 if (!_learned || setting || !feedback || control_type == none || !controllable) {
357 msg[0] = (control_type & 0xF0) | (control_channel & 0xF);
358 msg[1] = control_additional;
360 if (controllable->is_gain_like()) {
361 msg[2] = (byte) lrintf (gain_to_slider_position (controllable->get_value()) * 127.0f);
363 msg[2] = (byte) (control_to_midi(controllable->get_value()));
366 _port.write (msg, 3, 0);
370 MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool /*force*/)
372 if (controllable && control_type != none && feedback && bufsize > 2) {
376 if (controllable->is_gain_like()) {
377 gm = (byte) lrintf (gain_to_slider_position (controllable->get_value()) * 127.0f);
379 gm = (byte) (control_to_midi(controllable->get_value()));
382 if (gm != last_value) {
383 *buf++ = (0xF0 & control_type) | (0xF & control_channel);
384 *buf++ = control_additional; /* controller number */
395 MIDIControllable::set_state (const XMLNode& node, int /*version*/)
397 const XMLProperty* prop;
400 if ((prop = node.property ("event")) != 0) {
401 sscanf (prop->value().c_str(), "0x%x", &xx);
402 control_type = (MIDI::eventType) xx;
407 if ((prop = node.property ("channel")) != 0) {
408 sscanf (prop->value().c_str(), "%d", &xx);
409 control_channel = (MIDI::channel_t) xx;
414 if ((prop = node.property ("additional")) != 0) {
415 sscanf (prop->value().c_str(), "0x%x", &xx);
416 control_additional = (MIDI::byte) xx;
421 if ((prop = node.property ("feedback")) != 0) {
422 feedback = (prop->value() == "yes");
424 feedback = true; // default
427 bind_midi (control_channel, control_type, control_additional);
433 MIDIControllable::get_state ()
437 XMLNode* node = new XMLNode ("MIDIControllable");
439 if (_current_uri.empty()) {
440 node->add_property ("id", controllable->id().to_s());
442 node->add_property ("uri", _current_uri);
446 snprintf (buf, sizeof(buf), "0x%x", (int) control_type);
447 node->add_property ("event", buf);
448 snprintf (buf, sizeof(buf), "%d", (int) control_channel);
449 node->add_property ("channel", buf);
450 snprintf (buf, sizeof(buf), "0x%x", (int) control_additional);
451 node->add_property ("additional", buf);
452 node->add_property ("feedback", (feedback ? "yes" : "no"));