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>
29 Channel::Channel (byte channelnum, Port &p) : _port (p)
31 _channel_number = channelnum;
37 Channel::connect_input_signals ()
40 _port.input()->channel_pressure[_channel_number].connect
41 (mem_fun (*this, &Channel::process_chanpress));
42 _port.input()->channel_note_on[_channel_number].connect
43 (mem_fun (*this, &Channel::process_note_on));
44 _port.input()->channel_note_off[_channel_number].connect
45 (mem_fun (*this, &Channel::process_note_off));
46 _port.input()->channel_poly_pressure[_channel_number].connect
47 (mem_fun (*this, &Channel::process_polypress));
48 _port.input()->channel_program_change[_channel_number].connect
49 (mem_fun (*this, &Channel::process_program_change));
50 _port.input()->channel_controller[_channel_number].connect
51 (mem_fun (*this, &Channel::process_controller));
52 _port.input()->channel_pitchbend[_channel_number].connect
53 (mem_fun (*this, &Channel::process_pitchbend));
54 _port.input()->reset.connect (mem_fun (*this, &Channel::process_reset));
58 Channel::connect_output_signals ()
61 _port.output()->channel_pressure[_channel_number].connect
62 (mem_fun (*this, &Channel::process_chanpress));
63 _port.output()->channel_note_on[_channel_number].connect
64 (mem_fun (*this, &Channel::process_note_on));
65 _port.output()->channel_note_off[_channel_number].connect
66 (mem_fun (*this, &Channel::process_note_off));
67 _port.output()->channel_poly_pressure[_channel_number].connect
68 (mem_fun (*this, &Channel::process_polypress));
69 _port.output()->channel_program_change[_channel_number].connect
70 (mem_fun (*this, &Channel::process_program_change));
71 _port.output()->channel_controller[_channel_number].connect
72 (mem_fun (*this, &Channel::process_controller));
73 _port.output()->channel_pitchbend[_channel_number].connect
74 (mem_fun (*this, &Channel::process_pitchbend));
75 _port.output()->reset.connect (mem_fun (*this, &Channel::process_reset));
79 Channel::reset (timestamp_t timestamp, nframes_t nframes, bool notes_off)
81 _program_number = _channel_number;
87 _last_on_velocity = 0;
88 _last_off_velocity = 0;
91 all_notes_off (timestamp);
94 memset (_polypress, 0, sizeof (_polypress));
95 memset (_controller_msb, 0, sizeof (_controller_msb));
96 memset (_controller_lsb, 0, sizeof (_controller_lsb));
98 /* zero all controllers XXX not necessarily the right thing */
100 memset (_controller_val, 0, sizeof (_controller_val));
102 for (int n = 0; n < 128; n++) {
103 _controller_14bit[n] = false;
118 Channel::process_note_off (Parser &parser, EventTwoBytes *tb)
121 _last_note_off = tb->note_number;
122 _last_off_velocity = tb->velocity;
130 Channel::process_note_on (Parser &parser, EventTwoBytes *tb)
133 _last_note_on = tb->note_number;
134 _last_on_velocity = tb->velocity;
139 Channel::process_controller (Parser &parser, EventTwoBytes *tb)
144 /* XXX arguably need a lock here to protect non-atomic changes
145 to controller_val[...]. or rather, need to make sure that
146 all changes *are* atomic.
149 if (tb->controller_number <= 31) { /* unsigned: no test for >= 0 */
151 /* if this controller is already known to use 14 bits,
152 then treat this value as the MSB, and combine it
153 with the existing LSB.
155 otherwise, just treat it as a 7 bit value, and set
159 cv = (unsigned short) _controller_val[tb->controller_number];
161 if (_controller_14bit[tb->controller_number]) {
162 cv = ((tb->value << 7) | (cv & 0x7f));
167 _controller_val[tb->controller_number] = (controller_value_t)cv;
169 } else if ((tb->controller_number >= 32 &&
170 tb->controller_number <= 63)) {
172 cv = (unsigned short) _controller_val[tb->controller_number];
174 /* LSB for CC 0-31 arrived.
176 If this is the first time (i.e. its currently
177 flagged as a 7 bit controller), mark the
178 controller as 14 bit, adjust the existing value
179 to be the MSB, and OR-in the new LSB value.
181 otherwise, OR-in the new low 7bits with the old
185 int cn = tb->controller_number - 32;
187 if (_controller_14bit[cn] == false) {
188 _controller_14bit[cn] = true;
189 cv = (cv << 7) | (tb->value & 0x7f);
191 cv = (cv & 0x3f80) | (tb->value & 0x7f);
194 _controller_val[tb->controller_number] =
195 (controller_value_t) cv;
198 /* controller can only take 7 bit values */
200 _controller_val[tb->controller_number] =
201 (controller_value_t) tb->value;
204 /* bank numbers are special, in that they have their own signal
207 if (tb->controller_number == 0) {
208 _bank_number = (unsigned short) _controller_val[0];
210 _port.input()->bank_change (*_port.input(), _bank_number);
211 _port.input()->channel_bank_change[_channel_number]
212 (*_port.input(), _bank_number);
219 Channel::process_program_change (Parser &parser, byte val)
222 _program_number = val;
226 Channel::process_chanpress (Parser &parser, byte val)
233 Channel::process_polypress (Parser &parser, EventTwoBytes *tb)
236 _polypress[tb->note_number] = tb->value;
240 Channel::process_pitchbend (Parser &parser, pitchbend_t val)
247 Channel::process_reset (Parser &parser)
253 /** Write a message to a channel.
254 * \return true if success
257 Channel::channel_msg (byte id, byte val1, byte val2, timestamp_t timestamp)
259 unsigned char msg[3];
262 msg[0] = id | (_channel_number & 0xf);
266 msg[1] = val1 & 0x7F;
267 msg[2] = val2 & 0x7F;
272 msg[1] = val1 & 0x7F;
273 msg[2] = val2 & 0x7F;
277 case MIDI::polypress:
278 msg[1] = val1 & 0x7F;
279 msg[2] = val2 & 0x7F;
284 msg[1] = val1 & 0x7F;
285 msg[2] = val2 & 0x7F;
290 msg[1] = val1 & 0x7F;
294 case MIDI::chanpress:
295 msg[1] = val1 & 0x7F;
299 case MIDI::pitchbend:
300 msg[1] = val1 & 0x7F;
301 msg[2] = val2 & 0x7F;
306 return _port.midimsg (msg, len, timestamp);