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