globally change all use of "frame" to refer to audio into "sample".
[ardour.git] / libs / midi++2 / channel.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 <cstring>
22 #include "midi++/types.h"
23 #include "midi++/port.h"
24 #include "midi++/channel.h"
25
26 using namespace MIDI;
27
28 Channel::Channel (MIDI::byte channelnum, Port &p)
29         : _port (p)
30         , _channel_number (channelnum)
31         , _rpn_msb (0)
32         , _rpn_lsb (0)
33         , _nrpn_msb (0)
34         , _nrpn_lsb (0)
35         , _rpn_state (RPNState (0))
36         , _nrpn_state (RPNState (0))
37 {
38         reset (0, 1, false);
39 }
40
41 void
42 Channel::connect_signals ()
43 {
44         _port.parser()->channel_pressure[_channel_number].connect_same_thread (*this, boost::bind (&Channel::process_chanpress, this, _1, _2));
45         _port.parser()->channel_note_on[_channel_number].connect_same_thread (*this, boost::bind (&Channel::process_note_on, this, _1, _2));
46         _port.parser()->channel_note_off[_channel_number].connect_same_thread (*this, boost::bind (&Channel::process_note_off, this, _1, _2));
47         _port.parser()->channel_poly_pressure[_channel_number].connect_same_thread (*this, boost::bind (&Channel::process_polypress, this, _1, _2));
48         _port.parser()->channel_program_change[_channel_number].connect_same_thread (*this, boost::bind (&Channel::process_program_change, this, _1, _2));
49         _port.parser()->channel_controller[_channel_number].connect_same_thread (*this, boost::bind (&Channel::process_controller, this, _1, _2));
50         _port.parser()->channel_pitchbend[_channel_number].connect_same_thread (*this, boost::bind (&Channel::process_pitchbend, this, _1, _2));
51
52         _port.parser()->reset.connect_same_thread (*this, boost::bind (&Channel::process_reset, this, _1));
53 }
54
55 void
56 Channel::reset (timestamp_t timestamp, samplecnt_t /*nframes*/, bool notes_off)
57 {
58         _program_number = _channel_number;
59         _bank_number = 0;
60         _pitch_bend = 0;
61
62         _last_note_on = 0;
63         _last_note_off = 0;
64         _last_on_velocity = 0;
65         _last_off_velocity = 0;
66
67         if (notes_off) {
68                 all_notes_off (timestamp);
69         }
70
71         memset (_polypress, 0, sizeof (_polypress));
72         memset (_controller_msb, 0, sizeof (_controller_msb));
73         memset (_controller_lsb, 0, sizeof (_controller_lsb));
74
75         /* zero all controllers XXX not necessarily the right thing */
76
77         memset (_controller_val, 0, sizeof (_controller_val));
78
79         for (int n = 0; n < 128; n++) {
80                 _controller_14bit[n] = false;
81         }
82
83         rpn_reset ();
84         nrpn_reset ();
85
86         _omni = true;
87         _poly = false;
88         _mono = true;
89         _notes_on = 0;
90 }
91
92 void
93 Channel::rpn_reset ()
94 {
95         _rpn_msb = 0;
96         _rpn_lsb = 0;
97         _rpn_val_msb = 0;
98         _rpn_val_lsb = 0;
99         _rpn_state = RPNState (0);
100 }
101
102 void
103 Channel::nrpn_reset ()
104 {
105         _nrpn_msb = 0;
106         _nrpn_lsb = 0;
107         _nrpn_val_msb = 0;
108         _nrpn_val_lsb = 0;
109         _nrpn_state = RPNState (0);
110 }
111
112 void
113 Channel::process_note_off (Parser & /*parser*/, EventTwoBytes *tb)
114 {
115         _last_note_off = tb->note_number;
116         _last_off_velocity = tb->velocity;
117
118         if (_notes_on) {
119                 _notes_on--;
120         }
121 }
122
123 void
124 Channel::process_note_on (Parser & /*parser*/, EventTwoBytes *tb)
125 {
126         _last_note_on = tb->note_number;
127         _last_on_velocity = tb->velocity;
128         _notes_on++;
129 }
130
131 const Channel::RPNState Channel::RPN_READY_FOR_VALUE = RPNState (HaveLSB|HaveMSB);
132 const Channel::RPNState Channel::RPN_VALUE_READY = RPNState (HaveLSB|HaveMSB|HaveValue);
133
134 bool
135 Channel::maybe_process_rpns (Parser& parser, EventTwoBytes *tb)
136 {
137         switch (tb->controller_number) {
138         case 0x62:
139                 _rpn_state = RPNState (_rpn_state|HaveMSB);
140                 _rpn_lsb = tb->value;
141                 if (_rpn_msb == 0x7f && _rpn_lsb == 0x7f) {
142                         rpn_reset ();
143                 }
144                 return true;
145         case 0x63:
146                 _rpn_state = RPNState (_rpn_state|HaveLSB);
147                 _rpn_msb = tb->value;
148                 if (_rpn_msb == 0x7f && _rpn_lsb == 0x7f) {
149                         rpn_reset ();
150                 }
151                 return true;
152
153         case 0x64:
154                 _nrpn_state = RPNState (_rpn_state|HaveMSB);
155                 _rpn_lsb = tb->value;
156                 if (_nrpn_msb == 0x7f && _nrpn_lsb == 0x7f) {
157                         nrpn_reset ();
158                 }
159                 return true;
160         case 0x65:
161                 _nrpn_state = RPNState (_rpn_state|HaveLSB);
162                 _rpn_msb = tb->value;
163                 if (_rpn_msb == 0x7f && _rpn_lsb == 0x7f) {
164                         nrpn_reset ();
165                 }
166                 return true;
167         }
168
169         if ((_nrpn_state & RPN_READY_FOR_VALUE) == RPN_READY_FOR_VALUE) {
170
171                 uint16_t rpn_id = (_rpn_msb << 7)|_rpn_lsb;
172
173                 switch (tb->controller_number) {
174                 case 0x60:
175                         /* data increment */
176                         _nrpn_state = RPNState (_nrpn_state|HaveValue);
177                         parser.channel_nrpn_change[_channel_number] (parser, rpn_id, 1); /* EMIT SIGNAL */
178                         return true;
179                 case 0x61:
180                         /* data decrement */
181                         _nrpn_state = RPNState (_nrpn_state|HaveValue);
182                         parser.channel_nrpn_change[_channel_number] (parser, rpn_id, -1); /* EMIT SIGNAL */
183                         return true;
184                 case 0x06:
185                         /* data entry MSB */
186                         _nrpn_state = RPNState (_nrpn_state|HaveValue);
187                         _nrpn_val_msb = tb->value;
188                         break;
189                 case 0x26:
190                         /* data entry LSB */
191                         _nrpn_state = RPNState (_nrpn_state|HaveValue);
192                         _nrpn_val_lsb = tb->value;
193                 }
194
195                 if (_nrpn_state == RPN_VALUE_READY) {
196
197                         float rpn_val = ((_rpn_val_msb << 7)|_rpn_val_lsb)/16384.0;
198
199                         std::pair<RPNList::iterator,bool> result = nrpns.insert (std::make_pair (rpn_id, rpn_val));
200
201                         if (!result.second) {
202                                 result.first->second = rpn_val;
203                         }
204
205                         parser.channel_nrpn[_channel_number] (parser, rpn_id, rpn_val); /* EMIT SIGNAL */
206                         return true;
207                 }
208
209         } else if ((_rpn_state & RPN_READY_FOR_VALUE) == RPN_READY_FOR_VALUE) {
210
211                 uint16_t rpn_id = (_rpn_msb << 7)|_rpn_lsb;
212
213                 switch (tb->controller_number) {
214                 case 0x60:
215                         /* data increment */
216                         _rpn_state = RPNState (_rpn_state|HaveValue);
217                         parser.channel_rpn_change[_channel_number] (parser, rpn_id, 1); /* EMIT SIGNAL */
218                         return true;
219                 case 0x61:
220                         /* data decrement */
221                         _rpn_state = RPNState (_rpn_state|HaveValue);
222                         parser.channel_rpn_change[_channel_number] (parser, rpn_id, -1); /* EMIT SIGNAL */
223                         return true;
224                 case 0x06:
225                         /* data entry MSB */
226                         _rpn_state = RPNState (_rpn_state|HaveValue);
227                         _rpn_val_msb = tb->value;
228                         break;
229                 case 0x26:
230                         /* data entry LSB */
231                         _rpn_state = RPNState (_rpn_state|HaveValue);
232                         _rpn_val_lsb = tb->value;
233                 }
234
235                 if (_rpn_state == RPN_VALUE_READY) {
236
237                         float    rpn_val = ((_rpn_val_msb << 7)|_rpn_val_lsb)/16384.0;
238
239                         std::pair<RPNList::iterator,bool> result = rpns.insert (std::make_pair (rpn_id, rpn_val));
240
241                         if (!result.second) {
242                                 result.first->second = rpn_val;
243                         }
244
245                         parser.channel_rpn[_channel_number] (parser, rpn_id, rpn_val); /* EMIT SIGNAL */
246                         return true;
247                 }
248         }
249
250         return false;
251 }
252
253 void
254 Channel::process_controller (Parser & parser, EventTwoBytes *tb)
255 {
256         unsigned short cv;
257
258         /* XXX arguably need a lock here to protect non-atomic changes
259            to controller_val[...]. or rather, need to make sure that
260            all changes *are* atomic.
261         */
262
263         if (maybe_process_rpns (parser, tb)) {
264                 return;
265         }
266
267         /* Note: if RPN data controllers (0x60, 0x61, 0x6, 0x26) are received
268          * without a previous RPN parameter ID message, or after the RPN ID
269          * has been reset, they will be treated like ordinary CC messages.
270          */
271
272
273         if (tb->controller_number < 32) { /* unsigned: no test for >= 0 */
274
275                 /* if this controller is already known to use 14 bits,
276                    then treat this value as the MSB, and combine it
277                    with the existing LSB.
278
279                    otherwise, just treat it as a 7 bit value, and set
280                    it directly.
281                 */
282
283                 cv = (unsigned short) _controller_val[tb->controller_number];
284
285                 if (_controller_14bit[tb->controller_number]) {
286                         cv = ((tb->value & 0x7f) << 7) | (cv & 0x7f);
287                 } else {
288                         cv = tb->value;
289                 }
290
291                 _controller_val[tb->controller_number] = (controller_value_t)cv;
292
293         } else if ((tb->controller_number >= 32 &&
294                     tb->controller_number <= 63)) {
295
296                 int cn = tb->controller_number - 32;
297
298                 cv = (unsigned short) _controller_val[cn];
299
300                 /* LSB for CC 0-31 arrived.
301
302                    If this is the first time (i.e. its currently
303                    flagged as a 7 bit controller), mark the
304                    controller as 14 bit, adjust the existing value
305                    to be the MSB, and OR-in the new LSB value.
306
307                    otherwise, OR-in the new low 7bits with the old
308                    high 7.
309                 */
310
311
312                 if (_controller_14bit[cn] == false) {
313                         _controller_14bit[cn] = true;
314                         cv = (cv << 7) | (tb->value & 0x7f);
315                 } else {
316                         cv = (cv & 0x3f80) | (tb->value & 0x7f);
317                 }
318
319                 /* update the 14 bit value */
320                 _controller_val[cn] = (controller_value_t) cv;
321
322                 /* also store the "raw" 7 bit value in the incoming controller
323                    value store
324                 */
325                 _controller_val[tb->controller_number] = (controller_value_t) tb->value;
326
327         } else {
328
329                 /* controller can only take 7 bit values */
330
331                 _controller_val[tb->controller_number] =
332                         (controller_value_t) tb->value;
333         }
334
335         /* bank numbers are special, in that they have their own signal
336          */
337
338         if (tb->controller_number == 0 || tb->controller_number == 0x20) {
339                 _bank_number = _controller_val[0];
340                 _port.parser()->bank_change (*_port.parser(), _bank_number);
341                 _port.parser()->channel_bank_change[_channel_number] (*_port.parser(), _bank_number);
342         }
343 }
344
345 void
346 Channel::process_program_change (Parser & /*parser*/, MIDI::byte val)
347 {
348         _program_number = val;
349 }
350
351 void
352 Channel::process_chanpress (Parser & /*parser*/, MIDI::byte val)
353 {
354         _chanpress = val;
355 }
356
357 void
358 Channel::process_polypress (Parser & /*parser*/, EventTwoBytes *tb)
359 {
360         _polypress[tb->note_number] = tb->value;
361 }
362
363 void
364 Channel::process_pitchbend (Parser & /*parser*/, pitchbend_t val)
365 {
366         _pitch_bend = val;
367 }
368
369 void
370 Channel::process_reset (Parser & /*parser*/)
371 {
372         reset (0, 1);
373 }
374
375 /** Write a message to a channel.
376  * \return true if success
377  */
378 bool
379 Channel::channel_msg (MIDI::byte id, MIDI::byte val1, MIDI::byte val2, timestamp_t timestamp)
380 {
381         unsigned char msg[3];
382         int len = 0;
383
384         msg[0] = id | (_channel_number & 0xf);
385
386         switch (id) {
387         case off:
388                 msg[1] = val1 & 0x7F;
389                 msg[2] = val2 & 0x7F;
390                 len = 3;
391                 break;
392
393         case on:
394                 msg[1] = val1 & 0x7F;
395                 msg[2] = val2 & 0x7F;
396                 len = 3;
397                 break;
398
399         case MIDI::polypress:
400                 msg[1] = val1 & 0x7F;
401                 msg[2] = val2 & 0x7F;
402                 len = 3;
403                 break;
404
405         case controller:
406                 msg[1] = val1 & 0x7F;
407                 msg[2] = val2 & 0x7F;
408                 len = 3;
409                 break;
410
411         case MIDI::program:
412                 msg[1] = val1 & 0x7F;
413                 len = 2;
414                 break;
415
416         case MIDI::chanpress:
417                 msg[1] = val1 & 0x7F;
418                 len = 2;
419                 break;
420
421         case MIDI::pitchbend:
422                 msg[1] = val1 & 0x7F;
423                 msg[2] = val2 & 0x7F;
424                 len = 3;
425                 break;
426         }
427
428         return _port.midimsg (msg, len, timestamp);
429 }
430
431 float
432 Channel::rpn_value (uint16_t rpn) const
433 {
434         return rpn_value_absolute (rpn) / 16384.0f;
435 }
436
437 float
438 Channel::rpn_value_absolute (uint16_t rpn) const
439 {
440         RPNList::const_iterator r = rpns.find (rpn);
441         if (r == rpns.end()) {
442                 return 0.0;
443         }
444         return r->second;
445 }
446
447 float
448 Channel::nrpn_value (uint16_t nrpn) const
449 {
450         return nrpn_value_absolute (nrpn) / 16384.0f;
451 }
452
453 float
454 Channel::nrpn_value_absolute (uint16_t nrpn) const
455 {
456         RPNList::const_iterator r = nrpns.find (nrpn);
457         if (r == nrpns.end()) {
458                 return 0.0;
459         }
460         return r->second;
461 }