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