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