source: roaraudio/roard/midi.c @ 1866:ebdddc368270

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

moved adding of buffer out of parser

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