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