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