[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; |
---|
[1879] | 169 | size_t need = 0, have = 0, last_have = 0, old_have = 0; |
---|
[1863] | 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 | } |
---|
[1879] | 189 | old_have = have; |
---|
[1863] | 190 | |
---|
| 191 | while (have && alive) { |
---|
| 192 | if ( *data & 0x80 ) { |
---|
[1865] | 193 | if ( buf != NULL ) { |
---|
[1866] | 194 | midi_add_buf(id, buf); |
---|
[1865] | 195 | buf = NULL; |
---|
| 196 | } |
---|
| 197 | |
---|
[1879] | 198 | last_have = have; |
---|
| 199 | |
---|
[1863] | 200 | need = 0; |
---|
| 201 | if ( midi_new_bufmes(&buf, &mes) == -1 ) { |
---|
| 202 | alive = 0; |
---|
| 203 | continue; |
---|
| 204 | } |
---|
[1865] | 205 | |
---|
| 206 | if (*data == MIDI_TYPE_CLOCK_TICK || *data == MIDI_TYPE_CLOCK_START || *data == MIDI_TYPE_CLOCK_STOP ) { |
---|
| 207 | mes->type = *data; |
---|
[1891] | 208 | |
---|
| 209 | if ( *data == MIDI_TYPE_CLOCK_TICK ) { |
---|
| 210 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1); |
---|
| 211 | } |
---|
[1865] | 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 | |
---|
[1863] | 239 | } else { |
---|
[1871] | 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) { |
---|
[1873] | 249 | case MIDI_TYPE_NOTE_ON: |
---|
| 250 | case MIDI_TYPE_NOTE_OFF: |
---|
[1881] | 251 | if ( need == 2 ) { |
---|
| 252 | roar_midi_note_from_midiid(&(mes->d.note), *data); |
---|
| 253 | } |
---|
[1874] | 254 | case MIDI_TYPE_PA: |
---|
| 255 | case MIDI_TYPE_CONTROLER: |
---|
[1873] | 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: |
---|
[1874] | 264 | mes->vv = *data; |
---|
| 265 | break; |
---|
[1873] | 266 | case MIDI_TYPE_PB: |
---|
| 267 | case MIDI_TYPE_SYSEX: |
---|
[1874] | 268 | ROAR_WARN("midi_conv_midi2mes(id=%i): Message of Type 0x%.2X (PB or SysEx) not yet supported", id, mes->type); |
---|
[1873] | 269 | break; |
---|
[1871] | 270 | } |
---|
| 271 | |
---|
[1863] | 272 | if ( need ) |
---|
| 273 | need--; |
---|
[1893] | 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 | |
---|
[1863] | 291 | } |
---|
| 292 | data++; |
---|
| 293 | have--; |
---|
| 294 | } |
---|
| 295 | |
---|
[1865] | 296 | if ( need ) { |
---|
[1879] | 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 | } |
---|
[1865] | 300 | } else if ( !have ) { |
---|
[1863] | 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 ) { |
---|
[1866] | 308 | midi_add_buf(id, buf); |
---|
[1863] | 309 | buf = NULL; |
---|
| 310 | } |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | return 0; |
---|
[1858] | 314 | } |
---|
| 315 | |
---|
[1877] | 316 | #define _nb len++; *(d++) |
---|
[1858] | 317 | int midi_conv_mes2midi (int id) { |
---|
[1877] | 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 | |
---|
| 326 | if ( g_streams[id] == NULL ) |
---|
| 327 | return -1; |
---|
| 328 | |
---|
| 329 | ROAR_DBG("midi_conv_mes2midi(id=%i) = ?", id); |
---|
| 330 | |
---|
| 331 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
| 332 | |
---|
| 333 | while (buf != NULL) { |
---|
| 334 | if ( roar_buffer_get_data(buf, (void**)&mes) == -1 ) { |
---|
| 335 | return -1; |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | if (mes->type == MIDI_TYPE_CLOCK_TICK || mes->type == MIDI_TYPE_CLOCK_START || mes->type == MIDI_TYPE_CLOCK_STOP ) { |
---|
| 339 | if ( stream_vio_s_write(ss, &(mes->type), 1) != 1 ) { |
---|
| 340 | streams_delete(id); |
---|
| 341 | return -1; |
---|
| 342 | } |
---|
| 343 | } else { |
---|
| 344 | len = 0; |
---|
| 345 | d = data; |
---|
| 346 | |
---|
| 347 | _nb = (mes->type & 0xF0) | (mes->channel & 0x0F); |
---|
| 348 | |
---|
| 349 | switch (mes->type) { |
---|
| 350 | case MIDI_TYPE_NOTE_ON: |
---|
| 351 | case MIDI_TYPE_NOTE_OFF: |
---|
| 352 | case MIDI_TYPE_PA: |
---|
| 353 | case MIDI_TYPE_CONTROLER: |
---|
| 354 | _nb = mes->kk; |
---|
| 355 | _nb = mes->vv; |
---|
| 356 | break; |
---|
| 357 | case MIDI_TYPE_PROGRAM: |
---|
| 358 | case MIDI_TYPE_MA: |
---|
| 359 | _nb = mes->vv; |
---|
| 360 | break; |
---|
| 361 | case MIDI_TYPE_PB: |
---|
| 362 | case MIDI_TYPE_SYSEX: |
---|
| 363 | break; |
---|
| 364 | } |
---|
| 365 | |
---|
| 366 | if ( stream_vio_s_write(ss, data, len) != len ) { |
---|
| 367 | streams_delete(id); |
---|
| 368 | return -1; |
---|
| 369 | } |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | if ( roar_buffer_get_next(buf, &buf) == -1 ) |
---|
| 373 | buf = NULL; |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | return 0; |
---|
[1858] | 377 | } |
---|
[1877] | 378 | #undef _nb |
---|
[1858] | 379 | |
---|
[1863] | 380 | int midi_new_bufmes (struct roar_buffer ** buf, struct midi_message ** mes) { |
---|
| 381 | if ( buf == NULL || mes == NULL ) |
---|
| 382 | return -1; |
---|
| 383 | |
---|
[1867] | 384 | *buf = (void*)(*mes = NULL); |
---|
[1863] | 385 | |
---|
| 386 | if ( roar_buffer_new(buf, sizeof(struct midi_message)) == -1 ) |
---|
| 387 | return -1; |
---|
| 388 | |
---|
| 389 | if ( roar_buffer_get_data(*buf, (void**)mes) == -1 ) { |
---|
| 390 | roar_buffer_free(*buf); |
---|
[1867] | 391 | *buf = (void*)(*mes = NULL); |
---|
[1863] | 392 | return -1; |
---|
| 393 | } |
---|
| 394 | |
---|
| 395 | memset((void*)*mes, 0, sizeof(struct midi_message)); |
---|
| 396 | |
---|
| 397 | (*mes)->type = MIDI_TYPE_NONE; |
---|
| 398 | |
---|
| 399 | return 0; |
---|
| 400 | } |
---|
| 401 | |
---|
[1866] | 402 | int midi_add_buf (int id, struct roar_buffer * buf) { |
---|
[1869] | 403 | struct midi_message * mes; |
---|
| 404 | |
---|
[1866] | 405 | if ( id == -1 || buf == NULL ) |
---|
| 406 | return -1; |
---|
| 407 | |
---|
[1872] | 408 | if ( roar_buffer_get_data(buf, (void**)&mes) == -1 ) |
---|
[1869] | 409 | return -1; |
---|
| 410 | |
---|
[1878] | 411 | ROAR_DBG("midi_add_buf(id=%i, buf=%p) = ?", id, buf); |
---|
| 412 | ROAR_DBG("midi_add_buf(*): MIDI Message of Type 0x%.2X", mes->type); |
---|
| 413 | ROAR_DBG("midi_add_buf(*): Channel: %i", mes->channel); |
---|
| 414 | ROAR_DBG("midi_add_buf(*): flags=0x%.2X", mes->flags); |
---|
| 415 | ROAR_DBG("midi_add_buf(*): kk=0x%.2X, vv=0x%.2X", mes->kk, mes->vv); |
---|
[1869] | 416 | |
---|
[1866] | 417 | if ( g_midi_mess.buf == NULL ) { |
---|
| 418 | g_midi_mess.buf = buf; |
---|
| 419 | } else { |
---|
| 420 | roar_buffer_add(g_midi_mess.buf, buf); |
---|
| 421 | } |
---|
| 422 | |
---|
| 423 | return 0; |
---|
| 424 | } |
---|
| 425 | |
---|
[1848] | 426 | // bridges: |
---|
| 427 | int midi_check_bridge (int id) { |
---|
[1851] | 428 | |
---|
[1860] | 429 | ROAR_DBG("midi_check_bridge(id=%i) = ?", id); |
---|
[1851] | 430 | |
---|
| 431 | if ( id == g_midi_clock.stream ) { |
---|
| 432 | midi_clock_tick(); |
---|
| 433 | |
---|
| 434 | return 0; |
---|
| 435 | } |
---|
| 436 | |
---|
[1848] | 437 | return -1; |
---|
| 438 | } |
---|
| 439 | |
---|
| 440 | // clock: |
---|
| 441 | |
---|
| 442 | int midi_clock_init (void) { |
---|
| 443 | struct roar_stream * s; |
---|
| 444 | struct roar_stream_server * ss; |
---|
| 445 | |
---|
[1850] | 446 | if ( (g_midi_clock.stream = streams_new()) == -1 ) { |
---|
[1848] | 447 | ROAR_WARN("Error while initializing MIDI subsystem component clock"); |
---|
| 448 | return -1; |
---|
| 449 | } |
---|
| 450 | |
---|
[1850] | 451 | midi_vio_set_dummy(g_midi_clock.stream); |
---|
[1848] | 452 | |
---|
[1850] | 453 | streams_get(g_midi_clock.stream, &ss); |
---|
[1848] | 454 | s = ROAR_STREAM(ss); |
---|
| 455 | |
---|
| 456 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 457 | |
---|
[1850] | 458 | s->pos_rel_id = g_midi_clock.stream; |
---|
[1848] | 459 | |
---|
| 460 | s->info.codec = ROAR_CODEC_MIDI; |
---|
| 461 | ss->codec_orgi = ROAR_CODEC_MIDI; |
---|
| 462 | |
---|
| 463 | s->info.channels = 0; |
---|
| 464 | s->info.rate = MIDI_RATE; |
---|
| 465 | s->info.bits = 8; |
---|
| 466 | |
---|
[1850] | 467 | if ( streams_set_dir(g_midi_clock.stream, ROAR_DIR_BRIDGE, 1) == -1 ) { |
---|
[1848] | 468 | ROAR_WARN("Error while initializing MIDI subsystem component clock"); |
---|
| 469 | return -1; |
---|
| 470 | } |
---|
| 471 | |
---|
[1850] | 472 | streams_set_name(g_midi_clock.stream, "MIDI Clock"); |
---|
[1848] | 473 | |
---|
[1850] | 474 | streams_set_flag(g_midi_clock.stream, ROAR_FLAG_PRIMARY); |
---|
| 475 | streams_set_flag(g_midi_clock.stream, ROAR_FLAG_SYNC); |
---|
[1848] | 476 | |
---|
[1851] | 477 | midi_clock_set_bph(3600); // one tick per sec |
---|
| 478 | |
---|
| 479 | return 0; |
---|
| 480 | } |
---|
| 481 | |
---|
| 482 | int midi_clock_set_bph (uint_least32_t bph) { |
---|
| 483 | uint_least32_t sph = g_sa->rate/2 * 75 * g_sa->channels; // samples per houre |
---|
| 484 | |
---|
| 485 | g_midi_clock.bph = bph; |
---|
| 486 | |
---|
| 487 | g_midi_clock.spt = sph/bph; |
---|
| 488 | |
---|
| 489 | g_midi_clock.nt = ROAR_MATH_OVERFLOW_ADD(g_pos, g_midi_clock.spt); |
---|
| 490 | |
---|
| 491 | return 0; |
---|
| 492 | } |
---|
| 493 | |
---|
| 494 | int midi_clock_tick (void) { |
---|
[1875] | 495 | struct roar_buffer * buf; |
---|
| 496 | struct midi_message * mes; |
---|
[1888] | 497 | struct roar_stream_server * ss; |
---|
[1851] | 498 | unsigned int diff; |
---|
| 499 | |
---|
[1888] | 500 | if ( streams_get(g_midi_clock.stream, &ss) == -1 ) { |
---|
| 501 | ROAR_ERR("midi_clock_tick(void): Something very BAD happend: can not get stream object of my own stream!"); |
---|
| 502 | g_midi_clock.stream = -1; |
---|
| 503 | ROAR_WARN("midi_clock_tick(void): Disabled MIDI Clock"); |
---|
| 504 | return -1; |
---|
| 505 | } |
---|
| 506 | |
---|
[1851] | 507 | while ( g_pos >= g_midi_clock.nt ) { |
---|
| 508 | diff = g_pos - g_midi_clock.nt; |
---|
[1860] | 509 | ROAR_DBG("midi_clock_tick(void): g_pos is %u samples (%5.2f%%) ahead of nt.", diff, (float)diff/g_midi_clock.spt); |
---|
[1851] | 510 | |
---|
| 511 | g_midi_clock.nt = ROAR_MATH_OVERFLOW_ADD(g_midi_clock.nt, g_midi_clock.spt); |
---|
| 512 | |
---|
| 513 | if ( streams_get_flag(g_midi_clock.stream, ROAR_FLAG_SYNC) ) { |
---|
[1860] | 514 | ROAR_DBG("midi_clock_tick(void): TICK! (nt=%lu)", g_midi_clock.nt); |
---|
[1875] | 515 | if ( midi_new_bufmes(&buf, &mes) == -1 ) { |
---|
| 516 | ROAR_ERR("midi_clock_tick(void): Can not create Clock-Tick-Message"); |
---|
| 517 | } |
---|
| 518 | |
---|
| 519 | mes->type = MIDI_TYPE_CLOCK_TICK; |
---|
| 520 | |
---|
| 521 | if ( midi_add_buf(g_midi_clock.stream, buf) == -1 ) { |
---|
| 522 | ROAR_ERR("midi_clock_tick(void): Can not add Clock-Tick-Message"); |
---|
| 523 | roar_buffer_free(buf); |
---|
| 524 | return -1; |
---|
| 525 | } |
---|
[1888] | 526 | |
---|
| 527 | ROAR_STREAM(ss)->pos = ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(ss)->pos, 1); |
---|
[1851] | 528 | } else { |
---|
[1860] | 529 | ROAR_DBG("midi_clock_tick(void): silent tick. (nt=%lu)", g_midi_clock.nt); |
---|
[1851] | 530 | } |
---|
| 531 | } |
---|
| 532 | |
---|
[1848] | 533 | return 0; |
---|
| 534 | } |
---|
| 535 | |
---|
[1845] | 536 | // CB: |
---|
[1843] | 537 | |
---|
| 538 | int midi_cb_init (void) { |
---|
[1751] | 539 | #ifdef _HAVE_CONSOLE |
---|
[1843] | 540 | struct roar_stream * s; |
---|
| 541 | struct roar_stream_server * ss; |
---|
[185] | 542 | int i; |
---|
| 543 | char * files[] = { |
---|
| 544 | "/dev/console", |
---|
| 545 | #ifdef __linux__ |
---|
| 546 | "/dev/tty0", |
---|
| 547 | "/dev/vc/0", |
---|
| 548 | #endif |
---|
| 549 | NULL |
---|
| 550 | }; |
---|
| 551 | |
---|
[1853] | 552 | g_midi_cb.console = -1; |
---|
| 553 | g_midi_cb.stream = -1; |
---|
| 554 | g_midi_cb.stoptime = 0; |
---|
| 555 | g_midi_cb.playing = 0; |
---|
[185] | 556 | |
---|
| 557 | for (i = 0; files[i] != NULL; i++) { |
---|
[1853] | 558 | if ( (g_midi_cb.console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 ) |
---|
[185] | 559 | break; |
---|
| 560 | } |
---|
| 561 | |
---|
[1853] | 562 | if ( g_midi_cb.console == -1 ) |
---|
[1843] | 563 | return -1; |
---|
| 564 | |
---|
[1853] | 565 | if ( (g_midi_cb.stream = streams_new()) == -1 ) { |
---|
[1843] | 566 | ROAR_WARN("Error while initializing MIDI subsystem component CB"); |
---|
| 567 | midi_cb_free(); |
---|
[187] | 568 | return -1; |
---|
| 569 | } |
---|
[1843] | 570 | |
---|
[1853] | 571 | midi_vio_set_dummy(g_midi_cb.stream); |
---|
[1848] | 572 | |
---|
[1853] | 573 | streams_get(g_midi_cb.stream, &ss); |
---|
[1843] | 574 | s = ROAR_STREAM(ss); |
---|
| 575 | |
---|
| 576 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 577 | |
---|
| 578 | s->pos_rel_id = -1; |
---|
| 579 | |
---|
| 580 | s->info.codec = 0; |
---|
| 581 | ss->codec_orgi = 0; |
---|
| 582 | |
---|
| 583 | s->info.channels = 1; |
---|
| 584 | s->info.rate = 1193180; |
---|
| 585 | s->info.bits = 8; |
---|
| 586 | |
---|
[1853] | 587 | if ( streams_set_dir(g_midi_cb.stream, ROAR_DIR_BRIDGE, 1) == -1 ) { |
---|
[1843] | 588 | ROAR_WARN("Error while initializing MIDI subsystem component CB"); |
---|
| 589 | midi_cb_free(); |
---|
| 590 | return -1; |
---|
| 591 | } |
---|
| 592 | |
---|
[1853] | 593 | streams_set_name(g_midi_cb.stream, "Console speaker bridge"); |
---|
[1843] | 594 | |
---|
[1853] | 595 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_OUTPUT); |
---|
| 596 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_PRIMARY); |
---|
| 597 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_HWMIXER); |
---|
[1885] | 598 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_MUTE); |
---|
[1843] | 599 | |
---|
| 600 | return 0; |
---|
[1485] | 601 | #else |
---|
[1853] | 602 | g_midi_cb.console = -1; |
---|
| 603 | g_midi_cb.stream = -1; |
---|
[1843] | 604 | |
---|
[1485] | 605 | return -1; |
---|
| 606 | #endif |
---|
[185] | 607 | } |
---|
| 608 | |
---|
[1843] | 609 | int midi_cb_free (void) { |
---|
[1751] | 610 | #ifdef _HAVE_CONSOLE |
---|
[1843] | 611 | |
---|
| 612 | midi_cb_stop(); |
---|
| 613 | |
---|
[1853] | 614 | if ( g_midi_cb.stream != -1 ) |
---|
| 615 | streams_delete(g_midi_cb.stream); |
---|
[1843] | 616 | |
---|
[1853] | 617 | if ( g_midi_cb.console != -1 ) |
---|
| 618 | close(g_midi_cb.console); |
---|
[1843] | 619 | |
---|
[185] | 620 | return 0; |
---|
[1485] | 621 | #else |
---|
| 622 | return -1; |
---|
| 623 | #endif |
---|
[185] | 624 | } |
---|
| 625 | |
---|
| 626 | int midi_cb_play(float t, float freq, int override) { |
---|
[191] | 627 | float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels; |
---|
| 628 | |
---|
[189] | 629 | /* |
---|
| 630 | #define MIDI_CB_NOOVERRIDE 0 |
---|
| 631 | #define MIDI_CB_OVERRIDE 1 |
---|
| 632 | */ |
---|
[1853] | 633 | if ( g_midi_cb.playing && override != MIDI_CB_OVERRIDE ) |
---|
[191] | 634 | return -1; |
---|
| 635 | |
---|
[1853] | 636 | g_midi_cb.stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t); |
---|
[191] | 637 | midi_cb_start(freq); |
---|
| 638 | |
---|
| 639 | return 0; |
---|
[185] | 640 | } |
---|
| 641 | |
---|
[190] | 642 | int midi_cb_update (void) { |
---|
[1884] | 643 | |
---|
| 644 | if ( !streams_get_flag(g_midi_cb.stream, ROAR_FLAG_MUTE) ) { |
---|
| 645 | if ( midi_cb_readbuf() == -1 ) |
---|
| 646 | return -1; |
---|
| 647 | } else if ( g_midi_cb.playing ) { |
---|
| 648 | midi_cb_stop(); |
---|
| 649 | } |
---|
[1882] | 650 | |
---|
[1853] | 651 | if ( !g_midi_cb.playing ) |
---|
[192] | 652 | return 0; |
---|
| 653 | |
---|
[1884] | 654 | /* |
---|
[1853] | 655 | if ( g_midi_cb.stoptime <= g_pos ) |
---|
[189] | 656 | midi_cb_stop(); |
---|
[1884] | 657 | */ |
---|
[189] | 658 | |
---|
[1882] | 659 | ROAR_DBG("midi_cb_update(void) = ?"); |
---|
| 660 | |
---|
[189] | 661 | return 0; |
---|
| 662 | } |
---|
| 663 | |
---|
[185] | 664 | int midi_cb_start(float freq) { |
---|
| 665 | // On linux this uses ioctl KIOCSOUND |
---|
[188] | 666 | #ifdef __linux__ |
---|
[1853] | 667 | if ( g_midi_cb.console == -1 ) |
---|
[188] | 668 | return -1; |
---|
| 669 | |
---|
[1853] | 670 | if ( ioctl(g_midi_cb.console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 ) |
---|
[188] | 671 | return -1; |
---|
| 672 | |
---|
[1884] | 673 | g_midi_cb.playing = 1; |
---|
| 674 | |
---|
[188] | 675 | return 0; |
---|
| 676 | #else |
---|
[185] | 677 | return -1; |
---|
[188] | 678 | #endif |
---|
[185] | 679 | } |
---|
| 680 | |
---|
| 681 | int midi_cb_stop (void) { |
---|
| 682 | #ifdef __linux__ |
---|
[1884] | 683 | int ret; |
---|
| 684 | |
---|
| 685 | ret = midi_cb_start(0); |
---|
[1853] | 686 | g_midi_cb.playing = 0; |
---|
[1884] | 687 | |
---|
| 688 | return ret; |
---|
[185] | 689 | #else |
---|
| 690 | return -1; |
---|
| 691 | #endif |
---|
| 692 | } |
---|
| 693 | |
---|
[1882] | 694 | int midi_cb_readbuf(void) { |
---|
| 695 | struct roar_buffer * buf = g_midi_mess.buf; |
---|
| 696 | struct midi_message * mes = NULL; |
---|
| 697 | |
---|
| 698 | ROAR_DBG("midi_cb_readbuf(void) = ?"); |
---|
| 699 | |
---|
| 700 | while (buf != NULL) { |
---|
| 701 | ROAR_DBG("midi_cb_readbuf(void): buf=%p", buf); |
---|
| 702 | |
---|
| 703 | if ( roar_buffer_get_data(buf, (void**)&mes) == -1 ) { |
---|
| 704 | return -1; |
---|
| 705 | } |
---|
| 706 | |
---|
| 707 | switch (mes->type) { |
---|
| 708 | case MIDI_TYPE_NOTE_ON: |
---|
| 709 | midi_cb_start(mes->d.note.freq); |
---|
| 710 | break; |
---|
| 711 | case MIDI_TYPE_NOTE_OFF: |
---|
| 712 | midi_cb_stop(); |
---|
| 713 | break; |
---|
[1890] | 714 | case MIDI_TYPE_CONTROLER: |
---|
[1894] | 715 | if ( mes->kk == MIDI_CCE_ALL_NOTE_OFF ) /* all note off */ |
---|
[1890] | 716 | midi_cb_stop(); |
---|
| 717 | break; |
---|
[1882] | 718 | } |
---|
| 719 | |
---|
| 720 | if ( roar_buffer_get_next(buf, &buf) == -1 ) |
---|
| 721 | buf = NULL; |
---|
| 722 | } |
---|
| 723 | |
---|
| 724 | return 0; |
---|
| 725 | } |
---|
| 726 | |
---|
[1848] | 727 | // VIO: |
---|
| 728 | |
---|
| 729 | int midi_vio_set_dummy(int stream) { |
---|
| 730 | struct roar_stream_server * ss; |
---|
| 731 | |
---|
| 732 | if ( streams_get(stream, &ss) == -1 ) |
---|
| 733 | return -1; |
---|
| 734 | |
---|
| 735 | ss->vio.read = NULL; |
---|
| 736 | ss->vio.write = NULL; |
---|
| 737 | ss->vio.lseek = NULL; |
---|
[1849] | 738 | ss->vio.nonblock = (int (*)(struct roar_vio_calls * vio, int state))midi_vio_ok; |
---|
| 739 | ss->vio.sync = (int (*)(struct roar_vio_calls * vio))midi_vio_ok; |
---|
[1848] | 740 | ss->vio.ctl = NULL; |
---|
[1849] | 741 | ss->vio.close = (int (*)(struct roar_vio_calls * vio))midi_vio_ok; |
---|
| 742 | |
---|
| 743 | return 0; |
---|
[1848] | 744 | } |
---|
| 745 | |
---|
| 746 | int midi_vio_ok(struct roar_vio_calls * vio, ...) { |
---|
| 747 | return 0; |
---|
| 748 | } |
---|
| 749 | |
---|
[169] | 750 | //ll |
---|