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