Merged with trunk R920.
[ardour.git] / libs / surfaces / generic_midi / midicontrollable.cc
1 /*
2     Copyright (C) 1998-2006 Paul 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: midicontrollable.cc 629 2006-06-21 23:01:03Z paul $
19 */
20
21 #include <cstdio> /* for sprintf, sigh */
22 #include <climits>
23 #include <pbd/error.h>
24 #include <pbd/xml++.h>
25 #include <midi++/port.h>
26 #include <midi++/channel.h>
27
28 #include "midicontrollable.h"
29
30 using namespace sigc;
31 using namespace MIDI;
32 using namespace PBD;
33 using namespace ARDOUR;
34
35 bool MIDIControllable::_send_feedback = false;
36
37 MIDIControllable::MIDIControllable (Port& p, Controllable& c, bool is_bistate)
38         : controllable (c), _port (p), bistate (is_bistate)
39 {
40         setting = false;
41         last_written = 0; // got a better idea ?
42         control_type = none;
43         _control_description = "MIDI Control: none";
44         control_additional = (byte) -1;
45         connections = 0;
46         feedback = true; // for now
47         
48         /* use channel 0 ("1") as the initial channel */
49
50         midi_rebind (0);
51 }
52
53 MIDIControllable::~MIDIControllable ()
54 {
55         drop_external_control ();
56 }
57
58 void
59 MIDIControllable::midi_forget ()
60 {
61         /* stop listening for incoming messages, but retain
62            our existing event + type information.
63         */
64         
65         if (connections > 0) {
66                 midi_sense_connection[0].disconnect ();
67         } 
68         
69         if (connections > 1) {
70                 midi_sense_connection[1].disconnect ();
71         }
72         
73         connections = 0;
74         midi_learn_connection.disconnect ();
75         
76 }
77
78 void
79 MIDIControllable::midi_rebind (channel_t c)
80 {
81         if (c >= 0) {
82                 bind_midi (c, control_type, control_additional);
83         } else {
84                 midi_forget ();
85         }
86 }
87
88 void
89 MIDIControllable::learn_about_external_control ()
90 {
91         drop_external_control ();
92         midi_learn_connection = _port.input()->any.connect (mem_fun (*this, &MIDIControllable::midi_receiver));
93 }
94
95 void
96 MIDIControllable::stop_learning ()
97 {
98         midi_learn_connection.disconnect ();
99 }
100
101 void
102 MIDIControllable::drop_external_control ()
103 {
104         if (connections > 0) {
105                 midi_sense_connection[0].disconnect ();
106         } 
107         if (connections > 1) {
108                 midi_sense_connection[1].disconnect ();
109         }
110
111         connections = 0;
112         midi_learn_connection.disconnect ();
113
114         control_type = none;
115         control_additional = (byte) -1;
116 }
117
118 void 
119 MIDIControllable::midi_sense_note_on (Parser &p, EventTwoBytes *tb) 
120 {
121         midi_sense_note (p, tb, true);
122 }
123
124 void 
125 MIDIControllable::midi_sense_note_off (Parser &p, EventTwoBytes *tb) 
126 {
127         midi_sense_note (p, tb, false);
128 }
129
130 void
131 MIDIControllable::midi_sense_note (Parser &p, EventTwoBytes *msg, bool is_on)
132 {
133         if (!bistate) {
134                 controllable.set_value (msg->note_number/127.0);
135         } else {
136
137                 /* Note: parser handles the use of zero velocity to
138                    mean note off. if we get called with is_on=true, then we
139                    got a *real* note on.  
140                 */
141
142                 if (msg->note_number == control_additional) {
143                         controllable.set_value (is_on ? 1 : 0);
144                 }
145         }
146 }
147
148 void
149 MIDIControllable::midi_sense_controller (Parser &, EventTwoBytes *msg)
150 {
151         if (control_additional == msg->controller_number) {
152                 if (!bistate) {
153                         controllable.set_value (msg->value/127.0);
154                 } else {
155                         if (msg->value > 64.0) {
156                                 controllable.set_value (1);
157                         } else {
158                                 controllable.set_value (0);
159                         }
160                 }
161         }
162 }
163
164 void
165 MIDIControllable::midi_sense_program_change (Parser &p, byte msg)
166 {
167         /* XXX program change messages make no sense for bistates */
168
169         if (!bistate) {
170                 controllable.set_value (msg/127.0);
171         } 
172 }
173
174 void
175 MIDIControllable::midi_sense_pitchbend (Parser &p, pitchbend_t pb)
176 {
177         /* pitchbend messages make no sense for bistates */
178
179         /* XXX gack - get rid of assumption about typeof pitchbend_t */
180
181         controllable.set_value ((pb/(float) SHRT_MAX));
182 }                       
183
184 void
185 MIDIControllable::midi_receiver (Parser &p, byte *msg, size_t len)
186 {
187         /* we only respond to channel messages */
188
189         if ((msg[0] & 0xF0) < 0x80 || (msg[0] & 0xF0) > 0xE0) {
190                 return;
191         }
192
193         /* if the our port doesn't do input anymore, forget it ... */
194
195         if (!_port.input()) {
196                 return;
197         }
198
199         bind_midi ((channel_t) (msg[0] & 0xf), eventType (msg[0] & 0xF0), msg[1]);
200
201         controllable.LearningFinished ();
202 }
203
204 void
205 MIDIControllable::bind_midi (channel_t chn, eventType ev, MIDI::byte additional)
206 {
207         char buf[64];
208
209         drop_external_control ();
210
211         control_type = ev;
212         control_channel = chn;
213         control_additional = additional;
214
215         if (_port.input() == 0) {
216                 return;
217         }
218         
219         Parser& p = *_port.input();
220
221         int chn_i = chn;
222         switch (ev) {
223         case MIDI::off:
224                 midi_sense_connection[0] = p.channel_note_off[chn_i].connect
225                         (mem_fun (*this, &MIDIControllable::midi_sense_note_off));
226
227                 /* if this is a bistate, connect to noteOn as well,
228                    and we'll toggle back and forth between the two.
229                 */
230
231                 if (bistate) {
232                         midi_sense_connection[1] = p.channel_note_on[chn_i].connect
233                                 (mem_fun (*this, &MIDIControllable::midi_sense_note_on));
234                         connections = 2;
235                 } else {
236                         connections = 1;
237                 }
238                 _control_description = "MIDI control: NoteOff";
239                 break;
240
241         case MIDI::on:
242                 midi_sense_connection[0] = p.channel_note_on[chn_i].connect
243                         (mem_fun (*this, &MIDIControllable::midi_sense_note_on));
244                 if (bistate) {
245                         midi_sense_connection[1] = p.channel_note_off[chn_i].connect 
246                                 (mem_fun (*this, &MIDIControllable::midi_sense_note_off));
247                         connections = 2;
248                 } else {
249                         connections = 1;
250                 }
251                 _control_description = "MIDI control: NoteOn";
252                 break;
253
254         case MIDI::controller:
255                 midi_sense_connection[0] = p.channel_controller[chn_i].connect 
256                         (mem_fun (*this, &MIDIControllable::midi_sense_controller));
257                 connections = 1;
258                 snprintf (buf, sizeof (buf), "MIDI control: Controller %d", control_additional);
259                 _control_description = buf;
260                 break;
261
262         case MIDI::program:
263                 if (!bistate) {
264                         midi_sense_connection[0] = p.channel_program_change[chn_i].connect
265                                 (mem_fun (*this, 
266                                        &MIDIControllable::midi_sense_program_change));
267                         connections = 1;
268                         _control_description = "MIDI control: ProgramChange";
269                 }
270                 break;
271
272         case MIDI::pitchbend:
273                 if (!bistate) {
274                         midi_sense_connection[0] = p.channel_pitchbend[chn_i].connect
275                                 (mem_fun (*this, &MIDIControllable::midi_sense_pitchbend));
276                         connections = 1;
277                         _control_description = "MIDI control: Pitchbend";
278                 }
279                 break;
280
281         default:
282                 break;
283         }
284 }
285
286 void
287 MIDIControllable::send_feedback ()
288 {
289         byte msg[3];
290
291         if (setting || !_send_feedback || control_type == none) {
292                 return;
293         }
294         
295         msg[0] = (control_type & 0xF0) | (control_channel & 0xF); 
296         msg[1] = control_additional;
297         msg[2] = (byte) (controllable.get_value() * 127.0f);
298
299         //_port.write (msg, 3);
300 }
301
302 MIDI::byte*
303 MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool force)
304 {
305         if (control_type != none &&_send_feedback && bufsize > 2) {
306
307                 MIDI::byte gm = (MIDI::byte) (controllable.get_value() * 127.0);
308                 
309                 if (gm != last_written) {
310                         *buf++ = (0xF0 & control_type) | (0xF & control_channel);
311                         *buf++ = control_additional; /* controller number */
312                         *buf++ = gm;
313                         last_written = gm;
314                         bufsize -= 3;
315                 }
316         }
317         
318         return buf;
319 }
320
321 int 
322 MIDIControllable::set_state (const XMLNode& node)
323 {
324         const XMLProperty* prop;
325         int xx;
326
327         if ((prop = node.property ("event")) != 0) {
328                 sscanf (prop->value().c_str(), "0x%x", &xx);
329                 control_type = (MIDI::eventType) xx;
330         } else {
331                 return -1;
332         }
333
334         if ((prop = node.property ("channel")) != 0) {
335                 sscanf (prop->value().c_str(), "%d", &xx);
336                 control_channel = (MIDI::channel_t) xx;
337         } else {
338                 return -1;
339         }
340
341         if ((prop = node.property ("additional")) != 0) {
342                 sscanf (prop->value().c_str(), "0x%x", &xx);
343                 control_additional = (MIDI::byte) xx;
344         } else {
345                 return -1;
346         }
347
348         return 0;
349 }
350
351 XMLNode&
352 MIDIControllable::get_state ()
353 {
354         char buf[32];
355         XMLNode& node (controllable.get_state ());
356
357         snprintf (buf, sizeof(buf), "0x%x", (int) control_type);
358         node.add_property ("event", buf);
359         snprintf (buf, sizeof(buf), "%d", (int) control_channel);
360         node.add_property ("channel", buf);
361         snprintf (buf, sizeof(buf), "0x%x", (int) control_additional);
362         node.add_property ("additional", buf);
363
364         return node;
365 }
366