[169] | 1 | //midi.c: |
---|
| 2 | |
---|
[668] | 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 | |
---|
[169] | 25 | #include "roard.h" |
---|
| 26 | |
---|
[1751] | 27 | #if defined(ROAR_HAVE_IO_POSIX) && !defined(ROAR_TARGET_WIN32) |
---|
| 28 | #define _HAVE_CONSOLE |
---|
| 29 | #endif |
---|
| 30 | |
---|
[185] | 31 | int midi_init (void) { |
---|
[1843] | 32 | |
---|
| 33 | if ( midi_cb_init() == -1 ) { |
---|
| 34 | ROAR_WARN("Can not initialize MIDI subsystem component CB"); |
---|
| 35 | } |
---|
| 36 | |
---|
[1848] | 37 | if ( midi_clock_init() == -1 ) { |
---|
| 38 | ROAR_WARN("Can not initialize MIDI subsystem component clock"); |
---|
| 39 | } |
---|
| 40 | |
---|
[1863] | 41 | g_midi_mess.buf = NULL; |
---|
| 42 | |
---|
[1843] | 43 | return 0; |
---|
| 44 | } |
---|
[1848] | 45 | |
---|
[1843] | 46 | int midi_free (void) { |
---|
| 47 | return midi_cb_free(); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | int midi_update(void) { |
---|
[1848] | 51 | |
---|
[1850] | 52 | if ( g_midi_clock.stream != -1 ) |
---|
| 53 | midi_check_bridge(g_midi_clock.stream); |
---|
[1848] | 54 | |
---|
[1843] | 55 | return midi_cb_update(); |
---|
| 56 | } |
---|
| 57 | |
---|
[1855] | 58 | int midi_reinit(void) { |
---|
[1868] | 59 | |
---|
| 60 | if ( g_midi_mess.buf != NULL ) |
---|
| 61 | roar_buffer_free(g_midi_mess.buf); |
---|
| 62 | |
---|
| 63 | g_midi_mess.buf = NULL; |
---|
| 64 | |
---|
[1855] | 65 | return 0; |
---|
| 66 | } |
---|
| 67 | |
---|
[1845] | 68 | // STREAMS: |
---|
| 69 | |
---|
| 70 | int midi_check_stream (int id) { |
---|
[1846] | 71 | struct roar_stream * s; |
---|
| 72 | struct roar_stream_server * ss; |
---|
[1858] | 73 | struct roar_buffer * b; |
---|
| 74 | char * buf; |
---|
| 75 | ssize_t len; |
---|
[1859] | 76 | int i; |
---|
[1846] | 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 | |
---|
[1848] | 85 | if ( s->dir == ROAR_DIR_BRIDGE ) |
---|
| 86 | return midi_check_bridge(id); |
---|
| 87 | |
---|
[1846] | 88 | switch (s->info.codec) { |
---|
[1858] | 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); |
---|
[1862] | 106 | roar_buffer_free(b); |
---|
[1858] | 107 | return -1; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | roar_buffer_set_len(b, len); |
---|
| 111 | |
---|
[1859] | 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 | } |
---|
[1858] | 125 | } |
---|
[1859] | 126 | } |
---|
[1858] | 127 | |
---|
| 128 | switch (s->info.codec) { |
---|
| 129 | case ROAR_CODEC_MIDI: |
---|
| 130 | return midi_conv_midi2mes(id); |
---|
| 131 | break; |
---|
[1846] | 132 | default: |
---|
| 133 | streams_delete(id); |
---|
| 134 | return -1; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | return 0; |
---|
[1845] | 138 | } |
---|
[1846] | 139 | |
---|
[1845] | 140 | int midi_send_stream (int id) { |
---|
[1846] | 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) { |
---|
[1877] | 152 | case ROAR_CODEC_MIDI: |
---|
| 153 | return midi_conv_mes2midi(id); |
---|
| 154 | break; |
---|
[1846] | 155 | default: |
---|
| 156 | streams_delete(id); |
---|
| 157 | return -1; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | return 0; |
---|
[1845] | 161 | } |
---|
| 162 | |
---|
[1858] | 163 | int midi_conv_midi2mes (int id) { |
---|
[1863] | 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; |
---|
| 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 | |
---|
| 190 | while (have && alive) { |
---|
| 191 | if ( *data & 0x80 ) { |
---|
[1865] | 192 | if ( buf != NULL ) { |
---|
[1866] | 193 | midi_add_buf(id, buf); |
---|
[1865] | 194 | buf = NULL; |
---|
| 195 | } |
---|
| 196 | |
---|
[1863] | 197 | need = 0; |
---|
| 198 | if ( midi_new_bufmes(&buf, &mes) == -1 ) { |
---|
| 199 | alive = 0; |
---|
| 200 | continue; |
---|
| 201 | } |
---|
[1865] | 202 | |
---|
| 203 | if (*data == MIDI_TYPE_CLOCK_TICK || *data == MIDI_TYPE_CLOCK_START || *data == MIDI_TYPE_CLOCK_STOP ) { |
---|
| 204 | mes->type = *data; |
---|
| 205 | } else { |
---|
| 206 | mes->type = *data & 0xF0; |
---|
| 207 | mes->channel = *data & 0x0F; |
---|
| 208 | switch (*data & 0xF0) { |
---|
| 209 | case MIDI_TYPE_NOTE_ON: |
---|
| 210 | case MIDI_TYPE_NOTE_OFF: |
---|
| 211 | need = 2; |
---|
| 212 | break; |
---|
| 213 | case MIDI_TYPE_PA: |
---|
| 214 | need = 2; |
---|
| 215 | break; |
---|
| 216 | case MIDI_TYPE_CONTROLER: |
---|
| 217 | need = 2; |
---|
| 218 | break; |
---|
| 219 | case MIDI_TYPE_PROGRAM: |
---|
| 220 | need = 1; |
---|
| 221 | break; |
---|
| 222 | case MIDI_TYPE_MA: |
---|
| 223 | need = 1; |
---|
| 224 | break; |
---|
| 225 | case MIDI_TYPE_PB: |
---|
| 226 | case MIDI_TYPE_SYSEX: |
---|
| 227 | need = 1; |
---|
| 228 | break; |
---|
| 229 | } |
---|
| 230 | } |
---|
| 231 | |
---|
[1863] | 232 | } else { |
---|
[1871] | 233 | |
---|
| 234 | if ( mes == NULL ) { |
---|
| 235 | ROAR_WARN("midi_conv_midi2mes(id=%i): Lost sync.", id); |
---|
| 236 | data++; |
---|
| 237 | have--; |
---|
| 238 | continue; |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | switch (mes->type) { |
---|
[1873] | 242 | case MIDI_TYPE_NOTE_ON: |
---|
| 243 | case MIDI_TYPE_NOTE_OFF: |
---|
[1874] | 244 | case MIDI_TYPE_PA: |
---|
| 245 | case MIDI_TYPE_CONTROLER: |
---|
[1873] | 246 | if ( need == 2 ) { |
---|
| 247 | mes->kk = *data; |
---|
| 248 | } else { |
---|
| 249 | mes->vv = *data; |
---|
| 250 | } |
---|
| 251 | break; |
---|
| 252 | case MIDI_TYPE_PROGRAM: |
---|
| 253 | case MIDI_TYPE_MA: |
---|
[1874] | 254 | mes->vv = *data; |
---|
| 255 | break; |
---|
[1873] | 256 | case MIDI_TYPE_PB: |
---|
| 257 | case MIDI_TYPE_SYSEX: |
---|
[1874] | 258 | ROAR_WARN("midi_conv_midi2mes(id=%i): Message of Type 0x%.2X (PB or SysEx) not yet supported", id, mes->type); |
---|
[1873] | 259 | break; |
---|
[1871] | 260 | } |
---|
| 261 | |
---|
[1863] | 262 | if ( need ) |
---|
| 263 | need--; |
---|
| 264 | } |
---|
| 265 | data++; |
---|
| 266 | have--; |
---|
| 267 | } |
---|
| 268 | |
---|
[1865] | 269 | if ( need ) { |
---|
| 270 | ROAR_ERR("midi_conv_midi2mes(*): FIXME: BUG!!! Need to restore buffer here with corrected length"); |
---|
| 271 | } else if ( !have ) { |
---|
[1863] | 272 | roar_buffer_next(&(ss->buffer)); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | if ( need && buf != NULL ) { |
---|
| 276 | roar_buffer_free(buf); |
---|
| 277 | buf = NULL; |
---|
| 278 | } else if ( buf != NULL ) { |
---|
[1866] | 279 | midi_add_buf(id, buf); |
---|
[1863] | 280 | buf = NULL; |
---|
| 281 | } |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | return 0; |
---|
[1858] | 285 | } |
---|
| 286 | |
---|
[1877] | 287 | #define _nb len++; *(d++) |
---|
[1858] | 288 | int midi_conv_mes2midi (int id) { |
---|
[1877] | 289 | struct roar_stream * s; |
---|
| 290 | struct roar_stream_server * ss; |
---|
| 291 | struct roar_buffer * buf = g_midi_mess.buf; |
---|
| 292 | struct midi_message * mes = NULL; |
---|
| 293 | unsigned char data[3]; |
---|
| 294 | unsigned char * d; |
---|
| 295 | int len; |
---|
| 296 | |
---|
| 297 | if ( g_streams[id] == NULL ) |
---|
| 298 | return -1; |
---|
| 299 | |
---|
| 300 | ROAR_DBG("midi_conv_mes2midi(id=%i) = ?", id); |
---|
| 301 | |
---|
| 302 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
| 303 | |
---|
| 304 | while (buf != NULL) { |
---|
| 305 | if ( roar_buffer_get_data(buf, (void**)&mes) == -1 ) { |
---|
| 306 | return -1; |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | if (mes->type == MIDI_TYPE_CLOCK_TICK || mes->type == MIDI_TYPE_CLOCK_START || mes->type == MIDI_TYPE_CLOCK_STOP ) { |
---|
| 310 | if ( stream_vio_s_write(ss, &(mes->type), 1) != 1 ) { |
---|
| 311 | streams_delete(id); |
---|
| 312 | return -1; |
---|
| 313 | } |
---|
| 314 | } else { |
---|
| 315 | len = 0; |
---|
| 316 | d = data; |
---|
| 317 | |
---|
| 318 | _nb = (mes->type & 0xF0) | (mes->channel & 0x0F); |
---|
| 319 | |
---|
| 320 | switch (mes->type) { |
---|
| 321 | case MIDI_TYPE_NOTE_ON: |
---|
| 322 | case MIDI_TYPE_NOTE_OFF: |
---|
| 323 | case MIDI_TYPE_PA: |
---|
| 324 | case MIDI_TYPE_CONTROLER: |
---|
| 325 | _nb = mes->kk; |
---|
| 326 | _nb = mes->vv; |
---|
| 327 | break; |
---|
| 328 | case MIDI_TYPE_PROGRAM: |
---|
| 329 | case MIDI_TYPE_MA: |
---|
| 330 | _nb = mes->vv; |
---|
| 331 | break; |
---|
| 332 | case MIDI_TYPE_PB: |
---|
| 333 | case MIDI_TYPE_SYSEX: |
---|
| 334 | break; |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | if ( stream_vio_s_write(ss, data, len) != len ) { |
---|
| 338 | streams_delete(id); |
---|
| 339 | return -1; |
---|
| 340 | } |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | if ( roar_buffer_get_next(buf, &buf) == -1 ) |
---|
| 344 | buf = NULL; |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | return 0; |
---|
[1858] | 348 | } |
---|
[1877] | 349 | #undef _nb |
---|
[1858] | 350 | |
---|
[1863] | 351 | int midi_new_bufmes (struct roar_buffer ** buf, struct midi_message ** mes) { |
---|
| 352 | if ( buf == NULL || mes == NULL ) |
---|
| 353 | return -1; |
---|
| 354 | |
---|
[1867] | 355 | *buf = (void*)(*mes = NULL); |
---|
[1863] | 356 | |
---|
| 357 | if ( roar_buffer_new(buf, sizeof(struct midi_message)) == -1 ) |
---|
| 358 | return -1; |
---|
| 359 | |
---|
| 360 | if ( roar_buffer_get_data(*buf, (void**)mes) == -1 ) { |
---|
| 361 | roar_buffer_free(*buf); |
---|
[1867] | 362 | *buf = (void*)(*mes = NULL); |
---|
[1863] | 363 | return -1; |
---|
| 364 | } |
---|
| 365 | |
---|
| 366 | memset((void*)*mes, 0, sizeof(struct midi_message)); |
---|
| 367 | |
---|
| 368 | (*mes)->type = MIDI_TYPE_NONE; |
---|
| 369 | |
---|
| 370 | return 0; |
---|
| 371 | } |
---|
| 372 | |
---|
[1866] | 373 | int midi_add_buf (int id, struct roar_buffer * buf) { |
---|
[1869] | 374 | struct midi_message * mes; |
---|
| 375 | |
---|
[1866] | 376 | if ( id == -1 || buf == NULL ) |
---|
| 377 | return -1; |
---|
| 378 | |
---|
[1872] | 379 | if ( roar_buffer_get_data(buf, (void**)&mes) == -1 ) |
---|
[1869] | 380 | return -1; |
---|
| 381 | |
---|
[1878] | 382 | ROAR_DBG("midi_add_buf(id=%i, buf=%p) = ?", id, buf); |
---|
| 383 | ROAR_DBG("midi_add_buf(*): MIDI Message of Type 0x%.2X", mes->type); |
---|
| 384 | ROAR_DBG("midi_add_buf(*): Channel: %i", mes->channel); |
---|
| 385 | ROAR_DBG("midi_add_buf(*): flags=0x%.2X", mes->flags); |
---|
| 386 | ROAR_DBG("midi_add_buf(*): kk=0x%.2X, vv=0x%.2X", mes->kk, mes->vv); |
---|
[1869] | 387 | |
---|
[1866] | 388 | if ( g_midi_mess.buf == NULL ) { |
---|
| 389 | g_midi_mess.buf = buf; |
---|
| 390 | } else { |
---|
| 391 | roar_buffer_add(g_midi_mess.buf, buf); |
---|
| 392 | } |
---|
| 393 | |
---|
| 394 | return 0; |
---|
| 395 | } |
---|
| 396 | |
---|
[1848] | 397 | // bridges: |
---|
| 398 | int midi_check_bridge (int id) { |
---|
[1851] | 399 | |
---|
[1860] | 400 | ROAR_DBG("midi_check_bridge(id=%i) = ?", id); |
---|
[1851] | 401 | |
---|
| 402 | if ( id == g_midi_clock.stream ) { |
---|
| 403 | midi_clock_tick(); |
---|
| 404 | |
---|
| 405 | return 0; |
---|
| 406 | } |
---|
| 407 | |
---|
[1848] | 408 | return -1; |
---|
| 409 | } |
---|
| 410 | |
---|
| 411 | // clock: |
---|
| 412 | |
---|
| 413 | int midi_clock_init (void) { |
---|
| 414 | struct roar_stream * s; |
---|
| 415 | struct roar_stream_server * ss; |
---|
| 416 | |
---|
[1850] | 417 | if ( (g_midi_clock.stream = streams_new()) == -1 ) { |
---|
[1848] | 418 | ROAR_WARN("Error while initializing MIDI subsystem component clock"); |
---|
| 419 | return -1; |
---|
| 420 | } |
---|
| 421 | |
---|
[1850] | 422 | midi_vio_set_dummy(g_midi_clock.stream); |
---|
[1848] | 423 | |
---|
[1850] | 424 | streams_get(g_midi_clock.stream, &ss); |
---|
[1848] | 425 | s = ROAR_STREAM(ss); |
---|
| 426 | |
---|
| 427 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 428 | |
---|
[1850] | 429 | s->pos_rel_id = g_midi_clock.stream; |
---|
[1848] | 430 | |
---|
| 431 | s->info.codec = ROAR_CODEC_MIDI; |
---|
| 432 | ss->codec_orgi = ROAR_CODEC_MIDI; |
---|
| 433 | |
---|
| 434 | s->info.channels = 0; |
---|
| 435 | s->info.rate = MIDI_RATE; |
---|
| 436 | s->info.bits = 8; |
---|
| 437 | |
---|
[1850] | 438 | if ( streams_set_dir(g_midi_clock.stream, ROAR_DIR_BRIDGE, 1) == -1 ) { |
---|
[1848] | 439 | ROAR_WARN("Error while initializing MIDI subsystem component clock"); |
---|
| 440 | return -1; |
---|
| 441 | } |
---|
| 442 | |
---|
[1850] | 443 | streams_set_name(g_midi_clock.stream, "MIDI Clock"); |
---|
[1848] | 444 | |
---|
[1850] | 445 | streams_set_flag(g_midi_clock.stream, ROAR_FLAG_PRIMARY); |
---|
| 446 | streams_set_flag(g_midi_clock.stream, ROAR_FLAG_SYNC); |
---|
[1848] | 447 | |
---|
[1851] | 448 | midi_clock_set_bph(3600); // one tick per sec |
---|
| 449 | |
---|
| 450 | return 0; |
---|
| 451 | } |
---|
| 452 | |
---|
| 453 | int midi_clock_set_bph (uint_least32_t bph) { |
---|
| 454 | uint_least32_t sph = g_sa->rate/2 * 75 * g_sa->channels; // samples per houre |
---|
| 455 | |
---|
| 456 | g_midi_clock.bph = bph; |
---|
| 457 | |
---|
| 458 | g_midi_clock.spt = sph/bph; |
---|
| 459 | |
---|
| 460 | g_midi_clock.nt = ROAR_MATH_OVERFLOW_ADD(g_pos, g_midi_clock.spt); |
---|
| 461 | |
---|
| 462 | return 0; |
---|
| 463 | } |
---|
| 464 | |
---|
| 465 | int midi_clock_tick (void) { |
---|
[1875] | 466 | struct roar_buffer * buf; |
---|
| 467 | struct midi_message * mes; |
---|
[1851] | 468 | unsigned int diff; |
---|
| 469 | |
---|
| 470 | while ( g_pos >= g_midi_clock.nt ) { |
---|
| 471 | diff = g_pos - g_midi_clock.nt; |
---|
[1860] | 472 | ROAR_DBG("midi_clock_tick(void): g_pos is %u samples (%5.2f%%) ahead of nt.", diff, (float)diff/g_midi_clock.spt); |
---|
[1851] | 473 | |
---|
| 474 | g_midi_clock.nt = ROAR_MATH_OVERFLOW_ADD(g_midi_clock.nt, g_midi_clock.spt); |
---|
| 475 | |
---|
| 476 | if ( streams_get_flag(g_midi_clock.stream, ROAR_FLAG_SYNC) ) { |
---|
[1860] | 477 | ROAR_DBG("midi_clock_tick(void): TICK! (nt=%lu)", g_midi_clock.nt); |
---|
[1875] | 478 | if ( midi_new_bufmes(&buf, &mes) == -1 ) { |
---|
| 479 | ROAR_ERR("midi_clock_tick(void): Can not create Clock-Tick-Message"); |
---|
| 480 | } |
---|
| 481 | |
---|
| 482 | mes->type = MIDI_TYPE_CLOCK_TICK; |
---|
| 483 | |
---|
| 484 | if ( midi_add_buf(g_midi_clock.stream, buf) == -1 ) { |
---|
| 485 | ROAR_ERR("midi_clock_tick(void): Can not add Clock-Tick-Message"); |
---|
| 486 | roar_buffer_free(buf); |
---|
| 487 | return -1; |
---|
| 488 | } |
---|
[1851] | 489 | } else { |
---|
[1860] | 490 | ROAR_DBG("midi_clock_tick(void): silent tick. (nt=%lu)", g_midi_clock.nt); |
---|
[1851] | 491 | } |
---|
| 492 | } |
---|
| 493 | |
---|
[1848] | 494 | return 0; |
---|
| 495 | } |
---|
| 496 | |
---|
[1845] | 497 | // CB: |
---|
[1843] | 498 | |
---|
| 499 | int midi_cb_init (void) { |
---|
[1751] | 500 | #ifdef _HAVE_CONSOLE |
---|
[1843] | 501 | struct roar_stream * s; |
---|
| 502 | struct roar_stream_server * ss; |
---|
[185] | 503 | int i; |
---|
| 504 | char * files[] = { |
---|
| 505 | "/dev/console", |
---|
| 506 | #ifdef __linux__ |
---|
| 507 | "/dev/tty0", |
---|
| 508 | "/dev/vc/0", |
---|
| 509 | #endif |
---|
| 510 | NULL |
---|
| 511 | }; |
---|
| 512 | |
---|
[1853] | 513 | g_midi_cb.console = -1; |
---|
| 514 | g_midi_cb.stream = -1; |
---|
| 515 | g_midi_cb.stoptime = 0; |
---|
| 516 | g_midi_cb.playing = 0; |
---|
[185] | 517 | |
---|
| 518 | for (i = 0; files[i] != NULL; i++) { |
---|
[1853] | 519 | if ( (g_midi_cb.console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 ) |
---|
[185] | 520 | break; |
---|
| 521 | } |
---|
| 522 | |
---|
[1853] | 523 | if ( g_midi_cb.console == -1 ) |
---|
[1843] | 524 | return -1; |
---|
| 525 | |
---|
[1853] | 526 | if ( (g_midi_cb.stream = streams_new()) == -1 ) { |
---|
[1843] | 527 | ROAR_WARN("Error while initializing MIDI subsystem component CB"); |
---|
| 528 | midi_cb_free(); |
---|
[187] | 529 | return -1; |
---|
| 530 | } |
---|
[1843] | 531 | |
---|
[1853] | 532 | midi_vio_set_dummy(g_midi_cb.stream); |
---|
[1848] | 533 | |
---|
[1853] | 534 | streams_get(g_midi_cb.stream, &ss); |
---|
[1843] | 535 | s = ROAR_STREAM(ss); |
---|
| 536 | |
---|
| 537 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 538 | |
---|
| 539 | s->pos_rel_id = -1; |
---|
| 540 | |
---|
| 541 | s->info.codec = 0; |
---|
| 542 | ss->codec_orgi = 0; |
---|
| 543 | |
---|
| 544 | s->info.channels = 1; |
---|
| 545 | s->info.rate = 1193180; |
---|
| 546 | s->info.bits = 8; |
---|
| 547 | |
---|
[1853] | 548 | if ( streams_set_dir(g_midi_cb.stream, ROAR_DIR_BRIDGE, 1) == -1 ) { |
---|
[1843] | 549 | ROAR_WARN("Error while initializing MIDI subsystem component CB"); |
---|
| 550 | midi_cb_free(); |
---|
| 551 | return -1; |
---|
| 552 | } |
---|
| 553 | |
---|
[1853] | 554 | streams_set_name(g_midi_cb.stream, "Console speaker bridge"); |
---|
[1843] | 555 | |
---|
[1853] | 556 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_OUTPUT); |
---|
| 557 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_PRIMARY); |
---|
| 558 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_HWMIXER); |
---|
[1843] | 559 | |
---|
| 560 | return 0; |
---|
[1485] | 561 | #else |
---|
[1853] | 562 | g_midi_cb.console = -1; |
---|
| 563 | g_midi_cb.stream = -1; |
---|
[1843] | 564 | |
---|
[1485] | 565 | return -1; |
---|
| 566 | #endif |
---|
[185] | 567 | } |
---|
| 568 | |
---|
[1843] | 569 | int midi_cb_free (void) { |
---|
[1751] | 570 | #ifdef _HAVE_CONSOLE |
---|
[1843] | 571 | |
---|
| 572 | midi_cb_stop(); |
---|
| 573 | |
---|
[1853] | 574 | if ( g_midi_cb.stream != -1 ) |
---|
| 575 | streams_delete(g_midi_cb.stream); |
---|
[1843] | 576 | |
---|
[1853] | 577 | if ( g_midi_cb.console != -1 ) |
---|
| 578 | close(g_midi_cb.console); |
---|
[1843] | 579 | |
---|
[185] | 580 | return 0; |
---|
[1485] | 581 | #else |
---|
| 582 | return -1; |
---|
| 583 | #endif |
---|
[185] | 584 | } |
---|
| 585 | |
---|
| 586 | int midi_cb_play(float t, float freq, int override) { |
---|
[191] | 587 | float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels; |
---|
| 588 | |
---|
[189] | 589 | /* |
---|
| 590 | #define MIDI_CB_NOOVERRIDE 0 |
---|
| 591 | #define MIDI_CB_OVERRIDE 1 |
---|
| 592 | */ |
---|
[1853] | 593 | if ( g_midi_cb.playing && override != MIDI_CB_OVERRIDE ) |
---|
[191] | 594 | return -1; |
---|
| 595 | |
---|
[1853] | 596 | g_midi_cb.stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t); |
---|
[191] | 597 | midi_cb_start(freq); |
---|
[1853] | 598 | g_midi_cb.playing = 1; |
---|
[191] | 599 | |
---|
| 600 | return 0; |
---|
[185] | 601 | } |
---|
| 602 | |
---|
[190] | 603 | int midi_cb_update (void) { |
---|
[1853] | 604 | if ( !g_midi_cb.playing ) |
---|
[192] | 605 | return 0; |
---|
| 606 | |
---|
[1853] | 607 | if ( g_midi_cb.stoptime <= g_pos ) |
---|
[189] | 608 | midi_cb_stop(); |
---|
| 609 | |
---|
| 610 | return 0; |
---|
| 611 | } |
---|
| 612 | |
---|
[185] | 613 | int midi_cb_start(float freq) { |
---|
| 614 | // On linux this uses ioctl KIOCSOUND |
---|
[188] | 615 | #ifdef __linux__ |
---|
[1853] | 616 | if ( g_midi_cb.console == -1 ) |
---|
[188] | 617 | return -1; |
---|
| 618 | |
---|
[1853] | 619 | if ( ioctl(g_midi_cb.console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 ) |
---|
[188] | 620 | return -1; |
---|
| 621 | |
---|
| 622 | return 0; |
---|
| 623 | #else |
---|
[185] | 624 | return -1; |
---|
[188] | 625 | #endif |
---|
[185] | 626 | } |
---|
| 627 | |
---|
| 628 | int midi_cb_stop (void) { |
---|
| 629 | #ifdef __linux__ |
---|
[1853] | 630 | g_midi_cb.playing = 0; |
---|
[185] | 631 | return midi_cb_start(0); |
---|
| 632 | #else |
---|
| 633 | return -1; |
---|
| 634 | #endif |
---|
| 635 | } |
---|
| 636 | |
---|
[1848] | 637 | // VIO: |
---|
| 638 | |
---|
| 639 | int midi_vio_set_dummy(int stream) { |
---|
| 640 | struct roar_stream_server * ss; |
---|
| 641 | |
---|
| 642 | if ( streams_get(stream, &ss) == -1 ) |
---|
| 643 | return -1; |
---|
| 644 | |
---|
| 645 | ss->vio.read = NULL; |
---|
| 646 | ss->vio.write = NULL; |
---|
| 647 | ss->vio.lseek = NULL; |
---|
[1849] | 648 | ss->vio.nonblock = (int (*)(struct roar_vio_calls * vio, int state))midi_vio_ok; |
---|
| 649 | ss->vio.sync = (int (*)(struct roar_vio_calls * vio))midi_vio_ok; |
---|
[1848] | 650 | ss->vio.ctl = NULL; |
---|
[1849] | 651 | ss->vio.close = (int (*)(struct roar_vio_calls * vio))midi_vio_ok; |
---|
| 652 | |
---|
| 653 | return 0; |
---|
[1848] | 654 | } |
---|
| 655 | |
---|
| 656 | int midi_vio_ok(struct roar_vio_calls * vio, ...) { |
---|
| 657 | return 0; |
---|
| 658 | } |
---|
| 659 | |
---|
[169] | 660 | //ll |
---|