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