source: roaraudio/roard/midi.c @ 1847:f2ec50e745af

Last change on this file since 1847:f2ec50e745af was 1847:f2ec50e745af, checked in by phi, 15 years ago

also set HWMIXER flag on cb stream

File size: 4.4 KB
Line 
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
31int 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}
39int midi_free (void) {
40 return midi_cb_free();
41}
42
43int midi_update(void) {
44 return midi_cb_update();
45}
46
47// STREAMS:
48
49int midi_check_stream  (int id) {
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;
67}
68
69int midi_send_stream   (int id) {
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;
87}
88
89// CB:
90
91int midi_cb_init (void) {
92#ifdef _HAVE_CONSOLE
93 struct roar_stream * s;
94 struct roar_stream_server * ss;
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
105 g_console          = -1;
106 g_midi_cb_stream   = -1;
107 g_midi_cb_stoptime =  0;
108 g_midi_cb_playing  =  0;
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
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();
121  return -1;
122 }
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);
148 streams_set_flag(g_midi_cb_stream, ROAR_FLAG_HWMIXER);
149
150 return 0;
151#else
152 g_console          = -1;
153 g_midi_cb_stream   = -1;
154
155 return -1;
156#endif
157}
158
159int midi_cb_free (void) {
160#ifdef _HAVE_CONSOLE
161
162 midi_cb_stop();
163
164 if ( g_midi_cb_stream != -1 )
165  streams_delete(g_midi_cb_stream);
166
167 if ( g_console != -1 )
168  close(g_console);
169
170 return 0;
171#else
172 return -1;
173#endif
174}
175
176int midi_cb_play(float t, float freq, int override) {
177 float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels;
178
179/*
180#define MIDI_CB_NOOVERRIDE 0
181#define MIDI_CB_OVERRIDE   1
182*/
183 if ( g_midi_cb_playing && override != MIDI_CB_OVERRIDE )
184  return -1;
185
186 g_midi_cb_stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t);
187 midi_cb_start(freq);
188 g_midi_cb_playing = 1;
189
190 return 0;
191}
192
193int midi_cb_update (void) {
194 if ( !g_midi_cb_playing )
195  return 0;
196
197 if ( g_midi_cb_stoptime <= g_pos )
198  midi_cb_stop();
199
200 return 0;
201}
202
203int midi_cb_start(float freq) {
204// On linux this uses ioctl KIOCSOUND
205#ifdef __linux__
206 if ( g_console == -1 )
207  return -1;
208
209 if ( ioctl(g_console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 )
210  return -1;
211
212 return 0;
213#else
214 return -1;
215#endif
216}
217
218int midi_cb_stop (void) {
219#ifdef __linux__
220 g_midi_cb_playing = 0;
221 return midi_cb_start(0);
222#else
223 return -1;
224#endif
225}
226
227//ll
Note: See TracBrowser for help on using the repository browser.