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