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