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