source: roaraudio/roard/midi.c @ 1865:80a9af2b423c

Last change on this file since 1865:80a9af2b423c was 1865:80a9af2b423c, checked in by phi, 15 years ago

parese status byte

File size: 11.0 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    if ( buf != NULL ) {
185     if ( g_midi_mess.buf == NULL ) {
186      g_midi_mess.buf = buf;
187     } else {
188      roar_buffer_add(g_midi_mess.buf, buf);
189     }
190     buf = NULL;
191    }
192
193    need = 0;
194    printf("S\n");
195    if ( midi_new_bufmes(&buf, &mes) == -1 ) {
196     alive = 0;
197     continue;
198    }
199
200    if (*data == MIDI_TYPE_CLOCK_TICK || *data == MIDI_TYPE_CLOCK_START || *data == MIDI_TYPE_CLOCK_STOP ) {
201     mes->type = *data;
202    } else {
203     mes->type    = *data & 0xF0;
204     mes->channel = *data & 0x0F;
205     switch (*data & 0xF0) {
206      case MIDI_TYPE_NOTE_ON:
207      case MIDI_TYPE_NOTE_OFF:
208        need = 2;
209       break;
210      case MIDI_TYPE_PA:
211        need = 2;
212       break;
213      case MIDI_TYPE_CONTROLER:
214        need = 2;
215       break;
216      case MIDI_TYPE_PROGRAM:
217        need = 1;
218       break;
219      case MIDI_TYPE_MA:
220        need = 1;
221       break;
222      case MIDI_TYPE_PB:
223      case MIDI_TYPE_SYSEX:
224        need = 1;
225       break;
226     }
227    }
228
229   } else {
230    printf("D\n");
231    if ( need )
232     need--;
233   }
234   data++;
235   have--;
236  }
237
238  if ( need ) {
239   ROAR_ERR("midi_conv_midi2mes(*): FIXME: BUG!!! Need to restore buffer here with corrected length");
240  } else if ( !have ) {
241   roar_buffer_next(&(ss->buffer));
242  }
243
244  if ( need && buf != NULL ) {
245   roar_buffer_free(buf);
246   buf = NULL;
247  } else if ( buf != NULL ) {
248   if ( g_midi_mess.buf == NULL ) {
249    g_midi_mess.buf = buf;
250   } else {
251    roar_buffer_add(g_midi_mess.buf, buf);
252   }
253
254   buf = NULL;
255  }
256 }
257
258 return 0;
259}
260
261int midi_conv_mes2midi (int id) {
262 return -1;
263}
264
265int midi_new_bufmes    (struct roar_buffer ** buf, struct midi_message ** mes) {
266 if ( buf == NULL || mes == NULL )
267  return -1;
268
269 *buf = *mes = NULL;
270
271 if ( roar_buffer_new(buf, sizeof(struct midi_message)) == -1 )
272  return -1;
273
274 if ( roar_buffer_get_data(*buf, (void**)mes) == -1 ) {
275  roar_buffer_free(*buf);
276  *buf = *mes = NULL;
277  return -1;
278 }
279
280 memset((void*)*mes, 0, sizeof(struct midi_message));
281
282 (*mes)->type = MIDI_TYPE_NONE;
283
284 return 0;
285}
286
287// bridges:
288int midi_check_bridge  (int id) {
289
290 ROAR_DBG("midi_check_bridge(id=%i) = ?", id);
291
292 if ( id == g_midi_clock.stream ) {
293  midi_clock_tick();
294
295  return 0;
296 }
297
298 return -1;
299}
300
301// clock:
302
303int midi_clock_init (void) {
304 struct roar_stream * s;
305 struct roar_stream_server * ss;
306
307 if ( (g_midi_clock.stream = streams_new()) == -1 ) {
308  ROAR_WARN("Error while initializing MIDI subsystem component clock");
309  return -1;
310 }
311
312 midi_vio_set_dummy(g_midi_clock.stream);
313
314 streams_get(g_midi_clock.stream, &ss);
315 s = ROAR_STREAM(ss);
316
317 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
318
319 s->pos_rel_id    =  g_midi_clock.stream;
320
321 s->info.codec    =  ROAR_CODEC_MIDI;
322 ss->codec_orgi   =  ROAR_CODEC_MIDI;
323
324 s->info.channels =  0;
325 s->info.rate     = MIDI_RATE;
326 s->info.bits     =  8;
327
328 if ( streams_set_dir(g_midi_clock.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
329  ROAR_WARN("Error while initializing MIDI subsystem component clock");
330  return -1;
331 }
332
333 streams_set_name(g_midi_clock.stream, "MIDI Clock");
334
335 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_PRIMARY);
336 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_SYNC);
337
338 midi_clock_set_bph(3600); // one tick per sec
339
340 return 0;
341}
342
343int midi_clock_set_bph (uint_least32_t bph) {
344 uint_least32_t sph = g_sa->rate/2 * 75 * g_sa->channels; // samples per houre
345
346 g_midi_clock.bph  = bph;
347
348 g_midi_clock.spt  = sph/bph;
349
350 g_midi_clock.nt   = ROAR_MATH_OVERFLOW_ADD(g_pos, g_midi_clock.spt);
351
352 return 0;
353}
354
355int midi_clock_tick (void) {
356 unsigned int diff;
357
358 while ( g_pos >= g_midi_clock.nt ) {
359  diff = g_pos - g_midi_clock.nt;
360  ROAR_DBG("midi_clock_tick(void): g_pos is %u samples (%5.2f%%) ahead of nt.", diff, (float)diff/g_midi_clock.spt);
361
362  g_midi_clock.nt   = ROAR_MATH_OVERFLOW_ADD(g_midi_clock.nt, g_midi_clock.spt);
363
364  if ( streams_get_flag(g_midi_clock.stream, ROAR_FLAG_SYNC) ) {
365   ROAR_DBG("midi_clock_tick(void): TICK! (nt=%lu)", g_midi_clock.nt);
366  } else {
367   ROAR_DBG("midi_clock_tick(void): silent tick. (nt=%lu)", g_midi_clock.nt);
368  }
369 }
370
371 return 0;
372}
373
374// CB:
375
376int midi_cb_init (void) {
377#ifdef _HAVE_CONSOLE
378 struct roar_stream * s;
379 struct roar_stream_server * ss;
380 int i;
381 char * files[] = {
382                   "/dev/console",
383#ifdef __linux__
384                   "/dev/tty0",
385                   "/dev/vc/0",
386#endif
387                   NULL
388                  };
389
390 g_midi_cb.console  = -1;
391 g_midi_cb.stream   = -1;
392 g_midi_cb.stoptime =  0;
393 g_midi_cb.playing  =  0;
394
395 for (i = 0; files[i] != NULL; i++) {
396  if ( (g_midi_cb.console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 )
397   break;
398 }
399
400 if ( g_midi_cb.console == -1 )
401  return -1;
402
403 if ( (g_midi_cb.stream = streams_new()) == -1 ) {
404  ROAR_WARN("Error while initializing MIDI subsystem component CB");
405  midi_cb_free();
406  return -1;
407 }
408
409 midi_vio_set_dummy(g_midi_cb.stream);
410
411 streams_get(g_midi_cb.stream, &ss);
412 s = ROAR_STREAM(ss);
413
414 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
415
416 s->pos_rel_id    = -1;
417
418 s->info.codec    =  0;
419 ss->codec_orgi   =  0;
420
421 s->info.channels =  1;
422 s->info.rate     = 1193180;
423 s->info.bits     =  8;
424
425 if ( streams_set_dir(g_midi_cb.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
426  ROAR_WARN("Error while initializing MIDI subsystem component CB");
427  midi_cb_free();
428  return -1;
429 }
430
431 streams_set_name(g_midi_cb.stream, "Console speaker bridge");
432
433 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_OUTPUT);
434 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_PRIMARY);
435 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_HWMIXER);
436
437 return 0;
438#else
439 g_midi_cb.console  = -1;
440 g_midi_cb.stream   = -1;
441
442 return -1;
443#endif
444}
445
446int midi_cb_free (void) {
447#ifdef _HAVE_CONSOLE
448
449 midi_cb_stop();
450
451 if ( g_midi_cb.stream != -1 )
452  streams_delete(g_midi_cb.stream);
453
454 if ( g_midi_cb.console != -1 )
455  close(g_midi_cb.console);
456
457 return 0;
458#else
459 return -1;
460#endif
461}
462
463int midi_cb_play(float t, float freq, int override) {
464 float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels;
465
466/*
467#define MIDI_CB_NOOVERRIDE 0
468#define MIDI_CB_OVERRIDE   1
469*/
470 if ( g_midi_cb.playing && override != MIDI_CB_OVERRIDE )
471  return -1;
472
473 g_midi_cb.stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t);
474 midi_cb_start(freq);
475 g_midi_cb.playing = 1;
476
477 return 0;
478}
479
480int midi_cb_update (void) {
481 if ( !g_midi_cb.playing )
482  return 0;
483
484 if ( g_midi_cb.stoptime <= g_pos )
485  midi_cb_stop();
486
487 return 0;
488}
489
490int midi_cb_start(float freq) {
491// On linux this uses ioctl KIOCSOUND
492#ifdef __linux__
493 if ( g_midi_cb.console == -1 )
494  return -1;
495
496 if ( ioctl(g_midi_cb.console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 )
497  return -1;
498
499 return 0;
500#else
501 return -1;
502#endif
503}
504
505int midi_cb_stop (void) {
506#ifdef __linux__
507 g_midi_cb.playing = 0;
508 return midi_cb_start(0);
509#else
510 return -1;
511#endif
512}
513
514// VIO:
515
516int     midi_vio_set_dummy(int stream) {
517 struct roar_stream_server * ss;
518
519 if ( streams_get(stream, &ss) == -1 )
520  return -1;
521
522 ss->vio.read     = NULL;
523 ss->vio.write    = NULL;
524 ss->vio.lseek    = NULL;
525 ss->vio.nonblock = (int (*)(struct roar_vio_calls * vio, int state))midi_vio_ok;
526 ss->vio.sync     = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
527 ss->vio.ctl      = NULL;
528 ss->vio.close    = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
529
530 return 0;
531}
532
533int     midi_vio_ok(struct roar_vio_calls * vio, ...) {
534 return 0;
535}
536
537//ll
Note: See TracBrowser for help on using the repository browser.