1 | //midi.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010 |
---|
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 | #if defined(ROAR_HAVE_IO_POSIX) && !defined(ROAR_TARGET_WIN32) |
---|
31 | #define _HAVE_CONSOLE |
---|
32 | #endif |
---|
33 | |
---|
34 | int midi_init_config(void) { |
---|
35 | midi_config.init = 1; |
---|
36 | midi_config.inited = 0; |
---|
37 | |
---|
38 | #ifndef ROAR_WITHOUT_DCOMP_CB |
---|
39 | midi_config.init_cb = 0; |
---|
40 | midi_config.console_dev = NULL; |
---|
41 | #endif |
---|
42 | |
---|
43 | return 0; |
---|
44 | } |
---|
45 | |
---|
46 | int midi_init (void) { |
---|
47 | struct roar_stream_server * ss; |
---|
48 | char cmap[16]; |
---|
49 | int i; |
---|
50 | |
---|
51 | midi_config.inited = 0; |
---|
52 | |
---|
53 | if ( midi_config.init ) { |
---|
54 | #ifndef ROAR_WITHOUT_DCOMP_CB |
---|
55 | if ( midi_config.init_cb ) { |
---|
56 | if ( midi_cb_init() == -1 ) { |
---|
57 | ROAR_WARN("Can not initialize MIDI subsystem component CB"); |
---|
58 | } |
---|
59 | } else { |
---|
60 | g_midi_cb.console = -1; |
---|
61 | } |
---|
62 | #endif |
---|
63 | |
---|
64 | if ( midi_clock_init() == -1 ) { |
---|
65 | ROAR_WARN("Can not initialize MIDI subsystem component clock"); |
---|
66 | } |
---|
67 | |
---|
68 | } |
---|
69 | |
---|
70 | g_midi_mess.buf = NULL; |
---|
71 | |
---|
72 | if ( (g_midi_mixer.stream = add_mixer(ROAR_SUBSYS_MIDI, _MIXER_NAME("MIDI"), &ss)) == -1 ) { |
---|
73 | ROAR_WARN("Can not create MIDI mixer"); |
---|
74 | } |
---|
75 | |
---|
76 | roardsp_chanlist_init(cmap, 16, ROARDSP_CHANLIST_MAP_MIDI); |
---|
77 | streams_set_map(g_midi_mixer.stream, cmap, 16); |
---|
78 | |
---|
79 | ss->state = ROAR_STREAMSTATE_OLD; |
---|
80 | |
---|
81 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
82 | if ( g_streams[i] != NULL ) { |
---|
83 | if ( streams_get_subsys(i) == ROAR_SUBSYS_MIDI ) { |
---|
84 | streams_set_mixer_stream(i, g_midi_mixer.stream); |
---|
85 | } |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | midi_config.inited |= MIDI_INITED_MAIN; |
---|
90 | |
---|
91 | return 0; |
---|
92 | } |
---|
93 | |
---|
94 | int midi_free (void) { |
---|
95 | if ( midi_reinit() == -1 ) |
---|
96 | return -1; |
---|
97 | |
---|
98 | #ifndef ROAR_WITHOUT_DCOMP_CB |
---|
99 | if ( midi_cb_free() == -1 ) |
---|
100 | return -1; |
---|
101 | #endif |
---|
102 | |
---|
103 | return 0; |
---|
104 | } |
---|
105 | |
---|
106 | int midi_update(void) { |
---|
107 | |
---|
108 | if ( g_midi_clock.stream != -1 ) |
---|
109 | midi_check_bridge(g_midi_clock.stream); |
---|
110 | |
---|
111 | #ifndef ROAR_WITHOUT_DCOMP_SSYNTH |
---|
112 | midi_conv_mes2ssynth(); |
---|
113 | #endif |
---|
114 | |
---|
115 | #ifndef ROAR_WITHOUT_DCOMP_CB |
---|
116 | return midi_cb_update(); |
---|
117 | #else |
---|
118 | return 0; |
---|
119 | #endif |
---|
120 | } |
---|
121 | |
---|
122 | int midi_reinit(void) { |
---|
123 | |
---|
124 | if ( g_midi_mess.buf != NULL ) |
---|
125 | roar_buffer_free(g_midi_mess.buf); |
---|
126 | |
---|
127 | g_midi_mess.buf = NULL; |
---|
128 | |
---|
129 | return 0; |
---|
130 | } |
---|
131 | |
---|
132 | // STREAMS: |
---|
133 | |
---|
134 | int midi_check_stream (int id) { |
---|
135 | struct roar_stream * s; |
---|
136 | struct roar_stream_server * ss; |
---|
137 | struct roar_buffer * b; |
---|
138 | char * buf; |
---|
139 | ssize_t len; |
---|
140 | |
---|
141 | if ( g_streams[id] == NULL ) |
---|
142 | return -1; |
---|
143 | |
---|
144 | ROAR_DBG("midi_check_stream(id=%i) = ?", id); |
---|
145 | |
---|
146 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
147 | |
---|
148 | if ( s->dir == ROAR_DIR_BRIDGE ) |
---|
149 | return midi_check_bridge(id); |
---|
150 | |
---|
151 | switch (s->info.codec) { |
---|
152 | case ROAR_CODEC_MIDI: |
---|
153 | break; |
---|
154 | default: |
---|
155 | streams_delete(id); |
---|
156 | return -1; |
---|
157 | } |
---|
158 | |
---|
159 | if ( roar_buffer_new(&b, MIDI_READ_SIZE) == -1 ) { |
---|
160 | ROAR_ERR("midi_check_stream(*): Can not alloc buffer space!"); |
---|
161 | ROAR_DBG("midi_check_stream(*) = -1"); |
---|
162 | return -1; |
---|
163 | } |
---|
164 | |
---|
165 | roar_buffer_get_data(b, (void **)&buf); |
---|
166 | |
---|
167 | if ( (len = stream_vio_s_read(ss, buf, MIDI_READ_SIZE)) < 1 ) { |
---|
168 | streams_delete(id); |
---|
169 | roar_buffer_free(b); |
---|
170 | return -1; |
---|
171 | } |
---|
172 | |
---|
173 | roar_buffer_set_len(b, len); |
---|
174 | |
---|
175 | if ( stream_add_buffer(id, b) == -1 ) { |
---|
176 | roar_buffer_free(b); |
---|
177 | streams_delete(id); |
---|
178 | return -1; |
---|
179 | } |
---|
180 | |
---|
181 | switch (s->info.codec) { |
---|
182 | case ROAR_CODEC_MIDI: |
---|
183 | return midi_conv_midi2mes(id); |
---|
184 | break; |
---|
185 | default: |
---|
186 | streams_delete(id); |
---|
187 | return -1; |
---|
188 | } |
---|
189 | |
---|
190 | return 0; |
---|
191 | } |
---|
192 | |
---|
193 | int midi_send_stream (int id) { |
---|
194 | struct roar_stream * s; |
---|
195 | struct roar_stream_server * ss; |
---|
196 | |
---|
197 | if ( g_streams[id] == NULL ) |
---|
198 | return -1; |
---|
199 | |
---|
200 | ROAR_DBG("midi_send_stream(id=%i) = ?", id); |
---|
201 | |
---|
202 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
203 | |
---|
204 | switch (s->info.codec) { |
---|
205 | case ROAR_CODEC_MIDI: |
---|
206 | return midi_conv_mes2midi(id); |
---|
207 | break; |
---|
208 | default: |
---|
209 | streams_delete(id); |
---|
210 | return -1; |
---|
211 | } |
---|
212 | |
---|
213 | return 0; |
---|
214 | } |
---|
215 | |
---|
216 | int midi_conv_midi2mes (int id) { |
---|
217 | unsigned char * data; |
---|
218 | struct roar_stream * s; |
---|
219 | struct roar_stream_server * ss; |
---|
220 | struct roar_buffer * buf = NULL; |
---|
221 | struct midi_message * mes = NULL; |
---|
222 | size_t need = 0, have = 0, last_have = 0, old_have = 0; |
---|
223 | int alive = 1; |
---|
224 | |
---|
225 | if ( g_streams[id] == NULL ) |
---|
226 | return -1; |
---|
227 | |
---|
228 | ROAR_DBG("midi_conv_midi2mes(id=%i) = ?", id); |
---|
229 | |
---|
230 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
231 | |
---|
232 | while (ss->buffer != NULL && alive) { |
---|
233 | if ( roar_buffer_get_data(ss->buffer, (void**)&data) == -1 ) { |
---|
234 | alive = 0; |
---|
235 | continue; |
---|
236 | } |
---|
237 | |
---|
238 | if ( roar_buffer_get_len(ss->buffer, &have) == -1 ) { |
---|
239 | alive = 0; |
---|
240 | continue; |
---|
241 | } |
---|
242 | old_have = have; |
---|
243 | |
---|
244 | while (have && alive) { |
---|
245 | if ( *data & 0x80 ) { |
---|
246 | if ( buf != NULL ) { |
---|
247 | midi_add_buf(id, buf); |
---|
248 | buf = NULL; |
---|
249 | } |
---|
250 | |
---|
251 | last_have = have; |
---|
252 | |
---|
253 | need = 0; |
---|
254 | if ( midi_new_bufmes(&buf, &mes) == -1 ) { |
---|
255 | alive = 0; |
---|
256 | continue; |
---|
257 | } |
---|
258 | |
---|
259 | if (*data == MIDI_TYPE_CLOCK_TICK || *data == MIDI_TYPE_CLOCK_START || *data == MIDI_TYPE_CLOCK_STOP ) { |
---|
260 | mes->type = *data; |
---|
261 | |
---|
262 | if ( *data == MIDI_TYPE_CLOCK_TICK ) { |
---|
263 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1); |
---|
264 | } |
---|
265 | } else { |
---|
266 | mes->type = *data & 0xF0; |
---|
267 | mes->channel = *data & 0x0F; |
---|
268 | switch (*data & 0xF0) { |
---|
269 | case MIDI_TYPE_NOTE_ON: |
---|
270 | case MIDI_TYPE_NOTE_OFF: |
---|
271 | need = 2; |
---|
272 | break; |
---|
273 | case MIDI_TYPE_PA: |
---|
274 | need = 2; |
---|
275 | break; |
---|
276 | case MIDI_TYPE_CONTROLER: |
---|
277 | need = 2; |
---|
278 | break; |
---|
279 | case MIDI_TYPE_PROGRAM: |
---|
280 | need = 1; |
---|
281 | break; |
---|
282 | case MIDI_TYPE_MA: |
---|
283 | need = 1; |
---|
284 | break; |
---|
285 | case MIDI_TYPE_PB: |
---|
286 | case MIDI_TYPE_SYSEX: |
---|
287 | need = 1; |
---|
288 | break; |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | } else { |
---|
293 | |
---|
294 | if ( mes == NULL ) { |
---|
295 | ROAR_WARN("midi_conv_midi2mes(id=%i): Lost sync.", id); |
---|
296 | data++; |
---|
297 | have--; |
---|
298 | continue; |
---|
299 | } |
---|
300 | |
---|
301 | switch (mes->type) { |
---|
302 | case MIDI_TYPE_NOTE_ON: |
---|
303 | case MIDI_TYPE_NOTE_OFF: |
---|
304 | if ( need == 2 ) { |
---|
305 | roar_midi_note_from_midiid(&(mes->d.note), *data); |
---|
306 | |
---|
307 | // if velocity is zero we have a NOTE OFF event |
---|
308 | if ( data[1] == 0 ) |
---|
309 | mes->type = MIDI_TYPE_NOTE_OFF; |
---|
310 | } |
---|
311 | case MIDI_TYPE_PA: |
---|
312 | case MIDI_TYPE_CONTROLER: |
---|
313 | if ( need == 2 ) { |
---|
314 | mes->kk = *data; |
---|
315 | } else { |
---|
316 | mes->vv = *data; |
---|
317 | } |
---|
318 | break; |
---|
319 | case MIDI_TYPE_PROGRAM: |
---|
320 | case MIDI_TYPE_MA: |
---|
321 | mes->vv = *data; |
---|
322 | break; |
---|
323 | case MIDI_TYPE_PB: |
---|
324 | case MIDI_TYPE_SYSEX: |
---|
325 | ROAR_WARN("midi_conv_midi2mes(id=%i): Message of Type 0x%.2X (PB or SysEx) not yet supported", id, mes->type); |
---|
326 | break; |
---|
327 | } |
---|
328 | |
---|
329 | if ( need ) |
---|
330 | need--; |
---|
331 | |
---|
332 | if ( !need ) { |
---|
333 | switch (mes->type) { |
---|
334 | case MIDI_TYPE_CONTROLER: |
---|
335 | switch (mes->kk) { |
---|
336 | case MIDI_CCE_MAIN_VOL: |
---|
337 | if ( 516 * mes->vv > 65100 ) { // max volume |
---|
338 | ss->mixer.mixer[mes->channel] = 65535; |
---|
339 | } else { |
---|
340 | ss->mixer.mixer[mes->channel] = 516 * mes->vv; |
---|
341 | } |
---|
342 | break; |
---|
343 | } |
---|
344 | break; |
---|
345 | } |
---|
346 | } |
---|
347 | |
---|
348 | } |
---|
349 | data++; |
---|
350 | have--; |
---|
351 | } |
---|
352 | |
---|
353 | if ( need ) { |
---|
354 | if ( roar_buffer_set_offset(ss->buffer, old_have - last_have) == -1 ) { |
---|
355 | ROAR_ERR("midi_conv_midi2mes(*): FIXME: BUG!!! Need to restore buffer here with corrected length"); |
---|
356 | } |
---|
357 | } else if ( !have ) { |
---|
358 | roar_buffer_next(&(ss->buffer)); |
---|
359 | } |
---|
360 | |
---|
361 | if ( need && buf != NULL ) { |
---|
362 | roar_buffer_free(buf); |
---|
363 | buf = NULL; |
---|
364 | } else if ( buf != NULL ) { |
---|
365 | midi_add_buf(id, buf); |
---|
366 | buf = NULL; |
---|
367 | } |
---|
368 | } |
---|
369 | |
---|
370 | return 0; |
---|
371 | } |
---|
372 | |
---|
373 | #define _nb len++; *(d++) |
---|
374 | int midi_conv_mes2midi (int id) { |
---|
375 | struct roar_stream * s; |
---|
376 | struct roar_stream_server * ss; |
---|
377 | struct roar_buffer * buf = g_midi_mess.buf; |
---|
378 | struct midi_message * mes = NULL; |
---|
379 | unsigned char data[3]; |
---|
380 | unsigned char * d; |
---|
381 | int len; |
---|
382 | int send_clock; |
---|
383 | |
---|
384 | if ( g_streams[id] == NULL ) |
---|
385 | return -1; |
---|
386 | |
---|
387 | ROAR_DBG("midi_conv_mes2midi(id=%i) = ?", id); |
---|
388 | |
---|
389 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
390 | |
---|
391 | send_clock = streams_get_flag(id, ROAR_FLAG_SYNC); |
---|
392 | |
---|
393 | while (buf != NULL) { |
---|
394 | if ( roar_buffer_get_data(buf, (void**)&mes) == -1 ) { |
---|
395 | return -1; |
---|
396 | } |
---|
397 | |
---|
398 | if (mes->type == MIDI_TYPE_CLOCK_TICK || mes->type == MIDI_TYPE_CLOCK_START || mes->type == MIDI_TYPE_CLOCK_STOP ) { |
---|
399 | if ( send_clock ) { |
---|
400 | if ( stream_vio_s_write(ss, &(mes->type), 1) != 1 ) { |
---|
401 | streams_delete(id); |
---|
402 | return -1; |
---|
403 | } |
---|
404 | } |
---|
405 | |
---|
406 | if ( mes->type == MIDI_TYPE_CLOCK_TICK ) { |
---|
407 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1); |
---|
408 | } |
---|
409 | } else { |
---|
410 | len = 0; |
---|
411 | d = data; |
---|
412 | |
---|
413 | _nb = (mes->type & 0xF0) | (mes->channel & 0x0F); |
---|
414 | |
---|
415 | switch (mes->type) { |
---|
416 | case MIDI_TYPE_CONTROLER: |
---|
417 | switch (mes->kk) { |
---|
418 | case MIDI_CCE_MAIN_VOL: |
---|
419 | if ( 516 * mes->vv > 65100 ) { // max volume |
---|
420 | ss->mixer.mixer[mes->channel] = 65535; |
---|
421 | } else { |
---|
422 | ss->mixer.mixer[mes->channel] = 516 * mes->vv; |
---|
423 | } |
---|
424 | break; |
---|
425 | } |
---|
426 | case MIDI_TYPE_NOTE_ON: |
---|
427 | case MIDI_TYPE_NOTE_OFF: |
---|
428 | case MIDI_TYPE_PA: |
---|
429 | _nb = mes->kk; |
---|
430 | _nb = mes->vv; |
---|
431 | break; |
---|
432 | case MIDI_TYPE_PROGRAM: |
---|
433 | case MIDI_TYPE_MA: |
---|
434 | _nb = mes->vv; |
---|
435 | break; |
---|
436 | case MIDI_TYPE_PB: |
---|
437 | case MIDI_TYPE_SYSEX: |
---|
438 | break; |
---|
439 | } |
---|
440 | |
---|
441 | if ( stream_vio_s_write(ss, data, len) != len ) { |
---|
442 | streams_delete(id); |
---|
443 | return -1; |
---|
444 | } |
---|
445 | } |
---|
446 | |
---|
447 | if ( roar_buffer_get_next(buf, &buf) == -1 ) |
---|
448 | buf = NULL; |
---|
449 | } |
---|
450 | |
---|
451 | return 0; |
---|
452 | } |
---|
453 | #undef _nb |
---|
454 | |
---|
455 | #ifndef ROAR_WITHOUT_DCOMP_SSYNTH |
---|
456 | int midi_conv_mes2ssynth(void) { |
---|
457 | struct roar_buffer * buf = g_midi_mess.buf; |
---|
458 | struct midi_message * mes = NULL; |
---|
459 | |
---|
460 | while (buf != NULL) { |
---|
461 | if ( roar_buffer_get_data(buf, (void**)&mes) == -1 ) { |
---|
462 | return -1; |
---|
463 | } |
---|
464 | |
---|
465 | if ( ssynth_eval_message(mes) == -1 ) |
---|
466 | return -1; |
---|
467 | |
---|
468 | if ( roar_buffer_get_next(buf, &buf) == -1 ) |
---|
469 | buf = NULL; |
---|
470 | } |
---|
471 | |
---|
472 | return 0; |
---|
473 | } |
---|
474 | #endif |
---|
475 | |
---|
476 | int midi_new_bufmes (struct roar_buffer ** buf, struct midi_message ** mes) { |
---|
477 | if ( buf == NULL || mes == NULL ) |
---|
478 | return -1; |
---|
479 | |
---|
480 | *buf = (void*)(*mes = NULL); |
---|
481 | |
---|
482 | if ( roar_buffer_new(buf, sizeof(struct midi_message)) == -1 ) |
---|
483 | return -1; |
---|
484 | |
---|
485 | if ( roar_buffer_get_data(*buf, (void**)mes) == -1 ) { |
---|
486 | roar_buffer_free(*buf); |
---|
487 | *buf = (void*)(*mes = NULL); |
---|
488 | return -1; |
---|
489 | } |
---|
490 | |
---|
491 | memset((void*)*mes, 0, sizeof(struct midi_message)); |
---|
492 | |
---|
493 | (*mes)->type = MIDI_TYPE_NONE; |
---|
494 | |
---|
495 | return 0; |
---|
496 | } |
---|
497 | |
---|
498 | int midi_add_buf (int id, struct roar_buffer * buf) { |
---|
499 | struct midi_message * mes; |
---|
500 | |
---|
501 | if ( id == -1 || buf == NULL ) |
---|
502 | return -1; |
---|
503 | |
---|
504 | if ( roar_buffer_get_data(buf, (void**)&mes) == -1 ) |
---|
505 | return -1; |
---|
506 | |
---|
507 | ROAR_DBG("midi_add_buf(id=%i, buf=%p) = ?", id, buf); |
---|
508 | ROAR_DBG("midi_add_buf(*): MIDI Message of Type 0x%.2X", mes->type); |
---|
509 | ROAR_DBG("midi_add_buf(*): Channel: %i", mes->channel); |
---|
510 | ROAR_DBG("midi_add_buf(*): flags=0x%.2X", mes->flags); |
---|
511 | ROAR_DBG("midi_add_buf(*): kk=0x%.2X, vv=0x%.2X", mes->kk, mes->vv); |
---|
512 | |
---|
513 | if ( g_midi_mess.buf == NULL ) { |
---|
514 | g_midi_mess.buf = buf; |
---|
515 | } else { |
---|
516 | roar_buffer_add(g_midi_mess.buf, buf); |
---|
517 | } |
---|
518 | |
---|
519 | return 0; |
---|
520 | } |
---|
521 | |
---|
522 | // bridges: |
---|
523 | int midi_check_bridge (int id) { |
---|
524 | |
---|
525 | ROAR_DBG("midi_check_bridge(id=%i) = ?", id); |
---|
526 | |
---|
527 | if ( id == g_midi_clock.stream ) { |
---|
528 | midi_clock_tick(); |
---|
529 | |
---|
530 | return 0; |
---|
531 | } |
---|
532 | |
---|
533 | return -1; |
---|
534 | } |
---|
535 | |
---|
536 | // clock: |
---|
537 | |
---|
538 | int midi_clock_init (void) { |
---|
539 | struct roar_stream * s; |
---|
540 | struct roar_stream_server * ss; |
---|
541 | |
---|
542 | if ( (g_midi_clock.stream = streams_new()) == -1 ) { |
---|
543 | ROAR_WARN("Error while initializing MIDI subsystem component clock"); |
---|
544 | return -1; |
---|
545 | } |
---|
546 | |
---|
547 | client_stream_add(g_self_client, g_midi_clock.stream); |
---|
548 | |
---|
549 | midi_vio_set_dummy(g_midi_clock.stream); |
---|
550 | |
---|
551 | streams_get(g_midi_clock.stream, &ss); |
---|
552 | s = ROAR_STREAM(ss); |
---|
553 | |
---|
554 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
555 | |
---|
556 | s->pos_rel_id = g_midi_clock.stream; |
---|
557 | |
---|
558 | s->info.codec = ROAR_CODEC_MIDI; |
---|
559 | ss->codec_orgi = ROAR_CODEC_MIDI; |
---|
560 | |
---|
561 | s->info.channels = 1; // we have only one channel, ticking on channel 0 |
---|
562 | s->info.rate = ROAR_MIDI_TICKS_PER_BEAT; // one beat per sec |
---|
563 | s->info.bits = ROAR_MIDI_BITS; |
---|
564 | |
---|
565 | if ( streams_set_dir(g_midi_clock.stream, ROAR_DIR_BRIDGE, 1) == -1 ) { |
---|
566 | ROAR_WARN("Error while initializing MIDI subsystem component clock"); |
---|
567 | return -1; |
---|
568 | } |
---|
569 | |
---|
570 | streams_set_name(g_midi_clock.stream, "MIDI Clock"); |
---|
571 | |
---|
572 | streams_set_flag(g_midi_clock.stream, ROAR_FLAG_PRIMARY); |
---|
573 | streams_set_flag(g_midi_clock.stream, ROAR_FLAG_SYNC); |
---|
574 | |
---|
575 | midi_clock_set_bph(3600); // one beat per sec |
---|
576 | |
---|
577 | ss->state = ROAR_STREAMSTATE_NEW; |
---|
578 | |
---|
579 | midi_config.inited |= MIDI_INITED_CLOCK; |
---|
580 | |
---|
581 | return 0; |
---|
582 | } |
---|
583 | |
---|
584 | int midi_clock_set_bph (uint_least32_t bph) { |
---|
585 | uint_least32_t sph = g_sa->rate/2 * 75 * g_sa->channels; // samples per houre |
---|
586 | |
---|
587 | g_midi_clock.bph = bph; |
---|
588 | |
---|
589 | g_midi_clock.spt = sph/bph; |
---|
590 | |
---|
591 | g_midi_clock.nt = ROAR_MATH_OVERFLOW_ADD(g_pos, g_midi_clock.spt); |
---|
592 | |
---|
593 | return 0; |
---|
594 | } |
---|
595 | |
---|
596 | int midi_clock_tick (void) { |
---|
597 | struct roar_buffer * buf; |
---|
598 | struct midi_message * mes; |
---|
599 | struct roar_stream_server * ss; |
---|
600 | unsigned int diff; |
---|
601 | |
---|
602 | if ( streams_get(g_midi_clock.stream, &ss) == -1 ) { |
---|
603 | ROAR_ERR("midi_clock_tick(void): Something very BAD happend: can not get stream object of my own stream!"); |
---|
604 | g_midi_clock.stream = -1; |
---|
605 | ROAR_WARN("midi_clock_tick(void): Disabled MIDI Clock"); |
---|
606 | return -1; |
---|
607 | } |
---|
608 | |
---|
609 | while ( g_pos >= g_midi_clock.nt ) { |
---|
610 | diff = g_pos - g_midi_clock.nt; |
---|
611 | ROAR_DBG("midi_clock_tick(void): g_pos is %u samples (%5.2f%%) after of nt.", diff, (float)diff/g_midi_clock.spt); |
---|
612 | |
---|
613 | g_midi_clock.nt = ROAR_MATH_OVERFLOW_ADD(g_midi_clock.nt, g_midi_clock.spt); |
---|
614 | |
---|
615 | if ( streams_get_flag(g_midi_clock.stream, ROAR_FLAG_SYNC) ) { |
---|
616 | ROAR_DBG("midi_clock_tick(void): TICK! (nt=%lu)", g_midi_clock.nt); |
---|
617 | if ( midi_new_bufmes(&buf, &mes) == -1 ) { |
---|
618 | ROAR_ERR("midi_clock_tick(void): Can not create Clock-Tick-Message"); |
---|
619 | } |
---|
620 | |
---|
621 | mes->type = MIDI_TYPE_CLOCK_TICK; |
---|
622 | |
---|
623 | if ( midi_add_buf(g_midi_clock.stream, buf) == -1 ) { |
---|
624 | ROAR_ERR("midi_clock_tick(void): Can not add Clock-Tick-Message"); |
---|
625 | roar_buffer_free(buf); |
---|
626 | return -1; |
---|
627 | } |
---|
628 | |
---|
629 | ROAR_STREAM(ss)->pos = ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(ss)->pos, 1); |
---|
630 | ss->state = ROAR_STREAMSTATE_OLD; |
---|
631 | } else { |
---|
632 | ROAR_DBG("midi_clock_tick(void): silent tick. (nt=%lu)", g_midi_clock.nt); |
---|
633 | } |
---|
634 | } |
---|
635 | |
---|
636 | return 0; |
---|
637 | } |
---|
638 | |
---|
639 | // CB: |
---|
640 | |
---|
641 | #ifndef ROAR_WITHOUT_DCOMP_CB |
---|
642 | int midi_cb_init (void) { |
---|
643 | #ifdef _HAVE_CONSOLE |
---|
644 | struct roar_stream * s; |
---|
645 | struct roar_stream_server * ss; |
---|
646 | int i; |
---|
647 | char * files[] = { |
---|
648 | "/NX", |
---|
649 | "/dev/roarconsole", |
---|
650 | "/dev/console", |
---|
651 | #ifdef __linux__ |
---|
652 | "/dev/tty0", |
---|
653 | "/dev/vc/0", |
---|
654 | #endif |
---|
655 | NULL |
---|
656 | }; |
---|
657 | |
---|
658 | g_midi_cb.console = -1; |
---|
659 | g_midi_cb.stream = -1; |
---|
660 | g_midi_cb.stoptime = 0; |
---|
661 | g_midi_cb.playing = 0; |
---|
662 | |
---|
663 | if ( midi_config.console_dev != NULL ) { |
---|
664 | files[0] = midi_config.console_dev; |
---|
665 | } |
---|
666 | |
---|
667 | for (i = 0; files[i] != NULL; i++) { |
---|
668 | if ( (g_midi_cb.console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 ) |
---|
669 | break; |
---|
670 | } |
---|
671 | |
---|
672 | if ( g_midi_cb.console == -1 ) |
---|
673 | return -1; |
---|
674 | |
---|
675 | if ( (g_midi_cb.stream = streams_new()) == -1 ) { |
---|
676 | ROAR_WARN("Error while initializing MIDI subsystem component CB"); |
---|
677 | midi_cb_free(); |
---|
678 | return -1; |
---|
679 | } |
---|
680 | |
---|
681 | client_stream_add(g_self_client, g_midi_clock.stream); |
---|
682 | |
---|
683 | midi_vio_set_dummy(g_midi_cb.stream); |
---|
684 | |
---|
685 | streams_get(g_midi_cb.stream, &ss); |
---|
686 | s = ROAR_STREAM(ss); |
---|
687 | |
---|
688 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
689 | |
---|
690 | s->pos_rel_id = -1; |
---|
691 | |
---|
692 | s->info.codec = 0; |
---|
693 | ss->codec_orgi = 0; |
---|
694 | |
---|
695 | s->info.channels = 1; |
---|
696 | s->info.rate = 1193180; |
---|
697 | s->info.bits = 8; |
---|
698 | |
---|
699 | if ( streams_set_dir(g_midi_cb.stream, ROAR_DIR_BRIDGE, 1) == -1 ) { |
---|
700 | ROAR_WARN("Error while initializing MIDI subsystem component CB"); |
---|
701 | midi_cb_free(); |
---|
702 | return -1; |
---|
703 | } |
---|
704 | |
---|
705 | streams_set_name(g_midi_cb.stream, "Console speaker bridge"); |
---|
706 | |
---|
707 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_OUTPUT); |
---|
708 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_PRIMARY); |
---|
709 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_HWMIXER); |
---|
710 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_MUTE); |
---|
711 | |
---|
712 | midi_config.inited |= MIDI_INITED_CB; |
---|
713 | |
---|
714 | return 0; |
---|
715 | #else |
---|
716 | g_midi_cb.console = -1; |
---|
717 | g_midi_cb.stream = -1; |
---|
718 | |
---|
719 | return -1; |
---|
720 | #endif |
---|
721 | } |
---|
722 | |
---|
723 | int midi_cb_free (void) { |
---|
724 | #ifdef _HAVE_CONSOLE |
---|
725 | |
---|
726 | midi_cb_stop(); |
---|
727 | |
---|
728 | if ( g_midi_cb.stream != -1 ) |
---|
729 | streams_delete(g_midi_cb.stream); |
---|
730 | |
---|
731 | if ( g_midi_cb.console != -1 ) |
---|
732 | close(g_midi_cb.console); |
---|
733 | |
---|
734 | return 0; |
---|
735 | #else |
---|
736 | return -1; |
---|
737 | #endif |
---|
738 | } |
---|
739 | |
---|
740 | int midi_cb_play(float t, float freq, int override) { |
---|
741 | float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels; |
---|
742 | |
---|
743 | /* |
---|
744 | #define MIDI_CB_NOOVERRIDE 0 |
---|
745 | #define MIDI_CB_OVERRIDE 1 |
---|
746 | */ |
---|
747 | if ( g_midi_cb.playing && override != MIDI_CB_OVERRIDE ) |
---|
748 | return -1; |
---|
749 | |
---|
750 | g_midi_cb.stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t); |
---|
751 | midi_cb_start(freq); |
---|
752 | |
---|
753 | return 0; |
---|
754 | } |
---|
755 | |
---|
756 | int midi_cb_update (void) { |
---|
757 | |
---|
758 | if ( !streams_get_flag(g_midi_cb.stream, ROAR_FLAG_MUTE) ) { |
---|
759 | if ( midi_cb_readbuf() == -1 ) |
---|
760 | return -1; |
---|
761 | } else if ( g_midi_cb.playing ) { |
---|
762 | midi_cb_stop(); |
---|
763 | } |
---|
764 | |
---|
765 | if ( !g_midi_cb.playing ) |
---|
766 | return 0; |
---|
767 | |
---|
768 | /* |
---|
769 | if ( g_midi_cb.stoptime <= g_pos ) |
---|
770 | midi_cb_stop(); |
---|
771 | */ |
---|
772 | |
---|
773 | ROAR_DBG("midi_cb_update(void) = ?"); |
---|
774 | |
---|
775 | return 0; |
---|
776 | } |
---|
777 | |
---|
778 | int midi_cb_start(float freq) { |
---|
779 | // On linux this uses ioctl KIOCSOUND |
---|
780 | #ifdef __linux__ |
---|
781 | if ( g_midi_cb.console == -1 ) |
---|
782 | return -1; |
---|
783 | |
---|
784 | if ( ioctl(g_midi_cb.console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 ) |
---|
785 | return -1; |
---|
786 | |
---|
787 | g_midi_cb.playing = 1; |
---|
788 | |
---|
789 | return 0; |
---|
790 | #else |
---|
791 | return -1; |
---|
792 | #endif |
---|
793 | } |
---|
794 | |
---|
795 | int midi_cb_stop (void) { |
---|
796 | #ifdef __linux__ |
---|
797 | int ret; |
---|
798 | |
---|
799 | ret = midi_cb_start(0); |
---|
800 | g_midi_cb.playing = 0; |
---|
801 | |
---|
802 | return ret; |
---|
803 | #else |
---|
804 | return -1; |
---|
805 | #endif |
---|
806 | } |
---|
807 | |
---|
808 | int midi_cb_readbuf(void) { |
---|
809 | struct roar_buffer * buf = g_midi_mess.buf; |
---|
810 | struct midi_message * mes = NULL; |
---|
811 | |
---|
812 | ROAR_DBG("midi_cb_readbuf(void) = ?"); |
---|
813 | |
---|
814 | while (buf != NULL) { |
---|
815 | ROAR_DBG("midi_cb_readbuf(void): buf=%p", buf); |
---|
816 | |
---|
817 | if ( roar_buffer_get_data(buf, (void**)&mes) == -1 ) { |
---|
818 | return -1; |
---|
819 | } |
---|
820 | |
---|
821 | switch (mes->type) { |
---|
822 | case MIDI_TYPE_NOTE_ON: |
---|
823 | midi_cb_start(mes->d.note.freq); |
---|
824 | break; |
---|
825 | case MIDI_TYPE_NOTE_OFF: |
---|
826 | midi_cb_stop(); |
---|
827 | break; |
---|
828 | case MIDI_TYPE_CONTROLER: |
---|
829 | if ( mes->kk == MIDI_CCE_ALL_NOTE_OFF ) /* all note off */ |
---|
830 | midi_cb_stop(); |
---|
831 | break; |
---|
832 | } |
---|
833 | |
---|
834 | if ( roar_buffer_get_next(buf, &buf) == -1 ) |
---|
835 | buf = NULL; |
---|
836 | } |
---|
837 | |
---|
838 | return 0; |
---|
839 | } |
---|
840 | #endif |
---|
841 | |
---|
842 | // VIO: |
---|
843 | |
---|
844 | int midi_vio_set_dummy(int stream) { |
---|
845 | struct roar_stream_server * ss; |
---|
846 | |
---|
847 | if ( streams_get(stream, &ss) == -1 ) |
---|
848 | return -1; |
---|
849 | |
---|
850 | ss->vio.read = NULL; |
---|
851 | ss->vio.write = NULL; |
---|
852 | ss->vio.lseek = NULL; |
---|
853 | ss->vio.nonblock = (int (*)(struct roar_vio_calls * vio, int state))midi_vio_ok; |
---|
854 | ss->vio.sync = (int (*)(struct roar_vio_calls * vio))midi_vio_ok; |
---|
855 | ss->vio.ctl = NULL; |
---|
856 | ss->vio.close = (int (*)(struct roar_vio_calls * vio))midi_vio_ok; |
---|
857 | |
---|
858 | return 0; |
---|
859 | } |
---|
860 | |
---|
861 | int midi_vio_ok(struct roar_vio_calls * vio, ...) { |
---|
862 | return 0; |
---|
863 | } |
---|
864 | |
---|
865 | #endif |
---|
866 | |
---|
867 | //ll |
---|