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