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