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