midictl.c revision 1.1.2.2 1 /* $NetBSD: midictl.c,v 1.1.2.2 2006/06/07 00:09:39 chap Exp $ */
2
3 /*-
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Chapman Flack.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: midictl.c,v 1.1.2.2 2006/06/07 00:09:39 chap Exp $");
40
41 /*
42 * See midictl.h for an overview of the purpose and use of this module.
43 */
44
45 #if defined(_KERNEL)
46 #define _MIDICTL_ASSERT(x) KASSERT(x)
47 #define _MIDICTL_MALLOC(s,t) malloc((s), (t), M_WAITOK)
48 #define _MIDICTL_FREE(s,t) free((s), (t))
49 #include <sys/systm.h>
50 #include <sys/types.h>
51 #else
52 #include <assert.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #define _MIDICTL_ASSERT(x) assert(x)
56 #define _MIDICTL_MALLOC(s,t) malloc((s))
57 #define _MIDICTL_FREE(s,t) free((s))
58 #endif
59
60 #include "midictl.h"
61
62 /*
63 * The upper part of this file is MIDI-aware, and deals with things like
64 * decoding MIDI Control Change messages, dealing with the ones that require
65 * special handling as mode messages or parameter updates, and so on.
66 *
67 * It relies on a "store" layer (implemented in the lower part of this file)
68 * that only must be able to stash away 2-, 8-, or 16-bit quantities (which
69 * it may pack into larger units as it sees fit) and find them again given
70 * a class, channel, and key (controller/parameter number).
71 *
72 * The MIDI controllers can have 1-, 7-, or 14-bit values; the parameters are
73 * also 14-bit. The 14-bit values have to be set in two MIDI messages, 7 bits
74 * at a time. The MIDI layer uses store-managed 2- or 8-bit slots for the
75 * smaller types, and uses the free high bit to indicate that it has explicitly
76 * set the value. (Because the store is allowed to pack things, it may 'find'
77 * a zero entry for a value we never set, because it shares a word with a
78 * different value that has been set. We know it is not a real value because
79 * the high bit is clear.)
80 *
81 * The 14-bit values are handled similarly: 16-bit store slots are used to hold
82 * them, with the two free high bits indicating independently whether the MSB
83 * and the LSB have been explicitly set--as two separate MIDI messages are
84 * required. If such a control is queried when only one half has been explicitly
85 * set, the result is as if it had been set to the specified default value
86 * before the explicit set.
87 */
88 typedef struct bucket bucket; /* the store layer completes this type */
89
90 typedef enum { CTL1, CTL7, CTL14, RPN, NRPN } class;
91
92 /*
93 * assert(does_not_apply(KNFNamespaceArgumentAgainstNamesInPrototypes,
94 * PrototypesOfStaticFunctionsWithinNonIncludedFile));
95 */
96 static void reset_all_controllers(midictl *mc, uint_fast8_t chan);
97 static void enter14(midictl *mc, uint_fast8_t chan, class c,
98 uint_fast16_t key, _Bool islsb, uint8_t val);
99 static uint_fast16_t read14(midictl *mc, uint_fast8_t chan, class c,
100 uint_fast16_t key, uint_fast16_t dflt);
101 static class classify(uint_fast16_t *key, _Bool *islsb);
102 static midictl_notify notify_no_one;
103
104 static midictl_store *store_init(void);
105 static void store_done(midictl_store *s);
106 static bucket *store_locate(midictl_store *s, class c,
107 uint_fast8_t chan, uint_fast16_t key);
108 static uint16_t store_extract(bucket *b, class c,
109 uint_fast8_t chan, uint_fast16_t key);
110 static void store_update(midictl_store *s, bucket *b, class c,
111 uint_fast8_t chan, uint_fast16_t key, uint16_t value);
112
113 #define PN_SET 0x8000 /* a parameter number has been explicitly set */
114 #define C14MSET 0x8000 /* MSB of a 14-bit val has been set */
115 #define C14LSET 0x4000 /* LSB of a 14-bit val has been set */
116 #define C7_SET 0x80 /* a 7-bit ctl has been set */
117 #define C1_SET 2 /* a 1-bit ctl has been set */
118
119 #if defined(_MIDICTL_MAIN)
120 #define XS(s) [MIDICTL_##s]=#s
121 char const * const evt_strings[] = {
122 XS(CTLR), XS(RPN), XS(NRPN), XS(RESET), XS(NOTES_OFF),
123 XS(SOUND_OFF), XS(LOCAL), XS(MODE)
124 };
125 #undef XS
126
127 void
128 dbgnotify(void *cookie, midictl_evt e, uint_fast8_t chan, uint_fast16_t key)
129 {
130 printf("NFY %p %s chan %u #%u\n", cookie, evt_strings[e], chan, key);
131 }
132
133 midictl mc = {
134 .accept_any_ctl_rpn = 0,
135 .accept_any_nrpn = 0,
136 .base_channel = 16,
137 .cookie = NULL,
138 .notify = dbgnotify
139 };
140
141 int
142 main(int argc, char **argv)
143 {
144 int cnt, a, b, c;
145
146 midictl_open(&mc);
147 do {
148 cnt = scanf("%i %i %i", &a, &b, &c);
149 if ( 3 == cnt ) {
150 midictl_change(&mc, a, (uint8_t[]){b,c});
151 }
152 } while ( EOF != cnt );
153 midictl_close(&mc);
154 return 0;
155 }
156 #endif /* defined(_MIDICTL_MAIN) */
157
158 void
159 midictl_open(midictl *mc)
160 {
161 if ( NULL == mc->notify )
162 mc->notify = notify_no_one;
163 mc->store = store_init();
164 }
165
166 void
167 midictl_close(midictl *mc)
168 {
169 store_done(mc->store);
170 }
171
172 void
173 midictl_change(midictl *mc, uint_fast8_t chan, uint8_t *ctlval)
174 {
175 class c;
176 uint_fast16_t key, val;
177 _Bool islsb;
178 bucket *bkt;
179
180 switch ( ctlval[0] ) {
181 /*
182 * Channel mode messages:
183 */
184 case MIDI_CTRL_OMNI_OFF:
185 case MIDI_CTRL_OMNI_ON:
186 case MIDI_CTRL_POLY_OFF:
187 case MIDI_CTRL_POLY_ON:
188 if ( chan != mc->base_channel )
189 return; /* ignored - not on base channel */
190 else
191 return; /* XXX ignored anyway - not implemented yet */
192 case MIDI_CTRL_NOTES_OFF:
193 mc->notify(mc->cookie, MIDICTL_NOTES_OFF, chan, 0);
194 return;
195 case MIDI_CTRL_LOCAL:
196 mc->notify(mc->cookie, MIDICTL_LOCAL, chan, ctlval[1]);
197 return;
198 case MIDI_CTRL_SOUND_OFF:
199 mc->notify(mc->cookie, MIDICTL_SOUND_OFF, chan, 0);
200 return;
201 case MIDI_CTRL_RESET:
202 reset_all_controllers(mc, chan);
203 return;
204 /*
205 * Control changes to be handled specially:
206 */
207 case MIDI_CTRL_RPN_LSB:
208 mc-> rpn |= PN_SET | (0x7f & ctlval[1]);
209 mc->nrpn &= ~PN_SET;
210 return;
211 case MIDI_CTRL_RPN_MSB:
212 mc-> rpn |= PN_SET | (0x7f & ctlval[1])<<7;
213 mc->nrpn &= ~PN_SET;
214 return;
215 case MIDI_CTRL_NRPN_LSB:
216 mc->nrpn |= PN_SET | (0x7f & ctlval[1]);
217 mc-> rpn &= ~PN_SET;
218 return;
219 case MIDI_CTRL_NRPN_MSB:
220 mc->nrpn |= PN_SET | (0x7f & ctlval[1])<<7;
221 mc-> rpn &= ~PN_SET;
222 return;
223 case MIDI_CTRL_DATA_ENTRY_LSB:
224 islsb = 1;
225 goto whichparm;
226 case MIDI_CTRL_DATA_ENTRY_MSB:
227 islsb = 0;
228 whichparm:
229 if ( 0 == ( (mc->rpn ^ mc->nrpn) & PN_SET ) )
230 return; /* exactly one must be current */
231 if ( mc->rpn & PN_SET ) {
232 key = mc->rpn;
233 c = RPN;
234 } else {
235 key = mc->nrpn;
236 c = NRPN;
237 }
238 key &= 0x3fff;
239 if ( 0x3fff == key ) /* 'null' parm# to lock out changes */
240 return;
241 enter14(mc, chan, c, key, islsb, ctlval[1]);
242 return;
243 case MIDI_CTRL_RPN_INCREMENT: /* XXX for later - these are a PITA to */
244 case MIDI_CTRL_RPN_DECREMENT: /* get right - 'right' varies by param */
245 /* see http://www.midi.org/about-midi/rp18.shtml */
246 return;
247 }
248
249 /*
250 * Channel mode, RPN, and NRPN operations have been ruled out.
251 * This is an ordinary control change.
252 */
253
254 key = ctlval[0];
255 c = classify(&key, &islsb);
256
257 switch ( c ) {
258 case CTL14:
259 enter14(mc, chan, c, key, islsb, ctlval[1]);
260 break;
261 case CTL7:
262 bkt = store_locate(mc->store, c, chan, key);
263 if ( !mc->accept_any_ctl_rpn ) {
264 if ( NULL == bkt )
265 break;
266 val = store_extract(bkt, c, chan, key);
267 if ( !(val&C7_SET) )
268 break;
269 }
270 store_update(mc->store, bkt, c, chan, key,
271 C7_SET | (0x7f & ctlval[1]));
272 mc->notify(mc->cookie, MIDICTL_CTLR, chan, key);
273 break;
274 case CTL1:
275 bkt = store_locate(mc->store, c, chan, key);
276 if ( !mc->accept_any_ctl_rpn ) {
277 if ( NULL == bkt )
278 break;
279 val = store_extract(bkt, c, chan, key);
280 if ( !(val&C1_SET) )
281 break;
282 }
283 store_update(mc->store, bkt, c, chan, key,
284 C1_SET | (ctlval[1]>63));
285 mc->notify(mc->cookie, MIDICTL_CTLR, chan, key);
286 break;
287 case RPN:
288 case NRPN:
289 break; /* won't see these - sop for gcc */
290 }
291 }
292
293 uint_fast16_t
294 midictl_read(midictl *mc, uint_fast8_t chan, uint_fast8_t ctlr,
295 uint_fast16_t dflt)
296 {
297 bucket *bkt;
298 uint_fast16_t key, val;
299 class c;
300 _Bool islsb;
301
302 key = ctlr;
303 c = classify(&key, &islsb);
304 switch ( c ) {
305 case CTL1:
306 bkt = store_locate(mc->store, c, chan, key);
307 if ( NULL == bkt ||
308 !(C1_SET&(val = store_extract(bkt, c, chan, key))) ) {
309 val = C1_SET | (dflt > 63);
310 store_update(mc->store, bkt, c, chan, key, val);
311 }
312 return (val & 1) ? 127 : 0;
313 case CTL7:
314 bkt = store_locate(mc->store, c, chan, key);
315 if ( NULL == bkt ||
316 !(C7_SET&(val = store_extract(bkt, c, chan, key))) ) {
317 val = C7_SET | (dflt & 0x7f);
318 store_update(mc->store, bkt, c, chan, key, val);
319 }
320 return val & 0x7f;
321 case CTL14:
322 _MIDICTL_ASSERT(!islsb);
323 return read14(mc, chan, c, key, dflt);
324 case RPN:
325 case NRPN:
326 break; /* sop for gcc */
327 }
328 return 0; /* sop for gcc */
329 }
330
331 uint_fast16_t
332 midictl_rpn_read(midictl *mc, uint_fast8_t chan, uint_fast16_t ctlr,
333 uint_fast16_t dflt)
334 {
335 return read14(mc, chan, RPN, ctlr, dflt);
336 }
337
338 uint_fast16_t
339 midictl_nrpn_read(midictl *mc, uint_fast8_t chan, uint_fast16_t ctlr,
340 uint_fast16_t dflt)
341 {
342 return read14(mc, chan, NRPN, ctlr, dflt);
343 }
344
345 static void
346 reset_all_controllers(midictl *mc, uint_fast8_t chan)
347 {
348 uint_fast16_t ctlr, key;
349 class c;
350 _Bool islsb;
351 bucket *bkt;
352
353 for ( ctlr = 0 ; ; ++ ctlr ) {
354 switch ( ctlr ) {
355 /*
356 * exempt by http://www.midi.org/about-midi/rp15.shtml:
357 */
358 case MIDI_CTRL_BANK_SELECT_MSB: /* 0 */
359 case MIDI_CTRL_CHANNEL_VOLUME_MSB: /* 7 */
360 case MIDI_CTRL_PAN_MSB: /* 10 */
361 continue;
362 case MIDI_CTRL_BANK_SELECT_LSB: /* 32 */
363 ctlr += 31; /* skip all these LSBs anyway */
364 continue;
365 case MIDI_CTRL_SOUND_VARIATION: /* 70 */
366 ctlr += 9; /* skip all Sound Controllers */
367 continue;
368 case MIDI_CTRL_EFFECT_DEPTH_1: /* 91 */
369 goto loop_exit; /* nothing more gets reset */
370 /*
371 * exempt for our own personal reasons:
372 */
373 case MIDI_CTRL_DATA_ENTRY_MSB: /* 6 */
374 continue; /* doesn't go to the store */
375 }
376
377 key = ctlr;
378 c = classify(&key, &islsb);
379
380 bkt = store_locate(mc->store, c, chan, key);
381 if ( NULL == bkt )
382 continue;
383 store_update(mc->store, bkt, c, chan, key, 0); /* no C*SET */
384 }
385 loop_exit:
386 mc->notify(mc->cookie, MIDICTL_RESET, chan, 0);
387 }
388
389 static void
390 enter14(midictl *mc, uint_fast8_t chan, class c, uint_fast16_t key,
391 _Bool islsb, uint8_t val)
392 {
393 bucket *bkt;
394 uint16_t stval;
395
396 bkt = store_locate(mc->store, c, chan, key);
397 stval = (NULL == bkt) ? 0 : store_extract(bkt, c, chan, key);
398 if ( !(stval&(C14MSET|C14LSET)) ) {
399 if ( !((NRPN==c)? mc->accept_any_nrpn: mc->accept_any_ctl_rpn) )
400 return;
401 }
402 if ( islsb )
403 stval = C14LSET | val | ( stval & ~0x7f );
404 else
405 stval = C14MSET | ( val << 7 ) | ( stval & ~0x3f80 );
406 store_update(mc->store, bkt, c, chan, key, stval);
407 mc->notify(mc->cookie, CTL14 == c ? MIDICTL_CTLR
408 : RPN == c ? MIDICTL_RPN
409 : MIDICTL_NRPN, chan, key);
410 }
411
412 static uint_fast16_t
413 read14(midictl *mc, uint_fast8_t chan, class c, uint_fast16_t key,
414 uint_fast16_t dflt)
415 {
416 bucket *bkt;
417 uint16_t val;
418
419 bkt = store_locate(mc->store, c, chan, key);
420 if ( NULL == bkt )
421 goto neitherset;
422
423 val = store_extract(bkt, c, chan, key);
424 switch ( val & (C14MSET|C14LSET) ) {
425 case C14MSET|C14LSET:
426 return val & 0x3fff;
427 case C14MSET:
428 val = C14LSET | (val & ~0x7f) | (dflt & 0x7f);
429 break;
430 case C14LSET:
431 val = C14MSET | (val & ~0x3f8) | (dflt & 0x3f8);
432 break;
433 neitherset:
434 case 0:
435 val = C14MSET|C14LSET | (dflt & 0x3fff);
436 }
437 store_update(mc->store, bkt, c, chan, key, val);
438 return val & 0x3fff;
439 }
440
441 /*
442 * Determine the controller class; ranges based on
443 * http://www.midi.org/about-midi/table3.shtml dated 1995/1999/2002
444 * and viewed 2 June 2006.
445 */
446 static class
447 classify(uint_fast16_t *key, _Bool *islsb) {
448 if ( *key < 32 ) {
449 *islsb = 0;
450 return CTL14;
451 } else if ( *key < 64 ) {
452 *islsb = 1;
453 *key -= 32;
454 return CTL14;
455 } else if ( *key < 70 ) {
456 *key -= 64;
457 return CTL1;
458 } /* 70-84 defined, 85-90 undef'd, 91-95 def'd */
459 return CTL7; /* 96-101,120- handled above, 102-119 all undef'd */
460 /* treat them all as CTL7 */
461 }
462
463 static void
464 notify_no_one(void *cookie, midictl_evt evt, uint_fast8_t chan, uint_fast16_t k)
465 {
466 }
467
468 #undef PN_SET
469 #undef C14MSET
470 #undef C14LSET
471 #undef C7_SET
472 #undef C1_SET
473
474 /*
475 * I M P L E M E N T A T I O N O F T H E S T O R E :
476 *
477 * MIDI defines a metric plethora of possible controllers, registered
478 * parameters, and nonregistered parameters: a bit more than 32k possible words
479 * to store. The saving grace is that only a handful are likely to appear in
480 * typical MIDI data, and only a handful are likely implemented by or
481 * interesting to a typical client. So the store implementation needs to be
482 * suited to a largish but quite sparse data set.
483 *
484 * For greatest efficiency, this could be implemented over the hash API.
485 * For now, it is implemented over libprop, which is not a perfect fit,
486 * but because that API does so much more than the hash API, this code
487 * has to do less, and simplicity is worth something.
488 *
489 * prop_numbers are uintmax_t's, which are wider than anything we store, and
490 * to reduce waste we want to fill them. The choice is to fill an entry
491 * with values for the same controller across some consecutive channels
492 * (rather than for consecutive controllers on a channel) because very few
493 * controllers are likely to be used, but those that are will probably be used
494 * on more than one channel.
495 */
496
497 #include <prop/proplib.h>
498 #include <sys/malloc.h>
499
500 #define KEYSTRSIZE 8
501 static void tokeystr(char s[static KEYSTRSIZE],
502 class c, uint_fast8_t chan, uint_fast16_t key);
503
504 static uint_fast8_t const packing[] = {
505 [CTL1 ] = 4*sizeof(uintmax_t)/sizeof(uint8_t),
506 [CTL7 ] = sizeof(uintmax_t)/sizeof(uint8_t),
507 [CTL14] = sizeof(uintmax_t)/sizeof(uint16_t),
508 [RPN ] = sizeof(uintmax_t)/sizeof(uint16_t),
509 [NRPN ] = sizeof(uintmax_t)/sizeof(uint16_t)
510 };
511
512 struct bucket {
513 union {
514 uintmax_t val;
515 uint8_t c7[sizeof(uintmax_t)/sizeof(uint8_t)];
516 uint16_t c14[sizeof(uintmax_t)/sizeof(uint16_t)];
517 } __packed un;
518 midictl_store *ms;
519 };
520
521 struct midictl_store {
522 prop_dictionary_t pd;
523 bucket bkt; /* assume any one client nonreentrant (for now?) */
524 };
525
526 static midictl_store *
527 store_init(void)
528 {
529 midictl_store *s;
530
531 s = _MIDICTL_MALLOC(sizeof *s, M_DEVBUF);
532 s->pd = prop_dictionary_create();
533 s->bkt.ms = s;
534 return s;
535 }
536
537 static void
538 store_done(midictl_store *s)
539 {
540 prop_object_release(s->pd);
541 _MIDICTL_FREE(s, M_DEVBUF);
542 }
543
544 static bucket *
545 store_locate(midictl_store *s, class c, uint_fast8_t chan, uint_fast16_t key)
546 {
547 char buf[8];
548 prop_number_t pn;
549
550 tokeystr(buf, c, chan, key);
551 pn = (prop_number_t)prop_dictionary_get(s->pd, buf);
552 if ( NULL == pn ) {
553 s->bkt.un.val = 0;
554 return NULL;
555 }
556 s->bkt.un.val = prop_number_integer_value(pn);
557 return &s->bkt;
558 }
559
560 static uint16_t
561 store_extract(bucket *b, class c, uint_fast8_t chan, uint_fast16_t key)
562 {
563 chan %= packing[c];
564 switch ( c ) {
565 case CTL1:
566 return 3 & (b->un.c7[chan/4]>>(chan%4)*2);
567 case CTL7:
568 return b->un.c7[chan];
569 case CTL14:
570 case RPN:
571 case NRPN:
572 break;
573 }
574 return b->un.c14[chan];
575 }
576
577 static void
578 store_update(midictl_store *s, bucket *b, class c, uint_fast8_t chan,
579 uint_fast16_t key, uint16_t value)
580 {
581 uintmax_t orig;
582 char buf[KEYSTRSIZE];
583 prop_number_t pn;
584 boolean_t success;
585 uint_fast8_t ent;
586
587 if ( NULL == b ) {
588 b = &s->bkt;
589 orig = 0;
590 } else
591 orig = b->un.val;
592
593 ent = chan % packing[c];
594
595 switch ( c ) {
596 case CTL1:
597 b->un.c7[ent/4] &= ~(3<<(ent%4)*2);
598 b->un.c7[ent/4] |= (3&value)<<(ent%4)*2;
599 break;
600 case CTL7:
601 b->un.c7[ent] = value;
602 break;
603 case CTL14:
604 case RPN:
605 case NRPN:
606 b->un.c14[ent] = value;
607 break;
608 }
609
610 if ( orig == b->un.val )
611 return;
612
613 tokeystr(buf, c, chan, key);
614
615 if ( 0 == b->un.val )
616 prop_dictionary_remove(s->pd, buf);
617 else {
618 pn = prop_number_create_integer(b->un.val);
619 _MIDICTL_ASSERT(NULL != pn);
620 success = prop_dictionary_set(s->pd, buf, pn);
621 _MIDICTL_ASSERT(success);
622 prop_object_release(pn);
623 }
624 }
625
626 static void
627 tokeystr(char s[static KEYSTRSIZE],
628 class c, uint_fast8_t chan, uint_fast16_t key)
629 {
630 snprintf(s, KEYSTRSIZE, "%x%x%x", c, chan/packing[c], key);
631 }
632
633 #if defined(_MIDICTL_MAIN)
634 void
635 dumpstore(void)
636 {
637 char *s = prop_dictionary_externalize(mc.store->pd);
638 printf("%s", s);
639 free(s);
640 }
641 #endif
642