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>
30 Controllable::Controllable (Port *p, bool is_bistate)
33 _control_description = "MIDI Control: none";
34 control_additional = (byte) -1;
37 feedback = true; // for now
39 /* use channel 0 ("1") as the initial channel */
44 Controllable::~Controllable ()
46 drop_external_control ();
50 Controllable::midi_forget ()
52 /* stop listening for incoming messages, but retain
53 our existing event + type information.
56 if (connections > 0) {
57 midi_sense_connection[0].disconnect ();
60 if (connections > 1) {
61 midi_sense_connection[1].disconnect ();
65 midi_learn_connection.disconnect ();
70 Controllable::midi_rebind (Port *p, channel_t c)
72 if ((port = p) == 0) {
76 bind_midi (c, control_type, control_additional);
84 Controllable::learn_about_external_control ()
86 drop_external_control ();
89 midi_learn_connection = port->input()->any.connect (mem_fun (*this, &Controllable::midi_receiver));
93 info << "No MIDI port specified - external control disabled" << endmsg;
98 Controllable::stop_learning ()
100 midi_learn_connection.disconnect ();
104 Controllable::drop_external_control ()
106 if (connections > 0) {
107 midi_sense_connection[0].disconnect ();
109 if (connections > 1) {
110 midi_sense_connection[1].disconnect ();
114 midi_learn_connection.disconnect ();
117 control_additional = (byte) -1;
121 Controllable::midi_sense_note_on (Parser &p, EventTwoBytes *tb)
123 midi_sense_note (p, tb, true);
127 Controllable::midi_sense_note_off (Parser &p, EventTwoBytes *tb)
129 midi_sense_note (p, tb, false);
133 Controllable::midi_sense_note (Parser &p, EventTwoBytes *msg, bool is_on)
136 set_value (msg->note_number/127.0);
139 /* Note: parser handles the use of zero velocity to
140 mean note off. if we get called with is_on=true, then we
141 got a *real* note on.
144 if (msg->note_number == control_additional) {
145 set_value (is_on ? 1 : 0);
151 Controllable::midi_sense_controller (Parser &, EventTwoBytes *msg)
153 if (control_additional == msg->controller_number) {
155 set_value (msg->value/127.0);
157 if (msg->value > 64.0) {
167 Controllable::midi_sense_program_change (Parser &p, byte msg)
169 /* XXX program change messages make no sense for bistates */
172 set_value (msg/127.0);
177 Controllable::midi_sense_pitchbend (Parser &p, pitchbend_t pb)
179 /* pitchbend messages make no sense for bistates */
181 /* XXX gack - get rid of assumption about typeof pitchbend_t */
183 set_value ((pb/(float) SHRT_MAX));
187 Controllable::midi_receiver (Parser &p, byte *msg, size_t len)
189 /* we only respond to channel messages */
191 if ((msg[0] & 0xF0) < 0x80 || (msg[0] & 0xF0) > 0xE0) {
195 /* if the our port doesn't do input anymore, forget it ... */
197 if (!port->input()) {
201 bind_midi ((channel_t) (msg[0] & 0xf), eventType (msg[0] & 0xF0), msg[1]);
207 Controllable::bind_midi (channel_t chn, eventType ev, MIDI::byte additional)
211 drop_external_control ();
214 control_channel = chn;
215 control_additional = additional;
217 if (port == 0 || port->input() == 0) {
221 Parser& p = *port->input();
226 midi_sense_connection[0] = p.channel_note_off[chn_i].connect
227 (mem_fun (*this, &Controllable::midi_sense_note_off));
229 /* if this is a bistate, connect to noteOn as well,
230 and we'll toggle back and forth between the two.
234 midi_sense_connection[1] = p.channel_note_on[chn_i].connect
235 (mem_fun (*this, &Controllable::midi_sense_note_on));
240 _control_description = "MIDI control: NoteOff";
244 midi_sense_connection[0] = p.channel_note_on[chn_i].connect
245 (mem_fun (*this, &Controllable::midi_sense_note_on));
247 midi_sense_connection[1] = p.channel_note_off[chn_i].connect
248 (mem_fun (*this, &Controllable::midi_sense_note_off));
253 _control_description = "MIDI control: NoteOn";
256 case MIDI::controller:
257 midi_sense_connection[0] = p.channel_controller[chn_i].connect
258 (mem_fun (*this, &Controllable::midi_sense_controller));
260 snprintf (buf, sizeof (buf), "MIDI control: Controller %d", control_additional);
261 _control_description = buf;
266 midi_sense_connection[0] = p.channel_program_change[chn_i].connect
268 &Controllable::midi_sense_program_change));
270 _control_description = "MIDI control: ProgramChange";
274 case MIDI::pitchbend:
276 midi_sense_connection[0] = p.channel_pitchbend[chn_i].connect
277 (mem_fun (*this, &Controllable::midi_sense_pitchbend));
279 _control_description = "MIDI control: Pitchbend";
289 Controllable::set_control_type (channel_t chn, eventType ev, MIDI::byte additional)
291 bind_midi (chn, ev, additional);
295 Controllable::get_control_info (channel_t& chn, eventType& ev, byte& additional)
297 if (control_type == none) {
303 chn = control_channel;
304 additional = control_additional;
311 Controllable::send_midi_feedback (float val)
315 if (port == 0 || control_type == none) {
319 msg[0] = (control_type & 0xF0) | (control_channel & 0xF);
320 msg[1] = control_additional;
321 msg[2] = (byte) (val * 127.0f);
323 port->write (msg, 3);