2 * Copyright (C) 1998-2017 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2009-2010 Carl Hetherington <carl@carlh.net>
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.
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.
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.
21 #include "midi++/types.h"
22 #include "midi++/port.h"
23 #include "midi++/channel.h"
27 Channel::Channel (MIDI::byte channelnum, Port &p)
29 , _channel_number (channelnum)
34 , _rpn_state (RPNState (0))
35 , _nrpn_state (RPNState (0))
41 Channel::connect_signals ()
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));
51 _port.parser()->reset.connect_same_thread (*this, boost::bind (&Channel::process_reset, this, _1));
55 Channel::reset (timestamp_t timestamp, samplecnt_t /*nframes*/, bool notes_off)
57 _program_number = _channel_number;
63 _last_on_velocity = 0;
64 _last_off_velocity = 0;
67 all_notes_off (timestamp);
70 memset (_polypress, 0, sizeof (_polypress));
71 memset (_controller_msb, 0, sizeof (_controller_msb));
72 memset (_controller_lsb, 0, sizeof (_controller_lsb));
74 /* zero all controllers XXX not necessarily the right thing */
76 memset (_controller_val, 0, sizeof (_controller_val));
78 for (int n = 0; n < 128; n++) {
79 _controller_14bit[n] = false;
98 _rpn_state = RPNState (0);
102 Channel::nrpn_reset ()
108 _nrpn_state = RPNState (0);
112 Channel::process_note_off (Parser & /*parser*/, EventTwoBytes *tb)
114 _last_note_off = tb->note_number;
115 _last_off_velocity = tb->velocity;
123 Channel::process_note_on (Parser & /*parser*/, EventTwoBytes *tb)
125 _last_note_on = tb->note_number;
126 _last_on_velocity = tb->velocity;
130 const Channel::RPNState Channel::RPN_READY_FOR_VALUE = RPNState (HaveLSB|HaveMSB);
131 const Channel::RPNState Channel::RPN_VALUE_READY = RPNState (HaveLSB|HaveMSB|HaveValue);
134 Channel::maybe_process_rpns (Parser& parser, EventTwoBytes *tb)
136 switch (tb->controller_number) {
138 _rpn_state = RPNState (_rpn_state|HaveMSB);
139 _rpn_lsb = tb->value;
140 if (_rpn_msb == 0x7f && _rpn_lsb == 0x7f) {
145 _rpn_state = RPNState (_rpn_state|HaveLSB);
146 _rpn_msb = tb->value;
147 if (_rpn_msb == 0x7f && _rpn_lsb == 0x7f) {
153 _nrpn_state = RPNState (_rpn_state|HaveMSB);
154 _rpn_lsb = tb->value;
155 if (_nrpn_msb == 0x7f && _nrpn_lsb == 0x7f) {
160 _nrpn_state = RPNState (_rpn_state|HaveLSB);
161 _rpn_msb = tb->value;
162 if (_rpn_msb == 0x7f && _rpn_lsb == 0x7f) {
168 if ((_nrpn_state & RPN_READY_FOR_VALUE) == RPN_READY_FOR_VALUE) {
170 uint16_t rpn_id = (_rpn_msb << 7)|_rpn_lsb;
172 switch (tb->controller_number) {
175 _nrpn_state = RPNState (_nrpn_state|HaveValue);
176 parser.channel_nrpn_change[_channel_number] (parser, rpn_id, 1); /* EMIT SIGNAL */
180 _nrpn_state = RPNState (_nrpn_state|HaveValue);
181 parser.channel_nrpn_change[_channel_number] (parser, rpn_id, -1); /* EMIT SIGNAL */
185 _nrpn_state = RPNState (_nrpn_state|HaveValue);
186 _nrpn_val_msb = tb->value;
190 _nrpn_state = RPNState (_nrpn_state|HaveValue);
191 _nrpn_val_lsb = tb->value;
194 if (_nrpn_state == RPN_VALUE_READY) {
196 float rpn_val = ((_rpn_val_msb << 7)|_rpn_val_lsb)/16384.0;
198 std::pair<RPNList::iterator,bool> result = nrpns.insert (std::make_pair (rpn_id, rpn_val));
200 if (!result.second) {
201 result.first->second = rpn_val;
204 parser.channel_nrpn[_channel_number] (parser, rpn_id, rpn_val); /* EMIT SIGNAL */
208 } else if ((_rpn_state & RPN_READY_FOR_VALUE) == RPN_READY_FOR_VALUE) {
210 uint16_t rpn_id = (_rpn_msb << 7)|_rpn_lsb;
212 switch (tb->controller_number) {
215 _rpn_state = RPNState (_rpn_state|HaveValue);
216 parser.channel_rpn_change[_channel_number] (parser, rpn_id, 1); /* EMIT SIGNAL */
220 _rpn_state = RPNState (_rpn_state|HaveValue);
221 parser.channel_rpn_change[_channel_number] (parser, rpn_id, -1); /* EMIT SIGNAL */
225 _rpn_state = RPNState (_rpn_state|HaveValue);
226 _rpn_val_msb = tb->value;
230 _rpn_state = RPNState (_rpn_state|HaveValue);
231 _rpn_val_lsb = tb->value;
234 if (_rpn_state == RPN_VALUE_READY) {
236 float rpn_val = ((_rpn_val_msb << 7)|_rpn_val_lsb)/16384.0;
238 std::pair<RPNList::iterator,bool> result = rpns.insert (std::make_pair (rpn_id, rpn_val));
240 if (!result.second) {
241 result.first->second = rpn_val;
244 parser.channel_rpn[_channel_number] (parser, rpn_id, rpn_val); /* EMIT SIGNAL */
253 Channel::process_controller (Parser & parser, EventTwoBytes *tb)
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.
262 if (maybe_process_rpns (parser, tb)) {
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.
272 if (tb->controller_number < 32) { /* unsigned: no test for >= 0 */
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.
278 otherwise, just treat it as a 7 bit value, and set
282 cv = (unsigned short) _controller_val[tb->controller_number];
284 if (_controller_14bit[tb->controller_number]) {
285 cv = ((tb->value & 0x7f) << 7) | (cv & 0x7f);
290 _controller_val[tb->controller_number] = (controller_value_t)cv;
292 } else if ((tb->controller_number >= 32 &&
293 tb->controller_number <= 63)) {
295 int cn = tb->controller_number - 32;
297 cv = (unsigned short) _controller_val[cn];
299 /* LSB for CC 0-31 arrived.
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.
306 otherwise, OR-in the new low 7bits with the old
311 if (_controller_14bit[cn] == false) {
312 _controller_14bit[cn] = true;
313 cv = (cv << 7) | (tb->value & 0x7f);
315 cv = (cv & 0x3f80) | (tb->value & 0x7f);
318 /* update the 14 bit value */
319 _controller_val[cn] = (controller_value_t) cv;
321 /* also store the "raw" 7 bit value in the incoming controller
324 _controller_val[tb->controller_number] = (controller_value_t) tb->value;
328 /* controller can only take 7 bit values */
330 _controller_val[tb->controller_number] =
331 (controller_value_t) tb->value;
334 /* bank numbers are special, in that they have their own signal
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);
345 Channel::process_program_change (Parser & /*parser*/, MIDI::byte val)
347 _program_number = val;
351 Channel::process_chanpress (Parser & /*parser*/, MIDI::byte val)
357 Channel::process_polypress (Parser & /*parser*/, EventTwoBytes *tb)
359 _polypress[tb->note_number] = tb->value;
363 Channel::process_pitchbend (Parser & /*parser*/, pitchbend_t val)
369 Channel::process_reset (Parser & /*parser*/)
374 /** Write a message to a channel.
375 * \return true if success
378 Channel::channel_msg (MIDI::byte id, MIDI::byte val1, MIDI::byte val2, timestamp_t timestamp)
380 unsigned char msg[3];
383 msg[0] = id | (_channel_number & 0xf);
387 msg[1] = val1 & 0x7F;
388 msg[2] = val2 & 0x7F;
393 msg[1] = val1 & 0x7F;
394 msg[2] = val2 & 0x7F;
398 case MIDI::polypress:
399 msg[1] = val1 & 0x7F;
400 msg[2] = val2 & 0x7F;
405 msg[1] = val1 & 0x7F;
406 msg[2] = val2 & 0x7F;
411 msg[1] = val1 & 0x7F;
415 case MIDI::chanpress:
416 msg[1] = val1 & 0x7F;
420 case MIDI::pitchbend:
421 msg[1] = val1 & 0x7F;
422 msg[2] = val2 & 0x7F;
427 return _port.midimsg (msg, len, timestamp);
431 Channel::rpn_value (uint16_t rpn) const
433 return rpn_value_absolute (rpn) / 16384.0f;
437 Channel::rpn_value_absolute (uint16_t rpn) const
439 RPNList::const_iterator r = rpns.find (rpn);
440 if (r == rpns.end()) {
447 Channel::nrpn_value (uint16_t nrpn) const
449 return nrpn_value_absolute (nrpn) / 16384.0f;
453 Channel::nrpn_value_absolute (uint16_t nrpn) const
455 RPNList::const_iterator r = nrpns.find (nrpn);
456 if (r == nrpns.end()) {