source: roaraudio/roard/midi.c @ 1859:5bae46f45f79

Last change on this file since 1859:5bae46f45f79 was 1859:5bae46f45f79, checked in by phi, 15 years ago

write to midi thru

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