source: roaraudio/roard/midi.c @ 1863:76178443318a

Last change on this file since 1863:76178443318a was 1863:76178443318a, checked in by phi, 15 years ago

do very ruimentary MIDI analysis, need to implement the protocol next

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