[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 | |
---|
[1843] | 41 | return 0; |
---|
| 42 | } |
---|
[1848] | 43 | |
---|
[1843] | 44 | int midi_free (void) { |
---|
| 45 | return midi_cb_free(); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | int midi_update(void) { |
---|
[1848] | 49 | |
---|
[1850] | 50 | if ( g_midi_clock.stream != -1 ) |
---|
| 51 | midi_check_bridge(g_midi_clock.stream); |
---|
[1848] | 52 | |
---|
[1843] | 53 | return midi_cb_update(); |
---|
| 54 | } |
---|
| 55 | |
---|
[1855] | 56 | int midi_reinit(void) { |
---|
| 57 | return 0; |
---|
| 58 | } |
---|
| 59 | |
---|
[1845] | 60 | // STREAMS: |
---|
| 61 | |
---|
| 62 | int midi_check_stream (int id) { |
---|
[1846] | 63 | struct roar_stream * s; |
---|
| 64 | struct roar_stream_server * ss; |
---|
[1858] | 65 | struct roar_buffer * b; |
---|
| 66 | char * buf; |
---|
| 67 | ssize_t len; |
---|
[1859] | 68 | int i; |
---|
[1846] | 69 | |
---|
| 70 | if ( g_streams[id] == NULL ) |
---|
| 71 | return -1; |
---|
| 72 | |
---|
| 73 | ROAR_DBG("midi_check_stream(id=%i) = ?", id); |
---|
| 74 | |
---|
| 75 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
| 76 | |
---|
[1848] | 77 | if ( s->dir == ROAR_DIR_BRIDGE ) |
---|
| 78 | return midi_check_bridge(id); |
---|
| 79 | |
---|
[1846] | 80 | switch (s->info.codec) { |
---|
[1858] | 81 | case ROAR_CODEC_MIDI: |
---|
| 82 | break; |
---|
| 83 | default: |
---|
| 84 | streams_delete(id); |
---|
| 85 | return -1; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | if ( roar_buffer_new(&b, MIDI_READ_SIZE) == -1 ) { |
---|
| 89 | ROAR_ERR("midi_check_stream(*): Can not alloc buffer space!"); |
---|
| 90 | ROAR_DBG("midi_check_stream(*) = -1"); |
---|
| 91 | return -1; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | roar_buffer_get_data(b, (void **)&buf); |
---|
| 95 | |
---|
| 96 | if ( (len = stream_vio_s_read(ss, buf, MIDI_READ_SIZE)) < 1 ) { |
---|
| 97 | streams_delete(id); |
---|
[1862] | 98 | roar_buffer_free(b); |
---|
[1858] | 99 | return -1; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | roar_buffer_set_len(b, len); |
---|
| 103 | |
---|
[1859] | 104 | if ( stream_add_buffer(id, b) == -1 ) { |
---|
| 105 | roar_buffer_free(b); |
---|
| 106 | streams_delete(id); |
---|
| 107 | return -1; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 111 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) { |
---|
| 112 | if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) { |
---|
| 113 | if ( stream_vio_write(i, buf, len) != len ) { |
---|
| 114 | streams_delete(i); |
---|
| 115 | } |
---|
| 116 | } |
---|
[1858] | 117 | } |
---|
[1859] | 118 | } |
---|
[1858] | 119 | |
---|
| 120 | switch (s->info.codec) { |
---|
| 121 | case ROAR_CODEC_MIDI: |
---|
| 122 | return midi_conv_midi2mes(id); |
---|
| 123 | break; |
---|
[1846] | 124 | default: |
---|
| 125 | streams_delete(id); |
---|
| 126 | return -1; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | return 0; |
---|
[1845] | 130 | } |
---|
[1846] | 131 | |
---|
[1845] | 132 | int midi_send_stream (int id) { |
---|
[1846] | 133 | struct roar_stream * s; |
---|
| 134 | struct roar_stream_server * ss; |
---|
| 135 | |
---|
| 136 | if ( g_streams[id] == NULL ) |
---|
| 137 | return -1; |
---|
| 138 | |
---|
| 139 | ROAR_DBG("midi_send_stream(id=%i) = ?", id); |
---|
| 140 | |
---|
| 141 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
| 142 | |
---|
| 143 | switch (s->info.codec) { |
---|
| 144 | default: |
---|
| 145 | streams_delete(id); |
---|
| 146 | return -1; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | return 0; |
---|
[1845] | 150 | } |
---|
| 151 | |
---|
[1858] | 152 | int midi_conv_midi2mes (int id) { |
---|
| 153 | return -1; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | int midi_conv_mes2midi (int id) { |
---|
| 157 | return -1; |
---|
| 158 | } |
---|
| 159 | |
---|
[1848] | 160 | // bridges: |
---|
| 161 | int midi_check_bridge (int id) { |
---|
[1851] | 162 | |
---|
[1860] | 163 | ROAR_DBG("midi_check_bridge(id=%i) = ?", id); |
---|
[1851] | 164 | |
---|
| 165 | if ( id == g_midi_clock.stream ) { |
---|
| 166 | midi_clock_tick(); |
---|
| 167 | |
---|
| 168 | return 0; |
---|
| 169 | } |
---|
| 170 | |
---|
[1848] | 171 | return -1; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | // clock: |
---|
| 175 | |
---|
| 176 | int midi_clock_init (void) { |
---|
| 177 | struct roar_stream * s; |
---|
| 178 | struct roar_stream_server * ss; |
---|
| 179 | |
---|
[1850] | 180 | if ( (g_midi_clock.stream = streams_new()) == -1 ) { |
---|
[1848] | 181 | ROAR_WARN("Error while initializing MIDI subsystem component clock"); |
---|
| 182 | return -1; |
---|
| 183 | } |
---|
| 184 | |
---|
[1850] | 185 | midi_vio_set_dummy(g_midi_clock.stream); |
---|
[1848] | 186 | |
---|
[1850] | 187 | streams_get(g_midi_clock.stream, &ss); |
---|
[1848] | 188 | s = ROAR_STREAM(ss); |
---|
| 189 | |
---|
| 190 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 191 | |
---|
[1850] | 192 | s->pos_rel_id = g_midi_clock.stream; |
---|
[1848] | 193 | |
---|
| 194 | s->info.codec = ROAR_CODEC_MIDI; |
---|
| 195 | ss->codec_orgi = ROAR_CODEC_MIDI; |
---|
| 196 | |
---|
| 197 | s->info.channels = 0; |
---|
| 198 | s->info.rate = MIDI_RATE; |
---|
| 199 | s->info.bits = 8; |
---|
| 200 | |
---|
[1850] | 201 | if ( streams_set_dir(g_midi_clock.stream, ROAR_DIR_BRIDGE, 1) == -1 ) { |
---|
[1848] | 202 | ROAR_WARN("Error while initializing MIDI subsystem component clock"); |
---|
| 203 | return -1; |
---|
| 204 | } |
---|
| 205 | |
---|
[1850] | 206 | streams_set_name(g_midi_clock.stream, "MIDI Clock"); |
---|
[1848] | 207 | |
---|
[1850] | 208 | streams_set_flag(g_midi_clock.stream, ROAR_FLAG_PRIMARY); |
---|
| 209 | streams_set_flag(g_midi_clock.stream, ROAR_FLAG_SYNC); |
---|
[1848] | 210 | |
---|
[1851] | 211 | midi_clock_set_bph(3600); // one tick per sec |
---|
| 212 | |
---|
| 213 | return 0; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | int midi_clock_set_bph (uint_least32_t bph) { |
---|
| 217 | uint_least32_t sph = g_sa->rate/2 * 75 * g_sa->channels; // samples per houre |
---|
| 218 | |
---|
| 219 | g_midi_clock.bph = bph; |
---|
| 220 | |
---|
| 221 | g_midi_clock.spt = sph/bph; |
---|
| 222 | |
---|
| 223 | g_midi_clock.nt = ROAR_MATH_OVERFLOW_ADD(g_pos, g_midi_clock.spt); |
---|
| 224 | |
---|
| 225 | return 0; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | int midi_clock_tick (void) { |
---|
| 229 | unsigned int diff; |
---|
| 230 | |
---|
| 231 | while ( g_pos >= g_midi_clock.nt ) { |
---|
| 232 | diff = g_pos - g_midi_clock.nt; |
---|
[1860] | 233 | ROAR_DBG("midi_clock_tick(void): g_pos is %u samples (%5.2f%%) ahead of nt.", diff, (float)diff/g_midi_clock.spt); |
---|
[1851] | 234 | |
---|
| 235 | g_midi_clock.nt = ROAR_MATH_OVERFLOW_ADD(g_midi_clock.nt, g_midi_clock.spt); |
---|
| 236 | |
---|
| 237 | if ( streams_get_flag(g_midi_clock.stream, ROAR_FLAG_SYNC) ) { |
---|
[1860] | 238 | ROAR_DBG("midi_clock_tick(void): TICK! (nt=%lu)", g_midi_clock.nt); |
---|
[1851] | 239 | } else { |
---|
[1860] | 240 | ROAR_DBG("midi_clock_tick(void): silent tick. (nt=%lu)", g_midi_clock.nt); |
---|
[1851] | 241 | } |
---|
| 242 | } |
---|
| 243 | |
---|
[1848] | 244 | return 0; |
---|
| 245 | } |
---|
| 246 | |
---|
[1845] | 247 | // CB: |
---|
[1843] | 248 | |
---|
| 249 | int midi_cb_init (void) { |
---|
[1751] | 250 | #ifdef _HAVE_CONSOLE |
---|
[1843] | 251 | struct roar_stream * s; |
---|
| 252 | struct roar_stream_server * ss; |
---|
[185] | 253 | int i; |
---|
| 254 | char * files[] = { |
---|
| 255 | "/dev/console", |
---|
| 256 | #ifdef __linux__ |
---|
| 257 | "/dev/tty0", |
---|
| 258 | "/dev/vc/0", |
---|
| 259 | #endif |
---|
| 260 | NULL |
---|
| 261 | }; |
---|
| 262 | |
---|
[1853] | 263 | g_midi_cb.console = -1; |
---|
| 264 | g_midi_cb.stream = -1; |
---|
| 265 | g_midi_cb.stoptime = 0; |
---|
| 266 | g_midi_cb.playing = 0; |
---|
[185] | 267 | |
---|
| 268 | for (i = 0; files[i] != NULL; i++) { |
---|
[1853] | 269 | if ( (g_midi_cb.console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 ) |
---|
[185] | 270 | break; |
---|
| 271 | } |
---|
| 272 | |
---|
[1853] | 273 | if ( g_midi_cb.console == -1 ) |
---|
[1843] | 274 | return -1; |
---|
| 275 | |
---|
[1853] | 276 | if ( (g_midi_cb.stream = streams_new()) == -1 ) { |
---|
[1843] | 277 | ROAR_WARN("Error while initializing MIDI subsystem component CB"); |
---|
| 278 | midi_cb_free(); |
---|
[187] | 279 | return -1; |
---|
| 280 | } |
---|
[1843] | 281 | |
---|
[1853] | 282 | midi_vio_set_dummy(g_midi_cb.stream); |
---|
[1848] | 283 | |
---|
[1853] | 284 | streams_get(g_midi_cb.stream, &ss); |
---|
[1843] | 285 | s = ROAR_STREAM(ss); |
---|
| 286 | |
---|
| 287 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 288 | |
---|
| 289 | s->pos_rel_id = -1; |
---|
| 290 | |
---|
| 291 | s->info.codec = 0; |
---|
| 292 | ss->codec_orgi = 0; |
---|
| 293 | |
---|
| 294 | s->info.channels = 1; |
---|
| 295 | s->info.rate = 1193180; |
---|
| 296 | s->info.bits = 8; |
---|
| 297 | |
---|
[1853] | 298 | if ( streams_set_dir(g_midi_cb.stream, ROAR_DIR_BRIDGE, 1) == -1 ) { |
---|
[1843] | 299 | ROAR_WARN("Error while initializing MIDI subsystem component CB"); |
---|
| 300 | midi_cb_free(); |
---|
| 301 | return -1; |
---|
| 302 | } |
---|
| 303 | |
---|
[1853] | 304 | streams_set_name(g_midi_cb.stream, "Console speaker bridge"); |
---|
[1843] | 305 | |
---|
[1853] | 306 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_OUTPUT); |
---|
| 307 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_PRIMARY); |
---|
| 308 | streams_set_flag(g_midi_cb.stream, ROAR_FLAG_HWMIXER); |
---|
[1843] | 309 | |
---|
| 310 | return 0; |
---|
[1485] | 311 | #else |
---|
[1853] | 312 | g_midi_cb.console = -1; |
---|
| 313 | g_midi_cb.stream = -1; |
---|
[1843] | 314 | |
---|
[1485] | 315 | return -1; |
---|
| 316 | #endif |
---|
[185] | 317 | } |
---|
| 318 | |
---|
[1843] | 319 | int midi_cb_free (void) { |
---|
[1751] | 320 | #ifdef _HAVE_CONSOLE |
---|
[1843] | 321 | |
---|
| 322 | midi_cb_stop(); |
---|
| 323 | |
---|
[1853] | 324 | if ( g_midi_cb.stream != -1 ) |
---|
| 325 | streams_delete(g_midi_cb.stream); |
---|
[1843] | 326 | |
---|
[1853] | 327 | if ( g_midi_cb.console != -1 ) |
---|
| 328 | close(g_midi_cb.console); |
---|
[1843] | 329 | |
---|
[185] | 330 | return 0; |
---|
[1485] | 331 | #else |
---|
| 332 | return -1; |
---|
| 333 | #endif |
---|
[185] | 334 | } |
---|
| 335 | |
---|
| 336 | int midi_cb_play(float t, float freq, int override) { |
---|
[191] | 337 | float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels; |
---|
| 338 | |
---|
[189] | 339 | /* |
---|
| 340 | #define MIDI_CB_NOOVERRIDE 0 |
---|
| 341 | #define MIDI_CB_OVERRIDE 1 |
---|
| 342 | */ |
---|
[1853] | 343 | if ( g_midi_cb.playing && override != MIDI_CB_OVERRIDE ) |
---|
[191] | 344 | return -1; |
---|
| 345 | |
---|
[1853] | 346 | g_midi_cb.stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t); |
---|
[191] | 347 | midi_cb_start(freq); |
---|
[1853] | 348 | g_midi_cb.playing = 1; |
---|
[191] | 349 | |
---|
| 350 | return 0; |
---|
[185] | 351 | } |
---|
| 352 | |
---|
[190] | 353 | int midi_cb_update (void) { |
---|
[1853] | 354 | if ( !g_midi_cb.playing ) |
---|
[192] | 355 | return 0; |
---|
| 356 | |
---|
[1853] | 357 | if ( g_midi_cb.stoptime <= g_pos ) |
---|
[189] | 358 | midi_cb_stop(); |
---|
| 359 | |
---|
| 360 | return 0; |
---|
| 361 | } |
---|
| 362 | |
---|
[185] | 363 | int midi_cb_start(float freq) { |
---|
| 364 | // On linux this uses ioctl KIOCSOUND |
---|
[188] | 365 | #ifdef __linux__ |
---|
[1853] | 366 | if ( g_midi_cb.console == -1 ) |
---|
[188] | 367 | return -1; |
---|
| 368 | |
---|
[1853] | 369 | if ( ioctl(g_midi_cb.console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 ) |
---|
[188] | 370 | return -1; |
---|
| 371 | |
---|
| 372 | return 0; |
---|
| 373 | #else |
---|
[185] | 374 | return -1; |
---|
[188] | 375 | #endif |
---|
[185] | 376 | } |
---|
| 377 | |
---|
| 378 | int midi_cb_stop (void) { |
---|
| 379 | #ifdef __linux__ |
---|
[1853] | 380 | g_midi_cb.playing = 0; |
---|
[185] | 381 | return midi_cb_start(0); |
---|
| 382 | #else |
---|
| 383 | return -1; |
---|
| 384 | #endif |
---|
| 385 | } |
---|
| 386 | |
---|
[1848] | 387 | // VIO: |
---|
| 388 | |
---|
| 389 | int midi_vio_set_dummy(int stream) { |
---|
| 390 | struct roar_stream_server * ss; |
---|
| 391 | |
---|
| 392 | if ( streams_get(stream, &ss) == -1 ) |
---|
| 393 | return -1; |
---|
| 394 | |
---|
| 395 | ss->vio.read = NULL; |
---|
| 396 | ss->vio.write = NULL; |
---|
| 397 | ss->vio.lseek = NULL; |
---|
[1849] | 398 | ss->vio.nonblock = (int (*)(struct roar_vio_calls * vio, int state))midi_vio_ok; |
---|
| 399 | ss->vio.sync = (int (*)(struct roar_vio_calls * vio))midi_vio_ok; |
---|
[1848] | 400 | ss->vio.ctl = NULL; |
---|
[1849] | 401 | ss->vio.close = (int (*)(struct roar_vio_calls * vio))midi_vio_ok; |
---|
| 402 | |
---|
| 403 | return 0; |
---|
[1848] | 404 | } |
---|
| 405 | |
---|
| 406 | int midi_vio_ok(struct roar_vio_calls * vio, ...) { |
---|
| 407 | return 0; |
---|
| 408 | } |
---|
| 409 | |
---|
[169] | 410 | //ll |
---|