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