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"
34 using namespace ARDOUR;
36 MIDIControllable::MIDIControllable (Port& p, const string& c, bool is_bistate)
37 : controllable (0), _current_uri (c), _port (p), bistate (is_bistate)
42 MIDIControllable::MIDIControllable (Port& p, Controllable& c, bool is_bistate)
43 : controllable (&c), _current_uri (c.uri()), _port (p), bistate (is_bistate)
48 MIDIControllable::~MIDIControllable ()
50 drop_external_control ();
54 MIDIControllable::init ()
57 last_value = 0; // got a better idea ?
59 _control_description = "MIDI Control: none";
60 control_additional = (byte) -1;
62 feedback = true; // for now
64 /* use channel 0 ("1") as the initial channel */
70 MIDIControllable::midi_forget ()
72 /* stop listening for incoming messages, but retain
73 our existing event + type information.
76 if (connections > 0) {
77 midi_sense_connection[0].disconnect ();
80 if (connections > 1) {
81 midi_sense_connection[1].disconnect ();
85 midi_learn_connection.disconnect ();
90 MIDIControllable::reacquire_controllable ()
92 if (!_current_uri.empty()) {
93 controllable = Controllable::by_uri (_current_uri);
100 MIDIControllable::midi_rebind (channel_t c)
103 bind_midi (c, control_type, control_additional);
110 MIDIControllable::learn_about_external_control ()
112 drop_external_control ();
113 midi_learn_connection = _port.input()->any.connect (mem_fun (*this, &MIDIControllable::midi_receiver));
117 MIDIControllable::stop_learning ()
119 midi_learn_connection.disconnect ();
123 MIDIControllable::drop_external_control ()
125 if (connections > 0) {
126 midi_sense_connection[0].disconnect ();
128 if (connections > 1) {
129 midi_sense_connection[1].disconnect ();
133 midi_learn_connection.disconnect ();
136 control_additional = (byte) -1;
140 MIDIControllable::control_to_midi(float val)
142 float control_min = 0.0f;
143 float control_max = 1.0f;
144 ARDOUR::AutomationControl* ac = dynamic_cast<ARDOUR::AutomationControl*>(controllable);
146 control_min = ac->parameter().min();
147 control_max = ac->parameter().max();
150 const float control_range = control_max - control_min;
151 const float midi_range = 127.0f; // TODO: NRPN etc.
153 return (val - control_min) / control_range * midi_range;
157 MIDIControllable::midi_to_control(float val)
159 float control_min = 0.0f;
160 float control_max = 1.0f;
161 ARDOUR::AutomationControl* ac = dynamic_cast<ARDOUR::AutomationControl*>(controllable);
163 control_min = ac->parameter().min();
164 control_max = ac->parameter().max();
167 const float control_range = control_max - control_min;
168 const float midi_range = 127.0f; // TODO: NRPN etc.
170 return val / midi_range * control_range + control_min;
174 MIDIControllable::midi_sense_note_on (Parser &p, EventTwoBytes *tb)
176 midi_sense_note (p, tb, true);
180 MIDIControllable::midi_sense_note_off (Parser &p, EventTwoBytes *tb)
182 midi_sense_note (p, tb, false);
186 MIDIControllable::midi_sense_note (Parser &, EventTwoBytes *msg, bool is_on)
193 controllable->set_value (msg->note_number/127.0);
196 /* Note: parser handles the use of zero velocity to
197 mean note off. if we get called with is_on=true, then we
198 got a *real* note on.
201 if (msg->note_number == control_additional) {
202 controllable->set_value (is_on ? 1 : 0);
206 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
210 MIDIControllable::midi_sense_controller (Parser &, EventTwoBytes *msg)
216 if (controllable->touching()) {
217 return; // to prevent feedback fights when e.g. dragging a UI slider
220 if (control_additional == msg->controller_number) {
222 controllable->set_value (midi_to_control(msg->value));
224 if (msg->value > 64.0) {
225 controllable->set_value (1);
227 controllable->set_value (0);
231 last_value = (MIDI::byte) (control_to_midi(controllable->get_value())); // to prevent feedback fights
236 MIDIControllable::midi_sense_program_change (Parser &, byte msg)
241 /* XXX program change messages make no sense for bistates */
244 controllable->set_value (msg/127.0);
245 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
250 MIDIControllable::midi_sense_pitchbend (Parser &, pitchbend_t pb)
256 /* pitchbend messages make no sense for bistates */
258 /* XXX gack - get rid of assumption about typeof pitchbend_t */
260 controllable->set_value ((pb/(float) SHRT_MAX));
261 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
265 MIDIControllable::midi_receiver (Parser &, byte *msg, size_t /*len*/)
267 /* we only respond to channel messages */
269 if ((msg[0] & 0xF0) < 0x80 || (msg[0] & 0xF0) > 0xE0) {
273 /* if the our port doesn't do input anymore, forget it ... */
275 if (!_port.input()) {
279 bind_midi ((channel_t) (msg[0] & 0xf), eventType (msg[0] & 0xF0), msg[1]);
281 controllable->LearningFinished ();
285 MIDIControllable::bind_midi (channel_t chn, eventType ev, MIDI::byte additional)
289 drop_external_control ();
292 control_channel = chn;
293 control_additional = additional;
295 if (_port.input() == 0) {
299 Parser& p = *_port.input();
304 midi_sense_connection[0] = p.channel_note_off[chn_i].connect
305 (mem_fun (*this, &MIDIControllable::midi_sense_note_off));
307 /* if this is a bistate, connect to noteOn as well,
308 and we'll toggle back and forth between the two.
312 midi_sense_connection[1] = p.channel_note_on[chn_i].connect
313 (mem_fun (*this, &MIDIControllable::midi_sense_note_on));
318 _control_description = "MIDI control: NoteOff";
322 midi_sense_connection[0] = p.channel_note_on[chn_i].connect
323 (mem_fun (*this, &MIDIControllable::midi_sense_note_on));
325 midi_sense_connection[1] = p.channel_note_off[chn_i].connect
326 (mem_fun (*this, &MIDIControllable::midi_sense_note_off));
331 _control_description = "MIDI control: NoteOn";
334 case MIDI::controller:
335 midi_sense_connection[0] = p.channel_controller[chn_i].connect
336 (mem_fun (*this, &MIDIControllable::midi_sense_controller));
338 snprintf (buf, sizeof (buf), "MIDI control: Controller %d", control_additional);
339 _control_description = buf;
344 midi_sense_connection[0] = p.channel_program_change[chn_i].connect
346 &MIDIControllable::midi_sense_program_change));
348 _control_description = "MIDI control: ProgramChange";
352 case MIDI::pitchbend:
354 midi_sense_connection[0] = p.channel_pitchbend[chn_i].connect
355 (mem_fun (*this, &MIDIControllable::midi_sense_pitchbend));
357 _control_description = "MIDI control: Pitchbend";
367 MIDIControllable::send_feedback ()
371 if (setting || !feedback || control_type == none) {
375 msg[0] = (control_type & 0xF0) | (control_channel & 0xF);
376 msg[1] = control_additional;
377 msg[2] = (byte) (control_to_midi(controllable->get_value()));
379 _port.write (msg, 3, 0);
383 MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool /*force*/)
385 if (control_type != none && feedback && bufsize > 2) {
387 MIDI::byte gm = (MIDI::byte) (control_to_midi(controllable->get_value()));
389 if (gm != last_value) {
390 *buf++ = (0xF0 & control_type) | (0xF & control_channel);
391 *buf++ = control_additional; /* controller number */
402 MIDIControllable::set_state (const XMLNode& node, int /*version*/)
404 const XMLProperty* prop;
407 if ((prop = node.property ("event")) != 0) {
408 sscanf (prop->value().c_str(), "0x%x", &xx);
409 control_type = (MIDI::eventType) xx;
414 if ((prop = node.property ("channel")) != 0) {
415 sscanf (prop->value().c_str(), "%d", &xx);
416 control_channel = (MIDI::channel_t) xx;
421 if ((prop = node.property ("additional")) != 0) {
422 sscanf (prop->value().c_str(), "0x%x", &xx);
423 control_additional = (MIDI::byte) xx;
428 if ((prop = node.property ("feedback")) != 0) {
429 feedback = (prop->value() == "yes");
431 feedback = true; // default
434 bind_midi (control_channel, control_type, control_additional);
440 MIDIControllable::get_state ()
444 XMLNode* node = new XMLNode ("MIDIControllable");
447 node->add_property ("uri", controllable->uri());
448 snprintf (buf, sizeof(buf), "0x%x", (int) control_type);
449 node->add_property ("event", buf);
450 snprintf (buf, sizeof(buf), "%d", (int) control_channel);
451 node->add_property ("channel", buf);
452 snprintf (buf, sizeof(buf), "0x%x", (int) control_additional);
453 node->add_property ("additional", buf);
454 node->add_property ("feedback", (feedback ? "yes" : "no"));