[169] | 1 | //midi.c: |
---|
| 2 | |
---|
[668] | 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
| 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 |
---|
| 21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 22 | * |
---|
| 23 | */ |
---|
| 24 | |
---|
[169] | 25 | #include "roard.h" |
---|
| 26 | |
---|
[1751] | 27 | #if defined(ROAR_HAVE_IO_POSIX) && !defined(ROAR_TARGET_WIN32) |
---|
| 28 | #define _HAVE_CONSOLE |
---|
| 29 | #endif |
---|
| 30 | |
---|
[185] | 31 | int midi_init (void) { |
---|
[1843] | 32 | |
---|
| 33 | if ( midi_cb_init() == -1 ) { |
---|
| 34 | ROAR_WARN("Can not initialize MIDI subsystem component CB"); |
---|
| 35 | } |
---|
| 36 | |
---|
[1848] | 37 | if ( midi_clock_init() == -1 ) { |
---|
| 38 | ROAR_WARN("Can not initialize MIDI subsystem component clock"); |
---|
| 39 | } |
---|
| 40 | |
---|
[1863] | 41 | g_midi_mess.buf = NULL; |
---|
| 42 | |
---|
[1843] | 43 | return 0; |
---|
| 44 | } |
---|
[1848] | 45 | |
---|
[1843] | 46 | int midi_free (void) { |
---|
| 47 | return midi_cb_free(); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | int midi_update(void) { |
---|
[1848] | 51 | |
---|
[1850] | 52 | if ( g_midi_clock.stream != -1 ) |
---|
| 53 | midi_check_bridge(g_midi_clock.stream); |
---|
[1848] | 54 | |
---|
[1843] | 55 | return midi_cb_update(); |
---|
| 56 | } |
---|
| 57 | |
---|
[1855] | 58 | int midi_reinit(void) { |
---|
[1868] | 59 | |
---|
| 60 | if ( g_midi_mess.buf != NULL ) |
---|
| 61 | roar_buffer_free(g_midi_mess.buf); |
---|
| 62 | |
---|
| 63 | g_midi_mess.buf = NULL; |
---|
| 64 | |
---|
[1855] | 65 | return 0; |
---|
| 66 | } |
---|
| 67 | |
---|
[1845] | 68 | // STREAMS: |
---|
| 69 | |
---|
| 70 | int midi_check_stream (int id) { |
---|
[1846] | 71 | struct roar_stream * s; |
---|
| 72 | struct roar_stream_server * ss; |
---|
[1858] | 73 | struct roar_buffer * b; |
---|
| 74 | char * buf; |
---|
| 75 | ssize_t len; |
---|
[1859] | 76 | int i; |
---|
[1846] | 77 | |
---|
| 78 | if ( g_streams[id] == NULL ) |
---|
| 79 | return -1; |
---|
| 80 | |
---|
| 81 | ROAR_DBG("midi_check_stream(id=%i) = ?", id); |
---|
| 82 | |
---|
| 83 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
| 84 | |
---|
[1848] | 85 | if ( s->dir == ROAR_DIR_BRIDGE ) |
---|
| 86 | return midi_check_bridge(id); |
---|
| 87 | |
---|
[1846] | 88 | switch (s->info.codec) { |
---|
[1858] | 89 | case ROAR_CODEC_MIDI: |
---|
| 90 | break; |
---|
| 91 | default: |
---|
| 92 | streams_delete(id); |
---|
| 93 | return -1; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | if ( roar_buffer_new(&b, MIDI_READ_SIZE) == -1 ) { |
---|
| 97 | ROAR_ERR("midi_check_stream(*): Can not alloc buffer space!"); |
---|
| 98 | ROAR_DBG("midi_check_stream(*) = -1"); |
---|
| 99 | return -1; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | roar_buffer_get_data(b, (void **)&buf); |
---|
| 103 | |
---|
| 104 | if ( (len = stream_vio_s_read(ss, buf, MIDI_READ_SIZE)) < 1 ) { |
---|
| 105 | streams_delete(id); |
---|
[1862] | 106 | roar_buffer_free(b); |
---|
[1858] | 107 | return -1; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | roar_buffer_set_len(b, len); |
---|
| 111 | |
---|
[1859] | 112 | if ( stream_add_buffer(id, b) == -1 ) { |
---|
| 113 | roar_buffer_free(b); |
---|
| 114 | streams_delete(id); |
---|
| 115 | return -1; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 119 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) { |
---|
| 120 | if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) { |
---|
| 121 | if ( stream_vio_write(i, buf, len) != len ) { |
---|
| 122 | streams_delete(i); |
---|
| 123 | } |
---|
| 124 | } |
---|
[1858] | 125 | } |
---|
[1859] | 126 | } |
---|
[1858] | 127 | |
---|
| 128 | switch (s->info.codec) { |
---|
| 129 | case ROAR_CODEC_MIDI: |
---|
| 130 | return midi_conv_midi2mes(id); |
---|
| 131 | break; |
---|
[1846] | 132 | default: |
---|
| 133 | streams_delete(id); |
---|
| 134 | return -1; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | return 0; |
---|
[1845] | 138 | } |
---|
[1846] | 139 | |
---|
[1845] | 140 | int midi_send_stream (int id) { |
---|
[1846] | 141 | struct roar_stream * s; |
---|
| 142 | struct roar_stream_server * ss; |
---|
| 143 | |
---|
| 144 | if ( g_streams[id] == NULL ) |
---|
| 145 | return -1; |
---|
| 146 | |
---|
| 147 | ROAR_DBG("midi_send_stream(id=%i) = ?", id); |
---|
| 148 | |
---|
| 149 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
| 150 | |
---|
| 151 | switch (s->info.codec) { |
---|
| 152 | default: |
---|
| 153 | streams_delete(id); |
---|
| 154 | return -1; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | return 0; |
---|
[1845] | 158 | } |
---|
| 159 | |
---|
[1858] | 160 | int midi_conv_midi2mes (int id) { |
---|
[1863] | 161 | unsigned char * data; |
---|
| 162 | struct roar_stream * s; |
---|
| 163 | struct roar_stream_server * ss; |
---|
| 164 | struct roar_buffer * buf = NULL; |
---|
| 165 | struct midi_message * mes = NULL; |
---|
| 166 | size_t need = 0, have = 0; |
---|
| 167 | int alive = 1; |
---|
| 168 | |
---|
| 169 | if ( g_streams[id] == NULL ) |
---|
| 170 | return -1; |
---|
| 171 | |
---|
| 172 | ROAR_DBG("midi_conv_midi2mes(id=%i) = ?", id); |
---|
| 173 | |
---|
| 174 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
| 175 | |
---|
| 176 | while (ss->buffer != NULL && alive) { |
---|
| 177 | if ( roar_buffer_get_data(ss->buffer, (void**)&data) == -1 ) { |
---|
| 178 | alive = 0; |
---|
| 179 | continue; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | if ( roar_buffer_get_len(ss->buffer, &have) == -1 ) { |
---|
| 183 | alive = 0; |
---|
| 184 | continue; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | while (have && alive) { |
---|
| 188 | printf("%.2x=", *data); |
---|
| 189 | if ( *data & 0x80 ) { |
---|
[1865] | 190 | if ( buf != NULL ) { |
---|
[1866] | 191 | midi_add_buf(id, buf); |
---|
[1865] | 192 | buf = NULL; |
---|
| 193 | } |
---|
| 194 | |
---|
[1863] | 195 | need = 0; |
---|
| 196 | printf("S\n"); |
---|
| 197 | if ( midi_new_bufmes(&buf, &mes) == -1 ) { |
---|
| 198 | alive = 0; |
---|
| 199 | continue; |
---|
| 200 | } |
---|
[1865] | 201 | |
---|
| 202 | if (*data == MIDI_TYPE_CLOCK_TICK || *data == MIDI_TYPE_CLOCK_START || *data == MIDI_TYPE_CLOCK_STOP ) { |
---|
| 203 | mes->type = *data; |
---|
| 204 | } else { |
---|
| 205 | mes->type = *data & 0xF0; |
---|
| 206 | mes->channel = *data & 0x0F; |
---|
| 207 | switch (*data & 0xF0) { |
---|
| 208 | case MIDI_TYPE_NOTE_ON: |
---|
| 209 | case MIDI_TYPE_NOTE_OFF: |
---|
| 210 | need = 2; |
---|
| 211 | break; |
---|
| 212 | case MIDI_TYPE_PA: |
---|
| 213 | need = 2; |
---|
| 214 | break; |
---|
| 215 | case MIDI_TYPE_CONTROLER: |
---|
| 216 | need = 2; |
---|
| 217 | break; |
---|
| 218 | case MIDI_TYPE_PROGRAM: |
---|
| 219 | need = 1; |
---|
| 220 | break; |
---|
| 221 | case MIDI_TYPE_MA: |
---|
| 222 | need = 1; |
---|
| 223 | break; |
---|
| 224 | case MIDI_TYPE_PB: |
---|
| 225 | case MIDI_TYPE_SYSEX: |
---|
| 226 | need = 1; |
---|
| 227 | break; |
---|
| 228 | } |
---|
| 229 | } |
---|
| 230 | |
---|
[1863] | 231 | } else { |
---|
| 232 | printf("D\n"); |
---|
[1871] | 233 | |
---|
| 234 | if ( mes == NULL ) { |
---|
| 235 | ROAR_WARN("midi_conv_midi2mes(id=%i): Lost sync.", id); |
---|
| 236 | data++; |
---|
| 237 | have--; |
---|
| 238 | continue; |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | switch (mes->type) { |
---|
[1873] | 242 | case MIDI_TYPE_NOTE_ON: |
---|
| 243 | case MIDI_TYPE_NOTE_OFF: |
---|
| 244 | if ( need == 2 ) { |
---|
| 245 | mes->kk = *data; |
---|
| 246 | } else { |
---|
| 247 | mes->vv = *data; |
---|
| 248 | } |
---|
| 249 | break; |
---|
| 250 | case MIDI_TYPE_PA: |
---|
| 251 | case MIDI_TYPE_CONTROLER: |
---|
| 252 | case MIDI_TYPE_PROGRAM: |
---|
| 253 | case MIDI_TYPE_MA: |
---|
| 254 | case MIDI_TYPE_PB: |
---|
| 255 | case MIDI_TYPE_SYSEX: |
---|
| 256 | break; |
---|
[1871] | 257 | } |
---|
| 258 | |
---|
[1863] | 259 | if ( need ) |
---|
| 260 | need--; |
---|
| 261 | } |
---|
| 262 | data++; |
---|
| 263 | have--; |
---|
| 264 | } |
---|
| 265 | |
---|
[1865] | 266 | if ( need ) { |
---|
| 267 | ROAR_ERR("midi_conv_midi2mes(*): FIXME: BUG!!! Need to restore buffer here with corrected length"); |
---|
| 268 | } else if ( !have ) { |
---|
[1863] | 269 | roar_buffer_next(&(ss->buffer)); |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | if ( need && buf != NULL ) { |
---|
| 273 | roar_buffer_free(buf); |
---|
| 274 | buf = NULL; |
---|
| 275 | } else if ( buf != NULL ) { |
---|
[1866] | 276 | midi_add_buf(id, buf); |
---|
[1863] | 277 | buf = NULL; |
---|
| 278 | } |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | return 0; |
---|
[1858] | 282 | } |
---|
| 283 | |
---|
| 284 | int midi_conv_mes2midi (int id) { |
---|
| 285 | return -1; |
---|
| 286 | } |
---|
| 287 | |
---|
[1863] | 288 | int midi_new_bufmes (struct roar_buffer ** buf, struct midi_message ** mes) { |
---|
| 289 | if ( buf == NULL || mes == NULL ) |
---|
| 290 | return -1; |
---|
| 291 | |
---|
[1867] | 292 | *buf = (void*)(*mes = NULL); |
---|
[1863] | 293 | |
---|
| 294 | if ( roar_buffer_new(buf, sizeof(struct midi_message)) == -1 ) |
---|
| 295 | return -1; |
---|
| 296 | |
---|
| 297 | if ( roar_buffer_get_data(*buf, (void**)mes) == -1 ) { |
---|
| 298 | roar_buffer_free(*buf); |
---|
[1867] | 299 | *buf = (void*)(*mes = NULL); |
---|
[1863] | 300 | return -1; |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | memset((void*)*mes, 0, sizeof(struct midi_message)); |
---|
| 304 | |
---|
| 305 | (*mes)->type = MIDI_TYPE_NONE; |
---|
| 306 | |
---|
| 307 | return 0; |
---|
| 308 | } |
---|
| 309 | |
---|
[1866] | 310 | int midi_add_buf (int id, struct roar_buffer * buf) { |
---|
[1869] | 311 | struct midi_message * mes; |
---|
| 312 | |
---|
[1866] | 313 | if ( id == -1 || buf == NULL ) |
---|
| 314 | return -1; |
---|
| 315 | |
---|
[1872] | 316 | if ( roar_buffer_get_data(buf, (void**)&mes) == -1 ) |
---|
[1869] | 317 | return -1; |
---|
| 318 | |
---|
| 319 | ROAR_WARN("midi_add_buf(id=%i, buf=%p) = ?", id, buf); |
---|
[1870] | 320 | ROAR_WARN("midi_add_buf(*): MIDI Message of Type 0x%.2X", mes->type); |
---|
[1869] | 321 | ROAR_WARN("midi_add_buf(*): Channel: %i", mes->channel); |
---|
[1870] | 322 | ROAR_WARN("midi_add_buf(*): flags=0x%.2X", mes->flags); |
---|
| 323 | ROAR_WARN("midi_add_buf(*): kk=0x%.2X, vv=0x%.2X", mes->kk, mes->vv); |
---|
[1869] | 324 | |
---|
[1866] | 325 | if ( g_midi_mess.buf == NULL ) { |
---|
| 326 | g_midi_mess.buf = buf; |
---|
| 327 | } else { |
---|
| 328 | roar_buffer_add(g_midi_mess.buf, buf); |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | return 0; |
---|
| 332 | } |
---|
| 333 | |
---|
[1848] | 334 | // bridges: |
---|
| 335 | int midi_check_bridge (int id) { |
---|
[1851] | 336 | |
---|
[1860] | 337 | ROAR_DBG("midi_check_bridge(id=%i) = ?", id); |
---|
[1851] | 338 | |
---|
| 339 | if ( id == g_midi_clock.stream ) { |
---|
| 340 | midi_clock_tick(); |
---|
| 341 | |
---|
| 342 | return 0; |
---|
| 343 | } |
---|
| 344 | |
---|
[1848] | 345 | return -1; |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | // clock: |
---|
| 349 | |
---|
| 350 | int midi_clock_init (void) { |
---|
| 351 | struct roar_stream * s; |
---|
| 352 | struct roar_stream_server * ss; |
---|
| 353 | |
---|
[1850] | 354 | if ( (g_midi_clock.stream = streams_new()) == -1 ) { |
---|
[1848] | 355 | ROAR_WARN("Error while initializing MIDI subsystem component clock"); |
---|
| 356 | return -1; |
---|
| 357 | } |
---|
| 358 | |
---|
[1850] | 359 | midi_vio_set_dummy(g_midi_clock.stream); |
---|
[1848] | 360 | |
---|
[1850] | 361 | streams_get(g_midi_clock.stream, &ss); |
---|
[1848] | 362 | s = ROAR_STREAM(ss); |
---|
| 363 | |
---|
| 364 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 365 | |
---|
[1850] | 366 | s->pos_rel_id = g_midi_clock.stream; |
---|
[1848] | 367 | |
---|
| 368 | s->info.codec = ROAR_CODEC_MIDI; |
---|
| 369 | ss->codec_orgi = ROAR_CODEC_MIDI; |
---|
| 370 | |
---|
| 371 | s->info.channels = 0; |
---|
| 372 | s->info.rate = MIDI_RATE; |
---|
| 373 | s->info.bits = 8; |
---|
| 374 | |
---|
[1850] | 375 | if ( streams_set_dir(g_midi_clock.stream, ROAR_DIR_BRIDGE, 1) == -1 ) { |
---|
[1848] | 376 | ROAR_WARN("Error while initializing MIDI subsystem component clock"); |
---|
| 377 | return -1; |
---|
| 378 | } |
---|
| 379 | |
---|
[1850] | 380 | streams_set_name(g_midi_clock.stream, "MIDI Clock"); |
---|
[1848] | 381 | |
---|
[1850] | 382 | streams_set_flag(g_midi_clock.stream, ROAR_FLAG_PRIMARY); |
---|
| 383 | streams_set_flag(g_midi_clock.stream, ROAR_FLAG_SYNC); |
---|
[1848] | 384 | |
---|
[1851] | 385 | midi_clock_set_bph(3600); // one tick per sec |
---|
| 386 | |
---|
| 387 | return 0; |
---|
| 388 | } |
---|
| 389 | |
---|
| 390 | int midi_clock_set_bph (uint_least32_t bph) { |
---|
| 391 | uint_least32_t sph = g_sa->rate/2 * 75 * g_sa->channels; // samples per houre |
---|
| 392 | |
---|
| 393 | g_midi_clock.bph = bph; |
---|
| 394 | |
---|
| 395 | g_midi_clock.spt = sph/bph; |
---|
| 396 | |
---|
| 397 | g_midi_clock.nt = ROAR_MATH_OVERFLOW_ADD(g_pos, g_midi_clock.spt); |
---|
| 398 | |
---|
| 399 | return 0; |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | int midi_clock_tick (void) { |
---|
| 403 | unsigned int diff; |
---|
| 404 | |
---|
| 405 | while ( g_pos >= g_midi_clock.nt ) { |
---|
| 406 | diff = g_pos - g_midi_clock.nt; |
---|
[1860] | 407 | ROAR_DBG("midi_clock_tick(void): g_pos is %u samples (%5.2f%%) ahead of nt.", diff, (float)diff/g_midi_clock.spt); |
---|
[1851] | 408 | |
---|
| 409 | g_midi_clock.nt = ROAR_MATH_OVERFLOW_ADD(g_midi_clock.nt, g_midi_clock.spt); |
---|
| 410 | |
---|
| 411 | if ( streams_get_flag(g_midi_clock.stream, ROAR_FLAG_SYNC) ) { |
---|
[1860] | 412 | ROAR_DBG("midi_clock_tick(void): TICK! (nt=%lu)", g_midi_clock.nt); |
---|
[1851] | 413 | } else { |
---|
[1860] | 414 | ROAR_DBG("midi_clock_tick(void): silent tick. (nt=%lu)", g_midi_clock.nt); |
---|
[1851] | 415 | } |
---|
| 416 | } |
---|
| 417 | |
---|
[1848] | 418 | return 0; |
---|
| 419 | } |
---|
| 420 | |
---|
[1845] | 421 | // CB: |
---|
[1843] | 422 | |
---|
| 423 | int midi_cb_init (void) { |
---|
[1751] | 424 | #ifdef _HAVE_CONSOLE |
---|
[1843] | 425 | struct roar_stream * s; |
---|
| 426 | struct roar_stream_server * ss; |
---|
[185] | 427 | int i; |
---|
| 428 | char * files[] = { |
---|
| 429 | "/dev/console", |
---|
| 430 | #ifdef __linux__ |
---|
| 431 | "/dev/tty0", |
---|
| 432 | "/dev/vc/0", |
---|
| 433 | #endif |
---|
| 434 | NULL |
---|
| 435 | }; |
---|
| 436 | |
---|
[1853] | 437 | g_midi_cb.console = -1; |
---|
| 438 | g_midi_cb.stream = -1; |
---|
| 439 | g_midi_cb.stoptime = 0; |
---|
| 440 | g_midi_cb.playing = 0; |
---|
[185] | 441 | |
---|
| 442 | for (i = 0; files[i] != NULL; i++) { |
---|
[1853] | 443 | if ( (g_midi_cb.console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 ) |
---|
[185] | 444 | break; |
---|
| 445 | } |
---|
| 446 | |
---|
[1853] | 447 | if ( g_midi_cb.console == -1 ) |
---|
[1843] | 448 | return -1; |
---|
| 449 | |
---|
[1853] | 450 | if ( (g_midi_cb.stream = streams_new()) == -1 ) { |
---|
[1843] | 451 | ROAR_WARN("Error while initializing MIDI subsystem component CB"); |
---|
| 452 | midi_cb_free(); |
---|
[187] | 453 | return -1; |
---|
| 454 | } |
---|
[1843] | 455 | |
---|
[1853] | 456 | midi_vio_set_dummy(g_midi_cb.stream); |
---|
[1848] | 457 | |
---|
[1853] | 458 | streams_get(g_midi_cb.stream, &ss); |
---|
[1843] | 459 | s = ROAR_STREAM(ss); |
---|
| 460 | |
---|
| 461 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 462 | |
---|
| 463 | s->pos_rel_id = -1; |
---|
| 464 | |
---|
| 465 | s->info.codec = 0; |
---|
| 466 | ss->codec_orgi = 0; |
---|
| 467 | |
---|
| 468 | s->info.channels = 1; |
---|
| 469 | s->info.rate = 1193180; |
---|
| 470 | s->info.bits = 8; |
---|
| 471 | |
---|
[1853] | 472 | if ( streams_set_dir(g_midi_cb.stream, ROAR_DIR_BRIDGE, 1) == -1 ) { |
---|
[1843] | 473 | ROAR_WARN("Error while initializing MIDI subsystem component CB"); |
---|
| 474 | midi_cb_free(); |
---|
| 475 | return -1; |
---|
| 476 | } |
---|
| 477 | |
---|
[1853] | 478 | streams_set_name(g_midi_cb.stream, "Console speaker bridge"); |
---|
[1843] | 479 | |
---|
[1853] | 480 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_OUTPUT); |
---|
| 481 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_PRIMARY); |
---|
| 482 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_HWMIXER); |
---|
[1843] | 483 | |
---|
| 484 | return 0; |
---|
[1485] | 485 | #else |
---|
[1853] | 486 | g_midi_cb.console = -1; |
---|
| 487 | g_midi_cb.stream = -1; |
---|
[1843] | 488 | |
---|
[1485] | 489 | return -1; |
---|
| 490 | #endif |
---|
[185] | 491 | } |
---|
| 492 | |
---|
[1843] | 493 | int midi_cb_free (void) { |
---|
[1751] | 494 | #ifdef _HAVE_CONSOLE |
---|
[1843] | 495 | |
---|
| 496 | midi_cb_stop(); |
---|
| 497 | |
---|
[1853] | 498 | if ( g_midi_cb.stream != -1 ) |
---|
| 499 | streams_delete(g_midi_cb.stream); |
---|
[1843] | 500 | |
---|
[1853] | 501 | if ( g_midi_cb.console != -1 ) |
---|
| 502 | close(g_midi_cb.console); |
---|
[1843] | 503 | |
---|
[185] | 504 | return 0; |
---|
[1485] | 505 | #else |
---|
| 506 | return -1; |
---|
| 507 | #endif |
---|
[185] | 508 | } |
---|
| 509 | |
---|
| 510 | int midi_cb_play(float t, float freq, int override) { |
---|
[191] | 511 | float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels; |
---|
| 512 | |
---|
[189] | 513 | /* |
---|
| 514 | #define MIDI_CB_NOOVERRIDE 0 |
---|
| 515 | #define MIDI_CB_OVERRIDE 1 |
---|
| 516 | */ |
---|
[1853] | 517 | if ( g_midi_cb.playing && override != MIDI_CB_OVERRIDE ) |
---|
[191] | 518 | return -1; |
---|
| 519 | |
---|
[1853] | 520 | g_midi_cb.stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t); |
---|
[191] | 521 | midi_cb_start(freq); |
---|
[1853] | 522 | g_midi_cb.playing = 1; |
---|
[191] | 523 | |
---|
| 524 | return 0; |
---|
[185] | 525 | } |
---|
| 526 | |
---|
[190] | 527 | int midi_cb_update (void) { |
---|
[1853] | 528 | if ( !g_midi_cb.playing ) |
---|
[192] | 529 | return 0; |
---|
| 530 | |
---|
[1853] | 531 | if ( g_midi_cb.stoptime <= g_pos ) |
---|
[189] | 532 | midi_cb_stop(); |
---|
| 533 | |
---|
| 534 | return 0; |
---|
| 535 | } |
---|
| 536 | |
---|
[185] | 537 | int midi_cb_start(float freq) { |
---|
| 538 | // On linux this uses ioctl KIOCSOUND |
---|
[188] | 539 | #ifdef __linux__ |
---|
[1853] | 540 | if ( g_midi_cb.console == -1 ) |
---|
[188] | 541 | return -1; |
---|
| 542 | |
---|
[1853] | 543 | if ( ioctl(g_midi_cb.console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 ) |
---|
[188] | 544 | return -1; |
---|
| 545 | |
---|
| 546 | return 0; |
---|
| 547 | #else |
---|
[185] | 548 | return -1; |
---|
[188] | 549 | #endif |
---|
[185] | 550 | } |
---|
| 551 | |
---|
| 552 | int midi_cb_stop (void) { |
---|
| 553 | #ifdef __linux__ |
---|
[1853] | 554 | g_midi_cb.playing = 0; |
---|
[185] | 555 | return midi_cb_start(0); |
---|
| 556 | #else |
---|
| 557 | return -1; |
---|
| 558 | #endif |
---|
| 559 | } |
---|
| 560 | |
---|
[1848] | 561 | // VIO: |
---|
| 562 | |
---|
| 563 | int midi_vio_set_dummy(int stream) { |
---|
| 564 | struct roar_stream_server * ss; |
---|
| 565 | |
---|
| 566 | if ( streams_get(stream, &ss) == -1 ) |
---|
| 567 | return -1; |
---|
| 568 | |
---|
| 569 | ss->vio.read = NULL; |
---|
| 570 | ss->vio.write = NULL; |
---|
| 571 | ss->vio.lseek = NULL; |
---|
[1849] | 572 | ss->vio.nonblock = (int (*)(struct roar_vio_calls * vio, int state))midi_vio_ok; |
---|
| 573 | ss->vio.sync = (int (*)(struct roar_vio_calls * vio))midi_vio_ok; |
---|
[1848] | 574 | ss->vio.ctl = NULL; |
---|
[1849] | 575 | ss->vio.close = (int (*)(struct roar_vio_calls * vio))midi_vio_ok; |
---|
| 576 | |
---|
| 577 | return 0; |
---|
[1848] | 578 | } |
---|
| 579 | |
---|
| 580 | int midi_vio_ok(struct roar_vio_calls * vio, ...) { |
---|
| 581 | return 0; |
---|
| 582 | } |
---|
| 583 | |
---|
[169] | 584 | //ll |
---|