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