the buttons will submit to my rule! prelight-when-active, be gonecd /usr/local/music...
[ardour.git] / libs / midi++2 / midichannel.cc
1 /*
2     Copyright (C) 1998-99 Paul Barton-Davis 
3
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.
8
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.
13
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.
17
18     $Id$
19 */
20
21 #include <midi++/types.h>
22 #include <midi++/port.h>
23 #include <midi++/channel.h>
24
25 using namespace sigc;
26 using namespace MIDI;
27
28 Channel::Channel (byte channelnum, Port &p) : port (p)
29 {
30         channel_number = channelnum;
31
32         reset (false);
33 }       
34
35 void
36 Channel::connect_input_signals ()
37
38 {
39         port.input()->channel_pressure[channel_number].connect
40                 (mem_fun (*this, &Channel::process_chanpress));
41         port.input()->channel_note_on[channel_number].connect
42                 (mem_fun (*this, &Channel::process_note_on));
43         port.input()->channel_note_off[channel_number].connect
44                 (mem_fun (*this, &Channel::process_note_off));
45         port.input()->channel_poly_pressure[channel_number].connect
46                 (mem_fun (*this, &Channel::process_polypress));
47         port.input()->channel_program_change[channel_number].connect
48                 (mem_fun (*this, &Channel::process_program_change));
49         port.input()->channel_controller[channel_number].connect
50                 (mem_fun (*this, &Channel::process_controller));
51         port.input()->channel_pitchbend[channel_number].connect
52                 (mem_fun (*this, &Channel::process_pitchbend));
53         port.input()->reset.connect (mem_fun (*this, &Channel::process_reset));
54 }
55
56 void
57 Channel::connect_output_signals ()
58
59 {
60         port.output()->channel_pressure[channel_number].connect
61                 (mem_fun (*this, &Channel::process_chanpress));
62         port.output()->channel_note_on[channel_number].connect
63                 (mem_fun (*this, &Channel::process_note_on));
64         port.output()->channel_note_off[channel_number].connect
65                 (mem_fun (*this, &Channel::process_note_off));
66         port.output()->channel_poly_pressure[channel_number].connect
67                 (mem_fun (*this, &Channel::process_polypress));
68         port.output()->channel_program_change[channel_number].connect
69                 (mem_fun (*this, &Channel::process_program_change));
70         port.output()->channel_controller[channel_number].connect
71                 (mem_fun (*this, &Channel::process_controller));
72         port.output()->channel_pitchbend[channel_number].connect
73                 (mem_fun (*this, &Channel::process_pitchbend));
74         port.output()->reset.connect (mem_fun (*this, &Channel::process_reset));
75 }
76
77 void
78 Channel::reset (bool notes_off)
79 {
80         program_number = channel_number;
81         bank_number = 0;
82         pitch_bend = 0;
83
84         _last_note_on = 0;
85         _last_note_off = 0;
86         _last_on_velocity = 0;
87         _last_off_velocity = 0;
88
89         if (notes_off) {
90                 all_notes_off ();
91         }
92
93         memset (polypress, 0, sizeof (polypress));
94         memset (controller_msb, 0, sizeof (controller_msb));
95         memset (controller_lsb, 0, sizeof (controller_lsb));
96        
97         /* zero all controllers XXX not necessarily the right thing */
98
99         memset (controller_val, 0, sizeof (controller_val));
100
101         for (int n = 0; n < 128; n++) {
102                 controller_14bit[n] = false;
103         }
104
105         rpn_msb = 0;
106         rpn_lsb = 0;
107         nrpn_msb = 0;
108         nrpn_lsb = 0;
109
110         _omni = true;
111         _poly = false;
112         _mono = true;
113         _notes_on = 0;
114 }
115
116 void
117 Channel::process_note_off (Parser &parser, EventTwoBytes *tb) 
118
119 {
120         _last_note_off = tb->note_number;
121         _last_off_velocity = tb->velocity;
122
123         if (_notes_on) {
124                 _notes_on--;
125         }
126 }
127
128 void
129 Channel::process_note_on (Parser &parser, EventTwoBytes *tb) 
130
131 {
132         _last_note_on = tb->note_number;
133         _last_on_velocity = tb->velocity;
134         _notes_on++;
135 }
136
137 void
138 Channel::process_controller (Parser &parser, EventTwoBytes *tb) 
139
140 {
141         unsigned short cv;
142
143         /* XXX arguably need a lock here to protect non-atomic changes
144            to controller_val[...]. or rather, need to make sure that
145            all changes *are* atomic.
146         */
147
148         if (tb->controller_number <= 31) { /* unsigned: no test for >= 0 */
149
150                 /* if this controller is already known to use 14 bits,
151                    then treat this value as the MSB, and combine it 
152                    with the existing LSB.
153
154                    otherwise, just treat it as a 7 bit value, and set
155                    it directly.
156                 */
157
158                 cv = (unsigned short) controller_val[tb->controller_number];
159
160                 if (controller_14bit[tb->controller_number]) {
161                         cv = ((tb->value << 7) | (cv & 0x7f));
162                 } else {
163                         cv = tb->value;
164                 }
165
166                 controller_val[tb->controller_number] = (controller_value_t)cv;
167
168         } else if ((tb->controller_number >= 32 && 
169                     tb->controller_number <= 63)) {
170                    
171                 cv = (unsigned short) controller_val[tb->controller_number];
172
173                 /* LSB for CC 0-31 arrived. 
174
175                    If this is the first time (i.e. its currently
176                    flagged as a 7 bit controller), mark the
177                    controller as 14 bit, adjust the existing value
178                    to be the MSB, and OR-in the new LSB value. 
179
180                    otherwise, OR-in the new low 7bits with the old
181                    high 7.
182                 */
183
184                 int cn = tb->controller_number - 32;
185                    
186                 if (controller_14bit[cn] == false) {
187                         controller_14bit[cn] = true;
188                         cv = (cv << 7) | (tb->value & 0x7f);
189                 } else {
190                         cv = (cv & 0x3f80) | (tb->value & 0x7f);
191                 }
192
193                 controller_val[tb->controller_number] = 
194                         (controller_value_t) cv;
195         } else {
196
197                 /* controller can only take 7 bit values */
198                 
199                 controller_val[tb->controller_number] = 
200                         (controller_value_t) tb->value;
201         }
202
203         /* bank numbers are special, in that they have their own signal
204          */
205
206         if (tb->controller_number == 0) {
207                 bank_number = (unsigned short) controller_val[0];
208                 if (port.input()) {
209                         port.input()->bank_change (*port.input(), bank_number);
210                         port.input()->channel_bank_change[channel_number] 
211                                 (*port.input(), bank_number);
212                 }
213         }
214
215 }
216
217 void
218 Channel::process_program_change (Parser &parser, byte val) 
219
220 {
221         program_number = val;
222 }
223
224 void
225 Channel::process_chanpress (Parser &parser, byte val) 
226
227 {
228         chanpress = val;
229 }
230
231 void
232 Channel::process_polypress (Parser &parser, EventTwoBytes *tb) 
233
234 {
235         polypress[tb->note_number] = tb->value;
236 }
237
238 void
239 Channel::process_pitchbend (Parser &parser, pitchbend_t val) 
240
241 {
242         pitch_bend = val;
243 }
244
245 void
246 Channel::process_reset (Parser &parser) 
247
248 {
249         reset ();
250 }
251
252 int
253 Channel::channel_msg (byte id, byte val1, byte val2)
254
255 {
256         unsigned char msg[3];
257         int len = 0;
258
259         msg[0] = id | (channel_number & 0xf);
260
261         switch (id) {
262         case off:
263                 msg[1] = val1 & 0x7F;
264                 msg[2] = val2 & 0x7F;
265                 len = 3;
266                 break;
267
268         case on:
269                 msg[1] = val1 & 0x7F;
270                 msg[2] = val2 & 0x7F;
271                 len = 3;
272                 break;
273
274         case MIDI::polypress:
275                 msg[1] = val1 & 0x7F;
276                 msg[2] = val2 & 0x7F;
277                 len = 3;
278                 break;
279
280         case controller:
281                 msg[1] = val1 & 0x7F;
282                 msg[2] = val2 & 0x7F;
283                 len = 3;
284                 break;
285
286         case MIDI::program:
287                 msg[1] = val1 & 0x7F;
288                 len = 2;
289                 break;
290
291         case MIDI::chanpress:
292                 msg[1] = val1 & 0x7F;
293                 len = 2;
294                 break;
295
296         case MIDI::pitchbend:
297                 msg[1] = val1 & 0x7F;
298                 msg[2] = val2 & 0x7F;
299                 len = 3;
300                 break;
301         }
302
303         return port.midimsg (msg, len);
304 }