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