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