f0dbd9cb777af6a1617dc4ad06f389d727c4a499
[ardour.git] / libs / midi++2 / midicontrollable.cc
1 /*
2     Copyright (C) 1998-99 Paul Barton-Davis
3  
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.
8
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.
13
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.
17
18     $Id$
19 */
20
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>
26
27 using namespace sigc;
28 using namespace MIDI;
29 using namespace PBD;
30
31 Controllable::Controllable (Port *p, bool is_bistate)
32 {
33         control_type = none;
34         _control_description = "MIDI Control: none";
35         control_additional = (byte) -1;
36         bistate = is_bistate;
37         connections = 0;
38         feedback = true; // for now
39         
40         /* use channel 0 ("1") as the initial channel */
41
42         midi_rebind (p, 0);
43 }
44
45 Controllable::~Controllable ()
46 {
47         drop_external_control ();
48 }
49
50 void
51 Controllable::midi_forget ()
52 {
53         /* stop listening for incoming messages, but retain
54            our existing event + type information.
55         */
56         
57         if (connections > 0) {
58                 midi_sense_connection[0].disconnect ();
59         } 
60         
61         if (connections > 1) {
62                 midi_sense_connection[1].disconnect ();
63         }
64         
65         connections = 0;
66         midi_learn_connection.disconnect ();
67         
68 }
69
70 void
71 Controllable::midi_rebind (Port *p, channel_t c)
72 {
73         if ((port = p) == 0) {
74                 midi_forget ();
75         } else {
76                 if (c >= 0) {
77                         bind_midi (c, control_type, control_additional);
78                 } else {
79                         midi_forget ();
80                 }
81         }
82 }
83
84 void
85 Controllable::learn_about_external_control ()
86 {
87         drop_external_control ();
88
89         if (port) {
90                 midi_learn_connection = port->input()->any.connect (mem_fun (*this, &Controllable::midi_receiver));
91                 learning_started ();
92
93         } else {
94                 info << "No MIDI port specified - external control disabled" << endmsg;
95         }
96 }
97
98 void
99 Controllable::stop_learning ()
100 {
101         midi_learn_connection.disconnect ();
102 }
103
104 void
105 Controllable::drop_external_control ()
106 {
107         if (connections > 0) {
108                 midi_sense_connection[0].disconnect ();
109         } 
110         if (connections > 1) {
111                 midi_sense_connection[1].disconnect ();
112         }
113
114         connections = 0;
115         midi_learn_connection.disconnect ();
116
117         control_type = none;
118         control_additional = (byte) -1;
119 }
120
121 void 
122 Controllable::midi_sense_note_on (Parser &p, EventTwoBytes *tb) 
123 {
124         midi_sense_note (p, tb, true);
125 }
126
127 void 
128 Controllable::midi_sense_note_off (Parser &p, EventTwoBytes *tb) 
129 {
130         midi_sense_note (p, tb, false);
131 }
132
133 void
134 Controllable::midi_sense_note (Parser &p, EventTwoBytes *msg, bool is_on)
135 {
136         if (!bistate) {
137                 set_value (msg->note_number/127.0);
138         } else {
139
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.  
143                 */
144
145                 if (msg->note_number == control_additional) {
146                         set_value (is_on ? 1 : 0);
147                 }
148         }
149 }
150
151 void
152 Controllable::midi_sense_controller (Parser &, EventTwoBytes *msg)
153 {
154         if (control_additional == msg->controller_number) {
155                 if (!bistate) {
156                         set_value (msg->value/127.0);
157                 } else {
158                         if (msg->value > 64.0) {
159                                 set_value (1);
160                         } else {
161                                 set_value (0);
162                         }
163                 }
164         }
165 }
166
167 void
168 Controllable::midi_sense_program_change (Parser &p, byte msg)
169 {
170         /* XXX program change messages make no sense for bistates */
171
172         if (!bistate) {
173                 set_value (msg/127.0);
174         } 
175 }
176
177 void
178 Controllable::midi_sense_pitchbend (Parser &p, pitchbend_t pb)
179 {
180         /* pitchbend messages make no sense for bistates */
181
182         /* XXX gack - get rid of assumption about typeof pitchbend_t */
183
184         set_value ((pb/(float) SHRT_MAX));
185 }                       
186
187 void
188 Controllable::midi_receiver (Parser &p, byte *msg, size_t len)
189 {
190         /* we only respond to channel messages */
191
192         if ((msg[0] & 0xF0) < 0x80 || (msg[0] & 0xF0) > 0xE0) {
193                 return;
194         }
195
196         /* if the our port doesn't do input anymore, forget it ... */
197
198         if (!port->input()) {
199                 return;
200         }
201
202         bind_midi ((channel_t) (msg[0] & 0xf), eventType (msg[0] & 0xF0), msg[1]);
203
204         learning_stopped ();
205 }
206
207 void
208 Controllable::bind_midi (channel_t chn, eventType ev, MIDI::byte additional)
209 {
210         char buf[64];
211
212         drop_external_control ();
213
214         control_type = ev;
215         control_channel = chn;
216         control_additional = additional;
217
218         if (port == 0 || port->input() == 0) {
219                 return;
220         }
221         
222         Parser& p = *port->input();
223
224         int chn_i = chn;
225         switch (ev) {
226         case MIDI::off:
227                 midi_sense_connection[0] = p.channel_note_off[chn_i].connect
228                         (mem_fun (*this, &Controllable::midi_sense_note_off));
229
230                 /* if this is a bistate, connect to noteOn as well,
231                    and we'll toggle back and forth between the two.
232                 */
233
234                 if (bistate) {
235                         midi_sense_connection[1] = p.channel_note_on[chn_i].connect
236                                 (mem_fun (*this, &Controllable::midi_sense_note_on));
237                         connections = 2;
238                 } else {
239                         connections = 1;
240                 }
241                 _control_description = "MIDI control: NoteOff";
242                 break;
243
244         case MIDI::on:
245                 midi_sense_connection[0] = p.channel_note_on[chn_i].connect
246                         (mem_fun (*this, &Controllable::midi_sense_note_on));
247                 if (bistate) {
248                         midi_sense_connection[1] = p.channel_note_off[chn_i].connect 
249                                 (mem_fun (*this, &Controllable::midi_sense_note_off));
250                         connections = 2;
251                 } else {
252                         connections = 1;
253                 }
254                 _control_description = "MIDI control: NoteOn";
255                 break;
256
257         case MIDI::controller:
258                 midi_sense_connection[0] = p.channel_controller[chn_i].connect 
259                         (mem_fun (*this, &Controllable::midi_sense_controller));
260                 connections = 1;
261                 snprintf (buf, sizeof (buf), "MIDI control: Controller %d", control_additional);
262                 _control_description = buf;
263                 break;
264
265         case MIDI::program:
266                 if (!bistate) {
267                         midi_sense_connection[0] = p.channel_program_change[chn_i].connect
268                                 (mem_fun (*this, 
269                                        &Controllable::midi_sense_program_change));
270                         connections = 1;
271                         _control_description = "MIDI control: ProgramChange";
272                 }
273                 break;
274
275         case MIDI::pitchbend:
276                 if (!bistate) {
277                         midi_sense_connection[0] = p.channel_pitchbend[chn_i].connect
278                                 (mem_fun (*this, &Controllable::midi_sense_pitchbend));
279                         connections = 1;
280                         _control_description = "MIDI control: Pitchbend";
281                 }
282                 break;
283
284         default:
285                 break;
286         }
287 }
288
289 void
290 Controllable::set_control_type (channel_t chn, eventType ev, MIDI::byte additional)
291 {
292         bind_midi (chn, ev, additional);
293 }
294
295 bool
296 Controllable::get_control_info (channel_t& chn, eventType& ev, byte& additional)
297 {
298         if (control_type == none) {
299                 chn = -1;
300                 return false;
301         } 
302         
303         ev = control_type;
304         chn = control_channel;
305         additional = control_additional;
306
307         return true;
308 }
309
310
311 void
312 Controllable::send_midi_feedback (float val)
313 {
314         byte msg[3];
315
316         if (port == 0 || control_type == none) {
317                 return;
318         }
319         
320         msg[0] = (control_type & 0xF0) | (control_channel & 0xF); 
321         msg[1] = control_additional;
322         msg[2] = (byte) (val * 127.0f);
323
324         port->write (msg, 3);
325 }
326