2 Copyright (C) 1998-99 Paul Barton-Davis
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.
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.
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.
22 #include "midi++/types.h"
23 #include "midi++/port.h"
24 #include "midi++/channel.h"
28 Channel::Channel (MIDI::byte channelnum, Port &p)
30 , _channel_number (channelnum)
35 , _rpn_state (RPNState (0))
36 , _nrpn_state (RPNState (0))
42 Channel::connect_signals ()
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));
52 _port.parser()->reset.connect_same_thread (*this, boost::bind (&Channel::process_reset, this, _1));
56 Channel::reset (timestamp_t timestamp, samplecnt_t /*nframes*/, bool notes_off)
58 _program_number = _channel_number;
64 _last_on_velocity = 0;
65 _last_off_velocity = 0;
68 all_notes_off (timestamp);
71 memset (_polypress, 0, sizeof (_polypress));
72 memset (_controller_msb, 0, sizeof (_controller_msb));
73 memset (_controller_lsb, 0, sizeof (_controller_lsb));
75 /* zero all controllers XXX not necessarily the right thing */
77 memset (_controller_val, 0, sizeof (_controller_val));
79 for (int n = 0; n < 128; n++) {
80 _controller_14bit[n] = false;
99 _rpn_state = RPNState (0);
103 Channel::nrpn_reset ()
109 _nrpn_state = RPNState (0);
113 Channel::process_note_off (Parser & /*parser*/, EventTwoBytes *tb)
115 _last_note_off = tb->note_number;
116 _last_off_velocity = tb->velocity;
124 Channel::process_note_on (Parser & /*parser*/, EventTwoBytes *tb)
126 _last_note_on = tb->note_number;
127 _last_on_velocity = tb->velocity;
131 const Channel::RPNState Channel::RPN_READY_FOR_VALUE = RPNState (HaveLSB|HaveMSB);
132 const Channel::RPNState Channel::RPN_VALUE_READY = RPNState (HaveLSB|HaveMSB|HaveValue);
135 Channel::maybe_process_rpns (Parser& parser, EventTwoBytes *tb)
137 switch (tb->controller_number) {
139 _rpn_state = RPNState (_rpn_state|HaveMSB);
140 _rpn_lsb = tb->value;
141 if (_rpn_msb == 0x7f && _rpn_lsb == 0x7f) {
146 _rpn_state = RPNState (_rpn_state|HaveLSB);
147 _rpn_msb = tb->value;
148 if (_rpn_msb == 0x7f && _rpn_lsb == 0x7f) {
154 _nrpn_state = RPNState (_rpn_state|HaveMSB);
155 _rpn_lsb = tb->value;
156 if (_nrpn_msb == 0x7f && _nrpn_lsb == 0x7f) {
161 _nrpn_state = RPNState (_rpn_state|HaveLSB);
162 _rpn_msb = tb->value;
163 if (_rpn_msb == 0x7f && _rpn_lsb == 0x7f) {
169 if ((_nrpn_state & RPN_READY_FOR_VALUE) == RPN_READY_FOR_VALUE) {
171 uint16_t rpn_id = (_rpn_msb << 7)|_rpn_lsb;
173 switch (tb->controller_number) {
176 _nrpn_state = RPNState (_nrpn_state|HaveValue);
177 parser.channel_nrpn_change[_channel_number] (parser, rpn_id, 1); /* EMIT SIGNAL */
181 _nrpn_state = RPNState (_nrpn_state|HaveValue);
182 parser.channel_nrpn_change[_channel_number] (parser, rpn_id, -1); /* EMIT SIGNAL */
186 _nrpn_state = RPNState (_nrpn_state|HaveValue);
187 _nrpn_val_msb = tb->value;
191 _nrpn_state = RPNState (_nrpn_state|HaveValue);
192 _nrpn_val_lsb = tb->value;
195 if (_nrpn_state == RPN_VALUE_READY) {
197 float rpn_val = ((_rpn_val_msb << 7)|_rpn_val_lsb)/16384.0;
199 std::pair<RPNList::iterator,bool> result = nrpns.insert (std::make_pair (rpn_id, rpn_val));
201 if (!result.second) {
202 result.first->second = rpn_val;
205 parser.channel_nrpn[_channel_number] (parser, rpn_id, rpn_val); /* EMIT SIGNAL */
209 } else if ((_rpn_state & RPN_READY_FOR_VALUE) == RPN_READY_FOR_VALUE) {
211 uint16_t rpn_id = (_rpn_msb << 7)|_rpn_lsb;
213 switch (tb->controller_number) {
216 _rpn_state = RPNState (_rpn_state|HaveValue);
217 parser.channel_rpn_change[_channel_number] (parser, rpn_id, 1); /* EMIT SIGNAL */
221 _rpn_state = RPNState (_rpn_state|HaveValue);
222 parser.channel_rpn_change[_channel_number] (parser, rpn_id, -1); /* EMIT SIGNAL */
226 _rpn_state = RPNState (_rpn_state|HaveValue);
227 _rpn_val_msb = tb->value;
231 _rpn_state = RPNState (_rpn_state|HaveValue);
232 _rpn_val_lsb = tb->value;
235 if (_rpn_state == RPN_VALUE_READY) {
237 float rpn_val = ((_rpn_val_msb << 7)|_rpn_val_lsb)/16384.0;
239 std::pair<RPNList::iterator,bool> result = rpns.insert (std::make_pair (rpn_id, rpn_val));
241 if (!result.second) {
242 result.first->second = rpn_val;
245 parser.channel_rpn[_channel_number] (parser, rpn_id, rpn_val); /* EMIT SIGNAL */
254 Channel::process_controller (Parser & parser, EventTwoBytes *tb)
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.
263 if (maybe_process_rpns (parser, tb)) {
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.
273 if (tb->controller_number < 32) { /* unsigned: no test for >= 0 */
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.
279 otherwise, just treat it as a 7 bit value, and set
283 cv = (unsigned short) _controller_val[tb->controller_number];
285 if (_controller_14bit[tb->controller_number]) {
286 cv = ((tb->value & 0x7f) << 7) | (cv & 0x7f);
291 _controller_val[tb->controller_number] = (controller_value_t)cv;
293 } else if ((tb->controller_number >= 32 &&
294 tb->controller_number <= 63)) {
296 int cn = tb->controller_number - 32;
298 cv = (unsigned short) _controller_val[cn];
300 /* LSB for CC 0-31 arrived.
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.
307 otherwise, OR-in the new low 7bits with the old
312 if (_controller_14bit[cn] == false) {
313 _controller_14bit[cn] = true;
314 cv = (cv << 7) | (tb->value & 0x7f);
316 cv = (cv & 0x3f80) | (tb->value & 0x7f);
319 /* update the 14 bit value */
320 _controller_val[cn] = (controller_value_t) cv;
322 /* also store the "raw" 7 bit value in the incoming controller
325 _controller_val[tb->controller_number] = (controller_value_t) tb->value;
329 /* controller can only take 7 bit values */
331 _controller_val[tb->controller_number] =
332 (controller_value_t) tb->value;
335 /* bank numbers are special, in that they have their own signal
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);
346 Channel::process_program_change (Parser & /*parser*/, MIDI::byte val)
348 _program_number = val;
352 Channel::process_chanpress (Parser & /*parser*/, MIDI::byte val)
358 Channel::process_polypress (Parser & /*parser*/, EventTwoBytes *tb)
360 _polypress[tb->note_number] = tb->value;
364 Channel::process_pitchbend (Parser & /*parser*/, pitchbend_t val)
370 Channel::process_reset (Parser & /*parser*/)
375 /** Write a message to a channel.
376 * \return true if success
379 Channel::channel_msg (MIDI::byte id, MIDI::byte val1, MIDI::byte val2, timestamp_t timestamp)
381 unsigned char msg[3];
384 msg[0] = id | (_channel_number & 0xf);
388 msg[1] = val1 & 0x7F;
389 msg[2] = val2 & 0x7F;
394 msg[1] = val1 & 0x7F;
395 msg[2] = val2 & 0x7F;
399 case MIDI::polypress:
400 msg[1] = val1 & 0x7F;
401 msg[2] = val2 & 0x7F;
406 msg[1] = val1 & 0x7F;
407 msg[2] = val2 & 0x7F;
412 msg[1] = val1 & 0x7F;
416 case MIDI::chanpress:
417 msg[1] = val1 & 0x7F;
421 case MIDI::pitchbend:
422 msg[1] = val1 & 0x7F;
423 msg[2] = val2 & 0x7F;
428 return _port.midimsg (msg, len, timestamp);
432 Channel::rpn_value (uint16_t rpn) const
434 return rpn_value_absolute (rpn) / 16384.0f;
438 Channel::rpn_value_absolute (uint16_t rpn) const
440 RPNList::const_iterator r = rpns.find (rpn);
441 if (r == rpns.end()) {
448 Channel::nrpn_value (uint16_t nrpn) const
450 return nrpn_value_absolute (nrpn) / 16384.0f;
454 Channel::nrpn_value_absolute (uint16_t nrpn) const
456 RPNList::const_iterator r = nrpns.find (nrpn);
457 if (r == nrpns.end()) {