[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 | |
---|
[185] | 27 | int midi_init (void) { |
---|
[1485] | 28 | #ifdef ROAR_HAVE_IO_POSIX |
---|
[185] | 29 | int i; |
---|
| 30 | char * files[] = { |
---|
| 31 | "/dev/console", |
---|
| 32 | #ifdef __linux__ |
---|
| 33 | "/dev/tty0", |
---|
| 34 | "/dev/vc/0", |
---|
| 35 | #endif |
---|
| 36 | NULL |
---|
| 37 | }; |
---|
| 38 | |
---|
[192] | 39 | g_console = -1; |
---|
[191] | 40 | g_midi_cb_stoptime = 0; |
---|
[192] | 41 | g_midi_cb_playing = 0; |
---|
[185] | 42 | |
---|
| 43 | for (i = 0; files[i] != NULL; i++) { |
---|
| 44 | if ( (g_console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 ) |
---|
| 45 | break; |
---|
| 46 | } |
---|
| 47 | |
---|
[187] | 48 | if ( g_console != -1 ) { |
---|
| 49 | return 0; |
---|
| 50 | } else { |
---|
| 51 | return -1; |
---|
| 52 | } |
---|
[1485] | 53 | #else |
---|
| 54 | return -1; |
---|
| 55 | #endif |
---|
[185] | 56 | } |
---|
| 57 | |
---|
| 58 | int midi_free (void) { |
---|
[1485] | 59 | #ifdef ROAR_HAVE_IO_POSIX |
---|
[185] | 60 | if ( g_console != -1 ) |
---|
| 61 | close(g_console); |
---|
| 62 | return 0; |
---|
[1485] | 63 | #else |
---|
| 64 | return -1; |
---|
| 65 | #endif |
---|
[185] | 66 | } |
---|
| 67 | |
---|
| 68 | int midi_cb_play(float t, float freq, int override) { |
---|
[191] | 69 | float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels; |
---|
| 70 | |
---|
[189] | 71 | /* |
---|
| 72 | #define MIDI_CB_NOOVERRIDE 0 |
---|
| 73 | #define MIDI_CB_OVERRIDE 1 |
---|
| 74 | */ |
---|
[192] | 75 | if ( g_midi_cb_playing && override != MIDI_CB_OVERRIDE ) |
---|
[191] | 76 | return -1; |
---|
| 77 | |
---|
| 78 | g_midi_cb_stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t); |
---|
| 79 | midi_cb_start(freq); |
---|
[192] | 80 | g_midi_cb_playing = 1; |
---|
[191] | 81 | |
---|
| 82 | return 0; |
---|
[185] | 83 | } |
---|
| 84 | |
---|
[190] | 85 | int midi_cb_update (void) { |
---|
[192] | 86 | if ( !g_midi_cb_playing ) |
---|
| 87 | return 0; |
---|
| 88 | |
---|
[190] | 89 | if ( g_midi_cb_stoptime <= g_pos ) |
---|
[189] | 90 | midi_cb_stop(); |
---|
| 91 | |
---|
| 92 | return 0; |
---|
| 93 | } |
---|
| 94 | |
---|
[185] | 95 | int midi_cb_start(float freq) { |
---|
| 96 | // On linux this uses ioctl KIOCSOUND |
---|
[188] | 97 | #ifdef __linux__ |
---|
| 98 | if ( g_console == -1 ) |
---|
| 99 | return -1; |
---|
| 100 | |
---|
[192] | 101 | if ( ioctl(g_console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 ) |
---|
[188] | 102 | return -1; |
---|
| 103 | |
---|
| 104 | return 0; |
---|
| 105 | #else |
---|
[185] | 106 | return -1; |
---|
[188] | 107 | #endif |
---|
[185] | 108 | } |
---|
| 109 | |
---|
| 110 | int midi_cb_stop (void) { |
---|
| 111 | #ifdef __linux__ |
---|
[192] | 112 | g_midi_cb_playing = 0; |
---|
[185] | 113 | return midi_cb_start(0); |
---|
| 114 | #else |
---|
| 115 | return -1; |
---|
| 116 | #endif |
---|
| 117 | } |
---|
| 118 | |
---|
[169] | 119 | //ll |
---|