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