1 | //midi.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2013 |
---|
5 | * |
---|
6 | * This file is part of roard a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | #include "roard.h" |
---|
27 | |
---|
28 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
29 | |
---|
30 | // declared 'extern' |
---|
31 | struct midi_config midi_config; |
---|
32 | struct midi_state_mess g_midi_mess; |
---|
33 | #ifndef ROAR_WITHOUT_DCOMP_CB |
---|
34 | struct midi_state_cb g_midi_cb; |
---|
35 | #endif |
---|
36 | struct midi_clock g_midi_clock; |
---|
37 | struct midi_mixer g_midi_mixer; |
---|
38 | // // |
---|
39 | |
---|
40 | #if defined(ROAR_HAVE_IO_POSIX) && !defined(ROAR_TARGET_WIN32) |
---|
41 | #define _HAVE_CONSOLE |
---|
42 | #endif |
---|
43 | |
---|
44 | int midi_init_config(void) { |
---|
45 | midi_config.init = 1; |
---|
46 | midi_config.inited = 0; |
---|
47 | |
---|
48 | #ifndef ROAR_WITHOUT_DCOMP_CB |
---|
49 | midi_config.init_cb = 0; |
---|
50 | midi_config.console_dev = NULL; |
---|
51 | #endif |
---|
52 | |
---|
53 | return 0; |
---|
54 | } |
---|
55 | |
---|
56 | int midi_init (void) { |
---|
57 | struct roar_stream_server * ss; |
---|
58 | char cmap[16]; |
---|
59 | int i; |
---|
60 | |
---|
61 | midi_config.inited = 0; |
---|
62 | |
---|
63 | if ( midi_config.init ) { |
---|
64 | #ifndef ROAR_WITHOUT_DCOMP_CB |
---|
65 | if ( midi_config.init_cb ) { |
---|
66 | if ( midi_cb_init() == -1 ) { |
---|
67 | ROAR_WARN("Can not initialize MIDI subsystem component CB"); |
---|
68 | } |
---|
69 | } else { |
---|
70 | g_midi_cb.console = -1; |
---|
71 | } |
---|
72 | #endif |
---|
73 | |
---|
74 | if ( midi_clock_init() == -1 ) { |
---|
75 | ROAR_WARN("Can not initialize MIDI subsystem component clock"); |
---|
76 | } |
---|
77 | |
---|
78 | } |
---|
79 | |
---|
80 | g_midi_mess.buf = NULL; |
---|
81 | |
---|
82 | if ( (g_midi_mixer.stream = add_mixer(ROAR_SUBSYS_MIDI, _MIXER_NAME("MIDI"), &ss)) == -1 ) { |
---|
83 | ROAR_WARN("Can not create MIDI mixer"); |
---|
84 | } |
---|
85 | |
---|
86 | roardsp_chanlist_init(cmap, 16, ROARDSP_CHANLIST_MAP_MIDI); |
---|
87 | streams_set_map(g_midi_mixer.stream, cmap, 16); |
---|
88 | |
---|
89 | ss->state = ROAR_STREAMSTATE_OLD; |
---|
90 | |
---|
91 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
92 | if ( g_streams[i] != NULL ) { |
---|
93 | if ( streams_get_subsys(i) == ROAR_SUBSYS_MIDI ) { |
---|
94 | streams_set_mixer_stream(i, g_midi_mixer.stream); |
---|
95 | } |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | midi_config.inited |= MIDI_INITED_MAIN; |
---|
100 | |
---|
101 | return 0; |
---|
102 | } |
---|
103 | |
---|
104 | int midi_free (void) { |
---|
105 | if ( midi_reinit() == -1 ) |
---|
106 | return -1; |
---|
107 | |
---|
108 | #ifndef ROAR_WITHOUT_DCOMP_CB |
---|
109 | if ( midi_cb_free() == -1 ) |
---|
110 | return -1; |
---|
111 | #endif |
---|
112 | |
---|
113 | return 0; |
---|
114 | } |
---|
115 | |
---|
116 | int midi_update(void) { |
---|
117 | |
---|
118 | if ( g_midi_clock.stream != -1 ) |
---|
119 | midi_check_bridge(g_midi_clock.stream); |
---|
120 | |
---|
121 | #ifndef ROAR_WITHOUT_DCOMP_SSYNTH |
---|
122 | midi_conv_mes2ssynth(); |
---|
123 | #endif |
---|
124 | |
---|
125 | #ifndef ROAR_WITHOUT_DCOMP_CB |
---|
126 | return midi_cb_update(); |
---|
127 | #else |
---|
128 | return 0; |
---|
129 | #endif |
---|
130 | } |
---|
131 | |
---|
132 | int midi_reinit(void) { |
---|
133 | |
---|
134 | if ( g_midi_mess.buf != NULL ) |
---|
135 | roar_buffer_free(g_midi_mess.buf); |
---|
136 | |
---|
137 | g_midi_mess.buf = NULL; |
---|
138 | |
---|
139 | return 0; |
---|
140 | } |
---|
141 | |
---|
142 | // STREAMS: |
---|
143 | |
---|
144 | int midi_check_stream (int id) { |
---|
145 | struct roar_stream * s; |
---|
146 | struct roar_stream_server * ss; |
---|
147 | struct roar_buffer * b; |
---|
148 | void * buf; |
---|
149 | ssize_t len; |
---|
150 | |
---|
151 | if ( g_streams[id] == NULL ) |
---|
152 | return -1; |
---|
153 | |
---|
154 | ROAR_DBG("midi_check_stream(id=%i) = ?", id); |
---|
155 | |
---|
156 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
157 | |
---|
158 | if ( s->dir == ROAR_DIR_BRIDGE ) |
---|
159 | return midi_check_bridge(id); |
---|
160 | |
---|
161 | switch (s->info.codec) { |
---|
162 | case ROAR_CODEC_MIDI: |
---|
163 | break; |
---|
164 | default: |
---|
165 | streams_delete(id); |
---|
166 | return -1; |
---|
167 | } |
---|
168 | |
---|
169 | if ( roar_buffer_new_data(&b, MIDI_READ_SIZE, &buf) == -1 ) { |
---|
170 | ROAR_ERR("midi_check_stream(id=%i): Can not alloc buffer space!", id); |
---|
171 | ROAR_DBG("midi_check_stream(id=%i) = -1", id); |
---|
172 | return -1; |
---|
173 | } |
---|
174 | |
---|
175 | if ( (len = stream_vio_s_read(ss, buf, MIDI_READ_SIZE)) < 1 ) { |
---|
176 | streams_delete(id); |
---|
177 | roar_buffer_free(b); |
---|
178 | return -1; |
---|
179 | } |
---|
180 | |
---|
181 | if ( roar_buffer_set_len(b, len) == -1 ) { |
---|
182 | ROAR_WARN("midi_check_stream(id=%i): Can not set buffer size. BAD.", id); |
---|
183 | roar_buffer_free(b); |
---|
184 | streams_delete(id); |
---|
185 | return -1; |
---|
186 | } |
---|
187 | |
---|
188 | if ( stream_add_buffer(id, &b) == -1 ) { |
---|
189 | roar_buffer_free(b); |
---|
190 | streams_delete(id); |
---|
191 | return -1; |
---|
192 | } |
---|
193 | |
---|
194 | switch (s->info.codec) { |
---|
195 | case ROAR_CODEC_MIDI: |
---|
196 | return midi_conv_midi2mes(id); |
---|
197 | break; |
---|
198 | default: |
---|
199 | streams_delete(id); |
---|
200 | return -1; |
---|
201 | } |
---|
202 | |
---|
203 | ROAR_DBG("midi_check_stream(id=%i) = 0", id); |
---|
204 | return 0; |
---|
205 | } |
---|
206 | |
---|
207 | int midi_send_stream (int id) { |
---|
208 | struct roar_stream * s; |
---|
209 | struct roar_stream_server * ss; |
---|
210 | |
---|
211 | if ( g_streams[id] == NULL ) |
---|
212 | return -1; |
---|
213 | |
---|
214 | ROAR_DBG("midi_send_stream(id=%i) = ?", id); |
---|
215 | |
---|
216 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
217 | |
---|
218 | switch (s->info.codec) { |
---|
219 | case ROAR_CODEC_MIDI: |
---|
220 | return midi_conv_mes2midi(id); |
---|
221 | break; |
---|
222 | default: |
---|
223 | streams_delete(id); |
---|
224 | return -1; |
---|
225 | } |
---|
226 | |
---|
227 | return 0; |
---|
228 | } |
---|
229 | |
---|
230 | #if 1 |
---|
231 | static int _cache_to_mes(struct midi_message * mes, unsigned char * data, size_t len, |
---|
232 | int id, struct roar_stream_server * ss) { |
---|
233 | unsigned char type = *data; |
---|
234 | |
---|
235 | ROAR_DBG("_cache_to_mes(*): type=0x%.2X", (unsigned int)type); |
---|
236 | |
---|
237 | if (type == MIDI_TYPE_CLOCK_TICK || type == MIDI_TYPE_CLOCK_START || type == MIDI_TYPE_CLOCK_STOP ) { |
---|
238 | if ( len != 1 ) |
---|
239 | return -1; |
---|
240 | |
---|
241 | mes->type = type; |
---|
242 | |
---|
243 | if ( type == MIDI_TYPE_CLOCK_TICK ) { |
---|
244 | // TODO: |
---|
245 | ROAR_STREAM(ss)->pos = ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(ss)->pos, 1); |
---|
246 | } |
---|
247 | } else { |
---|
248 | mes->type = type & 0xF0; |
---|
249 | mes->channel = type & 0x0F; |
---|
250 | |
---|
251 | switch (mes->type) { |
---|
252 | case MIDI_TYPE_NOTE_ON: |
---|
253 | case MIDI_TYPE_NOTE_OFF: |
---|
254 | if ( len != 3 ) |
---|
255 | return -1; |
---|
256 | roar_midi_note_from_midiid(&(mes->d.note), data[1]); |
---|
257 | |
---|
258 | // if velocity is zero we have a NOTE OFF event |
---|
259 | if ( data[2] == 0 ) |
---|
260 | mes->type = MIDI_TYPE_NOTE_OFF; |
---|
261 | case MIDI_TYPE_PA: |
---|
262 | case MIDI_TYPE_CONTROLER: |
---|
263 | mes->kk = data[1]; |
---|
264 | mes->vv = data[2]; |
---|
265 | break; |
---|
266 | case MIDI_TYPE_PROGRAM: |
---|
267 | case MIDI_TYPE_MA: |
---|
268 | if ( len != 2 ) |
---|
269 | return -1; |
---|
270 | mes->vv = data[1]; |
---|
271 | break; |
---|
272 | case MIDI_TYPE_PB: |
---|
273 | case MIDI_TYPE_SYSEX: |
---|
274 | ROAR_WARN("_cache_to_mes(id=%i): Message of Type 0x%.2X (PB or SysEx) not yet supported", id, (unsigned int)type); |
---|
275 | return -1; |
---|
276 | break; |
---|
277 | } |
---|
278 | } |
---|
279 | |
---|
280 | switch (mes->type) { |
---|
281 | case MIDI_TYPE_CONTROLER: |
---|
282 | switch (mes->kk) { |
---|
283 | case MIDI_CCE_MAIN_VOL: |
---|
284 | if ( (516U * (unsigned int)mes->vv) > 65100 ) { // max volume |
---|
285 | ss->mixer.mixer[mes->channel] = 65535; |
---|
286 | } else { |
---|
287 | ss->mixer.mixer[mes->channel] = 516 * mes->vv; |
---|
288 | } |
---|
289 | break; |
---|
290 | } |
---|
291 | break; |
---|
292 | } |
---|
293 | |
---|
294 | ROAR_DBG("_cache_to_mes(*) = 0"); |
---|
295 | return 0; |
---|
296 | } |
---|
297 | |
---|
298 | int midi_conv_midi2mes (int id) { |
---|
299 | struct roar_stream * s; |
---|
300 | struct roar_stream_server * ss; |
---|
301 | size_t have; |
---|
302 | void * bufdata; |
---|
303 | unsigned char * data; |
---|
304 | unsigned char cache[3]; |
---|
305 | size_t cachepos = 0; |
---|
306 | int sync = 1; |
---|
307 | struct roar_buffer * buf = NULL; |
---|
308 | struct midi_message * mes = NULL; |
---|
309 | |
---|
310 | if ( g_streams[id] == NULL ) |
---|
311 | return -1; |
---|
312 | |
---|
313 | ROAR_DBG("midi_conv_midi2mes(id=%i) = ?", id); |
---|
314 | |
---|
315 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
316 | |
---|
317 | while (ss->buffer != NULL) { |
---|
318 | if ( roar_buffer_get_len(ss->buffer, &have) == -1 ) |
---|
319 | return -1; |
---|
320 | if ( roar_buffer_get_data(ss->buffer, &bufdata) == -1 ) |
---|
321 | return -1; |
---|
322 | data = bufdata; |
---|
323 | |
---|
324 | while (have) { |
---|
325 | if ( !sync ) { |
---|
326 | if ( *data & 0x80 ) { |
---|
327 | cachepos = 0; |
---|
328 | sync = 1; |
---|
329 | } else { |
---|
330 | data++; |
---|
331 | have--; |
---|
332 | continue; |
---|
333 | } |
---|
334 | } |
---|
335 | |
---|
336 | if ( (*data & 0x80) && cachepos != 0 ) { |
---|
337 | // submit cache. |
---|
338 | |
---|
339 | if ( midi_new_bufmes(&buf, &mes) == -1 ) { |
---|
340 | // this is bad. |
---|
341 | // TODO: handle better. |
---|
342 | return -1; |
---|
343 | } |
---|
344 | |
---|
345 | if ( _cache_to_mes(mes, cache, cachepos, id, ss) == -1 ) { |
---|
346 | ROAR_WARN("midi_conv_midi2mes(id=%i): Message can not be parsed.", id); |
---|
347 | roar_buffer_free(buf); |
---|
348 | } else { |
---|
349 | midi_add_buf(id, &buf); // FIXME: don't we need error handling here? |
---|
350 | } |
---|
351 | |
---|
352 | cachepos = 0; |
---|
353 | } else if ( cachepos == sizeof(cache) ) { |
---|
354 | // lost sync. |
---|
355 | sync = 0; |
---|
356 | continue; |
---|
357 | } |
---|
358 | |
---|
359 | ROAR_DBG("midi_conv_midi2mes(id=%i): cache[%u] = 0x%.2X", id, (unsigned int)cachepos, (unsigned int)*data); |
---|
360 | cache[cachepos++] = *data; |
---|
361 | data++; |
---|
362 | have--; |
---|
363 | } |
---|
364 | |
---|
365 | if ( roar_buffer_next(&(ss->buffer)) == -1 ) { |
---|
366 | ROAR_WARN("midi_conv_midi2mes(id=%i): Can not move to next buffer segment. BAD.", id); |
---|
367 | break; |
---|
368 | } |
---|
369 | } |
---|
370 | |
---|
371 | if ( cachepos ) { |
---|
372 | if ( midi_new_bufmes(&buf, &mes) == -1 ) { |
---|
373 | // this is bad. |
---|
374 | // TODO: handle better. |
---|
375 | return -1; |
---|
376 | } |
---|
377 | |
---|
378 | if ( _cache_to_mes(mes, cache, cachepos, id, ss) == -1 ) { |
---|
379 | roar_buffer_free(buf); |
---|
380 | } else { |
---|
381 | midi_add_buf(id, &buf); // FIXME: don't we need error handling here? |
---|
382 | cachepos = 0; |
---|
383 | } |
---|
384 | } |
---|
385 | |
---|
386 | if ( cachepos ) { |
---|
387 | if ( roar_buffer_new_data(&buf, cachepos, &bufdata) == -1 ) |
---|
388 | return -1; |
---|
389 | |
---|
390 | memcpy(bufdata, cache, cachepos); |
---|
391 | |
---|
392 | ss->buffer = buf; |
---|
393 | } |
---|
394 | |
---|
395 | return 0; |
---|
396 | } |
---|
397 | #else |
---|
398 | int midi_conv_midi2mes (int id) { |
---|
399 | void * bufdata; |
---|
400 | unsigned char * data; |
---|
401 | struct roar_stream * s; |
---|
402 | struct roar_stream_server * ss; |
---|
403 | struct roar_buffer * buf = NULL; |
---|
404 | struct midi_message * mes = NULL; |
---|
405 | size_t need = 0, have = 0, last_have = 0, old_have = 0; |
---|
406 | int alive = 1; |
---|
407 | |
---|
408 | if ( g_streams[id] == NULL ) |
---|
409 | return -1; |
---|
410 | |
---|
411 | ROAR_DBG("midi_conv_midi2mes(id=%i) = ?", id); |
---|
412 | |
---|
413 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
414 | |
---|
415 | while (ss->buffer != NULL && alive) { |
---|
416 | ROAR_DBG("midi_conv_midi2mes(id=%i): ss->buffer=%p, alive=%i", id, ss->buffer, alive); |
---|
417 | |
---|
418 | if ( roar_buffer_get_data(ss->buffer, &bufdata) == -1 ) { |
---|
419 | alive = 0; |
---|
420 | continue; |
---|
421 | } |
---|
422 | |
---|
423 | data = bufdata; |
---|
424 | |
---|
425 | if ( roar_buffer_get_len(ss->buffer, &have) == -1 ) { |
---|
426 | alive = 0; |
---|
427 | continue; |
---|
428 | } |
---|
429 | old_have = have; |
---|
430 | |
---|
431 | while (have && alive) { |
---|
432 | ROAR_DBG("midi_conv_midi2mes(id=%i): have=%llu, alive=%i", id, (long long unsigned int)have, alive); |
---|
433 | |
---|
434 | if ( *data & 0x80 ) { |
---|
435 | if ( buf != NULL ) { |
---|
436 | midi_add_buf(id, buf); |
---|
437 | buf = NULL; |
---|
438 | } |
---|
439 | |
---|
440 | last_have = have; |
---|
441 | |
---|
442 | need = 0; |
---|
443 | if ( midi_new_bufmes(&buf, &mes) == -1 ) { |
---|
444 | alive = 0; |
---|
445 | continue; |
---|
446 | } |
---|
447 | |
---|
448 | if (*data == MIDI_TYPE_CLOCK_TICK || *data == MIDI_TYPE_CLOCK_START || *data == MIDI_TYPE_CLOCK_STOP ) { |
---|
449 | mes->type = *data; |
---|
450 | |
---|
451 | if ( *data == MIDI_TYPE_CLOCK_TICK ) { |
---|
452 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1); |
---|
453 | } |
---|
454 | } else { |
---|
455 | mes->type = *data & 0xF0; |
---|
456 | mes->channel = *data & 0x0F; |
---|
457 | switch (*data & 0xF0) { |
---|
458 | case MIDI_TYPE_NOTE_ON: |
---|
459 | case MIDI_TYPE_NOTE_OFF: |
---|
460 | need = 2; |
---|
461 | break; |
---|
462 | case MIDI_TYPE_PA: |
---|
463 | need = 2; |
---|
464 | break; |
---|
465 | case MIDI_TYPE_CONTROLER: |
---|
466 | need = 2; |
---|
467 | break; |
---|
468 | case MIDI_TYPE_PROGRAM: |
---|
469 | need = 1; |
---|
470 | break; |
---|
471 | case MIDI_TYPE_MA: |
---|
472 | need = 1; |
---|
473 | break; |
---|
474 | case MIDI_TYPE_PB: |
---|
475 | case MIDI_TYPE_SYSEX: |
---|
476 | need = 1; |
---|
477 | break; |
---|
478 | } |
---|
479 | } |
---|
480 | |
---|
481 | } else { |
---|
482 | |
---|
483 | if ( mes == NULL ) { |
---|
484 | ROAR_WARN("midi_conv_midi2mes(id=%i): Lost sync.", id); |
---|
485 | data++; |
---|
486 | have--; |
---|
487 | continue; |
---|
488 | } |
---|
489 | |
---|
490 | switch (mes->type) { |
---|
491 | case MIDI_TYPE_NOTE_ON: |
---|
492 | case MIDI_TYPE_NOTE_OFF: |
---|
493 | if ( need == 2 ) { |
---|
494 | roar_midi_note_from_midiid(&(mes->d.note), *data); |
---|
495 | |
---|
496 | // if velocity is zero we have a NOTE OFF event |
---|
497 | if ( data[1] == 0 ) |
---|
498 | mes->type = MIDI_TYPE_NOTE_OFF; |
---|
499 | } |
---|
500 | case MIDI_TYPE_PA: |
---|
501 | case MIDI_TYPE_CONTROLER: |
---|
502 | if ( need == 2 ) { |
---|
503 | mes->kk = *data; |
---|
504 | } else { |
---|
505 | mes->vv = *data; |
---|
506 | } |
---|
507 | break; |
---|
508 | case MIDI_TYPE_PROGRAM: |
---|
509 | case MIDI_TYPE_MA: |
---|
510 | mes->vv = *data; |
---|
511 | break; |
---|
512 | case MIDI_TYPE_PB: |
---|
513 | case MIDI_TYPE_SYSEX: |
---|
514 | ROAR_WARN("midi_conv_midi2mes(id=%i): Message of Type 0x%.2X (PB or SysEx) not yet supported", id, mes->type); |
---|
515 | break; |
---|
516 | } |
---|
517 | |
---|
518 | if ( need ) |
---|
519 | need--; |
---|
520 | |
---|
521 | if ( !need ) { |
---|
522 | switch (mes->type) { |
---|
523 | case MIDI_TYPE_CONTROLER: |
---|
524 | switch (mes->kk) { |
---|
525 | case MIDI_CCE_MAIN_VOL: |
---|
526 | if ( 516 * mes->vv > 65100 ) { // max volume |
---|
527 | ss->mixer.mixer[mes->channel] = 65535; |
---|
528 | } else { |
---|
529 | ss->mixer.mixer[mes->channel] = 516 * mes->vv; |
---|
530 | } |
---|
531 | break; |
---|
532 | } |
---|
533 | break; |
---|
534 | } |
---|
535 | } |
---|
536 | |
---|
537 | } |
---|
538 | data++; |
---|
539 | have--; |
---|
540 | } |
---|
541 | |
---|
542 | if ( need ) { |
---|
543 | ROAR_DBG("midi_conv_midi2mes(id=%i) = ?", id); |
---|
544 | if ( roar_buffer_set_offset(ss->buffer, old_have - last_have) == -1 ) { |
---|
545 | ROAR_ERR("midi_conv_midi2mes(*): FIXME: BUG!!! Need to restore buffer here with corrected length"); |
---|
546 | } |
---|
547 | } else if ( !have ) { |
---|
548 | roar_buffer_next(&(ss->buffer)); |
---|
549 | } |
---|
550 | |
---|
551 | if ( need && buf != NULL ) { |
---|
552 | roar_buffer_free(buf); |
---|
553 | buf = NULL; |
---|
554 | } else if ( buf != NULL ) { |
---|
555 | midi_add_buf(id, buf); |
---|
556 | buf = NULL; |
---|
557 | } |
---|
558 | } |
---|
559 | |
---|
560 | return 0; |
---|
561 | } |
---|
562 | #endif |
---|
563 | |
---|
564 | #define _nb len++; *(d++) |
---|
565 | int midi_conv_mes2midi (int id) { |
---|
566 | struct roar_stream * s; |
---|
567 | struct roar_stream_server * ss; |
---|
568 | struct roar_buffer * buf = g_midi_mess.buf; |
---|
569 | struct midi_message * mes = NULL; |
---|
570 | void * bufdata; |
---|
571 | unsigned char data[3]; |
---|
572 | unsigned char * d; |
---|
573 | int len; |
---|
574 | int send_clock; |
---|
575 | |
---|
576 | if ( g_streams[id] == NULL ) |
---|
577 | return -1; |
---|
578 | |
---|
579 | ROAR_DBG("midi_conv_mes2midi(id=%i) = ?", id); |
---|
580 | |
---|
581 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
582 | |
---|
583 | send_clock = streams_get_flag(id, ROAR_FLAG_SYNC); |
---|
584 | |
---|
585 | if ( roar_buffer_ref(buf) == -1 ) |
---|
586 | return -1; |
---|
587 | |
---|
588 | roar_buffer_foreach(buf) { |
---|
589 | if ( roar_buffer_get_data(buf, &bufdata) == -1 ) { |
---|
590 | return -1; |
---|
591 | } |
---|
592 | |
---|
593 | mes = bufdata; |
---|
594 | |
---|
595 | if (mes->type == MIDI_TYPE_CLOCK_TICK || mes->type == MIDI_TYPE_CLOCK_START || mes->type == MIDI_TYPE_CLOCK_STOP ) { |
---|
596 | if ( send_clock ) { |
---|
597 | if ( stream_vio_s_write(ss, &(mes->type), 1) != 1 ) { |
---|
598 | streams_delete(id); |
---|
599 | return -1; |
---|
600 | } |
---|
601 | } |
---|
602 | |
---|
603 | if ( mes->type == MIDI_TYPE_CLOCK_TICK ) { |
---|
604 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1); |
---|
605 | } |
---|
606 | } else { |
---|
607 | len = 0; |
---|
608 | d = data; |
---|
609 | |
---|
610 | _nb = (mes->type & 0xF0) | (mes->channel & 0x0F); |
---|
611 | |
---|
612 | switch (mes->type) { |
---|
613 | case MIDI_TYPE_CONTROLER: |
---|
614 | switch (mes->kk) { |
---|
615 | case MIDI_CCE_MAIN_VOL: |
---|
616 | if ( 516U * (unsigned int)mes->vv > 65100 ) { // max volume |
---|
617 | ss->mixer.mixer[mes->channel] = 65535; |
---|
618 | } else { |
---|
619 | ss->mixer.mixer[mes->channel] = 516 * mes->vv; |
---|
620 | } |
---|
621 | break; |
---|
622 | } |
---|
623 | case MIDI_TYPE_NOTE_ON: |
---|
624 | case MIDI_TYPE_NOTE_OFF: |
---|
625 | case MIDI_TYPE_PA: |
---|
626 | _nb = mes->kk; |
---|
627 | _nb = mes->vv; |
---|
628 | break; |
---|
629 | case MIDI_TYPE_PROGRAM: |
---|
630 | case MIDI_TYPE_MA: |
---|
631 | _nb = mes->vv; |
---|
632 | break; |
---|
633 | case MIDI_TYPE_PB: |
---|
634 | case MIDI_TYPE_SYSEX: |
---|
635 | break; |
---|
636 | } |
---|
637 | |
---|
638 | if ( stream_vio_s_write(ss, data, len) != len ) { |
---|
639 | streams_delete(id); |
---|
640 | return -1; |
---|
641 | } |
---|
642 | } |
---|
643 | } |
---|
644 | |
---|
645 | return 0; |
---|
646 | } |
---|
647 | #undef _nb |
---|
648 | |
---|
649 | #ifndef ROAR_WITHOUT_DCOMP_SSYNTH |
---|
650 | int midi_conv_mes2ssynth(void) { |
---|
651 | struct roar_buffer * buf = g_midi_mess.buf; |
---|
652 | struct midi_message * mes = NULL; |
---|
653 | void * bufdata; |
---|
654 | |
---|
655 | if ( roar_buffer_ref(g_midi_mess.buf) == -1 ) |
---|
656 | return -1; |
---|
657 | |
---|
658 | roar_buffer_foreach(buf) { |
---|
659 | if ( roar_buffer_get_data(buf, &bufdata) == -1 ) { |
---|
660 | return -1; |
---|
661 | } |
---|
662 | |
---|
663 | mes = bufdata; |
---|
664 | |
---|
665 | if ( ssynth_eval_message(mes) == -1 ) |
---|
666 | return -1; |
---|
667 | } |
---|
668 | |
---|
669 | return 0; |
---|
670 | } |
---|
671 | #endif |
---|
672 | |
---|
673 | int midi_new_bufmes (struct roar_buffer ** buf, struct midi_message ** mes) { |
---|
674 | if ( buf == NULL || mes == NULL ) |
---|
675 | return -1; |
---|
676 | |
---|
677 | *buf = (void*)(*mes = NULL); |
---|
678 | |
---|
679 | if ( roar_buffer_new_data(buf, sizeof(struct midi_message), (void**)mes) == -1 ) { |
---|
680 | *buf = (void*)(*mes = NULL); |
---|
681 | return -1; |
---|
682 | } |
---|
683 | |
---|
684 | memset((void*)*mes, 0, sizeof(struct midi_message)); |
---|
685 | |
---|
686 | (*mes)->type = MIDI_TYPE_NONE; |
---|
687 | |
---|
688 | return 0; |
---|
689 | } |
---|
690 | |
---|
691 | int midi_add_buf (int id, struct roar_buffer ** buf) { |
---|
692 | #ifdef DEBUG |
---|
693 | struct midi_message * mes; |
---|
694 | void * bufdata; |
---|
695 | #endif |
---|
696 | |
---|
697 | if ( id == -1 || buf == NULL || *buf == NULL ) |
---|
698 | return -1; |
---|
699 | |
---|
700 | #ifdef DEBUG |
---|
701 | if ( roar_buffer_get_data(*buf, &bufdata) == -1 ) |
---|
702 | return -1; |
---|
703 | |
---|
704 | mes = bufdata; |
---|
705 | |
---|
706 | ROAR_DBG("midi_add_buf(id=%i, buf=%p) = ?", id, buf); |
---|
707 | ROAR_DBG("midi_add_buf(*): MIDI Message of Type 0x%.2X", mes->type); |
---|
708 | ROAR_DBG("midi_add_buf(*): Channel: %i", mes->channel); |
---|
709 | ROAR_DBG("midi_add_buf(*): flags=0x%.2X", mes->flags); |
---|
710 | ROAR_DBG("midi_add_buf(*): kk=0x%.2X, vv=0x%.2X", mes->kk, mes->vv); |
---|
711 | #endif |
---|
712 | |
---|
713 | if ( g_midi_mess.buf == NULL ) { |
---|
714 | g_midi_mess.buf = *buf; |
---|
715 | } else { |
---|
716 | if ( roar_buffer_moveinto(g_midi_mess.buf, buf) == -1 ) { |
---|
717 | ROAR_DBG("midi_add_buf(*) = -1"); |
---|
718 | return -1; |
---|
719 | } |
---|
720 | } |
---|
721 | |
---|
722 | ROAR_DBG("midi_add_buf(*) = 0"); |
---|
723 | return 0; |
---|
724 | } |
---|
725 | |
---|
726 | // bridges: |
---|
727 | int midi_check_bridge (int id) { |
---|
728 | |
---|
729 | ROAR_DBG("midi_check_bridge(id=%i) = ?", id); |
---|
730 | |
---|
731 | if ( id == g_midi_clock.stream ) { |
---|
732 | midi_clock_tick(); |
---|
733 | |
---|
734 | return 0; |
---|
735 | } |
---|
736 | |
---|
737 | return -1; |
---|
738 | } |
---|
739 | |
---|
740 | // clock: |
---|
741 | |
---|
742 | int midi_clock_init (void) { |
---|
743 | struct roar_stream * s; |
---|
744 | struct roar_stream_server * ss; |
---|
745 | |
---|
746 | if ( (g_midi_clock.stream = streams_new()) == -1 ) { |
---|
747 | ROAR_WARN("Error while initializing MIDI subsystem component clock"); |
---|
748 | return -1; |
---|
749 | } |
---|
750 | |
---|
751 | client_stream_add(g_self_client, g_midi_clock.stream); |
---|
752 | |
---|
753 | midi_vio_set_dummy(g_midi_clock.stream); |
---|
754 | |
---|
755 | streams_get(g_midi_clock.stream, &ss); |
---|
756 | s = ROAR_STREAM(ss); |
---|
757 | |
---|
758 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
759 | |
---|
760 | s->pos_rel_id = g_midi_clock.stream; |
---|
761 | |
---|
762 | s->info.codec = ROAR_CODEC_MIDI; |
---|
763 | ss->codec_orgi = ROAR_CODEC_MIDI; |
---|
764 | |
---|
765 | s->info.channels = 1; // we have only one channel, ticking on channel 0 |
---|
766 | s->info.rate = ROAR_MIDI_TICKS_PER_BEAT; // one beat per sec |
---|
767 | s->info.bits = ROAR_MIDI_BITS; |
---|
768 | |
---|
769 | if ( streams_set_dir(g_midi_clock.stream, ROAR_DIR_BRIDGE, 1) == -1 ) { |
---|
770 | ROAR_WARN("Error while initializing MIDI subsystem component clock"); |
---|
771 | return -1; |
---|
772 | } |
---|
773 | |
---|
774 | streams_set_name(g_midi_clock.stream, "MIDI Clock"); |
---|
775 | |
---|
776 | streams_set_flag(g_midi_clock.stream, ROAR_FLAG_PRIMARY); |
---|
777 | streams_set_flag(g_midi_clock.stream, ROAR_FLAG_SYNC); |
---|
778 | |
---|
779 | midi_clock_set_bph(3600); // one beat per sec |
---|
780 | |
---|
781 | ss->state = ROAR_STREAMSTATE_NEW; |
---|
782 | |
---|
783 | midi_config.inited |= MIDI_INITED_CLOCK; |
---|
784 | |
---|
785 | return 0; |
---|
786 | } |
---|
787 | |
---|
788 | int midi_clock_set_bph (uint_least32_t bph) { |
---|
789 | uint_least32_t sph = g_sa->rate/2 * 75 * g_sa->channels; // samples per houre |
---|
790 | |
---|
791 | g_midi_clock.bph = bph; |
---|
792 | |
---|
793 | g_midi_clock.spt = sph/bph; |
---|
794 | |
---|
795 | g_midi_clock.nt = ROAR_MATH_OVERFLOW_ADD(g_pos, g_midi_clock.spt); |
---|
796 | |
---|
797 | return 0; |
---|
798 | } |
---|
799 | |
---|
800 | int midi_clock_tick (void) { |
---|
801 | struct roar_buffer * buf; |
---|
802 | struct midi_message * mes; |
---|
803 | struct roar_stream_server * ss; |
---|
804 | #ifdef DEBUG |
---|
805 | unsigned int diff; |
---|
806 | #endif |
---|
807 | |
---|
808 | if ( streams_get(g_midi_clock.stream, &ss) == -1 ) { |
---|
809 | ROAR_ERR("midi_clock_tick(void): Something very BAD happend: can not get stream object of my own stream!"); |
---|
810 | g_midi_clock.stream = -1; |
---|
811 | ROAR_WARN("midi_clock_tick(void): Disabled MIDI Clock"); |
---|
812 | return -1; |
---|
813 | } |
---|
814 | |
---|
815 | // the strange expr at the end is to avoid warp around bugs. |
---|
816 | while ( g_pos >= g_midi_clock.nt && !(g_pos > (uint32_t)4294900000LU && g_midi_clock.nt < (uint32_t)1000LU) ) { |
---|
817 | #ifdef DEBUG |
---|
818 | diff = g_pos - g_midi_clock.nt; |
---|
819 | ROAR_DBG("midi_clock_tick(void): g_pos is %u samples (%5.2f%%) after of nt.", diff, (float)diff/g_midi_clock.spt); |
---|
820 | #endif |
---|
821 | |
---|
822 | g_midi_clock.nt = ROAR_MATH_OVERFLOW_ADD(g_midi_clock.nt, g_midi_clock.spt); |
---|
823 | |
---|
824 | if ( streams_get_flag(g_midi_clock.stream, ROAR_FLAG_SYNC) ) { |
---|
825 | ROAR_DBG("midi_clock_tick(void): TICK! (nt=%lu)", (unsigned long int)g_midi_clock.nt); |
---|
826 | if ( midi_new_bufmes(&buf, &mes) == -1 ) { |
---|
827 | ROAR_ERR("midi_clock_tick(void): Can not create Clock-Tick-Message"); |
---|
828 | } |
---|
829 | |
---|
830 | mes->type = MIDI_TYPE_CLOCK_TICK; |
---|
831 | |
---|
832 | if ( midi_add_buf(g_midi_clock.stream, &buf) == -1 ) { |
---|
833 | ROAR_ERR("midi_clock_tick(void): Can not add Clock-Tick-Message"); |
---|
834 | roar_buffer_free(buf); |
---|
835 | return -1; |
---|
836 | } |
---|
837 | |
---|
838 | ROAR_STREAM(ss)->pos = ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(ss)->pos, 1); |
---|
839 | ss->state = ROAR_STREAMSTATE_OLD; |
---|
840 | } else { |
---|
841 | ROAR_DBG("midi_clock_tick(void): silent tick. (nt=%lu)", (unsigned long int)g_midi_clock.nt); |
---|
842 | } |
---|
843 | } |
---|
844 | |
---|
845 | return 0; |
---|
846 | } |
---|
847 | |
---|
848 | // CB: |
---|
849 | |
---|
850 | #ifndef ROAR_WITHOUT_DCOMP_CB |
---|
851 | int midi_cb_init (void) { |
---|
852 | #ifdef _HAVE_CONSOLE |
---|
853 | struct roar_stream * s; |
---|
854 | struct roar_stream_server * ss; |
---|
855 | int i; |
---|
856 | char * files[] = { |
---|
857 | "/NX", |
---|
858 | "/dev/roarconsole", |
---|
859 | "/dev/console", |
---|
860 | #ifdef __linux__ |
---|
861 | "/dev/tty0", |
---|
862 | "/dev/vc/0", |
---|
863 | #endif |
---|
864 | NULL |
---|
865 | }; |
---|
866 | |
---|
867 | g_midi_cb.console = -1; |
---|
868 | g_midi_cb.stream = -1; |
---|
869 | g_midi_cb.stoptime = 0; |
---|
870 | g_midi_cb.playing = 0; |
---|
871 | |
---|
872 | if ( midi_config.console_dev != NULL ) { |
---|
873 | files[0] = midi_config.console_dev; |
---|
874 | } |
---|
875 | |
---|
876 | for (i = 0; files[i] != NULL; i++) { |
---|
877 | if ( (g_midi_cb.console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 ) |
---|
878 | break; |
---|
879 | } |
---|
880 | |
---|
881 | if ( g_midi_cb.console == -1 ) |
---|
882 | return -1; |
---|
883 | |
---|
884 | if ( (g_midi_cb.stream = streams_new()) == -1 ) { |
---|
885 | ROAR_WARN("Error while initializing MIDI subsystem component CB"); |
---|
886 | midi_cb_free(); |
---|
887 | return -1; |
---|
888 | } |
---|
889 | |
---|
890 | client_stream_add(g_self_client, g_midi_clock.stream); |
---|
891 | |
---|
892 | midi_vio_set_dummy(g_midi_cb.stream); |
---|
893 | |
---|
894 | streams_get(g_midi_cb.stream, &ss); |
---|
895 | s = ROAR_STREAM(ss); |
---|
896 | |
---|
897 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
898 | |
---|
899 | s->pos_rel_id = -1; |
---|
900 | |
---|
901 | s->info.codec = 0; |
---|
902 | ss->codec_orgi = 0; |
---|
903 | |
---|
904 | s->info.channels = 1; |
---|
905 | s->info.rate = 1193180; |
---|
906 | s->info.bits = 8; |
---|
907 | |
---|
908 | if ( streams_set_dir(g_midi_cb.stream, ROAR_DIR_BRIDGE, 1) == -1 ) { |
---|
909 | ROAR_WARN("Error while initializing MIDI subsystem component CB"); |
---|
910 | midi_cb_free(); |
---|
911 | return -1; |
---|
912 | } |
---|
913 | |
---|
914 | streams_set_name(g_midi_cb.stream, "Console speaker bridge"); |
---|
915 | |
---|
916 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_OUTPUT); |
---|
917 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_PRIMARY); |
---|
918 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_HWMIXER); |
---|
919 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_MUTE); |
---|
920 | |
---|
921 | midi_config.inited |= MIDI_INITED_CB; |
---|
922 | |
---|
923 | return 0; |
---|
924 | #else |
---|
925 | g_midi_cb.console = -1; |
---|
926 | g_midi_cb.stream = -1; |
---|
927 | |
---|
928 | return -1; |
---|
929 | #endif |
---|
930 | } |
---|
931 | |
---|
932 | int midi_cb_free (void) { |
---|
933 | #ifdef _HAVE_CONSOLE |
---|
934 | |
---|
935 | midi_cb_stop(); |
---|
936 | |
---|
937 | if ( g_midi_cb.stream != -1 ) |
---|
938 | streams_delete(g_midi_cb.stream); |
---|
939 | |
---|
940 | if ( g_midi_cb.console != -1 ) |
---|
941 | close(g_midi_cb.console); |
---|
942 | |
---|
943 | return 0; |
---|
944 | #else |
---|
945 | return -1; |
---|
946 | #endif |
---|
947 | } |
---|
948 | |
---|
949 | int midi_cb_play(float t, float freq, int override) { |
---|
950 | float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels; |
---|
951 | |
---|
952 | /* |
---|
953 | #define MIDI_CB_NOOVERRIDE 0 |
---|
954 | #define MIDI_CB_OVERRIDE 1 |
---|
955 | */ |
---|
956 | if ( g_midi_cb.playing && override != MIDI_CB_OVERRIDE ) |
---|
957 | return -1; |
---|
958 | |
---|
959 | g_midi_cb.stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t); |
---|
960 | midi_cb_start(freq); |
---|
961 | |
---|
962 | return 0; |
---|
963 | } |
---|
964 | |
---|
965 | int midi_cb_update (void) { |
---|
966 | |
---|
967 | if ( !streams_get_flag(g_midi_cb.stream, ROAR_FLAG_MUTE) ) { |
---|
968 | if ( midi_cb_readbuf() == -1 ) |
---|
969 | return -1; |
---|
970 | } else if ( g_midi_cb.playing ) { |
---|
971 | midi_cb_stop(); |
---|
972 | } |
---|
973 | |
---|
974 | if ( !g_midi_cb.playing ) |
---|
975 | return 0; |
---|
976 | |
---|
977 | /* |
---|
978 | if ( g_midi_cb.stoptime <= g_pos ) |
---|
979 | midi_cb_stop(); |
---|
980 | */ |
---|
981 | |
---|
982 | ROAR_DBG("midi_cb_update(void) = ?"); |
---|
983 | |
---|
984 | return 0; |
---|
985 | } |
---|
986 | |
---|
987 | int midi_cb_start(float freq) { |
---|
988 | // On linux this uses ioctl KIOCSOUND |
---|
989 | #ifdef __linux__ |
---|
990 | if ( g_midi_cb.console == -1 ) |
---|
991 | return -1; |
---|
992 | |
---|
993 | if ( ioctl(g_midi_cb.console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 ) |
---|
994 | return -1; |
---|
995 | |
---|
996 | g_midi_cb.playing = 1; |
---|
997 | |
---|
998 | return 0; |
---|
999 | #else |
---|
1000 | return -1; |
---|
1001 | #endif |
---|
1002 | } |
---|
1003 | |
---|
1004 | int midi_cb_stop (void) { |
---|
1005 | #ifdef __linux__ |
---|
1006 | int ret; |
---|
1007 | |
---|
1008 | ret = midi_cb_start(0); |
---|
1009 | g_midi_cb.playing = 0; |
---|
1010 | |
---|
1011 | return ret; |
---|
1012 | #else |
---|
1013 | return -1; |
---|
1014 | #endif |
---|
1015 | } |
---|
1016 | |
---|
1017 | int midi_cb_readbuf(void) { |
---|
1018 | struct roar_buffer * buf = g_midi_mess.buf; |
---|
1019 | struct midi_message * mes = NULL; |
---|
1020 | void * bufdata; |
---|
1021 | |
---|
1022 | ROAR_DBG("midi_cb_readbuf(void) = ?"); |
---|
1023 | |
---|
1024 | roar_buffer_foreach(buf) { |
---|
1025 | ROAR_DBG("midi_cb_readbuf(void): buf=%p", buf); |
---|
1026 | |
---|
1027 | if ( roar_buffer_get_data(buf, &bufdata) == -1 ) { |
---|
1028 | return -1; |
---|
1029 | } |
---|
1030 | |
---|
1031 | mes = bufdata; |
---|
1032 | |
---|
1033 | switch (mes->type) { |
---|
1034 | case MIDI_TYPE_NOTE_ON: |
---|
1035 | midi_cb_start(mes->d.note.freq); |
---|
1036 | break; |
---|
1037 | case MIDI_TYPE_NOTE_OFF: |
---|
1038 | midi_cb_stop(); |
---|
1039 | break; |
---|
1040 | case MIDI_TYPE_CONTROLER: |
---|
1041 | if ( mes->kk == MIDI_CCE_ALL_NOTE_OFF ) /* all note off */ |
---|
1042 | midi_cb_stop(); |
---|
1043 | break; |
---|
1044 | } |
---|
1045 | } |
---|
1046 | |
---|
1047 | return 0; |
---|
1048 | } |
---|
1049 | #endif |
---|
1050 | |
---|
1051 | // VIO: |
---|
1052 | |
---|
1053 | static int midi_vio_ctl(struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) { |
---|
1054 | if ( cmd == ROAR_VIO_CTL_NONBLOCK ) |
---|
1055 | return 0; |
---|
1056 | return -1; |
---|
1057 | } |
---|
1058 | |
---|
1059 | static int midi_vio_ok(struct roar_vio_calls * vio, ...) { |
---|
1060 | return 0; |
---|
1061 | } |
---|
1062 | |
---|
1063 | int midi_vio_set_dummy(int stream) { |
---|
1064 | struct roar_stream_server * ss; |
---|
1065 | |
---|
1066 | if ( streams_get(stream, &ss) == -1 ) |
---|
1067 | return -1; |
---|
1068 | |
---|
1069 | ss->vio.read = NULL; |
---|
1070 | ss->vio.write = NULL; |
---|
1071 | ss->vio.lseek = NULL; |
---|
1072 | ss->vio.sync = (int (*)(struct roar_vio_calls * vio))midi_vio_ok; |
---|
1073 | ss->vio.ctl = midi_vio_ctl; |
---|
1074 | ss->vio.close = (int (*)(struct roar_vio_calls * vio))midi_vio_ok; |
---|
1075 | |
---|
1076 | return 0; |
---|
1077 | } |
---|
1078 | |
---|
1079 | #endif |
---|
1080 | |
---|
1081 | //ll |
---|