source: roaraudio/roard/midi.c @ 1862:37629742006d

Last change on this file since 1862:37629742006d was 1862:37629742006d, checked in by phi, 15 years ago

free buffer in error case

File size: 8.5 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 if ( midi_clock_init() == -1 ) {
38  ROAR_WARN("Can not initialize MIDI subsystem component clock");
39 }
40
41 return 0;
42}
43
44int midi_free (void) {
45 return midi_cb_free();
46}
47
48int midi_update(void) {
49
50 if ( g_midi_clock.stream != -1 )
51  midi_check_bridge(g_midi_clock.stream);
52
53 return midi_cb_update();
54}
55
56int midi_reinit(void) {
57 return 0;
58}
59
60// STREAMS:
61
62int midi_check_stream  (int id) {
63 struct roar_stream        *   s;
64 struct roar_stream_server *  ss;
65 struct roar_buffer        *   b;
66 char                      * buf;
67 ssize_t                     len;
68 int                           i;
69
70 if ( g_streams[id] == NULL )
71  return -1;
72
73 ROAR_DBG("midi_check_stream(id=%i) = ?", id);
74
75 s = ROAR_STREAM(ss = g_streams[id]);
76
77 if ( s->dir == ROAR_DIR_BRIDGE )
78  return midi_check_bridge(id);
79
80 switch (s->info.codec) {
81  case ROAR_CODEC_MIDI:
82   break;
83  default:
84    streams_delete(id);
85    return -1;
86 }
87
88 if ( roar_buffer_new(&b, MIDI_READ_SIZE) == -1 ) {
89  ROAR_ERR("midi_check_stream(*): Can not alloc buffer space!");
90  ROAR_DBG("midi_check_stream(*) = -1");
91  return -1;
92 }
93
94 roar_buffer_get_data(b, (void **)&buf);
95
96 if ( (len = stream_vio_s_read(ss, buf, MIDI_READ_SIZE)) < 1 ) {
97  streams_delete(id);
98  roar_buffer_free(b);
99  return -1;
100 }
101
102 roar_buffer_set_len(b, len);
103
104 if ( stream_add_buffer(id, b) == -1 ) {
105  roar_buffer_free(b);
106  streams_delete(id);
107  return -1;
108 }
109
110 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
111  if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) {
112   if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) {
113    if ( stream_vio_write(i, buf, len) != len ) {
114     streams_delete(i);
115    }
116   }
117  }
118 }
119
120 switch (s->info.codec) {
121  case ROAR_CODEC_MIDI:
122    return midi_conv_midi2mes(id);
123   break;
124  default:
125    streams_delete(id);
126    return -1;
127 }
128
129 return 0;
130}
131
132int midi_send_stream   (int id) {
133 struct roar_stream        *   s;
134 struct roar_stream_server *  ss;
135
136 if ( g_streams[id] == NULL )
137  return -1;
138
139 ROAR_DBG("midi_send_stream(id=%i) = ?", id);
140
141 s = ROAR_STREAM(ss = g_streams[id]);
142
143 switch (s->info.codec) {
144  default:
145    streams_delete(id);
146    return -1;
147 }
148
149 return 0;
150}
151
152int midi_conv_midi2mes (int id) {
153 return -1;
154}
155
156int midi_conv_mes2midi (int id) {
157 return -1;
158}
159
160// bridges:
161int midi_check_bridge  (int id) {
162
163 ROAR_DBG("midi_check_bridge(id=%i) = ?", id);
164
165 if ( id == g_midi_clock.stream ) {
166  midi_clock_tick();
167
168  return 0;
169 }
170
171 return -1;
172}
173
174// clock:
175
176int midi_clock_init (void) {
177 struct roar_stream * s;
178 struct roar_stream_server * ss;
179
180 if ( (g_midi_clock.stream = streams_new()) == -1 ) {
181  ROAR_WARN("Error while initializing MIDI subsystem component clock");
182  return -1;
183 }
184
185 midi_vio_set_dummy(g_midi_clock.stream);
186
187 streams_get(g_midi_clock.stream, &ss);
188 s = ROAR_STREAM(ss);
189
190 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
191
192 s->pos_rel_id    =  g_midi_clock.stream;
193
194 s->info.codec    =  ROAR_CODEC_MIDI;
195 ss->codec_orgi   =  ROAR_CODEC_MIDI;
196
197 s->info.channels =  0;
198 s->info.rate     = MIDI_RATE;
199 s->info.bits     =  8;
200
201 if ( streams_set_dir(g_midi_clock.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
202  ROAR_WARN("Error while initializing MIDI subsystem component clock");
203  return -1;
204 }
205
206 streams_set_name(g_midi_clock.stream, "MIDI Clock");
207
208 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_PRIMARY);
209 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_SYNC);
210
211 midi_clock_set_bph(3600); // one tick per sec
212
213 return 0;
214}
215
216int midi_clock_set_bph (uint_least32_t bph) {
217 uint_least32_t sph = g_sa->rate/2 * 75 * g_sa->channels; // samples per houre
218
219 g_midi_clock.bph  = bph;
220
221 g_midi_clock.spt  = sph/bph;
222
223 g_midi_clock.nt   = ROAR_MATH_OVERFLOW_ADD(g_pos, g_midi_clock.spt);
224
225 return 0;
226}
227
228int midi_clock_tick (void) {
229 unsigned int diff;
230
231 while ( g_pos >= g_midi_clock.nt ) {
232  diff = g_pos - g_midi_clock.nt;
233  ROAR_DBG("midi_clock_tick(void): g_pos is %u samples (%5.2f%%) ahead of nt.", diff, (float)diff/g_midi_clock.spt);
234
235  g_midi_clock.nt   = ROAR_MATH_OVERFLOW_ADD(g_midi_clock.nt, g_midi_clock.spt);
236
237  if ( streams_get_flag(g_midi_clock.stream, ROAR_FLAG_SYNC) ) {
238   ROAR_DBG("midi_clock_tick(void): TICK! (nt=%lu)", g_midi_clock.nt);
239  } else {
240   ROAR_DBG("midi_clock_tick(void): silent tick. (nt=%lu)", g_midi_clock.nt);
241  }
242 }
243
244 return 0;
245}
246
247// CB:
248
249int midi_cb_init (void) {
250#ifdef _HAVE_CONSOLE
251 struct roar_stream * s;
252 struct roar_stream_server * ss;
253 int i;
254 char * files[] = {
255                   "/dev/console",
256#ifdef __linux__
257                   "/dev/tty0",
258                   "/dev/vc/0",
259#endif
260                   NULL
261                  };
262
263 g_midi_cb.console  = -1;
264 g_midi_cb.stream   = -1;
265 g_midi_cb.stoptime =  0;
266 g_midi_cb.playing  =  0;
267
268 for (i = 0; files[i] != NULL; i++) {
269  if ( (g_midi_cb.console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 )
270   break;
271 }
272
273 if ( g_midi_cb.console == -1 )
274  return -1;
275
276 if ( (g_midi_cb.stream = streams_new()) == -1 ) {
277  ROAR_WARN("Error while initializing MIDI subsystem component CB");
278  midi_cb_free();
279  return -1;
280 }
281
282 midi_vio_set_dummy(g_midi_cb.stream);
283
284 streams_get(g_midi_cb.stream, &ss);
285 s = ROAR_STREAM(ss);
286
287 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
288
289 s->pos_rel_id    = -1;
290
291 s->info.codec    =  0;
292 ss->codec_orgi   =  0;
293
294 s->info.channels =  1;
295 s->info.rate     = 1193180;
296 s->info.bits     =  8;
297
298 if ( streams_set_dir(g_midi_cb.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
299  ROAR_WARN("Error while initializing MIDI subsystem component CB");
300  midi_cb_free();
301  return -1;
302 }
303
304 streams_set_name(g_midi_cb.stream, "Console speaker bridge");
305
306 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_OUTPUT);
307 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_PRIMARY);
308 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_HWMIXER);
309
310 return 0;
311#else
312 g_midi_cb.console  = -1;
313 g_midi_cb.stream   = -1;
314
315 return -1;
316#endif
317}
318
319int midi_cb_free (void) {
320#ifdef _HAVE_CONSOLE
321
322 midi_cb_stop();
323
324 if ( g_midi_cb.stream != -1 )
325  streams_delete(g_midi_cb.stream);
326
327 if ( g_midi_cb.console != -1 )
328  close(g_midi_cb.console);
329
330 return 0;
331#else
332 return -1;
333#endif
334}
335
336int midi_cb_play(float t, float freq, int override) {
337 float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels;
338
339/*
340#define MIDI_CB_NOOVERRIDE 0
341#define MIDI_CB_OVERRIDE   1
342*/
343 if ( g_midi_cb.playing && override != MIDI_CB_OVERRIDE )
344  return -1;
345
346 g_midi_cb.stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t);
347 midi_cb_start(freq);
348 g_midi_cb.playing = 1;
349
350 return 0;
351}
352
353int midi_cb_update (void) {
354 if ( !g_midi_cb.playing )
355  return 0;
356
357 if ( g_midi_cb.stoptime <= g_pos )
358  midi_cb_stop();
359
360 return 0;
361}
362
363int midi_cb_start(float freq) {
364// On linux this uses ioctl KIOCSOUND
365#ifdef __linux__
366 if ( g_midi_cb.console == -1 )
367  return -1;
368
369 if ( ioctl(g_midi_cb.console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 )
370  return -1;
371
372 return 0;
373#else
374 return -1;
375#endif
376}
377
378int midi_cb_stop (void) {
379#ifdef __linux__
380 g_midi_cb.playing = 0;
381 return midi_cb_start(0);
382#else
383 return -1;
384#endif
385}
386
387// VIO:
388
389int     midi_vio_set_dummy(int stream) {
390 struct roar_stream_server * ss;
391
392 if ( streams_get(stream, &ss) == -1 )
393  return -1;
394
395 ss->vio.read     = NULL;
396 ss->vio.write    = NULL;
397 ss->vio.lseek    = NULL;
398 ss->vio.nonblock = (int (*)(struct roar_vio_calls * vio, int state))midi_vio_ok;
399 ss->vio.sync     = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
400 ss->vio.ctl      = NULL;
401 ss->vio.close    = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
402
403 return 0;
404}
405
406int     midi_vio_ok(struct roar_vio_calls * vio, ...) {
407 return 0;
408}
409
410//ll
Note: See TracBrowser for help on using the repository browser.