* fixed jitter problems for midi clock and removed superfluous UI elements
[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 <cstring>
22 #include <midi++/types.h>
23 #include <midi++/port.h>
24 #include <midi++/channel.h>
25
26 using namespace sigc;
27 using namespace MIDI;
28
29 Channel::Channel (byte channelnum, Port &p) : _port (p)
30 {
31         _channel_number = channelnum;
32
33         reset (0, 1, false);
34 }       
35
36 void
37 Channel::connect_input_signals ()
38
39 {
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));
55 }
56
57 void
58 Channel::connect_output_signals ()
59
60 {
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));
76 }
77
78 void
79 Channel::reset (timestamp_t timestamp, nframes_t nframes, bool notes_off)
80 {
81         _program_number = _channel_number;
82         _bank_number = 0;
83         _pitch_bend = 0;
84
85         _last_note_on = 0;
86         _last_note_off = 0;
87         _last_on_velocity = 0;
88         _last_off_velocity = 0;
89
90         if (notes_off) {
91                 all_notes_off (timestamp);
92         }
93
94         memset (_polypress, 0, sizeof (_polypress));
95         memset (_controller_msb, 0, sizeof (_controller_msb));
96         memset (_controller_lsb, 0, sizeof (_controller_lsb));
97        
98         /* zero all controllers XXX not necessarily the right thing */
99
100         memset (_controller_val, 0, sizeof (_controller_val));
101
102         for (int n = 0; n < 128; n++) {
103                 _controller_14bit[n] = false;
104         }
105
106         _rpn_msb = 0;
107         _rpn_lsb = 0;
108         _nrpn_msb = 0;
109         _nrpn_lsb = 0;
110
111         _omni = true;
112         _poly = false;
113         _mono = true;
114         _notes_on = 0;
115 }
116
117 void
118 Channel::process_note_off (Parser &parser, EventTwoBytes *tb) 
119
120 {
121         _last_note_off = tb->note_number;
122         _last_off_velocity = tb->velocity;
123
124         if (_notes_on) {
125                 _notes_on--;
126         }
127 }
128
129 void
130 Channel::process_note_on (Parser &parser, EventTwoBytes *tb) 
131
132 {
133         _last_note_on = tb->note_number;
134         _last_on_velocity = tb->velocity;
135         _notes_on++;
136 }
137
138 void
139 Channel::process_controller (Parser &parser, EventTwoBytes *tb) 
140
141 {
142         unsigned short cv;
143
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.
147         */
148
149         if (tb->controller_number <= 31) { /* unsigned: no test for >= 0 */
150
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.
154
155                    otherwise, just treat it as a 7 bit value, and set
156                    it directly.
157                 */
158
159                 cv = (unsigned short) _controller_val[tb->controller_number];
160
161                 if (_controller_14bit[tb->controller_number]) {
162                         cv = ((tb->value << 7) | (cv & 0x7f));
163                 } else {
164                         cv = tb->value;
165                 }
166
167                 _controller_val[tb->controller_number] = (controller_value_t)cv;
168
169         } else if ((tb->controller_number >= 32 && 
170                     tb->controller_number <= 63)) {
171                    
172                 cv = (unsigned short) _controller_val[tb->controller_number];
173
174                 /* LSB for CC 0-31 arrived. 
175
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. 
180
181                    otherwise, OR-in the new low 7bits with the old
182                    high 7.
183                 */
184
185                 int cn = tb->controller_number - 32;
186                    
187                 if (_controller_14bit[cn] == false) {
188                         _controller_14bit[cn] = true;
189                         cv = (cv << 7) | (tb->value & 0x7f);
190                 } else {
191                         cv = (cv & 0x3f80) | (tb->value & 0x7f);
192                 }
193
194                 _controller_val[tb->controller_number] = 
195                         (controller_value_t) cv;
196         } else {
197
198                 /* controller can only take 7 bit values */
199                 
200                 _controller_val[tb->controller_number] = 
201                         (controller_value_t) tb->value;
202         }
203
204         /* bank numbers are special, in that they have their own signal
205          */
206
207         if (tb->controller_number == 0) {
208                 _bank_number = (unsigned short) _controller_val[0];
209                 if (_port.input()) {
210                         _port.input()->bank_change (*_port.input(), _bank_number);
211                         _port.input()->channel_bank_change[_channel_number] 
212                                 (*_port.input(), _bank_number);
213                 }
214         }
215
216 }
217
218 void
219 Channel::process_program_change (Parser &parser, byte val) 
220
221 {
222         _program_number = val;
223 }
224
225 void
226 Channel::process_chanpress (Parser &parser, byte val) 
227
228 {
229         _chanpress = val;
230 }
231
232 void
233 Channel::process_polypress (Parser &parser, EventTwoBytes *tb) 
234
235 {
236         _polypress[tb->note_number] = tb->value;
237 }
238
239 void
240 Channel::process_pitchbend (Parser &parser, pitchbend_t val) 
241
242 {
243         _pitch_bend = val;
244 }
245
246 void
247 Channel::process_reset (Parser &parser) 
248
249 {
250         reset (0, 1);
251 }
252
253 /** Write a message to a channel.
254  * \return true if success
255  */
256 bool
257 Channel::channel_msg (byte id, byte val1, byte val2, timestamp_t timestamp)
258 {
259         unsigned char msg[3];
260         int len = 0;
261
262         msg[0] = id | (_channel_number & 0xf);
263
264         switch (id) {
265         case off:
266                 msg[1] = val1 & 0x7F;
267                 msg[2] = val2 & 0x7F;
268                 len = 3;
269                 break;
270
271         case on:
272                 msg[1] = val1 & 0x7F;
273                 msg[2] = val2 & 0x7F;
274                 len = 3;
275                 break;
276
277         case MIDI::polypress:
278                 msg[1] = val1 & 0x7F;
279                 msg[2] = val2 & 0x7F;
280                 len = 3;
281                 break;
282
283         case controller:
284                 msg[1] = val1 & 0x7F;
285                 msg[2] = val2 & 0x7F;
286                 len = 3;
287                 break;
288
289         case MIDI::program:
290                 msg[1] = val1 & 0x7F;
291                 len = 2;
292                 break;
293
294         case MIDI::chanpress:
295                 msg[1] = val1 & 0x7F;
296                 len = 2;
297                 break;
298
299         case MIDI::pitchbend:
300                 msg[1] = val1 & 0x7F;
301                 msg[2] = val2 & 0x7F;
302                 len = 3;
303                 break;
304         }
305
306         return _port.midimsg (msg, len, timestamp);
307 }