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