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.
20 #include <cstdio> /* for sprintf, sigh */
22 #include "pbd/error.h"
23 #include "pbd/xml++.h"
24 #include "midi++/port.h"
25 #include "midi++/channel.h"
26 #include "ardour/automation_control.h"
28 #include "midicontrollable.h"
33 using namespace ARDOUR;
35 MIDIControllable::MIDIControllable (Port& p, const string& c, bool is_bistate)
36 : controllable (0), _current_uri (c), _port (p), bistate (is_bistate)
41 MIDIControllable::MIDIControllable (Port& p, Controllable& c, bool is_bistate)
42 : controllable (&c), _current_uri (c.uri()), _port (p), bistate (is_bistate)
47 MIDIControllable::~MIDIControllable ()
49 drop_external_control ();
53 MIDIControllable::init ()
56 last_value = 0; // got a better idea ?
58 _control_description = "MIDI Control: none";
59 control_additional = (byte) -1;
60 feedback = true; // for now
62 /* use channel 0 ("1") as the initial channel */
68 MIDIControllable::midi_forget ()
70 /* stop listening for incoming messages, but retain
71 our existing event + type information.
74 midi_sense_connection[0].disconnect ();
75 midi_sense_connection[1].disconnect ();
76 midi_learn_connection.disconnect ();
80 MIDIControllable::drop_external_control ()
82 midi_sense_connection[0].disconnect ();
83 midi_sense_connection[1].disconnect ();
84 midi_learn_connection.disconnect ();
87 control_additional = (byte) -1;
91 MIDIControllable::reacquire_controllable ()
93 if (!_current_uri.empty()) {
94 controllable = Controllable::by_uri (_current_uri);
101 MIDIControllable::midi_rebind (channel_t c)
104 bind_midi (c, control_type, control_additional);
111 MIDIControllable::learn_about_external_control ()
113 drop_external_control ();
114 _port.input()->any.connect (midi_learn_connection, boost::bind (&MIDIControllable::midi_receiver, this, _1, _2, _3));
118 MIDIControllable::stop_learning ()
120 midi_learn_connection.disconnect ();
124 MIDIControllable::control_to_midi(float val)
126 float control_min = 0.0f;
127 float control_max = 1.0f;
128 ARDOUR::AutomationControl* ac = dynamic_cast<ARDOUR::AutomationControl*>(controllable);
130 control_min = ac->parameter().min();
131 control_max = ac->parameter().max();
134 const float control_range = control_max - control_min;
135 const float midi_range = 127.0f; // TODO: NRPN etc.
137 return (val - control_min) / control_range * midi_range;
141 MIDIControllable::midi_to_control(float val)
143 float control_min = 0.0f;
144 float control_max = 1.0f;
145 ARDOUR::AutomationControl* ac = dynamic_cast<ARDOUR::AutomationControl*>(controllable);
147 control_min = ac->parameter().min();
148 control_max = ac->parameter().max();
151 const float control_range = control_max - control_min;
152 const float midi_range = 127.0f; // TODO: NRPN etc.
154 return val / midi_range * control_range + control_min;
158 MIDIControllable::midi_sense_note_on (Parser &p, EventTwoBytes *tb)
160 midi_sense_note (p, tb, true);
164 MIDIControllable::midi_sense_note_off (Parser &p, EventTwoBytes *tb)
166 midi_sense_note (p, tb, false);
170 MIDIControllable::midi_sense_note (Parser &, EventTwoBytes *msg, bool is_on)
177 controllable->set_value (msg->note_number/127.0);
180 /* Note: parser handles the use of zero velocity to
181 mean note off. if we get called with is_on=true, then we
182 got a *real* note on.
185 if (msg->note_number == control_additional) {
186 controllable->set_value (is_on ? 1 : 0);
190 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
194 MIDIControllable::midi_sense_controller (Parser &, EventTwoBytes *msg)
200 if (controllable->touching()) {
201 return; // to prevent feedback fights when e.g. dragging a UI slider
204 if (control_additional == msg->controller_number) {
206 controllable->set_value (midi_to_control(msg->value));
208 if (msg->value > 64.0) {
209 controllable->set_value (1);
211 controllable->set_value (0);
215 last_value = (MIDI::byte) (control_to_midi(controllable->get_value())); // to prevent feedback fights
220 MIDIControllable::midi_sense_program_change (Parser &, byte msg)
225 /* XXX program change messages make no sense for bistates */
228 controllable->set_value (msg/127.0);
229 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
234 MIDIControllable::midi_sense_pitchbend (Parser &, pitchbend_t pb)
240 /* pitchbend messages make no sense for bistates */
242 /* XXX gack - get rid of assumption about typeof pitchbend_t */
244 controllable->set_value ((pb/(float) SHRT_MAX));
245 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
249 MIDIControllable::midi_receiver (Parser &, byte *msg, size_t /*len*/)
251 /* we only respond to channel messages */
253 if ((msg[0] & 0xF0) < 0x80 || (msg[0] & 0xF0) > 0xE0) {
257 /* if the our port doesn't do input anymore, forget it ... */
259 if (!_port.input()) {
263 bind_midi ((channel_t) (msg[0] & 0xf), eventType (msg[0] & 0xF0), msg[1]);
265 controllable->LearningFinished ();
269 MIDIControllable::bind_midi (channel_t chn, eventType ev, MIDI::byte additional)
273 drop_external_control ();
276 control_channel = chn;
277 control_additional = additional;
279 if (_port.input() == 0) {
283 Parser& p = *_port.input();
288 p.channel_note_off[chn_i].connect (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_note_off, this, _1, _2));
290 /* if this is a bistate, connect to noteOn as well,
291 and we'll toggle back and forth between the two.
295 p.channel_note_on[chn_i].connect (midi_sense_connection[1], boost::bind (&MIDIControllable::midi_sense_note_on, this, _1, _2));
298 _control_description = "MIDI control: NoteOff";
302 p.channel_note_on[chn_i].connect (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_note_on, this, _1, _2));
304 p.channel_note_off[chn_i].connect (midi_sense_connection[1], boost::bind (&MIDIControllable::midi_sense_note_off, this, _1, _2));
306 _control_description = "MIDI control: NoteOn";
309 case MIDI::controller:
310 p.channel_controller[chn_i].connect (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_controller, this, _1, _2));
311 snprintf (buf, sizeof (buf), "MIDI control: Controller %d", control_additional);
312 _control_description = buf;
317 p.channel_program_change[chn_i].connect (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_program_change, this, _1, _2));
318 _control_description = "MIDI control: ProgramChange";
322 case MIDI::pitchbend:
324 p.channel_pitchbend[chn_i].connect (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_pitchbend, this, _1, _2));
325 _control_description = "MIDI control: Pitchbend";
335 MIDIControllable::send_feedback ()
339 if (setting || !feedback || control_type == none) {
343 msg[0] = (control_type & 0xF0) | (control_channel & 0xF);
344 msg[1] = control_additional;
345 msg[2] = (byte) (control_to_midi(controllable->get_value()));
347 _port.write (msg, 3, 0);
351 MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool /*force*/)
353 if (control_type != none && feedback && bufsize > 2) {
355 MIDI::byte gm = (MIDI::byte) (control_to_midi(controllable->get_value()));
357 if (gm != last_value) {
358 *buf++ = (0xF0 & control_type) | (0xF & control_channel);
359 *buf++ = control_additional; /* controller number */
370 MIDIControllable::set_state (const XMLNode& node, int /*version*/)
372 const XMLProperty* prop;
375 if ((prop = node.property ("event")) != 0) {
376 sscanf (prop->value().c_str(), "0x%x", &xx);
377 control_type = (MIDI::eventType) xx;
382 if ((prop = node.property ("channel")) != 0) {
383 sscanf (prop->value().c_str(), "%d", &xx);
384 control_channel = (MIDI::channel_t) xx;
389 if ((prop = node.property ("additional")) != 0) {
390 sscanf (prop->value().c_str(), "0x%x", &xx);
391 control_additional = (MIDI::byte) xx;
396 if ((prop = node.property ("feedback")) != 0) {
397 feedback = (prop->value() == "yes");
399 feedback = true; // default
402 bind_midi (control_channel, control_type, control_additional);
408 MIDIControllable::get_state ()
412 XMLNode* node = new XMLNode ("MIDIControllable");
415 node->add_property ("uri", controllable->uri());
416 snprintf (buf, sizeof(buf), "0x%x", (int) control_type);
417 node->add_property ("event", buf);
418 snprintf (buf, sizeof(buf), "%d", (int) control_channel);
419 node->add_property ("channel", buf);
420 snprintf (buf, sizeof(buf), "0x%x", (int) control_additional);
421 node->add_property ("additional", buf);
422 node->add_property ("feedback", (feedback ? "yes" : "no"));