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