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), _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;
61 feedback = true; // for now
63 /* use channel 0 ("1") as the initial channel */
69 MIDIControllable::midi_forget ()
71 /* stop listening for incoming messages, but retain
72 our existing event + type information.
75 if (connections > 0) {
76 midi_sense_connection[0].disconnect ();
79 if (connections > 1) {
80 midi_sense_connection[1].disconnect ();
84 midi_learn_connection.disconnect ();
89 MIDIControllable::reacquire_controllable ()
91 _controllable = Controllable::controllable_by_uri (current_uri);
95 MIDIControllable::midi_rebind (channel_t c)
98 bind_midi (c, control_type, control_additional);
105 MIDIControllable::learn_about_external_control ()
107 drop_external_control ();
108 midi_learn_connection = _port.input()->any.connect (mem_fun (*this, &MIDIControllable::midi_receiver));
112 MIDIControllable::stop_learning ()
114 midi_learn_connection.disconnect ();
118 MIDIControllable::drop_external_control ()
120 if (connections > 0) {
121 midi_sense_connection[0].disconnect ();
123 if (connections > 1) {
124 midi_sense_connection[1].disconnect ();
128 midi_learn_connection.disconnect ();
131 control_additional = (byte) -1;
135 MIDIControllable::control_to_midi(float val)
137 float control_min = 0.0f;
138 float control_max = 1.0f;
139 ARDOUR::AutomationControl* ac = dynamic_cast<ARDOUR::AutomationControl*>(&controllable);
141 control_min = ac->parameter().min();
142 control_max = ac->parameter().max();
145 const float control_range = control_max - control_min;
146 const float midi_range = 127.0f; // TODO: NRPN etc.
148 return (val - control_min) / control_range * midi_range;
152 MIDIControllable::midi_to_control(float val)
154 float control_min = 0.0f;
155 float control_max = 1.0f;
156 ARDOUR::AutomationControl* ac = dynamic_cast<ARDOUR::AutomationControl*>(&controllable);
158 control_min = ac->parameter().min();
159 control_max = ac->parameter().max();
162 const float control_range = control_max - control_min;
163 const float midi_range = 127.0f; // TODO: NRPN etc.
165 return val / midi_range * control_range + control_min;
169 MIDIControllable::midi_sense_note_on (Parser &p, EventTwoBytes *tb)
171 midi_sense_note (p, tb, true);
175 MIDIControllable::midi_sense_note_off (Parser &p, EventTwoBytes *tb)
177 midi_sense_note (p, tb, false);
181 MIDIControllable::midi_sense_note (Parser &, EventTwoBytes *msg, bool is_on)
188 controllable->set_value (msg->note_number/127.0);
191 /* Note: parser handles the use of zero velocity to
192 mean note off. if we get called with is_on=true, then we
193 got a *real* note on.
196 if (msg->note_number == control_additional) {
197 controllable.set_value (is_on ? 1 : 0);
201 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
205 MIDIControllable::midi_sense_controller (Parser &, EventTwoBytes *msg)
211 if (controllable->touching()) {
212 return; // to prevent feedback fights when e.g. dragging a UI slider
215 if (control_additional == msg->controller_number) {
217 controllable->set_value (midi_to_control(msg->value));
219 if (msg->value > 64.0) {
220 controllable->set_value (1);
222 controllable->set_value (0);
226 last_value = (MIDI::byte) (control_to_midi(controllable->get_value())); // to prevent feedback fights
231 MIDIControllable::midi_sense_program_change (Parser &, byte msg)
236 /* XXX program change messages make no sense for bistates */
239 controllable->set_value (msg/127.0);
240 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
245 MIDIControllable::midi_sense_pitchbend (Parser &, pitchbend_t pb)
251 /* pitchbend messages make no sense for bistates */
253 /* XXX gack - get rid of assumption about typeof pitchbend_t */
255 controllable->set_value ((pb/(float) SHRT_MAX));
256 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
260 MIDIControllable::midi_receiver (Parser &, byte *msg, size_t /*len*/)
262 /* we only respond to channel messages */
264 if ((msg[0] & 0xF0) < 0x80 || (msg[0] & 0xF0) > 0xE0) {
268 /* if the our port doesn't do input anymore, forget it ... */
270 if (!_port.input()) {
274 bind_midi ((channel_t) (msg[0] & 0xf), eventType (msg[0] & 0xF0), msg[1]);
276 controllable->LearningFinished ();
280 MIDIControllable::bind_midi (channel_t chn, eventType ev, MIDI::byte additional)
284 drop_external_control ();
287 control_channel = chn;
288 control_additional = additional;
290 if (_port.input() == 0) {
294 Parser& p = *_port.input();
299 midi_sense_connection[0] = p.channel_note_off[chn_i].connect
300 (mem_fun (*this, &MIDIControllable::midi_sense_note_off));
302 /* if this is a bistate, connect to noteOn as well,
303 and we'll toggle back and forth between the two.
307 midi_sense_connection[1] = p.channel_note_on[chn_i].connect
308 (mem_fun (*this, &MIDIControllable::midi_sense_note_on));
313 _control_description = "MIDI control: NoteOff";
317 midi_sense_connection[0] = p.channel_note_on[chn_i].connect
318 (mem_fun (*this, &MIDIControllable::midi_sense_note_on));
320 midi_sense_connection[1] = p.channel_note_off[chn_i].connect
321 (mem_fun (*this, &MIDIControllable::midi_sense_note_off));
326 _control_description = "MIDI control: NoteOn";
329 case MIDI::controller:
330 midi_sense_connection[0] = p.channel_controller[chn_i].connect
331 (mem_fun (*this, &MIDIControllable::midi_sense_controller));
333 snprintf (buf, sizeof (buf), "MIDI control: Controller %d", control_additional);
334 _control_description = buf;
339 midi_sense_connection[0] = p.channel_program_change[chn_i].connect
341 &MIDIControllable::midi_sense_program_change));
343 _control_description = "MIDI control: ProgramChange";
347 case MIDI::pitchbend:
349 midi_sense_connection[0] = p.channel_pitchbend[chn_i].connect
350 (mem_fun (*this, &MIDIControllable::midi_sense_pitchbend));
352 _control_description = "MIDI control: Pitchbend";
362 MIDIControllable::send_feedback ()
366 if (setting || !feedback || control_type == none) {
370 msg[0] = (control_type & 0xF0) | (control_channel & 0xF);
371 msg[1] = control_additional;
372 msg[2] = (byte) (control_to_midi(controllable->get_value()));
374 _port.write (msg, 3, 0);
378 MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool /*force*/)
380 if (control_type != none && feedback && bufsize > 2) {
382 MIDI::byte gm = (MIDI::byte) (control_to_midi(controllable->get_value()));
384 if (gm != last_value) {
385 *buf++ = (0xF0 & control_type) | (0xF & control_channel);
386 *buf++ = control_additional; /* controller number */
397 MIDIControllable::set_state (const XMLNode& node, int /*version*/)
399 const XMLProperty* prop;
402 if ((prop = node.property ("event")) != 0) {
403 sscanf (prop->value().c_str(), "0x%x", &xx);
404 control_type = (MIDI::eventType) xx;
409 if ((prop = node.property ("channel")) != 0) {
410 sscanf (prop->value().c_str(), "%d", &xx);
411 control_channel = (MIDI::channel_t) xx;
416 if ((prop = node.property ("additional")) != 0) {
417 sscanf (prop->value().c_str(), "0x%x", &xx);
418 control_additional = (MIDI::byte) xx;
423 if ((prop = node.property ("feedback")) != 0) {
424 feedback = (prop->value() == "yes");
426 feedback = true; // default
429 bind_midi (control_channel, control_type, control_additional);
435 MIDIControllable::get_state ()
442 XMLNode& node (controllable->get_state ());
444 snprintf (buf, sizeof(buf), "0x%x", (int) control_type);
445 node.add_property ("event", buf);
446 snprintf (buf, sizeof(buf), "%d", (int) control_channel);
447 node.add_property ("channel", buf);
448 snprintf (buf, sizeof(buf), "0x%x", (int) control_additional);
449 node.add_property ("additional", buf);
450 node.add_property ("feedback", (feedback ? "yes" : "no"));