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