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>
27 #include "midicontrollable.h"
32 using namespace ARDOUR;
34 MIDIControllable::MIDIControllable (Port& p, Controllable& c, bool is_bistate)
35 : controllable (c), _port (p), bistate (is_bistate)
38 last_written = 0; // got a better idea ?
40 _control_description = "MIDI Control: none";
41 control_additional = (byte) -1;
43 feedback = true; // for now
45 /* use channel 0 ("1") as the initial channel */
50 MIDIControllable::~MIDIControllable ()
52 drop_external_control ();
56 MIDIControllable::midi_forget ()
58 /* stop listening for incoming messages, but retain
59 our existing event + type information.
62 if (connections > 0) {
63 midi_sense_connection[0].disconnect ();
66 if (connections > 1) {
67 midi_sense_connection[1].disconnect ();
71 midi_learn_connection.disconnect ();
76 MIDIControllable::midi_rebind (channel_t c)
79 bind_midi (c, control_type, control_additional);
86 MIDIControllable::learn_about_external_control ()
88 drop_external_control ();
89 midi_learn_connection = _port.input()->any.connect (mem_fun (*this, &MIDIControllable::midi_receiver));
93 MIDIControllable::stop_learning ()
95 midi_learn_connection.disconnect ();
99 MIDIControllable::drop_external_control ()
101 cerr << "Dropping existing control using " << connections << " connections\n";
103 if (connections > 0) {
104 midi_sense_connection[0].disconnect ();
106 if (connections > 1) {
107 midi_sense_connection[1].disconnect ();
111 midi_learn_connection.disconnect ();
114 control_additional = (byte) -1;
118 MIDIControllable::midi_sense_note_on (Parser &p, EventTwoBytes *tb)
120 midi_sense_note (p, tb, true);
124 MIDIControllable::midi_sense_note_off (Parser &p, EventTwoBytes *tb)
126 midi_sense_note (p, tb, false);
130 MIDIControllable::midi_sense_note (Parser &p, EventTwoBytes *msg, bool is_on)
133 controllable.set_value (msg->note_number/127.0);
136 /* Note: parser handles the use of zero velocity to
137 mean note off. if we get called with is_on=true, then we
138 got a *real* note on.
141 if (msg->note_number == control_additional) {
142 controllable.set_value (is_on ? 1 : 0);
148 MIDIControllable::midi_sense_controller (Parser &, EventTwoBytes *msg)
150 if (control_additional == msg->controller_number) {
152 controllable.set_value (msg->value/127.0);
154 if (msg->value > 64.0) {
155 controllable.set_value (1);
157 controllable.set_value (0);
164 MIDIControllable::midi_sense_program_change (Parser &p, byte msg)
166 /* XXX program change messages make no sense for bistates */
169 controllable.set_value (msg/127.0);
174 MIDIControllable::midi_sense_pitchbend (Parser &p, pitchbend_t pb)
176 /* pitchbend messages make no sense for bistates */
178 /* XXX gack - get rid of assumption about typeof pitchbend_t */
180 controllable.set_value ((pb/(float) SHRT_MAX));
184 MIDIControllable::midi_receiver (Parser &p, byte *msg, size_t len)
186 /* we only respond to channel messages */
188 if ((msg[0] & 0xF0) < 0x80 || (msg[0] & 0xF0) > 0xE0) {
192 /* if the our port doesn't do input anymore, forget it ... */
194 if (!_port.input()) {
198 bind_midi ((channel_t) (msg[0] & 0xf), eventType (msg[0] & 0xF0), msg[1]);
200 controllable.LearningFinished ();
204 MIDIControllable::bind_midi (channel_t chn, eventType ev, MIDI::byte additional)
208 drop_external_control ();
211 control_channel = chn;
212 control_additional = additional;
214 if (_port.input() == 0) {
218 Parser& p = *_port.input();
223 midi_sense_connection[0] = p.channel_note_off[chn_i].connect
224 (mem_fun (*this, &MIDIControllable::midi_sense_note_off));
226 /* if this is a bistate, connect to noteOn as well,
227 and we'll toggle back and forth between the two.
231 midi_sense_connection[1] = p.channel_note_on[chn_i].connect
232 (mem_fun (*this, &MIDIControllable::midi_sense_note_on));
237 _control_description = "MIDI control: NoteOff";
241 midi_sense_connection[0] = p.channel_note_on[chn_i].connect
242 (mem_fun (*this, &MIDIControllable::midi_sense_note_on));
244 midi_sense_connection[1] = p.channel_note_off[chn_i].connect
245 (mem_fun (*this, &MIDIControllable::midi_sense_note_off));
250 _control_description = "MIDI control: NoteOn";
253 case MIDI::controller:
254 midi_sense_connection[0] = p.channel_controller[chn_i].connect
255 (mem_fun (*this, &MIDIControllable::midi_sense_controller));
257 snprintf (buf, sizeof (buf), "MIDI control: Controller %d", control_additional);
258 _control_description = buf;
263 midi_sense_connection[0] = p.channel_program_change[chn_i].connect
265 &MIDIControllable::midi_sense_program_change));
267 _control_description = "MIDI control: ProgramChange";
271 case MIDI::pitchbend:
273 midi_sense_connection[0] = p.channel_pitchbend[chn_i].connect
274 (mem_fun (*this, &MIDIControllable::midi_sense_pitchbend));
276 _control_description = "MIDI control: Pitchbend";
284 cerr << "MIDI bound with " << connections << endl;
288 MIDIControllable::send_feedback ()
292 if (setting || !feedback || control_type == none) {
296 msg[0] = (control_type & 0xF0) | (control_channel & 0xF);
297 msg[1] = control_additional;
298 msg[2] = (byte) (controllable.get_value() * 127.0f);
300 _port.write (msg, 3);
304 MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool force)
306 if (control_type != none && feedback && bufsize > 2) {
308 MIDI::byte gm = (MIDI::byte) (controllable.get_value() * 127.0);
310 if (gm != last_written) {
311 *buf++ = (0xF0 & control_type) | (0xF & control_channel);
312 *buf++ = control_additional; /* controller number */
323 MIDIControllable::set_state (const XMLNode& node)
325 const XMLProperty* prop;
328 if ((prop = node.property ("event")) != 0) {
329 sscanf (prop->value().c_str(), "0x%x", &xx);
330 control_type = (MIDI::eventType) xx;
335 if ((prop = node.property ("channel")) != 0) {
336 sscanf (prop->value().c_str(), "%d", &xx);
337 control_channel = (MIDI::channel_t) xx;
342 if ((prop = node.property ("additional")) != 0) {
343 sscanf (prop->value().c_str(), "0x%x", &xx);
344 control_additional = (MIDI::byte) xx;
349 if ((prop = node.property ("feedback")) != 0) {
350 feedback = (prop->value() == "yes");
352 feedback = true; // default
355 bind_midi (control_channel, control_type, control_additional);
361 MIDIControllable::get_state ()
364 XMLNode& node (controllable.get_state ());
366 snprintf (buf, sizeof(buf), "0x%x", (int) control_type);
367 node.add_property ("event", buf);
368 snprintf (buf, sizeof(buf), "%d", (int) control_channel);
369 node.add_property ("channel", buf);
370 snprintf (buf, sizeof(buf), "0x%x", (int) control_additional);
371 node.add_property ("additional", buf);
372 node.add_property ("feedback", (feedback ? "yes" : "no"));