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