2 Copyright (C) 1998-99 Paul Barton-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.
21 #include <cstdio> /* for sprintf, sigh */
22 #include <pbd/error.h>
23 #include <midi++/port.h>
24 #include <midi++/channel.h>
25 #include <midi++/controllable.h>
31 Controllable::Controllable (Port *p, bool is_bistate)
34 _control_description = "MIDI Control: none";
35 control_additional = (byte) -1;
38 feedback = true; // for now
40 /* use channel 0 ("1") as the initial channel */
45 Controllable::~Controllable ()
47 drop_external_control ();
51 Controllable::midi_forget ()
53 /* stop listening for incoming messages, but retain
54 our existing event + type information.
57 if (connections > 0) {
58 midi_sense_connection[0].disconnect ();
61 if (connections > 1) {
62 midi_sense_connection[1].disconnect ();
66 midi_learn_connection.disconnect ();
71 Controllable::midi_rebind (Port *p, channel_t c)
73 if ((port = p) == 0) {
77 bind_midi (c, control_type, control_additional);
85 Controllable::learn_about_external_control ()
87 drop_external_control ();
90 midi_learn_connection = port->input()->any.connect (mem_fun (*this, &Controllable::midi_receiver));
94 info << "No MIDI port specified - external control disabled" << endmsg;
99 Controllable::stop_learning ()
101 midi_learn_connection.disconnect ();
105 Controllable::drop_external_control ()
107 if (connections > 0) {
108 midi_sense_connection[0].disconnect ();
110 if (connections > 1) {
111 midi_sense_connection[1].disconnect ();
115 midi_learn_connection.disconnect ();
118 control_additional = (byte) -1;
122 Controllable::midi_sense_note_on (Parser &p, EventTwoBytes *tb)
124 midi_sense_note (p, tb, true);
128 Controllable::midi_sense_note_off (Parser &p, EventTwoBytes *tb)
130 midi_sense_note (p, tb, false);
134 Controllable::midi_sense_note (Parser &p, EventTwoBytes *msg, bool is_on)
137 set_value (msg->note_number/127.0);
140 /* Note: parser handles the use of zero velocity to
141 mean note off. if we get called with is_on=true, then we
142 got a *real* note on.
145 if (msg->note_number == control_additional) {
146 set_value (is_on ? 1 : 0);
152 Controllable::midi_sense_controller (Parser &, EventTwoBytes *msg)
154 if (control_additional == msg->controller_number) {
156 set_value (msg->value/127.0);
158 if (msg->value > 64.0) {
168 Controllable::midi_sense_program_change (Parser &p, byte msg)
170 /* XXX program change messages make no sense for bistates */
173 set_value (msg/127.0);
178 Controllable::midi_sense_pitchbend (Parser &p, pitchbend_t pb)
180 /* pitchbend messages make no sense for bistates */
182 /* XXX gack - get rid of assumption about typeof pitchbend_t */
184 set_value ((pb/(float) SHRT_MAX));
188 Controllable::midi_receiver (Parser &p, byte *msg, size_t len)
190 /* we only respond to channel messages */
192 if ((msg[0] & 0xF0) < 0x80 || (msg[0] & 0xF0) > 0xE0) {
196 /* if the our port doesn't do input anymore, forget it ... */
198 if (!port->input()) {
202 bind_midi ((channel_t) (msg[0] & 0xf), eventType (msg[0] & 0xF0), msg[1]);
208 Controllable::bind_midi (channel_t chn, eventType ev, MIDI::byte additional)
212 drop_external_control ();
215 control_channel = chn;
216 control_additional = additional;
218 if (port == 0 || port->input() == 0) {
222 Parser& p = *port->input();
227 midi_sense_connection[0] = p.channel_note_off[chn_i].connect
228 (mem_fun (*this, &Controllable::midi_sense_note_off));
230 /* if this is a bistate, connect to noteOn as well,
231 and we'll toggle back and forth between the two.
235 midi_sense_connection[1] = p.channel_note_on[chn_i].connect
236 (mem_fun (*this, &Controllable::midi_sense_note_on));
241 _control_description = "MIDI control: NoteOff";
245 midi_sense_connection[0] = p.channel_note_on[chn_i].connect
246 (mem_fun (*this, &Controllable::midi_sense_note_on));
248 midi_sense_connection[1] = p.channel_note_off[chn_i].connect
249 (mem_fun (*this, &Controllable::midi_sense_note_off));
254 _control_description = "MIDI control: NoteOn";
257 case MIDI::controller:
258 midi_sense_connection[0] = p.channel_controller[chn_i].connect
259 (mem_fun (*this, &Controllable::midi_sense_controller));
261 snprintf (buf, sizeof (buf), "MIDI control: Controller %d", control_additional);
262 _control_description = buf;
267 midi_sense_connection[0] = p.channel_program_change[chn_i].connect
269 &Controllable::midi_sense_program_change));
271 _control_description = "MIDI control: ProgramChange";
275 case MIDI::pitchbend:
277 midi_sense_connection[0] = p.channel_pitchbend[chn_i].connect
278 (mem_fun (*this, &Controllable::midi_sense_pitchbend));
280 _control_description = "MIDI control: Pitchbend";
290 Controllable::set_control_type (channel_t chn, eventType ev, MIDI::byte additional)
292 bind_midi (chn, ev, additional);
296 Controllable::get_control_info (channel_t& chn, eventType& ev, byte& additional)
298 if (control_type == none) {
304 chn = control_channel;
305 additional = control_additional;
312 Controllable::send_midi_feedback (float val)
316 if (port == 0 || control_type == none) {
320 msg[0] = (control_type & 0xF0) | (control_channel & 0xF);
321 msg[1] = control_additional;
322 msg[2] = (byte) (val * 127.0f);
324 port->write (msg, 3);