Rev | Line | |
---|
[169] | 1 | //midi.c: |
---|
| 2 | |
---|
| 3 | #include "roard.h" |
---|
| 4 | |
---|
[185] | 5 | int midi_init (void) { |
---|
| 6 | int i; |
---|
| 7 | char * files[] = { |
---|
| 8 | "/dev/console", |
---|
| 9 | #ifdef __linux__ |
---|
| 10 | "/dev/tty0", |
---|
| 11 | "/dev/vc/0", |
---|
| 12 | #endif |
---|
| 13 | NULL |
---|
| 14 | }; |
---|
| 15 | |
---|
[192] | 16 | g_console = -1; |
---|
[191] | 17 | g_midi_cb_stoptime = 0; |
---|
[192] | 18 | g_midi_cb_playing = 0; |
---|
[185] | 19 | |
---|
| 20 | for (i = 0; files[i] != NULL; i++) { |
---|
| 21 | if ( (g_console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 ) |
---|
| 22 | break; |
---|
| 23 | } |
---|
| 24 | |
---|
[187] | 25 | if ( g_console != -1 ) { |
---|
| 26 | return 0; |
---|
| 27 | } else { |
---|
| 28 | return -1; |
---|
| 29 | } |
---|
[185] | 30 | } |
---|
| 31 | |
---|
| 32 | int midi_free (void) { |
---|
| 33 | if ( g_console != -1 ) |
---|
| 34 | close(g_console); |
---|
| 35 | return 0; |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | int midi_cb_play(float t, float freq, int override) { |
---|
[191] | 39 | float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels; |
---|
| 40 | |
---|
[189] | 41 | /* |
---|
| 42 | #define MIDI_CB_NOOVERRIDE 0 |
---|
| 43 | #define MIDI_CB_OVERRIDE 1 |
---|
| 44 | */ |
---|
[192] | 45 | if ( g_midi_cb_playing && override != MIDI_CB_OVERRIDE ) |
---|
[191] | 46 | return -1; |
---|
| 47 | |
---|
| 48 | g_midi_cb_stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t); |
---|
| 49 | midi_cb_start(freq); |
---|
[192] | 50 | g_midi_cb_playing = 1; |
---|
[191] | 51 | |
---|
| 52 | return 0; |
---|
[185] | 53 | } |
---|
| 54 | |
---|
[190] | 55 | int midi_cb_update (void) { |
---|
[192] | 56 | if ( !g_midi_cb_playing ) |
---|
| 57 | return 0; |
---|
| 58 | |
---|
[190] | 59 | if ( g_midi_cb_stoptime <= g_pos ) |
---|
[189] | 60 | midi_cb_stop(); |
---|
| 61 | |
---|
| 62 | return 0; |
---|
| 63 | } |
---|
| 64 | |
---|
[185] | 65 | int midi_cb_start(float freq) { |
---|
| 66 | // On linux this uses ioctl KIOCSOUND |
---|
[188] | 67 | #ifdef __linux__ |
---|
| 68 | if ( g_console == -1 ) |
---|
| 69 | return -1; |
---|
| 70 | |
---|
[192] | 71 | if ( ioctl(g_console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 ) |
---|
[188] | 72 | return -1; |
---|
| 73 | |
---|
| 74 | return 0; |
---|
| 75 | #else |
---|
[185] | 76 | return -1; |
---|
[188] | 77 | #endif |
---|
[185] | 78 | } |
---|
| 79 | |
---|
| 80 | int midi_cb_stop (void) { |
---|
| 81 | #ifdef __linux__ |
---|
[192] | 82 | g_midi_cb_playing = 0; |
---|
[185] | 83 | return midi_cb_start(0); |
---|
| 84 | #else |
---|
| 85 | return -1; |
---|
| 86 | #endif |
---|
| 87 | } |
---|
| 88 | |
---|
[169] | 89 | //ll |
---|
Note: See
TracBrowser
for help on using the repository browser.