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)
31 _channel_number = channelnum;
37 Channel::connect_signals ()
39 _port.parser()->channel_pressure[_channel_number].connect_same_thread (*this, boost::bind (&Channel::process_chanpress, this, _1, _2));
40 _port.parser()->channel_note_on[_channel_number].connect_same_thread (*this, boost::bind (&Channel::process_note_on, this, _1, _2));
41 _port.parser()->channel_note_off[_channel_number].connect_same_thread (*this, boost::bind (&Channel::process_note_off, this, _1, _2));
42 _port.parser()->channel_poly_pressure[_channel_number].connect_same_thread (*this, boost::bind (&Channel::process_polypress, this, _1, _2));
43 _port.parser()->channel_program_change[_channel_number].connect_same_thread (*this, boost::bind (&Channel::process_program_change, this, _1, _2));
44 _port.parser()->channel_controller[_channel_number].connect_same_thread (*this, boost::bind (&Channel::process_controller, this, _1, _2));
45 _port.parser()->channel_pitchbend[_channel_number].connect_same_thread (*this, boost::bind (&Channel::process_pitchbend, this, _1, _2));
47 _port.parser()->reset.connect_same_thread (*this, boost::bind (&Channel::process_reset, this, _1));
51 Channel::reset (timestamp_t timestamp, framecnt_t /*nframes*/, bool notes_off)
53 _program_number = _channel_number;
59 _last_on_velocity = 0;
60 _last_off_velocity = 0;
63 all_notes_off (timestamp);
66 memset (_polypress, 0, sizeof (_polypress));
67 memset (_controller_msb, 0, sizeof (_controller_msb));
68 memset (_controller_lsb, 0, sizeof (_controller_lsb));
70 /* zero all controllers XXX not necessarily the right thing */
72 memset (_controller_val, 0, sizeof (_controller_val));
74 for (int n = 0; n < 128; n++) {
75 _controller_14bit[n] = false;
90 Channel::process_note_off (Parser & /*parser*/, EventTwoBytes *tb)
92 _last_note_off = tb->note_number;
93 _last_off_velocity = tb->velocity;
101 Channel::process_note_on (Parser & /*parser*/, EventTwoBytes *tb)
103 _last_note_on = tb->note_number;
104 _last_on_velocity = tb->velocity;
109 Channel::process_controller (Parser & /*parser*/, EventTwoBytes *tb)
113 /* XXX arguably need a lock here to protect non-atomic changes
114 to controller_val[...]. or rather, need to make sure that
115 all changes *are* atomic.
118 if (tb->controller_number < 32) { /* unsigned: no test for >= 0 */
120 /* if this controller is already known to use 14 bits,
121 then treat this value as the MSB, and combine it
122 with the existing LSB.
124 otherwise, just treat it as a 7 bit value, and set
128 cv = (unsigned short) _controller_val[tb->controller_number];
130 if (_controller_14bit[tb->controller_number]) {
131 cv = ((tb->value & 0x7f) << 7) | (cv & 0x7f);
136 _controller_val[tb->controller_number] = (controller_value_t)cv;
138 } else if ((tb->controller_number >= 32 &&
139 tb->controller_number <= 63)) {
141 int cn = tb->controller_number - 32;
143 cv = (unsigned short) _controller_val[cn];
145 /* LSB for CC 0-31 arrived.
147 If this is the first time (i.e. its currently
148 flagged as a 7 bit controller), mark the
149 controller as 14 bit, adjust the existing value
150 to be the MSB, and OR-in the new LSB value.
152 otherwise, OR-in the new low 7bits with the old
157 if (_controller_14bit[cn] == false) {
158 _controller_14bit[cn] = true;
159 cv = (cv << 7) | (tb->value & 0x7f);
161 cv = (cv & 0x3f80) | (tb->value & 0x7f);
164 /* update the 14 bit value */
165 _controller_val[cn] = (controller_value_t) cv;
167 /* also store the "raw" 7 bit value in the incoming controller
170 _controller_val[tb->controller_number] = (controller_value_t) tb->value;
174 /* controller can only take 7 bit values */
176 _controller_val[tb->controller_number] =
177 (controller_value_t) tb->value;
180 /* bank numbers are special, in that they have their own signal
183 if (tb->controller_number == 0 || tb->controller_number == 0x20) {
184 _bank_number = _controller_val[0];
185 _port.parser()->bank_change (*_port.parser(), _bank_number);
186 _port.parser()->channel_bank_change[_channel_number] (*_port.parser(), _bank_number);
191 Channel::process_program_change (Parser & /*parser*/, MIDI::byte val)
193 _program_number = val;
197 Channel::process_chanpress (Parser & /*parser*/, MIDI::byte val)
203 Channel::process_polypress (Parser & /*parser*/, EventTwoBytes *tb)
205 _polypress[tb->note_number] = tb->value;
209 Channel::process_pitchbend (Parser & /*parser*/, pitchbend_t val)
215 Channel::process_reset (Parser & /*parser*/)
220 /** Write a message to a channel.
221 * \return true if success
224 Channel::channel_msg (MIDI::byte id, MIDI::byte val1, MIDI::byte val2, timestamp_t timestamp)
226 unsigned char msg[3];
229 msg[0] = id | (_channel_number & 0xf);
233 msg[1] = val1 & 0x7F;
234 msg[2] = val2 & 0x7F;
239 msg[1] = val1 & 0x7F;
240 msg[2] = val2 & 0x7F;
244 case MIDI::polypress:
245 msg[1] = val1 & 0x7F;
246 msg[2] = val2 & 0x7F;
251 msg[1] = val1 & 0x7F;
252 msg[2] = val2 & 0x7F;
257 msg[1] = val1 & 0x7F;
261 case MIDI::chanpress:
262 msg[1] = val1 & 0x7F;
266 case MIDI::pitchbend:
267 msg[1] = val1 & 0x7F;
268 msg[2] = val2 & 0x7F;
273 return _port.midimsg (msg, len, timestamp);