[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 | |
---|
[3764] | 159 | if ( roar_buffer_new_data(&b, MIDI_READ_SIZE, (void**)&buf) == -1 ) { |
---|
[1858] | 160 | ROAR_ERR("midi_check_stream(*): Can not alloc buffer space!"); |
---|
| 161 | ROAR_DBG("midi_check_stream(*) = -1"); |
---|
| 162 | return -1; |
---|
| 163 | } |
---|
| 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 | |
---|
[3764] | 480 | if ( roar_buffer_new_data(buf, sizeof(struct midi_message), (void**)mes) == -1 ) { |
---|
[1867] | 481 | *buf = (void*)(*mes = NULL); |
---|
[1863] | 482 | return -1; |
---|
| 483 | } |
---|
| 484 | |
---|
| 485 | memset((void*)*mes, 0, sizeof(struct midi_message)); |
---|
| 486 | |
---|
| 487 | (*mes)->type = MIDI_TYPE_NONE; |
---|
| 488 | |
---|
| 489 | return 0; |
---|
| 490 | } |
---|
| 491 | |
---|
[1866] | 492 | int midi_add_buf (int id, struct roar_buffer * buf) { |
---|
[1869] | 493 | struct midi_message * mes; |
---|
| 494 | |
---|
[1866] | 495 | if ( id == -1 || buf == NULL ) |
---|
| 496 | return -1; |
---|
| 497 | |
---|
[1872] | 498 | if ( roar_buffer_get_data(buf, (void**)&mes) == -1 ) |
---|
[1869] | 499 | return -1; |
---|
| 500 | |
---|
[1878] | 501 | ROAR_DBG("midi_add_buf(id=%i, buf=%p) = ?", id, buf); |
---|
| 502 | ROAR_DBG("midi_add_buf(*): MIDI Message of Type 0x%.2X", mes->type); |
---|
| 503 | ROAR_DBG("midi_add_buf(*): Channel: %i", mes->channel); |
---|
| 504 | ROAR_DBG("midi_add_buf(*): flags=0x%.2X", mes->flags); |
---|
| 505 | ROAR_DBG("midi_add_buf(*): kk=0x%.2X, vv=0x%.2X", mes->kk, mes->vv); |
---|
[1869] | 506 | |
---|
[1866] | 507 | if ( g_midi_mess.buf == NULL ) { |
---|
| 508 | g_midi_mess.buf = buf; |
---|
| 509 | } else { |
---|
| 510 | roar_buffer_add(g_midi_mess.buf, buf); |
---|
| 511 | } |
---|
| 512 | |
---|
| 513 | return 0; |
---|
| 514 | } |
---|
| 515 | |
---|
[1848] | 516 | // bridges: |
---|
| 517 | int midi_check_bridge (int id) { |
---|
[1851] | 518 | |
---|
[1860] | 519 | ROAR_DBG("midi_check_bridge(id=%i) = ?", id); |
---|
[1851] | 520 | |
---|
| 521 | if ( id == g_midi_clock.stream ) { |
---|
| 522 | midi_clock_tick(); |
---|
| 523 | |
---|
| 524 | return 0; |
---|
| 525 | } |
---|
| 526 | |
---|
[1848] | 527 | return -1; |
---|
| 528 | } |
---|
| 529 | |
---|
| 530 | // clock: |
---|
| 531 | |
---|
| 532 | int midi_clock_init (void) { |
---|
| 533 | struct roar_stream * s; |
---|
| 534 | struct roar_stream_server * ss; |
---|
| 535 | |
---|
[1850] | 536 | if ( (g_midi_clock.stream = streams_new()) == -1 ) { |
---|
[1848] | 537 | ROAR_WARN("Error while initializing MIDI subsystem component clock"); |
---|
| 538 | return -1; |
---|
| 539 | } |
---|
| 540 | |
---|
[2915] | 541 | client_stream_add(g_self_client, g_midi_clock.stream); |
---|
| 542 | |
---|
[1850] | 543 | midi_vio_set_dummy(g_midi_clock.stream); |
---|
[1848] | 544 | |
---|
[1850] | 545 | streams_get(g_midi_clock.stream, &ss); |
---|
[1848] | 546 | s = ROAR_STREAM(ss); |
---|
| 547 | |
---|
| 548 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 549 | |
---|
[1850] | 550 | s->pos_rel_id = g_midi_clock.stream; |
---|
[1848] | 551 | |
---|
| 552 | s->info.codec = ROAR_CODEC_MIDI; |
---|
| 553 | ss->codec_orgi = ROAR_CODEC_MIDI; |
---|
| 554 | |
---|
[2401] | 555 | s->info.channels = 1; // we have only one channel, ticking on channel 0 |
---|
[2400] | 556 | s->info.rate = ROAR_MIDI_TICKS_PER_BEAT; // one beat per sec |
---|
| 557 | s->info.bits = ROAR_MIDI_BITS; |
---|
[1848] | 558 | |
---|
[1850] | 559 | if ( streams_set_dir(g_midi_clock.stream, ROAR_DIR_BRIDGE, 1) == -1 ) { |
---|
[1848] | 560 | ROAR_WARN("Error while initializing MIDI subsystem component clock"); |
---|
| 561 | return -1; |
---|
| 562 | } |
---|
| 563 | |
---|
[1850] | 564 | streams_set_name(g_midi_clock.stream, "MIDI Clock"); |
---|
[1848] | 565 | |
---|
[1850] | 566 | streams_set_flag(g_midi_clock.stream, ROAR_FLAG_PRIMARY); |
---|
| 567 | streams_set_flag(g_midi_clock.stream, ROAR_FLAG_SYNC); |
---|
[1848] | 568 | |
---|
[2400] | 569 | midi_clock_set_bph(3600); // one beat per sec |
---|
[1851] | 570 | |
---|
[2943] | 571 | ss->state = ROAR_STREAMSTATE_NEW; |
---|
| 572 | |
---|
[1924] | 573 | midi_config.inited |= MIDI_INITED_CLOCK; |
---|
| 574 | |
---|
[1851] | 575 | return 0; |
---|
| 576 | } |
---|
| 577 | |
---|
| 578 | int midi_clock_set_bph (uint_least32_t bph) { |
---|
| 579 | uint_least32_t sph = g_sa->rate/2 * 75 * g_sa->channels; // samples per houre |
---|
| 580 | |
---|
| 581 | g_midi_clock.bph = bph; |
---|
| 582 | |
---|
| 583 | g_midi_clock.spt = sph/bph; |
---|
| 584 | |
---|
| 585 | g_midi_clock.nt = ROAR_MATH_OVERFLOW_ADD(g_pos, g_midi_clock.spt); |
---|
| 586 | |
---|
| 587 | return 0; |
---|
| 588 | } |
---|
| 589 | |
---|
| 590 | int midi_clock_tick (void) { |
---|
[1875] | 591 | struct roar_buffer * buf; |
---|
| 592 | struct midi_message * mes; |
---|
[1888] | 593 | struct roar_stream_server * ss; |
---|
[1851] | 594 | unsigned int diff; |
---|
| 595 | |
---|
[1888] | 596 | if ( streams_get(g_midi_clock.stream, &ss) == -1 ) { |
---|
| 597 | ROAR_ERR("midi_clock_tick(void): Something very BAD happend: can not get stream object of my own stream!"); |
---|
| 598 | g_midi_clock.stream = -1; |
---|
| 599 | ROAR_WARN("midi_clock_tick(void): Disabled MIDI Clock"); |
---|
| 600 | return -1; |
---|
| 601 | } |
---|
| 602 | |
---|
[1851] | 603 | while ( g_pos >= g_midi_clock.nt ) { |
---|
| 604 | diff = g_pos - g_midi_clock.nt; |
---|
[1911] | 605 | ROAR_DBG("midi_clock_tick(void): g_pos is %u samples (%5.2f%%) after of nt.", diff, (float)diff/g_midi_clock.spt); |
---|
[1851] | 606 | |
---|
| 607 | g_midi_clock.nt = ROAR_MATH_OVERFLOW_ADD(g_midi_clock.nt, g_midi_clock.spt); |
---|
| 608 | |
---|
| 609 | if ( streams_get_flag(g_midi_clock.stream, ROAR_FLAG_SYNC) ) { |
---|
[1860] | 610 | ROAR_DBG("midi_clock_tick(void): TICK! (nt=%lu)", g_midi_clock.nt); |
---|
[1875] | 611 | if ( midi_new_bufmes(&buf, &mes) == -1 ) { |
---|
| 612 | ROAR_ERR("midi_clock_tick(void): Can not create Clock-Tick-Message"); |
---|
| 613 | } |
---|
| 614 | |
---|
| 615 | mes->type = MIDI_TYPE_CLOCK_TICK; |
---|
| 616 | |
---|
| 617 | if ( midi_add_buf(g_midi_clock.stream, buf) == -1 ) { |
---|
| 618 | ROAR_ERR("midi_clock_tick(void): Can not add Clock-Tick-Message"); |
---|
| 619 | roar_buffer_free(buf); |
---|
| 620 | return -1; |
---|
| 621 | } |
---|
[1888] | 622 | |
---|
| 623 | ROAR_STREAM(ss)->pos = ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(ss)->pos, 1); |
---|
[2943] | 624 | ss->state = ROAR_STREAMSTATE_OLD; |
---|
[1851] | 625 | } else { |
---|
[1860] | 626 | ROAR_DBG("midi_clock_tick(void): silent tick. (nt=%lu)", g_midi_clock.nt); |
---|
[1851] | 627 | } |
---|
| 628 | } |
---|
| 629 | |
---|
[1848] | 630 | return 0; |
---|
| 631 | } |
---|
| 632 | |
---|
[1845] | 633 | // CB: |
---|
[1843] | 634 | |
---|
[2487] | 635 | #ifndef ROAR_WITHOUT_DCOMP_CB |
---|
[1843] | 636 | int midi_cb_init (void) { |
---|
[1751] | 637 | #ifdef _HAVE_CONSOLE |
---|
[1843] | 638 | struct roar_stream * s; |
---|
| 639 | struct roar_stream_server * ss; |
---|
[185] | 640 | int i; |
---|
| 641 | char * files[] = { |
---|
[1924] | 642 | "/NX", |
---|
| 643 | "/dev/roarconsole", |
---|
[185] | 644 | "/dev/console", |
---|
| 645 | #ifdef __linux__ |
---|
| 646 | "/dev/tty0", |
---|
| 647 | "/dev/vc/0", |
---|
| 648 | #endif |
---|
| 649 | NULL |
---|
| 650 | }; |
---|
| 651 | |
---|
[1853] | 652 | g_midi_cb.console = -1; |
---|
| 653 | g_midi_cb.stream = -1; |
---|
| 654 | g_midi_cb.stoptime = 0; |
---|
| 655 | g_midi_cb.playing = 0; |
---|
[185] | 656 | |
---|
[1924] | 657 | if ( midi_config.console_dev != NULL ) { |
---|
| 658 | files[0] = midi_config.console_dev; |
---|
| 659 | } |
---|
| 660 | |
---|
[185] | 661 | for (i = 0; files[i] != NULL; i++) { |
---|
[1853] | 662 | if ( (g_midi_cb.console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 ) |
---|
[185] | 663 | break; |
---|
| 664 | } |
---|
| 665 | |
---|
[1853] | 666 | if ( g_midi_cb.console == -1 ) |
---|
[1843] | 667 | return -1; |
---|
| 668 | |
---|
[1853] | 669 | if ( (g_midi_cb.stream = streams_new()) == -1 ) { |
---|
[1843] | 670 | ROAR_WARN("Error while initializing MIDI subsystem component CB"); |
---|
| 671 | midi_cb_free(); |
---|
[187] | 672 | return -1; |
---|
| 673 | } |
---|
[1843] | 674 | |
---|
[2915] | 675 | client_stream_add(g_self_client, g_midi_clock.stream); |
---|
| 676 | |
---|
[1853] | 677 | midi_vio_set_dummy(g_midi_cb.stream); |
---|
[1848] | 678 | |
---|
[1853] | 679 | streams_get(g_midi_cb.stream, &ss); |
---|
[1843] | 680 | s = ROAR_STREAM(ss); |
---|
| 681 | |
---|
| 682 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 683 | |
---|
| 684 | s->pos_rel_id = -1; |
---|
| 685 | |
---|
| 686 | s->info.codec = 0; |
---|
| 687 | ss->codec_orgi = 0; |
---|
| 688 | |
---|
| 689 | s->info.channels = 1; |
---|
| 690 | s->info.rate = 1193180; |
---|
| 691 | s->info.bits = 8; |
---|
| 692 | |
---|
[1853] | 693 | if ( streams_set_dir(g_midi_cb.stream, ROAR_DIR_BRIDGE, 1) == -1 ) { |
---|
[1843] | 694 | ROAR_WARN("Error while initializing MIDI subsystem component CB"); |
---|
| 695 | midi_cb_free(); |
---|
| 696 | return -1; |
---|
| 697 | } |
---|
| 698 | |
---|
[1853] | 699 | streams_set_name(g_midi_cb.stream, "Console speaker bridge"); |
---|
[1843] | 700 | |
---|
[1853] | 701 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_OUTPUT); |
---|
| 702 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_PRIMARY); |
---|
| 703 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_HWMIXER); |
---|
[1885] | 704 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_MUTE); |
---|
[1843] | 705 | |
---|
[1924] | 706 | midi_config.inited |= MIDI_INITED_CB; |
---|
| 707 | |
---|
[1843] | 708 | return 0; |
---|
[1485] | 709 | #else |
---|
[1853] | 710 | g_midi_cb.console = -1; |
---|
| 711 | g_midi_cb.stream = -1; |
---|
[1843] | 712 | |
---|
[1485] | 713 | return -1; |
---|
| 714 | #endif |
---|
[185] | 715 | } |
---|
| 716 | |
---|
[1843] | 717 | int midi_cb_free (void) { |
---|
[1751] | 718 | #ifdef _HAVE_CONSOLE |
---|
[1843] | 719 | |
---|
| 720 | midi_cb_stop(); |
---|
| 721 | |
---|
[1853] | 722 | if ( g_midi_cb.stream != -1 ) |
---|
| 723 | streams_delete(g_midi_cb.stream); |
---|
[1843] | 724 | |
---|
[1853] | 725 | if ( g_midi_cb.console != -1 ) |
---|
| 726 | close(g_midi_cb.console); |
---|
[1843] | 727 | |
---|
[185] | 728 | return 0; |
---|
[1485] | 729 | #else |
---|
| 730 | return -1; |
---|
| 731 | #endif |
---|
[185] | 732 | } |
---|
| 733 | |
---|
| 734 | int midi_cb_play(float t, float freq, int override) { |
---|
[191] | 735 | float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels; |
---|
| 736 | |
---|
[189] | 737 | /* |
---|
| 738 | #define MIDI_CB_NOOVERRIDE 0 |
---|
| 739 | #define MIDI_CB_OVERRIDE 1 |
---|
| 740 | */ |
---|
[1853] | 741 | if ( g_midi_cb.playing && override != MIDI_CB_OVERRIDE ) |
---|
[191] | 742 | return -1; |
---|
| 743 | |
---|
[1853] | 744 | g_midi_cb.stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t); |
---|
[191] | 745 | midi_cb_start(freq); |
---|
| 746 | |
---|
| 747 | return 0; |
---|
[185] | 748 | } |
---|
| 749 | |
---|
[190] | 750 | int midi_cb_update (void) { |
---|
[1884] | 751 | |
---|
| 752 | if ( !streams_get_flag(g_midi_cb.stream, ROAR_FLAG_MUTE) ) { |
---|
| 753 | if ( midi_cb_readbuf() == -1 ) |
---|
| 754 | return -1; |
---|
| 755 | } else if ( g_midi_cb.playing ) { |
---|
| 756 | midi_cb_stop(); |
---|
| 757 | } |
---|
[1882] | 758 | |
---|
[1853] | 759 | if ( !g_midi_cb.playing ) |
---|
[192] | 760 | return 0; |
---|
| 761 | |
---|
[1884] | 762 | /* |
---|
[1853] | 763 | if ( g_midi_cb.stoptime <= g_pos ) |
---|
[189] | 764 | midi_cb_stop(); |
---|
[1884] | 765 | */ |
---|
[189] | 766 | |
---|
[1882] | 767 | ROAR_DBG("midi_cb_update(void) = ?"); |
---|
| 768 | |
---|
[189] | 769 | return 0; |
---|
| 770 | } |
---|
| 771 | |
---|
[185] | 772 | int midi_cb_start(float freq) { |
---|
| 773 | // On linux this uses ioctl KIOCSOUND |
---|
[188] | 774 | #ifdef __linux__ |
---|
[1853] | 775 | if ( g_midi_cb.console == -1 ) |
---|
[188] | 776 | return -1; |
---|
| 777 | |
---|
[1853] | 778 | if ( ioctl(g_midi_cb.console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 ) |
---|
[188] | 779 | return -1; |
---|
| 780 | |
---|
[1884] | 781 | g_midi_cb.playing = 1; |
---|
| 782 | |
---|
[188] | 783 | return 0; |
---|
| 784 | #else |
---|
[185] | 785 | return -1; |
---|
[188] | 786 | #endif |
---|
[185] | 787 | } |
---|
| 788 | |
---|
| 789 | int midi_cb_stop (void) { |
---|
| 790 | #ifdef __linux__ |
---|
[1884] | 791 | int ret; |
---|
| 792 | |
---|
| 793 | ret = midi_cb_start(0); |
---|
[1853] | 794 | g_midi_cb.playing = 0; |
---|
[1884] | 795 | |
---|
| 796 | return ret; |
---|
[185] | 797 | #else |
---|
| 798 | return -1; |
---|
| 799 | #endif |
---|
| 800 | } |
---|
| 801 | |
---|
[1882] | 802 | int midi_cb_readbuf(void) { |
---|
| 803 | struct roar_buffer * buf = g_midi_mess.buf; |
---|
| 804 | struct midi_message * mes = NULL; |
---|
| 805 | |
---|
| 806 | ROAR_DBG("midi_cb_readbuf(void) = ?"); |
---|
| 807 | |
---|
| 808 | while (buf != NULL) { |
---|
| 809 | ROAR_DBG("midi_cb_readbuf(void): buf=%p", buf); |
---|
| 810 | |
---|
| 811 | if ( roar_buffer_get_data(buf, (void**)&mes) == -1 ) { |
---|
| 812 | return -1; |
---|
| 813 | } |
---|
| 814 | |
---|
| 815 | switch (mes->type) { |
---|
| 816 | case MIDI_TYPE_NOTE_ON: |
---|
| 817 | midi_cb_start(mes->d.note.freq); |
---|
| 818 | break; |
---|
| 819 | case MIDI_TYPE_NOTE_OFF: |
---|
| 820 | midi_cb_stop(); |
---|
| 821 | break; |
---|
[1890] | 822 | case MIDI_TYPE_CONTROLER: |
---|
[1894] | 823 | if ( mes->kk == MIDI_CCE_ALL_NOTE_OFF ) /* all note off */ |
---|
[1890] | 824 | midi_cb_stop(); |
---|
| 825 | break; |
---|
[1882] | 826 | } |
---|
| 827 | |
---|
| 828 | if ( roar_buffer_get_next(buf, &buf) == -1 ) |
---|
| 829 | buf = NULL; |
---|
| 830 | } |
---|
| 831 | |
---|
| 832 | return 0; |
---|
| 833 | } |
---|
[2487] | 834 | #endif |
---|
[1882] | 835 | |
---|
[1848] | 836 | // VIO: |
---|
| 837 | |
---|
| 838 | int midi_vio_set_dummy(int stream) { |
---|
| 839 | struct roar_stream_server * ss; |
---|
| 840 | |
---|
| 841 | if ( streams_get(stream, &ss) == -1 ) |
---|
| 842 | return -1; |
---|
| 843 | |
---|
| 844 | ss->vio.read = NULL; |
---|
| 845 | ss->vio.write = NULL; |
---|
| 846 | ss->vio.lseek = NULL; |
---|
[1849] | 847 | ss->vio.nonblock = (int (*)(struct roar_vio_calls * vio, int state))midi_vio_ok; |
---|
| 848 | ss->vio.sync = (int (*)(struct roar_vio_calls * vio))midi_vio_ok; |
---|
[1848] | 849 | ss->vio.ctl = NULL; |
---|
[1849] | 850 | ss->vio.close = (int (*)(struct roar_vio_calls * vio))midi_vio_ok; |
---|
| 851 | |
---|
| 852 | return 0; |
---|
[1848] | 853 | } |
---|
| 854 | |
---|
| 855 | int midi_vio_ok(struct roar_vio_calls * vio, ...) { |
---|
| 856 | return 0; |
---|
| 857 | } |
---|
| 858 | |
---|
[2500] | 859 | #endif |
---|
| 860 | |
---|
[169] | 861 | //ll |
---|