[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 | |
---|
| 37 | return 0; |
---|
| 38 | } |
---|
| 39 | int midi_free (void) { |
---|
| 40 | return midi_cb_free(); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | int midi_update(void) { |
---|
| 44 | return midi_cb_update(); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | int midi_cb_init (void) { |
---|
[1751] | 49 | #ifdef _HAVE_CONSOLE |
---|
[1843] | 50 | struct roar_stream * s; |
---|
| 51 | struct roar_stream_server * ss; |
---|
[185] | 52 | int i; |
---|
| 53 | char * files[] = { |
---|
| 54 | "/dev/console", |
---|
| 55 | #ifdef __linux__ |
---|
| 56 | "/dev/tty0", |
---|
| 57 | "/dev/vc/0", |
---|
| 58 | #endif |
---|
| 59 | NULL |
---|
| 60 | }; |
---|
| 61 | |
---|
[192] | 62 | g_console = -1; |
---|
[1843] | 63 | g_midi_cb_stream = -1; |
---|
| 64 | g_midi_cb_stoptime = 0; |
---|
| 65 | g_midi_cb_playing = 0; |
---|
[185] | 66 | |
---|
| 67 | for (i = 0; files[i] != NULL; i++) { |
---|
| 68 | if ( (g_console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 ) |
---|
| 69 | break; |
---|
| 70 | } |
---|
| 71 | |
---|
[1843] | 72 | if ( g_console == -1 ) |
---|
| 73 | return -1; |
---|
| 74 | |
---|
| 75 | if ( (g_midi_cb_stream = streams_new()) == -1 ) { |
---|
| 76 | ROAR_WARN("Error while initializing MIDI subsystem component CB"); |
---|
| 77 | midi_cb_free(); |
---|
[187] | 78 | return -1; |
---|
| 79 | } |
---|
[1843] | 80 | |
---|
| 81 | streams_get(g_midi_cb_stream, &ss); |
---|
| 82 | s = ROAR_STREAM(ss); |
---|
| 83 | |
---|
| 84 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 85 | |
---|
| 86 | s->pos_rel_id = -1; |
---|
| 87 | |
---|
| 88 | s->info.codec = 0; |
---|
| 89 | ss->codec_orgi = 0; |
---|
| 90 | |
---|
| 91 | s->info.channels = 1; |
---|
| 92 | s->info.rate = 1193180; |
---|
| 93 | s->info.bits = 8; |
---|
| 94 | |
---|
| 95 | if ( streams_set_dir(g_midi_cb_stream, ROAR_DIR_BRIDGE, 1) == -1 ) { |
---|
| 96 | ROAR_WARN("Error while initializing MIDI subsystem component CB"); |
---|
| 97 | midi_cb_free(); |
---|
| 98 | return -1; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | streams_set_name(g_midi_cb_stream, "Console speaker bridge"); |
---|
| 102 | |
---|
| 103 | streams_set_flag(g_midi_cb_stream, ROAR_FLAG_OUTPUT); |
---|
| 104 | streams_set_flag(g_midi_cb_stream, ROAR_FLAG_PRIMARY); |
---|
| 105 | |
---|
| 106 | return 0; |
---|
[1485] | 107 | #else |
---|
[1843] | 108 | g_console = -1; |
---|
| 109 | g_midi_cb_stream = -1; |
---|
| 110 | |
---|
[1485] | 111 | return -1; |
---|
| 112 | #endif |
---|
[185] | 113 | } |
---|
| 114 | |
---|
[1843] | 115 | int midi_cb_free (void) { |
---|
[1751] | 116 | #ifdef _HAVE_CONSOLE |
---|
[1843] | 117 | |
---|
| 118 | midi_cb_stop(); |
---|
| 119 | |
---|
| 120 | if ( g_midi_cb_stream != -1 ) |
---|
| 121 | streams_delete(g_midi_cb_stream); |
---|
| 122 | |
---|
[185] | 123 | if ( g_console != -1 ) |
---|
| 124 | close(g_console); |
---|
[1843] | 125 | |
---|
[185] | 126 | return 0; |
---|
[1485] | 127 | #else |
---|
| 128 | return -1; |
---|
| 129 | #endif |
---|
[185] | 130 | } |
---|
| 131 | |
---|
| 132 | int midi_cb_play(float t, float freq, int override) { |
---|
[191] | 133 | float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels; |
---|
| 134 | |
---|
[189] | 135 | /* |
---|
| 136 | #define MIDI_CB_NOOVERRIDE 0 |
---|
| 137 | #define MIDI_CB_OVERRIDE 1 |
---|
| 138 | */ |
---|
[192] | 139 | if ( g_midi_cb_playing && override != MIDI_CB_OVERRIDE ) |
---|
[191] | 140 | return -1; |
---|
| 141 | |
---|
| 142 | g_midi_cb_stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t); |
---|
| 143 | midi_cb_start(freq); |
---|
[192] | 144 | g_midi_cb_playing = 1; |
---|
[191] | 145 | |
---|
| 146 | return 0; |
---|
[185] | 147 | } |
---|
| 148 | |
---|
[190] | 149 | int midi_cb_update (void) { |
---|
[192] | 150 | if ( !g_midi_cb_playing ) |
---|
| 151 | return 0; |
---|
| 152 | |
---|
[190] | 153 | if ( g_midi_cb_stoptime <= g_pos ) |
---|
[189] | 154 | midi_cb_stop(); |
---|
| 155 | |
---|
| 156 | return 0; |
---|
| 157 | } |
---|
| 158 | |
---|
[185] | 159 | int midi_cb_start(float freq) { |
---|
| 160 | // On linux this uses ioctl KIOCSOUND |
---|
[188] | 161 | #ifdef __linux__ |
---|
| 162 | if ( g_console == -1 ) |
---|
| 163 | return -1; |
---|
| 164 | |
---|
[192] | 165 | if ( ioctl(g_console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 ) |
---|
[188] | 166 | return -1; |
---|
| 167 | |
---|
| 168 | return 0; |
---|
| 169 | #else |
---|
[185] | 170 | return -1; |
---|
[188] | 171 | #endif |
---|
[185] | 172 | } |
---|
| 173 | |
---|
| 174 | int midi_cb_stop (void) { |
---|
| 175 | #ifdef __linux__ |
---|
[192] | 176 | g_midi_cb_playing = 0; |
---|
[185] | 177 | return midi_cb_start(0); |
---|
| 178 | #else |
---|
| 179 | return -1; |
---|
| 180 | #endif |
---|
| 181 | } |
---|
| 182 | |
---|
[169] | 183 | //ll |
---|