source: roaraudio/roard/midi.c @ 1890:31ce7a132ea6

Last change on this file since 1890:31ce7a132ea6 was 1890:31ce7a132ea6, checked in by phi, 15 years ago

also stop on all-note-off event

File size: 15.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 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
60 if ( g_midi_mess.buf != NULL )
61  roar_buffer_free(g_midi_mess.buf);
62
63 g_midi_mess.buf = NULL;
64
65 return 0;
66}
67
68// STREAMS:
69
70int midi_check_stream  (int id) {
71 struct roar_stream        *   s;
72 struct roar_stream_server *  ss;
73 struct roar_buffer        *   b;
74 char                      * buf;
75 ssize_t                     len;
76 int                           i;
77
78 if ( g_streams[id] == NULL )
79  return -1;
80
81 ROAR_DBG("midi_check_stream(id=%i) = ?", id);
82
83 s = ROAR_STREAM(ss = g_streams[id]);
84
85 if ( s->dir == ROAR_DIR_BRIDGE )
86  return midi_check_bridge(id);
87
88 switch (s->info.codec) {
89  case ROAR_CODEC_MIDI:
90   break;
91  default:
92    streams_delete(id);
93    return -1;
94 }
95
96 if ( roar_buffer_new(&b, MIDI_READ_SIZE) == -1 ) {
97  ROAR_ERR("midi_check_stream(*): Can not alloc buffer space!");
98  ROAR_DBG("midi_check_stream(*) = -1");
99  return -1;
100 }
101
102 roar_buffer_get_data(b, (void **)&buf);
103
104 if ( (len = stream_vio_s_read(ss, buf, MIDI_READ_SIZE)) < 1 ) {
105  streams_delete(id);
106  roar_buffer_free(b);
107  return -1;
108 }
109
110 roar_buffer_set_len(b, len);
111
112 if ( stream_add_buffer(id, b) == -1 ) {
113  roar_buffer_free(b);
114  streams_delete(id);
115  return -1;
116 }
117
118 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
119  if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) {
120   if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) {
121    if ( stream_vio_write(i, buf, len) != len ) {
122     streams_delete(i);
123    }
124   }
125  }
126 }
127
128 switch (s->info.codec) {
129  case ROAR_CODEC_MIDI:
130    return midi_conv_midi2mes(id);
131   break;
132  default:
133    streams_delete(id);
134    return -1;
135 }
136
137 return 0;
138}
139
140int midi_send_stream   (int id) {
141 struct roar_stream        *   s;
142 struct roar_stream_server *  ss;
143
144 if ( g_streams[id] == NULL )
145  return -1;
146
147 ROAR_DBG("midi_send_stream(id=%i) = ?", id);
148
149 s = ROAR_STREAM(ss = g_streams[id]);
150
151 switch (s->info.codec) {
152  case ROAR_CODEC_MIDI:
153    return midi_conv_mes2midi(id);
154   break;
155  default:
156    streams_delete(id);
157    return -1;
158 }
159
160 return 0;
161}
162
163int midi_conv_midi2mes (int id) {
164 unsigned char * data;
165 struct roar_stream        *   s;
166 struct roar_stream_server *  ss;
167 struct roar_buffer        * buf = NULL;
168 struct midi_message       * mes = NULL;
169 size_t need = 0, have = 0, last_have = 0, old_have = 0;
170 int alive = 1;
171
172 if ( g_streams[id] == NULL )
173  return -1;
174
175 ROAR_DBG("midi_conv_midi2mes(id=%i) = ?", id);
176
177 s = ROAR_STREAM(ss = g_streams[id]);
178
179 while (ss->buffer != NULL && alive) {
180  if ( roar_buffer_get_data(ss->buffer, (void**)&data) == -1 ) {
181   alive = 0;
182   continue;
183  }
184
185  if ( roar_buffer_get_len(ss->buffer, &have) == -1 ) {
186   alive = 0;
187   continue;
188  }
189  old_have = have;
190
191  while (have && alive) {
192   if ( *data & 0x80 ) {
193    if ( buf != NULL ) {
194     midi_add_buf(id, buf);
195     buf = NULL;
196    }
197
198    last_have = have;
199
200    need = 0;
201    if ( midi_new_bufmes(&buf, &mes) == -1 ) {
202     alive = 0;
203     continue;
204    }
205
206    if (*data == MIDI_TYPE_CLOCK_TICK || *data == MIDI_TYPE_CLOCK_START || *data == MIDI_TYPE_CLOCK_STOP ) {
207     mes->type = *data;
208    } else {
209     mes->type    = *data & 0xF0;
210     mes->channel = *data & 0x0F;
211     switch (*data & 0xF0) {
212      case MIDI_TYPE_NOTE_ON:
213      case MIDI_TYPE_NOTE_OFF:
214        need = 2;
215       break;
216      case MIDI_TYPE_PA:
217        need = 2;
218       break;
219      case MIDI_TYPE_CONTROLER:
220        need = 2;
221       break;
222      case MIDI_TYPE_PROGRAM:
223        need = 1;
224       break;
225      case MIDI_TYPE_MA:
226        need = 1;
227       break;
228      case MIDI_TYPE_PB:
229      case MIDI_TYPE_SYSEX:
230        need = 1;
231       break;
232     }
233    }
234
235   } else {
236
237    if ( mes == NULL ) {
238     ROAR_WARN("midi_conv_midi2mes(id=%i): Lost sync.", id);
239     data++;
240     have--;
241     continue;
242    }
243
244    switch (mes->type) {
245      case MIDI_TYPE_NOTE_ON:
246      case MIDI_TYPE_NOTE_OFF:
247       if ( need == 2 ) {
248        roar_midi_note_from_midiid(&(mes->d.note), *data);
249       }
250      case MIDI_TYPE_PA:
251      case MIDI_TYPE_CONTROLER:
252        if ( need == 2 ) {
253         mes->kk = *data;
254        } else {
255         mes->vv = *data;
256        }
257       break;
258      case MIDI_TYPE_PROGRAM:
259      case MIDI_TYPE_MA:
260        mes->vv = *data;
261       break;
262      case MIDI_TYPE_PB:
263      case MIDI_TYPE_SYSEX:
264        ROAR_WARN("midi_conv_midi2mes(id=%i): Message of Type 0x%.2X (PB or SysEx) not yet supported", id, mes->type);
265       break;
266    }
267
268    if ( need )
269     need--;
270   }
271   data++;
272   have--;
273  }
274
275  if ( need ) {
276   if ( roar_buffer_set_offset(ss->buffer, old_have - last_have) == -1 ) {
277    ROAR_ERR("midi_conv_midi2mes(*): FIXME: BUG!!! Need to restore buffer here with corrected length");
278   }
279  } else if ( !have ) {
280   roar_buffer_next(&(ss->buffer));
281  }
282
283  if ( need && buf != NULL ) {
284   roar_buffer_free(buf);
285   buf = NULL;
286  } else if ( buf != NULL ) {
287   midi_add_buf(id, buf);
288   buf = NULL;
289  }
290 }
291
292 return 0;
293}
294
295#define _nb  len++; *(d++)
296int midi_conv_mes2midi (int id) {
297 struct roar_stream        *   s;
298 struct roar_stream_server *  ss;
299 struct roar_buffer        * buf = g_midi_mess.buf;
300 struct midi_message       * mes = NULL;
301 unsigned char               data[3];
302 unsigned char             * d;
303 int                         len;
304
305 if ( g_streams[id] == NULL )
306  return -1;
307
308 ROAR_DBG("midi_conv_mes2midi(id=%i) = ?", id);
309
310 s = ROAR_STREAM(ss = g_streams[id]);
311
312 while (buf != NULL) {
313  if ( roar_buffer_get_data(buf, (void**)&mes) == -1 ) {
314   return -1;
315  }
316
317  if (mes->type == MIDI_TYPE_CLOCK_TICK || mes->type == MIDI_TYPE_CLOCK_START || mes->type == MIDI_TYPE_CLOCK_STOP ) {
318   if ( stream_vio_s_write(ss, &(mes->type), 1) != 1 ) {
319    streams_delete(id);
320    return -1;
321   }
322  } else {
323   len = 0;
324   d   = data;
325
326   _nb = (mes->type & 0xF0) | (mes->channel & 0x0F);
327
328   switch (mes->type) {
329    case MIDI_TYPE_NOTE_ON:
330    case MIDI_TYPE_NOTE_OFF:
331    case MIDI_TYPE_PA:
332    case MIDI_TYPE_CONTROLER:
333      _nb = mes->kk;
334      _nb = mes->vv;
335     break;
336    case MIDI_TYPE_PROGRAM:
337    case MIDI_TYPE_MA:
338      _nb = mes->vv;
339     break;
340    case MIDI_TYPE_PB:
341    case MIDI_TYPE_SYSEX:
342     break;
343   }
344
345   if ( stream_vio_s_write(ss, data, len) != len ) {
346    streams_delete(id);
347    return -1;
348   }
349  }
350
351  if ( roar_buffer_get_next(buf, &buf) == -1 )
352   buf = NULL;
353 }
354
355 return 0;
356}
357#undef _nb
358
359int midi_new_bufmes    (struct roar_buffer ** buf, struct midi_message ** mes) {
360 if ( buf == NULL || mes == NULL )
361  return -1;
362
363 *buf = (void*)(*mes = NULL);
364
365 if ( roar_buffer_new(buf, sizeof(struct midi_message)) == -1 )
366  return -1;
367
368 if ( roar_buffer_get_data(*buf, (void**)mes) == -1 ) {
369  roar_buffer_free(*buf);
370  *buf = (void*)(*mes = NULL);
371  return -1;
372 }
373
374 memset((void*)*mes, 0, sizeof(struct midi_message));
375
376 (*mes)->type = MIDI_TYPE_NONE;
377
378 return 0;
379}
380
381int midi_add_buf       (int id, struct roar_buffer * buf) {
382 struct midi_message * mes;
383
384 if ( id == -1 || buf == NULL )
385  return -1;
386
387 if ( roar_buffer_get_data(buf, (void**)&mes) == -1 )
388  return -1;
389
390 ROAR_DBG("midi_add_buf(id=%i, buf=%p) = ?", id, buf);
391 ROAR_DBG("midi_add_buf(*): MIDI Message of Type 0x%.2X", mes->type);
392 ROAR_DBG("midi_add_buf(*): Channel: %i", mes->channel);
393 ROAR_DBG("midi_add_buf(*): flags=0x%.2X", mes->flags);
394 ROAR_DBG("midi_add_buf(*): kk=0x%.2X, vv=0x%.2X", mes->kk, mes->vv);
395
396 if ( g_midi_mess.buf == NULL ) {
397  g_midi_mess.buf = buf;
398 } else {
399  roar_buffer_add(g_midi_mess.buf, buf);
400 }
401
402 return 0;
403}
404
405// bridges:
406int midi_check_bridge  (int id) {
407
408 ROAR_DBG("midi_check_bridge(id=%i) = ?", id);
409
410 if ( id == g_midi_clock.stream ) {
411  midi_clock_tick();
412
413  return 0;
414 }
415
416 return -1;
417}
418
419// clock:
420
421int midi_clock_init (void) {
422 struct roar_stream * s;
423 struct roar_stream_server * ss;
424
425 if ( (g_midi_clock.stream = streams_new()) == -1 ) {
426  ROAR_WARN("Error while initializing MIDI subsystem component clock");
427  return -1;
428 }
429
430 midi_vio_set_dummy(g_midi_clock.stream);
431
432 streams_get(g_midi_clock.stream, &ss);
433 s = ROAR_STREAM(ss);
434
435 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
436
437 s->pos_rel_id    =  g_midi_clock.stream;
438
439 s->info.codec    =  ROAR_CODEC_MIDI;
440 ss->codec_orgi   =  ROAR_CODEC_MIDI;
441
442 s->info.channels =  0;
443 s->info.rate     = MIDI_RATE;
444 s->info.bits     =  8;
445
446 if ( streams_set_dir(g_midi_clock.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
447  ROAR_WARN("Error while initializing MIDI subsystem component clock");
448  return -1;
449 }
450
451 streams_set_name(g_midi_clock.stream, "MIDI Clock");
452
453 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_PRIMARY);
454 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_SYNC);
455
456 midi_clock_set_bph(3600); // one tick per sec
457
458 return 0;
459}
460
461int midi_clock_set_bph (uint_least32_t bph) {
462 uint_least32_t sph = g_sa->rate/2 * 75 * g_sa->channels; // samples per houre
463
464 g_midi_clock.bph  = bph;
465
466 g_midi_clock.spt  = sph/bph;
467
468 g_midi_clock.nt   = ROAR_MATH_OVERFLOW_ADD(g_pos, g_midi_clock.spt);
469
470 return 0;
471}
472
473int midi_clock_tick (void) {
474 struct roar_buffer  * buf;
475 struct midi_message * mes;
476 struct roar_stream_server * ss;
477 unsigned int diff;
478
479 if ( streams_get(g_midi_clock.stream, &ss) == -1 ) {
480  ROAR_ERR("midi_clock_tick(void): Something very BAD happend: can not get stream object of my own stream!");
481  g_midi_clock.stream = -1;
482  ROAR_WARN("midi_clock_tick(void): Disabled MIDI Clock");
483  return -1;
484 }
485
486 while ( g_pos >= g_midi_clock.nt ) {
487  diff = g_pos - g_midi_clock.nt;
488  ROAR_DBG("midi_clock_tick(void): g_pos is %u samples (%5.2f%%) ahead of nt.", diff, (float)diff/g_midi_clock.spt);
489
490  g_midi_clock.nt   = ROAR_MATH_OVERFLOW_ADD(g_midi_clock.nt, g_midi_clock.spt);
491
492  if ( streams_get_flag(g_midi_clock.stream, ROAR_FLAG_SYNC) ) {
493   ROAR_DBG("midi_clock_tick(void): TICK! (nt=%lu)", g_midi_clock.nt);
494   if ( midi_new_bufmes(&buf, &mes) == -1 ) {
495    ROAR_ERR("midi_clock_tick(void): Can not create Clock-Tick-Message");
496   }
497
498   mes->type = MIDI_TYPE_CLOCK_TICK;
499
500   if ( midi_add_buf(g_midi_clock.stream, buf) == -1 ) {
501    ROAR_ERR("midi_clock_tick(void): Can not add Clock-Tick-Message");
502    roar_buffer_free(buf);
503    return -1;
504   }
505
506   ROAR_STREAM(ss)->pos = ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(ss)->pos, 1);
507  } else {
508   ROAR_DBG("midi_clock_tick(void): silent tick. (nt=%lu)", g_midi_clock.nt);
509  }
510 }
511
512 return 0;
513}
514
515// CB:
516
517int midi_cb_init (void) {
518#ifdef _HAVE_CONSOLE
519 struct roar_stream * s;
520 struct roar_stream_server * ss;
521 int i;
522 char * files[] = {
523                   "/dev/console",
524#ifdef __linux__
525                   "/dev/tty0",
526                   "/dev/vc/0",
527#endif
528                   NULL
529                  };
530
531 g_midi_cb.console  = -1;
532 g_midi_cb.stream   = -1;
533 g_midi_cb.stoptime =  0;
534 g_midi_cb.playing  =  0;
535
536 for (i = 0; files[i] != NULL; i++) {
537  if ( (g_midi_cb.console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 )
538   break;
539 }
540
541 if ( g_midi_cb.console == -1 )
542  return -1;
543
544 if ( (g_midi_cb.stream = streams_new()) == -1 ) {
545  ROAR_WARN("Error while initializing MIDI subsystem component CB");
546  midi_cb_free();
547  return -1;
548 }
549
550 midi_vio_set_dummy(g_midi_cb.stream);
551
552 streams_get(g_midi_cb.stream, &ss);
553 s = ROAR_STREAM(ss);
554
555 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
556
557 s->pos_rel_id    = -1;
558
559 s->info.codec    =  0;
560 ss->codec_orgi   =  0;
561
562 s->info.channels =  1;
563 s->info.rate     = 1193180;
564 s->info.bits     =  8;
565
566 if ( streams_set_dir(g_midi_cb.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
567  ROAR_WARN("Error while initializing MIDI subsystem component CB");
568  midi_cb_free();
569  return -1;
570 }
571
572 streams_set_name(g_midi_cb.stream, "Console speaker bridge");
573
574 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_OUTPUT);
575 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_PRIMARY);
576 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_HWMIXER);
577 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_MUTE);
578
579 return 0;
580#else
581 g_midi_cb.console  = -1;
582 g_midi_cb.stream   = -1;
583
584 return -1;
585#endif
586}
587
588int midi_cb_free (void) {
589#ifdef _HAVE_CONSOLE
590
591 midi_cb_stop();
592
593 if ( g_midi_cb.stream != -1 )
594  streams_delete(g_midi_cb.stream);
595
596 if ( g_midi_cb.console != -1 )
597  close(g_midi_cb.console);
598
599 return 0;
600#else
601 return -1;
602#endif
603}
604
605int midi_cb_play(float t, float freq, int override) {
606 float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels;
607
608/*
609#define MIDI_CB_NOOVERRIDE 0
610#define MIDI_CB_OVERRIDE   1
611*/
612 if ( g_midi_cb.playing && override != MIDI_CB_OVERRIDE )
613  return -1;
614
615 g_midi_cb.stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t);
616 midi_cb_start(freq);
617
618 return 0;
619}
620
621int midi_cb_update (void) {
622
623 if ( !streams_get_flag(g_midi_cb.stream, ROAR_FLAG_MUTE) ) {
624  if ( midi_cb_readbuf() == -1 )
625   return -1;
626 } else if ( g_midi_cb.playing ) {
627  midi_cb_stop();
628 }
629
630 if ( !g_midi_cb.playing )
631  return 0;
632
633/*
634 if ( g_midi_cb.stoptime <= g_pos )
635  midi_cb_stop();
636*/
637
638 ROAR_DBG("midi_cb_update(void) = ?");
639
640 return 0;
641}
642
643int midi_cb_start(float freq) {
644// On linux this uses ioctl KIOCSOUND
645#ifdef __linux__
646 if ( g_midi_cb.console == -1 )
647  return -1;
648
649 if ( ioctl(g_midi_cb.console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 )
650  return -1;
651
652 g_midi_cb.playing = 1;
653
654 return 0;
655#else
656 return -1;
657#endif
658}
659
660int midi_cb_stop (void) {
661#ifdef __linux__
662 int ret;
663
664 ret = midi_cb_start(0);
665 g_midi_cb.playing = 0;
666
667 return ret;
668#else
669 return -1;
670#endif
671}
672
673int midi_cb_readbuf(void) {
674 struct roar_buffer        * buf = g_midi_mess.buf;
675 struct midi_message       * mes = NULL;
676
677 ROAR_DBG("midi_cb_readbuf(void) = ?");
678
679 while (buf != NULL) {
680  ROAR_DBG("midi_cb_readbuf(void): buf=%p", buf);
681
682  if ( roar_buffer_get_data(buf, (void**)&mes) == -1 ) {
683   return -1;
684  }
685
686  switch (mes->type) {
687   case MIDI_TYPE_NOTE_ON:
688     midi_cb_start(mes->d.note.freq);
689    break;
690   case MIDI_TYPE_NOTE_OFF:
691     midi_cb_stop();
692    break;
693   case MIDI_TYPE_CONTROLER:
694     if ( mes->kk == 123 ) /* all note off */
695      midi_cb_stop();
696    break;
697  }
698
699  if ( roar_buffer_get_next(buf, &buf) == -1 )
700   buf = NULL;
701 }
702
703 return 0;
704}
705
706// VIO:
707
708int     midi_vio_set_dummy(int stream) {
709 struct roar_stream_server * ss;
710
711 if ( streams_get(stream, &ss) == -1 )
712  return -1;
713
714 ss->vio.read     = NULL;
715 ss->vio.write    = NULL;
716 ss->vio.lseek    = NULL;
717 ss->vio.nonblock = (int (*)(struct roar_vio_calls * vio, int state))midi_vio_ok;
718 ss->vio.sync     = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
719 ss->vio.ctl      = NULL;
720 ss->vio.close    = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
721
722 return 0;
723}
724
725int     midi_vio_ok(struct roar_vio_calls * vio, ...) {
726 return 0;
727}
728
729//ll
Note: See TracBrowser for help on using the repository browser.