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 | // if velocity is zero we have a NOTE OFF event |
---|
268 | if ( data[1] == 0 ) |
---|
269 | mes->type = MIDI_TYPE_NOTE_OFF; |
---|
270 | } |
---|
271 | case MIDI_TYPE_PA: |
---|
272 | case MIDI_TYPE_CONTROLER: |
---|
273 | if ( need == 2 ) { |
---|
274 | mes->kk = *data; |
---|
275 | } else { |
---|
276 | mes->vv = *data; |
---|
277 | } |
---|
278 | break; |
---|
279 | case MIDI_TYPE_PROGRAM: |
---|
280 | case MIDI_TYPE_MA: |
---|
281 | mes->vv = *data; |
---|
282 | break; |
---|
283 | case MIDI_TYPE_PB: |
---|
284 | case MIDI_TYPE_SYSEX: |
---|
285 | ROAR_WARN("midi_conv_midi2mes(id=%i): Message of Type 0x%.2X (PB or SysEx) not yet supported", id, mes->type); |
---|
286 | break; |
---|
287 | } |
---|
288 | |
---|
289 | if ( need ) |
---|
290 | need--; |
---|
291 | |
---|
292 | if ( !need ) { |
---|
293 | switch (mes->type) { |
---|
294 | case MIDI_TYPE_CONTROLER: |
---|
295 | switch (mes->kk) { |
---|
296 | case MIDI_CCE_MAIN_VOL: |
---|
297 | if ( 516 * mes->vv > 65100 ) { // max volume |
---|
298 | ss->mixer.mixer[mes->channel] = 65535; |
---|
299 | } else { |
---|
300 | ss->mixer.mixer[mes->channel] = 516 * mes->vv; |
---|
301 | } |
---|
302 | break; |
---|
303 | } |
---|
304 | break; |
---|
305 | } |
---|
306 | } |
---|
307 | |
---|
308 | } |
---|
309 | data++; |
---|
310 | have--; |
---|
311 | } |
---|
312 | |
---|
313 | if ( need ) { |
---|
314 | if ( roar_buffer_set_offset(ss->buffer, old_have - last_have) == -1 ) { |
---|
315 | ROAR_ERR("midi_conv_midi2mes(*): FIXME: BUG!!! Need to restore buffer here with corrected length"); |
---|
316 | } |
---|
317 | } else if ( !have ) { |
---|
318 | roar_buffer_next(&(ss->buffer)); |
---|
319 | } |
---|
320 | |
---|
321 | if ( need && buf != NULL ) { |
---|
322 | roar_buffer_free(buf); |
---|
323 | buf = NULL; |
---|
324 | } else if ( buf != NULL ) { |
---|
325 | midi_add_buf(id, buf); |
---|
326 | buf = NULL; |
---|
327 | } |
---|
328 | } |
---|
329 | |
---|
330 | return 0; |
---|
331 | } |
---|
332 | |
---|
333 | #define _nb len++; *(d++) |
---|
334 | int midi_conv_mes2midi (int id) { |
---|
335 | struct roar_stream * s; |
---|
336 | struct roar_stream_server * ss; |
---|
337 | struct roar_buffer * buf = g_midi_mess.buf; |
---|
338 | struct midi_message * mes = NULL; |
---|
339 | unsigned char data[3]; |
---|
340 | unsigned char * d; |
---|
341 | int len; |
---|
342 | int send_clock; |
---|
343 | |
---|
344 | if ( g_streams[id] == NULL ) |
---|
345 | return -1; |
---|
346 | |
---|
347 | ROAR_DBG("midi_conv_mes2midi(id=%i) = ?", id); |
---|
348 | |
---|
349 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
350 | |
---|
351 | send_clock = streams_get_flag(id, ROAR_FLAG_SYNC); |
---|
352 | |
---|
353 | while (buf != NULL) { |
---|
354 | if ( roar_buffer_get_data(buf, (void**)&mes) == -1 ) { |
---|
355 | return -1; |
---|
356 | } |
---|
357 | |
---|
358 | if (mes->type == MIDI_TYPE_CLOCK_TICK || mes->type == MIDI_TYPE_CLOCK_START || mes->type == MIDI_TYPE_CLOCK_STOP ) { |
---|
359 | if ( send_clock ) { |
---|
360 | if ( stream_vio_s_write(ss, &(mes->type), 1) != 1 ) { |
---|
361 | streams_delete(id); |
---|
362 | return -1; |
---|
363 | } |
---|
364 | } |
---|
365 | |
---|
366 | if ( mes->type == MIDI_TYPE_CLOCK_TICK ) { |
---|
367 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1); |
---|
368 | } |
---|
369 | } else { |
---|
370 | len = 0; |
---|
371 | d = data; |
---|
372 | |
---|
373 | _nb = (mes->type & 0xF0) | (mes->channel & 0x0F); |
---|
374 | |
---|
375 | switch (mes->type) { |
---|
376 | case MIDI_TYPE_CONTROLER: |
---|
377 | switch (mes->kk) { |
---|
378 | case MIDI_CCE_MAIN_VOL: |
---|
379 | if ( 516 * mes->vv > 65100 ) { // max volume |
---|
380 | ss->mixer.mixer[mes->channel] = 65535; |
---|
381 | } else { |
---|
382 | ss->mixer.mixer[mes->channel] = 516 * mes->vv; |
---|
383 | } |
---|
384 | break; |
---|
385 | } |
---|
386 | case MIDI_TYPE_NOTE_ON: |
---|
387 | case MIDI_TYPE_NOTE_OFF: |
---|
388 | case MIDI_TYPE_PA: |
---|
389 | _nb = mes->kk; |
---|
390 | _nb = mes->vv; |
---|
391 | break; |
---|
392 | case MIDI_TYPE_PROGRAM: |
---|
393 | case MIDI_TYPE_MA: |
---|
394 | _nb = mes->vv; |
---|
395 | break; |
---|
396 | case MIDI_TYPE_PB: |
---|
397 | case MIDI_TYPE_SYSEX: |
---|
398 | break; |
---|
399 | } |
---|
400 | |
---|
401 | if ( stream_vio_s_write(ss, data, len) != len ) { |
---|
402 | streams_delete(id); |
---|
403 | return -1; |
---|
404 | } |
---|
405 | } |
---|
406 | |
---|
407 | if ( roar_buffer_get_next(buf, &buf) == -1 ) |
---|
408 | buf = NULL; |
---|
409 | } |
---|
410 | |
---|
411 | return 0; |
---|
412 | } |
---|
413 | #undef _nb |
---|
414 | |
---|
415 | int midi_new_bufmes (struct roar_buffer ** buf, struct midi_message ** mes) { |
---|
416 | if ( buf == NULL || mes == NULL ) |
---|
417 | return -1; |
---|
418 | |
---|
419 | *buf = (void*)(*mes = NULL); |
---|
420 | |
---|
421 | if ( roar_buffer_new(buf, sizeof(struct midi_message)) == -1 ) |
---|
422 | return -1; |
---|
423 | |
---|
424 | if ( roar_buffer_get_data(*buf, (void**)mes) == -1 ) { |
---|
425 | roar_buffer_free(*buf); |
---|
426 | *buf = (void*)(*mes = NULL); |
---|
427 | return -1; |
---|
428 | } |
---|
429 | |
---|
430 | memset((void*)*mes, 0, sizeof(struct midi_message)); |
---|
431 | |
---|
432 | (*mes)->type = MIDI_TYPE_NONE; |
---|
433 | |
---|
434 | return 0; |
---|
435 | } |
---|
436 | |
---|
437 | int midi_add_buf (int id, struct roar_buffer * buf) { |
---|
438 | struct midi_message * mes; |
---|
439 | |
---|
440 | if ( id == -1 || buf == NULL ) |
---|
441 | return -1; |
---|
442 | |
---|
443 | if ( roar_buffer_get_data(buf, (void**)&mes) == -1 ) |
---|
444 | return -1; |
---|
445 | |
---|
446 | ROAR_DBG("midi_add_buf(id=%i, buf=%p) = ?", id, buf); |
---|
447 | ROAR_DBG("midi_add_buf(*): MIDI Message of Type 0x%.2X", mes->type); |
---|
448 | ROAR_DBG("midi_add_buf(*): Channel: %i", mes->channel); |
---|
449 | ROAR_DBG("midi_add_buf(*): flags=0x%.2X", mes->flags); |
---|
450 | ROAR_DBG("midi_add_buf(*): kk=0x%.2X, vv=0x%.2X", mes->kk, mes->vv); |
---|
451 | |
---|
452 | if ( g_midi_mess.buf == NULL ) { |
---|
453 | g_midi_mess.buf = buf; |
---|
454 | } else { |
---|
455 | roar_buffer_add(g_midi_mess.buf, buf); |
---|
456 | } |
---|
457 | |
---|
458 | return 0; |
---|
459 | } |
---|
460 | |
---|
461 | // bridges: |
---|
462 | int midi_check_bridge (int id) { |
---|
463 | |
---|
464 | ROAR_DBG("midi_check_bridge(id=%i) = ?", id); |
---|
465 | |
---|
466 | if ( id == g_midi_clock.stream ) { |
---|
467 | midi_clock_tick(); |
---|
468 | |
---|
469 | return 0; |
---|
470 | } |
---|
471 | |
---|
472 | return -1; |
---|
473 | } |
---|
474 | |
---|
475 | // clock: |
---|
476 | |
---|
477 | int midi_clock_init (void) { |
---|
478 | struct roar_stream * s; |
---|
479 | struct roar_stream_server * ss; |
---|
480 | |
---|
481 | if ( (g_midi_clock.stream = streams_new()) == -1 ) { |
---|
482 | ROAR_WARN("Error while initializing MIDI subsystem component clock"); |
---|
483 | return -1; |
---|
484 | } |
---|
485 | |
---|
486 | midi_vio_set_dummy(g_midi_clock.stream); |
---|
487 | |
---|
488 | streams_get(g_midi_clock.stream, &ss); |
---|
489 | s = ROAR_STREAM(ss); |
---|
490 | |
---|
491 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
492 | |
---|
493 | s->pos_rel_id = g_midi_clock.stream; |
---|
494 | |
---|
495 | s->info.codec = ROAR_CODEC_MIDI; |
---|
496 | ss->codec_orgi = ROAR_CODEC_MIDI; |
---|
497 | |
---|
498 | s->info.channels = 1; // we have only one channel, ticking on channel 0 |
---|
499 | s->info.rate = ROAR_MIDI_TICKS_PER_BEAT; // one beat per sec |
---|
500 | s->info.bits = ROAR_MIDI_BITS; |
---|
501 | |
---|
502 | if ( streams_set_dir(g_midi_clock.stream, ROAR_DIR_BRIDGE, 1) == -1 ) { |
---|
503 | ROAR_WARN("Error while initializing MIDI subsystem component clock"); |
---|
504 | return -1; |
---|
505 | } |
---|
506 | |
---|
507 | streams_set_name(g_midi_clock.stream, "MIDI Clock"); |
---|
508 | |
---|
509 | streams_set_flag(g_midi_clock.stream, ROAR_FLAG_PRIMARY); |
---|
510 | streams_set_flag(g_midi_clock.stream, ROAR_FLAG_SYNC); |
---|
511 | |
---|
512 | midi_clock_set_bph(3600); // one beat per sec |
---|
513 | |
---|
514 | midi_config.inited |= MIDI_INITED_CLOCK; |
---|
515 | |
---|
516 | return 0; |
---|
517 | } |
---|
518 | |
---|
519 | int midi_clock_set_bph (uint_least32_t bph) { |
---|
520 | uint_least32_t sph = g_sa->rate/2 * 75 * g_sa->channels; // samples per houre |
---|
521 | |
---|
522 | g_midi_clock.bph = bph; |
---|
523 | |
---|
524 | g_midi_clock.spt = sph/bph; |
---|
525 | |
---|
526 | g_midi_clock.nt = ROAR_MATH_OVERFLOW_ADD(g_pos, g_midi_clock.spt); |
---|
527 | |
---|
528 | return 0; |
---|
529 | } |
---|
530 | |
---|
531 | int midi_clock_tick (void) { |
---|
532 | struct roar_buffer * buf; |
---|
533 | struct midi_message * mes; |
---|
534 | struct roar_stream_server * ss; |
---|
535 | unsigned int diff; |
---|
536 | |
---|
537 | if ( streams_get(g_midi_clock.stream, &ss) == -1 ) { |
---|
538 | ROAR_ERR("midi_clock_tick(void): Something very BAD happend: can not get stream object of my own stream!"); |
---|
539 | g_midi_clock.stream = -1; |
---|
540 | ROAR_WARN("midi_clock_tick(void): Disabled MIDI Clock"); |
---|
541 | return -1; |
---|
542 | } |
---|
543 | |
---|
544 | while ( g_pos >= g_midi_clock.nt ) { |
---|
545 | diff = g_pos - g_midi_clock.nt; |
---|
546 | ROAR_DBG("midi_clock_tick(void): g_pos is %u samples (%5.2f%%) after of nt.", diff, (float)diff/g_midi_clock.spt); |
---|
547 | |
---|
548 | g_midi_clock.nt = ROAR_MATH_OVERFLOW_ADD(g_midi_clock.nt, g_midi_clock.spt); |
---|
549 | |
---|
550 | if ( streams_get_flag(g_midi_clock.stream, ROAR_FLAG_SYNC) ) { |
---|
551 | ROAR_DBG("midi_clock_tick(void): TICK! (nt=%lu)", g_midi_clock.nt); |
---|
552 | if ( midi_new_bufmes(&buf, &mes) == -1 ) { |
---|
553 | ROAR_ERR("midi_clock_tick(void): Can not create Clock-Tick-Message"); |
---|
554 | } |
---|
555 | |
---|
556 | mes->type = MIDI_TYPE_CLOCK_TICK; |
---|
557 | |
---|
558 | if ( midi_add_buf(g_midi_clock.stream, buf) == -1 ) { |
---|
559 | ROAR_ERR("midi_clock_tick(void): Can not add Clock-Tick-Message"); |
---|
560 | roar_buffer_free(buf); |
---|
561 | return -1; |
---|
562 | } |
---|
563 | |
---|
564 | ROAR_STREAM(ss)->pos = ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(ss)->pos, 1); |
---|
565 | } else { |
---|
566 | ROAR_DBG("midi_clock_tick(void): silent tick. (nt=%lu)", g_midi_clock.nt); |
---|
567 | } |
---|
568 | } |
---|
569 | |
---|
570 | return 0; |
---|
571 | } |
---|
572 | |
---|
573 | // CB: |
---|
574 | |
---|
575 | int midi_cb_init (void) { |
---|
576 | #ifdef _HAVE_CONSOLE |
---|
577 | struct roar_stream * s; |
---|
578 | struct roar_stream_server * ss; |
---|
579 | int i; |
---|
580 | char * files[] = { |
---|
581 | "/NX", |
---|
582 | "/dev/roarconsole", |
---|
583 | "/dev/console", |
---|
584 | #ifdef __linux__ |
---|
585 | "/dev/tty0", |
---|
586 | "/dev/vc/0", |
---|
587 | #endif |
---|
588 | NULL |
---|
589 | }; |
---|
590 | |
---|
591 | g_midi_cb.console = -1; |
---|
592 | g_midi_cb.stream = -1; |
---|
593 | g_midi_cb.stoptime = 0; |
---|
594 | g_midi_cb.playing = 0; |
---|
595 | |
---|
596 | if ( midi_config.console_dev != NULL ) { |
---|
597 | files[0] = midi_config.console_dev; |
---|
598 | } |
---|
599 | |
---|
600 | for (i = 0; files[i] != NULL; i++) { |
---|
601 | if ( (g_midi_cb.console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 ) |
---|
602 | break; |
---|
603 | } |
---|
604 | |
---|
605 | if ( g_midi_cb.console == -1 ) |
---|
606 | return -1; |
---|
607 | |
---|
608 | if ( (g_midi_cb.stream = streams_new()) == -1 ) { |
---|
609 | ROAR_WARN("Error while initializing MIDI subsystem component CB"); |
---|
610 | midi_cb_free(); |
---|
611 | return -1; |
---|
612 | } |
---|
613 | |
---|
614 | midi_vio_set_dummy(g_midi_cb.stream); |
---|
615 | |
---|
616 | streams_get(g_midi_cb.stream, &ss); |
---|
617 | s = ROAR_STREAM(ss); |
---|
618 | |
---|
619 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
620 | |
---|
621 | s->pos_rel_id = -1; |
---|
622 | |
---|
623 | s->info.codec = 0; |
---|
624 | ss->codec_orgi = 0; |
---|
625 | |
---|
626 | s->info.channels = 1; |
---|
627 | s->info.rate = 1193180; |
---|
628 | s->info.bits = 8; |
---|
629 | |
---|
630 | if ( streams_set_dir(g_midi_cb.stream, ROAR_DIR_BRIDGE, 1) == -1 ) { |
---|
631 | ROAR_WARN("Error while initializing MIDI subsystem component CB"); |
---|
632 | midi_cb_free(); |
---|
633 | return -1; |
---|
634 | } |
---|
635 | |
---|
636 | streams_set_name(g_midi_cb.stream, "Console speaker bridge"); |
---|
637 | |
---|
638 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_OUTPUT); |
---|
639 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_PRIMARY); |
---|
640 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_HWMIXER); |
---|
641 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_MUTE); |
---|
642 | |
---|
643 | midi_config.inited |= MIDI_INITED_CB; |
---|
644 | |
---|
645 | return 0; |
---|
646 | #else |
---|
647 | g_midi_cb.console = -1; |
---|
648 | g_midi_cb.stream = -1; |
---|
649 | |
---|
650 | return -1; |
---|
651 | #endif |
---|
652 | } |
---|
653 | |
---|
654 | int midi_cb_free (void) { |
---|
655 | #ifdef _HAVE_CONSOLE |
---|
656 | |
---|
657 | midi_cb_stop(); |
---|
658 | |
---|
659 | if ( g_midi_cb.stream != -1 ) |
---|
660 | streams_delete(g_midi_cb.stream); |
---|
661 | |
---|
662 | if ( g_midi_cb.console != -1 ) |
---|
663 | close(g_midi_cb.console); |
---|
664 | |
---|
665 | return 0; |
---|
666 | #else |
---|
667 | return -1; |
---|
668 | #endif |
---|
669 | } |
---|
670 | |
---|
671 | int midi_cb_play(float t, float freq, int override) { |
---|
672 | float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels; |
---|
673 | |
---|
674 | /* |
---|
675 | #define MIDI_CB_NOOVERRIDE 0 |
---|
676 | #define MIDI_CB_OVERRIDE 1 |
---|
677 | */ |
---|
678 | if ( g_midi_cb.playing && override != MIDI_CB_OVERRIDE ) |
---|
679 | return -1; |
---|
680 | |
---|
681 | g_midi_cb.stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t); |
---|
682 | midi_cb_start(freq); |
---|
683 | |
---|
684 | return 0; |
---|
685 | } |
---|
686 | |
---|
687 | int midi_cb_update (void) { |
---|
688 | |
---|
689 | if ( !streams_get_flag(g_midi_cb.stream, ROAR_FLAG_MUTE) ) { |
---|
690 | if ( midi_cb_readbuf() == -1 ) |
---|
691 | return -1; |
---|
692 | } else if ( g_midi_cb.playing ) { |
---|
693 | midi_cb_stop(); |
---|
694 | } |
---|
695 | |
---|
696 | if ( !g_midi_cb.playing ) |
---|
697 | return 0; |
---|
698 | |
---|
699 | /* |
---|
700 | if ( g_midi_cb.stoptime <= g_pos ) |
---|
701 | midi_cb_stop(); |
---|
702 | */ |
---|
703 | |
---|
704 | ROAR_DBG("midi_cb_update(void) = ?"); |
---|
705 | |
---|
706 | return 0; |
---|
707 | } |
---|
708 | |
---|
709 | int midi_cb_start(float freq) { |
---|
710 | // On linux this uses ioctl KIOCSOUND |
---|
711 | #ifdef __linux__ |
---|
712 | if ( g_midi_cb.console == -1 ) |
---|
713 | return -1; |
---|
714 | |
---|
715 | if ( ioctl(g_midi_cb.console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 ) |
---|
716 | return -1; |
---|
717 | |
---|
718 | g_midi_cb.playing = 1; |
---|
719 | |
---|
720 | return 0; |
---|
721 | #else |
---|
722 | return -1; |
---|
723 | #endif |
---|
724 | } |
---|
725 | |
---|
726 | int midi_cb_stop (void) { |
---|
727 | #ifdef __linux__ |
---|
728 | int ret; |
---|
729 | |
---|
730 | ret = midi_cb_start(0); |
---|
731 | g_midi_cb.playing = 0; |
---|
732 | |
---|
733 | return ret; |
---|
734 | #else |
---|
735 | return -1; |
---|
736 | #endif |
---|
737 | } |
---|
738 | |
---|
739 | int midi_cb_readbuf(void) { |
---|
740 | struct roar_buffer * buf = g_midi_mess.buf; |
---|
741 | struct midi_message * mes = NULL; |
---|
742 | |
---|
743 | ROAR_DBG("midi_cb_readbuf(void) = ?"); |
---|
744 | |
---|
745 | while (buf != NULL) { |
---|
746 | ROAR_DBG("midi_cb_readbuf(void): buf=%p", buf); |
---|
747 | |
---|
748 | if ( roar_buffer_get_data(buf, (void**)&mes) == -1 ) { |
---|
749 | return -1; |
---|
750 | } |
---|
751 | |
---|
752 | switch (mes->type) { |
---|
753 | case MIDI_TYPE_NOTE_ON: |
---|
754 | midi_cb_start(mes->d.note.freq); |
---|
755 | break; |
---|
756 | case MIDI_TYPE_NOTE_OFF: |
---|
757 | midi_cb_stop(); |
---|
758 | break; |
---|
759 | case MIDI_TYPE_CONTROLER: |
---|
760 | if ( mes->kk == MIDI_CCE_ALL_NOTE_OFF ) /* all note off */ |
---|
761 | midi_cb_stop(); |
---|
762 | break; |
---|
763 | } |
---|
764 | |
---|
765 | if ( roar_buffer_get_next(buf, &buf) == -1 ) |
---|
766 | buf = NULL; |
---|
767 | } |
---|
768 | |
---|
769 | return 0; |
---|
770 | } |
---|
771 | |
---|
772 | // VIO: |
---|
773 | |
---|
774 | int midi_vio_set_dummy(int stream) { |
---|
775 | struct roar_stream_server * ss; |
---|
776 | |
---|
777 | if ( streams_get(stream, &ss) == -1 ) |
---|
778 | return -1; |
---|
779 | |
---|
780 | ss->vio.read = NULL; |
---|
781 | ss->vio.write = NULL; |
---|
782 | ss->vio.lseek = NULL; |
---|
783 | ss->vio.nonblock = (int (*)(struct roar_vio_calls * vio, int state))midi_vio_ok; |
---|
784 | ss->vio.sync = (int (*)(struct roar_vio_calls * vio))midi_vio_ok; |
---|
785 | ss->vio.ctl = NULL; |
---|
786 | ss->vio.close = (int (*)(struct roar_vio_calls * vio))midi_vio_ok; |
---|
787 | |
---|
788 | return 0; |
---|
789 | } |
---|
790 | |
---|
791 | int midi_vio_ok(struct roar_vio_calls * vio, ...) { |
---|
792 | return 0; |
---|
793 | } |
---|
794 | |
---|
795 | //ll |
---|