source: roaraudio/roard/midi.c @ 5089:b6accd8444a9

Last change on this file since 5089:b6accd8444a9 was 5089:b6accd8444a9, checked in by phi, 13 years ago

Fixed endlessloop in MIDI subsystem (Closes: #137)

File size: 22.6 KB
Line 
1//midi.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
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, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include "roard.h"
27
28#ifndef ROAR_WITHOUT_DCOMP_MIDI
29
30#if defined(ROAR_HAVE_IO_POSIX) && !defined(ROAR_TARGET_WIN32)
31#define _HAVE_CONSOLE
32#endif
33
34int midi_init_config(void) {
35 midi_config.init        = 1;
36 midi_config.inited      = 0;
37
38#ifndef ROAR_WITHOUT_DCOMP_CB
39 midi_config.init_cb     = 0;
40 midi_config.console_dev = NULL;
41#endif
42
43 return 0;
44}
45
46int midi_init (void) {
47 struct roar_stream_server * ss;
48 char cmap[16];
49 int i;
50
51 midi_config.inited = 0;
52
53 if ( midi_config.init ) {
54#ifndef ROAR_WITHOUT_DCOMP_CB
55  if ( midi_config.init_cb ) {
56   if ( midi_cb_init() == -1 ) {
57    ROAR_WARN("Can not initialize MIDI subsystem component CB");
58   }
59  } else {
60   g_midi_cb.console = -1;
61  }
62#endif
63
64  if ( midi_clock_init() == -1 ) {
65   ROAR_WARN("Can not initialize MIDI subsystem component clock");
66  }
67
68 }
69
70 g_midi_mess.buf = NULL;
71
72 if ( (g_midi_mixer.stream = add_mixer(ROAR_SUBSYS_MIDI, _MIXER_NAME("MIDI"), &ss)) == -1 ) {
73  ROAR_WARN("Can not create MIDI mixer");
74 }
75
76 roardsp_chanlist_init(cmap,   16, ROARDSP_CHANLIST_MAP_MIDI);
77 streams_set_map(g_midi_mixer.stream, cmap, 16);
78
79 ss->state = ROAR_STREAMSTATE_OLD;
80
81 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
82  if ( g_streams[i] != NULL ) {
83   if ( streams_get_subsys(i) == ROAR_SUBSYS_MIDI ) {
84    streams_set_mixer_stream(i, g_midi_mixer.stream);
85   }
86  }
87 }
88
89 midi_config.inited |= MIDI_INITED_MAIN;
90
91 return 0;
92}
93
94int midi_free (void) {
95 if ( midi_reinit() == -1 )
96  return -1;
97
98#ifndef ROAR_WITHOUT_DCOMP_CB
99 if ( midi_cb_free() == -1 )
100  return -1;
101#endif
102
103 return 0;
104}
105
106int midi_update(void) {
107
108 if ( g_midi_clock.stream != -1 )
109  midi_check_bridge(g_midi_clock.stream);
110
111#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
112 midi_conv_mes2ssynth();
113#endif
114
115#ifndef ROAR_WITHOUT_DCOMP_CB
116 return midi_cb_update();
117#else
118 return 0;
119#endif
120}
121
122int midi_reinit(void) {
123
124 if ( g_midi_mess.buf != NULL )
125  roar_buffer_free(g_midi_mess.buf);
126
127 g_midi_mess.buf = NULL;
128
129 return 0;
130}
131
132// STREAMS:
133
134int midi_check_stream  (int id) {
135 struct roar_stream        *   s;
136 struct roar_stream_server *  ss;
137 struct roar_buffer        *   b;
138 void                      * buf;
139 ssize_t                     len;
140
141 if ( g_streams[id] == NULL )
142  return -1;
143
144 ROAR_DBG("midi_check_stream(id=%i) = ?", id);
145
146 s = ROAR_STREAM(ss = g_streams[id]);
147
148 if ( s->dir == ROAR_DIR_BRIDGE )
149  return midi_check_bridge(id);
150
151 switch (s->info.codec) {
152  case ROAR_CODEC_MIDI:
153   break;
154  default:
155    streams_delete(id);
156    return -1;
157 }
158
159 if ( roar_buffer_new_data(&b, MIDI_READ_SIZE, &buf) == -1 ) {
160  ROAR_ERR("midi_check_stream(*): Can not alloc buffer space!");
161  ROAR_DBG("midi_check_stream(*) = -1");
162  return -1;
163 }
164
165 if ( (len = stream_vio_s_read(ss, buf, MIDI_READ_SIZE)) < 1 ) {
166  streams_delete(id);
167  roar_buffer_free(b);
168  return -1;
169 }
170
171 roar_buffer_set_len(b, len);
172
173 if ( stream_add_buffer(id, b) == -1 ) {
174  roar_buffer_free(b);
175  streams_delete(id);
176  return -1;
177 }
178
179 switch (s->info.codec) {
180  case ROAR_CODEC_MIDI:
181    return midi_conv_midi2mes(id);
182   break;
183  default:
184    streams_delete(id);
185    return -1;
186 }
187
188 ROAR_DBG("midi_check_stream(id=%i) = 0", id);
189 return 0;
190}
191
192int midi_send_stream   (int id) {
193 struct roar_stream        *   s;
194 struct roar_stream_server *  ss;
195
196 if ( g_streams[id] == NULL )
197  return -1;
198
199 ROAR_DBG("midi_send_stream(id=%i) = ?", id);
200
201 s = ROAR_STREAM(ss = g_streams[id]);
202
203 switch (s->info.codec) {
204  case ROAR_CODEC_MIDI:
205    return midi_conv_mes2midi(id);
206   break;
207  default:
208    streams_delete(id);
209    return -1;
210 }
211
212 return 0;
213}
214
215#if 1
216static int _cache_to_mes(struct midi_message * mes, unsigned char * data, size_t len,
217                         int id, struct roar_stream_server * ss) {
218 unsigned char type = *data;
219
220 ROAR_DBG("_cache_to_mes(*): type=0x%.2X", (unsigned int)type);
221
222 if (type == MIDI_TYPE_CLOCK_TICK || type == MIDI_TYPE_CLOCK_START || type == MIDI_TYPE_CLOCK_STOP ) {
223  if ( len != 1 )
224   return -1;
225
226  mes->type = type;
227
228  if ( type == MIDI_TYPE_CLOCK_TICK ) {
229// TODO:
230   ROAR_STREAM(ss)->pos = ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(ss)->pos, 1);
231  }
232 } else {
233  mes->type    = type & 0xF0;
234  mes->channel = type & 0x0F;
235
236  switch (mes->type) {
237   case MIDI_TYPE_NOTE_ON:
238   case MIDI_TYPE_NOTE_OFF:
239     if ( len != 3 )
240      return -1;
241     roar_midi_note_from_midiid(&(mes->d.note), data[1]);
242
243     // if velocity is zero we have a NOTE OFF event
244     if ( data[2] == 0 )
245      mes->type = MIDI_TYPE_NOTE_OFF;
246   case MIDI_TYPE_PA:
247   case MIDI_TYPE_CONTROLER:
248     mes->kk = data[1];
249     mes->vv = data[2];
250    break;
251   case MIDI_TYPE_PROGRAM:
252   case MIDI_TYPE_MA:
253     if ( len != 2 )
254      return -1;
255     mes->vv = data[1];
256    break;
257   case MIDI_TYPE_PB:
258   case MIDI_TYPE_SYSEX:
259     ROAR_WARN("_cache_to_mes(id=%i): Message of Type 0x%.2X (PB or SysEx) not yet supported", id, (unsigned int)type);
260     return -1;
261    break;
262  }
263 }
264
265 switch (mes->type) {
266  case MIDI_TYPE_CONTROLER:
267    switch (mes->kk) {
268     case MIDI_CCE_MAIN_VOL:
269       if ( (516 * mes->vv) > 65100 ) { // max volume
270        ss->mixer.mixer[mes->channel] = 65535;
271       } else {
272        ss->mixer.mixer[mes->channel] = 516 * mes->vv;
273       }
274      break;
275    }
276   break;
277 }
278
279 ROAR_DBG("_cache_to_mes(*) = 0");
280 return 0;
281}
282
283int midi_conv_midi2mes (int id) {
284 struct roar_stream        *   s;
285 struct roar_stream_server *  ss;
286 size_t have;
287 void * bufdata;
288 unsigned char * data;
289 unsigned char   cache[3];
290 size_t cachepos = 0;
291 int sync = 1;
292 struct roar_buffer        * buf = NULL;
293 struct midi_message       * mes = NULL;
294
295 if ( g_streams[id] == NULL )
296  return -1;
297
298 ROAR_DBG("midi_conv_midi2mes(id=%i) = ?", id);
299
300 s = ROAR_STREAM(ss = g_streams[id]);
301
302 while (ss->buffer != NULL) {
303  if ( roar_buffer_get_len(ss->buffer, &have) == -1 )
304   return -1;
305  if ( roar_buffer_get_data(ss->buffer, &bufdata) == -1 )
306   return -1;
307  data = bufdata;
308
309  while (have) {
310   if ( !sync ) {
311    if ( *data & 0x80 ) {
312     cachepos = 0;
313     sync = 1;
314    } else {
315     data++;
316     have--;
317     continue;
318    }
319   }
320
321   if ( (*data & 0x80) && cachepos != 0 ) {
322    // submit cache.
323
324    if ( midi_new_bufmes(&buf, &mes) == -1 ) {
325     // this is bad.
326     // TODO: handle better.
327     return -1;
328    }
329
330    if ( _cache_to_mes(mes, cache, cachepos, id, ss) == -1 ) {
331     ROAR_WARN("midi_conv_midi2mes(id=%i): Message can not be parsed.", id);
332     roar_buffer_free(buf);
333    } else {
334     midi_add_buf(id, buf); // FIXME: don't we need error handling here?
335    }
336
337    cachepos = 0;
338   } else if ( cachepos == sizeof(cache) ) {
339    // lost sync.
340    sync = 0;
341    continue;
342   }
343
344   ROAR_DBG("midi_conv_midi2mes(id=%i): cache[%u] = 0x%.2X", id, (unsigned int)cachepos, (unsigned int)*data);
345   cache[cachepos++] = *data;
346   data++;
347   have--;
348  }
349
350  roar_buffer_next(&(ss->buffer));
351 }
352
353 if ( cachepos ) {
354  if ( midi_new_bufmes(&buf, &mes) == -1 ) {
355   // this is bad.
356   // TODO: handle better.
357   return -1;
358  }
359
360  if ( _cache_to_mes(mes, cache, cachepos, id, ss) == -1 ) {
361   ROAR_WARN("midi_conv_midi2mes(id=%i): Message can not be parsed.", id);
362   roar_buffer_free(buf);
363  } else {
364   midi_add_buf(id, buf); // FIXME: don't we need error handling here?
365   cachepos = 0;
366  }
367 }
368
369 if ( cachepos ) {
370  if ( roar_buffer_new_data(&buf, cachepos, &bufdata) == -1 )
371   return -1;
372
373  memcpy(bufdata, cache, cachepos);
374
375  ss->buffer = buf;
376 }
377
378 return 0;
379}
380#else
381int midi_conv_midi2mes (int id) {
382 void          * bufdata;
383 unsigned char * data;
384 struct roar_stream        *   s;
385 struct roar_stream_server *  ss;
386 struct roar_buffer        * buf = NULL;
387 struct midi_message       * mes = NULL;
388 size_t need = 0, have = 0, last_have = 0, old_have = 0;
389 int alive = 1;
390
391 if ( g_streams[id] == NULL )
392  return -1;
393
394 ROAR_DBG("midi_conv_midi2mes(id=%i) = ?", id);
395
396 s = ROAR_STREAM(ss = g_streams[id]);
397
398 while (ss->buffer != NULL && alive) {
399  ROAR_DBG("midi_conv_midi2mes(id=%i): ss->buffer=%p, alive=%i", id, ss->buffer, alive);
400
401  if ( roar_buffer_get_data(ss->buffer, &bufdata) == -1 ) {
402   alive = 0;
403   continue;
404  }
405
406  data = bufdata;
407
408  if ( roar_buffer_get_len(ss->buffer, &have) == -1 ) {
409   alive = 0;
410   continue;
411  }
412  old_have = have;
413
414  while (have && alive) {
415   ROAR_DBG("midi_conv_midi2mes(id=%i): have=%llu, alive=%i", id, (long long unsigned int)have, alive);
416
417   if ( *data & 0x80 ) {
418    if ( buf != NULL ) {
419     midi_add_buf(id, buf);
420     buf = NULL;
421    }
422
423    last_have = have;
424
425    need = 0;
426    if ( midi_new_bufmes(&buf, &mes) == -1 ) {
427     alive = 0;
428     continue;
429    }
430
431    if (*data == MIDI_TYPE_CLOCK_TICK || *data == MIDI_TYPE_CLOCK_START || *data == MIDI_TYPE_CLOCK_STOP ) {
432     mes->type = *data;
433
434     if ( *data == MIDI_TYPE_CLOCK_TICK ) {
435      s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1);
436     }
437    } else {
438     mes->type    = *data & 0xF0;
439     mes->channel = *data & 0x0F;
440     switch (*data & 0xF0) {
441      case MIDI_TYPE_NOTE_ON:
442      case MIDI_TYPE_NOTE_OFF:
443        need = 2;
444       break;
445      case MIDI_TYPE_PA:
446        need = 2;
447       break;
448      case MIDI_TYPE_CONTROLER:
449        need = 2;
450       break;
451      case MIDI_TYPE_PROGRAM:
452        need = 1;
453       break;
454      case MIDI_TYPE_MA:
455        need = 1;
456       break;
457      case MIDI_TYPE_PB:
458      case MIDI_TYPE_SYSEX:
459        need = 1;
460       break;
461     }
462    }
463
464   } else {
465
466    if ( mes == NULL ) {
467     ROAR_WARN("midi_conv_midi2mes(id=%i): Lost sync.", id);
468     data++;
469     have--;
470     continue;
471    }
472
473    switch (mes->type) {
474      case MIDI_TYPE_NOTE_ON:
475      case MIDI_TYPE_NOTE_OFF:
476       if ( need == 2 ) {
477        roar_midi_note_from_midiid(&(mes->d.note), *data);
478
479        // if velocity is zero we have a NOTE OFF event
480        if ( data[1] == 0 )
481         mes->type = MIDI_TYPE_NOTE_OFF;
482       }
483      case MIDI_TYPE_PA:
484      case MIDI_TYPE_CONTROLER:
485        if ( need == 2 ) {
486         mes->kk = *data;
487        } else {
488         mes->vv = *data;
489        }
490       break;
491      case MIDI_TYPE_PROGRAM:
492      case MIDI_TYPE_MA:
493        mes->vv = *data;
494       break;
495      case MIDI_TYPE_PB:
496      case MIDI_TYPE_SYSEX:
497        ROAR_WARN("midi_conv_midi2mes(id=%i): Message of Type 0x%.2X (PB or SysEx) not yet supported", id, mes->type);
498       break;
499    }
500
501    if ( need )
502     need--;
503
504    if ( !need ) {
505     switch (mes->type) {
506      case MIDI_TYPE_CONTROLER:
507       switch (mes->kk) {
508        case MIDI_CCE_MAIN_VOL:
509         if ( 516 * mes->vv > 65100 ) { // max volume
510          ss->mixer.mixer[mes->channel] = 65535;
511         } else {
512          ss->mixer.mixer[mes->channel] = 516 * mes->vv;
513         }
514         break;
515       }
516      break;
517     }
518    }
519
520   }
521   data++;
522   have--;
523  }
524
525  if ( need ) {
526   ROAR_DBG("midi_conv_midi2mes(id=%i) = ?", id);
527   if ( roar_buffer_set_offset(ss->buffer, old_have - last_have) == -1 ) {
528    ROAR_ERR("midi_conv_midi2mes(*): FIXME: BUG!!! Need to restore buffer here with corrected length");
529   }
530  } else if ( !have ) {
531   roar_buffer_next(&(ss->buffer));
532  }
533
534  if ( need && buf != NULL ) {
535   roar_buffer_free(buf);
536   buf = NULL;
537  } else if ( buf != NULL ) {
538   midi_add_buf(id, buf);
539   buf = NULL;
540  }
541 }
542
543 return 0;
544}
545#endif
546
547#define _nb  len++; *(d++)
548int midi_conv_mes2midi (int id) {
549 struct roar_stream        *   s;
550 struct roar_stream_server *  ss;
551 struct roar_buffer        * buf = g_midi_mess.buf;
552 struct midi_message       * mes = NULL;
553 void                      * bufdata;
554 unsigned char               data[3];
555 unsigned char             * d;
556 int                         len;
557 int                         send_clock;
558
559 if ( g_streams[id] == NULL )
560  return -1;
561
562 ROAR_DBG("midi_conv_mes2midi(id=%i) = ?", id);
563
564 s = ROAR_STREAM(ss = g_streams[id]);
565
566 send_clock = streams_get_flag(id, ROAR_FLAG_SYNC);
567
568 while (buf != NULL) {
569  if ( roar_buffer_get_data(buf, &bufdata) == -1 ) {
570   return -1;
571  }
572
573  mes = bufdata;
574
575  if (mes->type == MIDI_TYPE_CLOCK_TICK || mes->type == MIDI_TYPE_CLOCK_START || mes->type == MIDI_TYPE_CLOCK_STOP ) {
576   if ( send_clock ) {
577    if ( stream_vio_s_write(ss, &(mes->type), 1) != 1 ) {
578     streams_delete(id);
579     return -1;
580    }
581   }
582
583   if ( mes->type == MIDI_TYPE_CLOCK_TICK ) {
584    s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1);
585   }
586  } else {
587   len = 0;
588   d   = data;
589
590   _nb = (mes->type & 0xF0) | (mes->channel & 0x0F);
591
592   switch (mes->type) {
593    case MIDI_TYPE_CONTROLER:
594      switch (mes->kk) {
595       case MIDI_CCE_MAIN_VOL:
596         if ( 516 * mes->vv > 65100 ) { // max volume
597          ss->mixer.mixer[mes->channel] = 65535;
598         } else {
599          ss->mixer.mixer[mes->channel] = 516 * mes->vv;
600         }
601        break;
602      }
603    case MIDI_TYPE_NOTE_ON:
604    case MIDI_TYPE_NOTE_OFF:
605    case MIDI_TYPE_PA:
606      _nb = mes->kk;
607      _nb = mes->vv;
608     break;
609    case MIDI_TYPE_PROGRAM:
610    case MIDI_TYPE_MA:
611      _nb = mes->vv;
612     break;
613    case MIDI_TYPE_PB:
614    case MIDI_TYPE_SYSEX:
615     break;
616   }
617
618   if ( stream_vio_s_write(ss, data, len) != len ) {
619    streams_delete(id);
620    return -1;
621   }
622  }
623
624  if ( roar_buffer_get_next(buf, &buf) == -1 )
625   buf = NULL;
626 }
627
628 return 0;
629}
630#undef _nb
631
632#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
633int midi_conv_mes2ssynth(void) {
634 struct roar_buffer        * buf = g_midi_mess.buf;
635 struct midi_message       * mes = NULL;
636 void                      * bufdata;
637
638 while (buf != NULL) {
639  if ( roar_buffer_get_data(buf, &bufdata) == -1 ) {
640   return -1;
641  }
642
643  mes = bufdata;
644
645  if ( ssynth_eval_message(mes) == -1 )
646   return -1;
647
648  if ( roar_buffer_get_next(buf, &buf) == -1 )
649   buf = NULL;
650 }
651
652 return 0;
653}
654#endif
655
656int midi_new_bufmes    (struct roar_buffer ** buf, struct midi_message ** mes) {
657 if ( buf == NULL || mes == NULL )
658  return -1;
659
660 *buf = (void*)(*mes = NULL);
661
662 if ( roar_buffer_new_data(buf, sizeof(struct midi_message), (void**)mes) == -1 ) {
663  *buf = (void*)(*mes = NULL);
664  return -1;
665 }
666
667 memset((void*)*mes, 0, sizeof(struct midi_message));
668
669 (*mes)->type = MIDI_TYPE_NONE;
670
671 return 0;
672}
673
674int midi_add_buf       (int id, struct roar_buffer * buf) {
675 struct midi_message * mes;
676 void * bufdata;
677
678 if ( id == -1 || buf == NULL )
679  return -1;
680
681 if ( roar_buffer_get_data(buf, &bufdata) == -1 )
682  return -1;
683
684 mes = bufdata;
685
686 ROAR_DBG("midi_add_buf(id=%i, buf=%p) = ?", id, buf);
687 ROAR_DBG("midi_add_buf(*): MIDI Message of Type 0x%.2X", mes->type);
688 ROAR_DBG("midi_add_buf(*): Channel: %i", mes->channel);
689 ROAR_DBG("midi_add_buf(*): flags=0x%.2X", mes->flags);
690 ROAR_DBG("midi_add_buf(*): kk=0x%.2X, vv=0x%.2X", mes->kk, mes->vv);
691
692 if ( g_midi_mess.buf == NULL ) {
693  g_midi_mess.buf = buf;
694 } else {
695  roar_buffer_add(g_midi_mess.buf, buf);
696 }
697
698 ROAR_DBG("midi_add_buf(*) = 0");
699 return 0;
700}
701
702// bridges:
703int midi_check_bridge  (int id) {
704
705 ROAR_DBG("midi_check_bridge(id=%i) = ?", id);
706
707 if ( id == g_midi_clock.stream ) {
708  midi_clock_tick();
709
710  return 0;
711 }
712
713 return -1;
714}
715
716// clock:
717
718int midi_clock_init (void) {
719 struct roar_stream * s;
720 struct roar_stream_server * ss;
721
722 if ( (g_midi_clock.stream = streams_new()) == -1 ) {
723  ROAR_WARN("Error while initializing MIDI subsystem component clock");
724  return -1;
725 }
726
727 client_stream_add(g_self_client, g_midi_clock.stream);
728
729 midi_vio_set_dummy(g_midi_clock.stream);
730
731 streams_get(g_midi_clock.stream, &ss);
732 s = ROAR_STREAM(ss);
733
734 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
735
736 s->pos_rel_id    =  g_midi_clock.stream;
737
738 s->info.codec    =  ROAR_CODEC_MIDI;
739 ss->codec_orgi   =  ROAR_CODEC_MIDI;
740
741 s->info.channels = 1; // we have only one channel, ticking on channel 0
742 s->info.rate     = ROAR_MIDI_TICKS_PER_BEAT; // one beat per sec
743 s->info.bits     = ROAR_MIDI_BITS;
744
745 if ( streams_set_dir(g_midi_clock.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
746  ROAR_WARN("Error while initializing MIDI subsystem component clock");
747  return -1;
748 }
749
750 streams_set_name(g_midi_clock.stream, "MIDI Clock");
751
752 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_PRIMARY);
753 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_SYNC);
754
755 midi_clock_set_bph(3600); // one beat per sec
756
757 ss->state = ROAR_STREAMSTATE_NEW;
758
759 midi_config.inited |= MIDI_INITED_CLOCK;
760
761 return 0;
762}
763
764int midi_clock_set_bph (uint_least32_t bph) {
765 uint_least32_t sph = g_sa->rate/2 * 75 * g_sa->channels; // samples per houre
766
767 g_midi_clock.bph  = bph;
768
769 g_midi_clock.spt  = sph/bph;
770
771 g_midi_clock.nt   = ROAR_MATH_OVERFLOW_ADD(g_pos, g_midi_clock.spt);
772
773 return 0;
774}
775
776int midi_clock_tick (void) {
777 struct roar_buffer  * buf;
778 struct midi_message * mes;
779 struct roar_stream_server * ss;
780 unsigned int diff;
781
782 if ( streams_get(g_midi_clock.stream, &ss) == -1 ) {
783  ROAR_ERR("midi_clock_tick(void): Something very BAD happend: can not get stream object of my own stream!");
784  g_midi_clock.stream = -1;
785  ROAR_WARN("midi_clock_tick(void): Disabled MIDI Clock");
786  return -1;
787 }
788
789 while ( g_pos >= g_midi_clock.nt ) {
790  diff = g_pos - g_midi_clock.nt;
791  ROAR_DBG("midi_clock_tick(void): g_pos is %u samples (%5.2f%%) after of nt.", diff, (float)diff/g_midi_clock.spt);
792
793  g_midi_clock.nt   = ROAR_MATH_OVERFLOW_ADD(g_midi_clock.nt, g_midi_clock.spt);
794
795  if ( streams_get_flag(g_midi_clock.stream, ROAR_FLAG_SYNC) ) {
796   ROAR_DBG("midi_clock_tick(void): TICK! (nt=%lu)", g_midi_clock.nt);
797   if ( midi_new_bufmes(&buf, &mes) == -1 ) {
798    ROAR_ERR("midi_clock_tick(void): Can not create Clock-Tick-Message");
799   }
800
801   mes->type = MIDI_TYPE_CLOCK_TICK;
802
803   if ( midi_add_buf(g_midi_clock.stream, buf) == -1 ) {
804    ROAR_ERR("midi_clock_tick(void): Can not add Clock-Tick-Message");
805    roar_buffer_free(buf);
806    return -1;
807   }
808
809   ROAR_STREAM(ss)->pos = ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(ss)->pos, 1);
810   ss->state = ROAR_STREAMSTATE_OLD;
811  } else {
812   ROAR_DBG("midi_clock_tick(void): silent tick. (nt=%lu)", g_midi_clock.nt);
813  }
814 }
815
816 return 0;
817}
818
819// CB:
820
821#ifndef ROAR_WITHOUT_DCOMP_CB
822int midi_cb_init (void) {
823#ifdef _HAVE_CONSOLE
824 struct roar_stream * s;
825 struct roar_stream_server * ss;
826 int i;
827 char * files[] = {
828                   "/NX",
829                   "/dev/roarconsole",
830                   "/dev/console",
831#ifdef __linux__
832                   "/dev/tty0",
833                   "/dev/vc/0",
834#endif
835                   NULL
836                  };
837
838 g_midi_cb.console  = -1;
839 g_midi_cb.stream   = -1;
840 g_midi_cb.stoptime =  0;
841 g_midi_cb.playing  =  0;
842
843 if ( midi_config.console_dev != NULL ) {
844  files[0] = midi_config.console_dev;
845 }
846
847 for (i = 0; files[i] != NULL; i++) {
848  if ( (g_midi_cb.console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 )
849   break;
850 }
851
852 if ( g_midi_cb.console == -1 )
853  return -1;
854
855 if ( (g_midi_cb.stream = streams_new()) == -1 ) {
856  ROAR_WARN("Error while initializing MIDI subsystem component CB");
857  midi_cb_free();
858  return -1;
859 }
860
861 client_stream_add(g_self_client, g_midi_clock.stream);
862
863 midi_vio_set_dummy(g_midi_cb.stream);
864
865 streams_get(g_midi_cb.stream, &ss);
866 s = ROAR_STREAM(ss);
867
868 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
869
870 s->pos_rel_id    = -1;
871
872 s->info.codec    =  0;
873 ss->codec_orgi   =  0;
874
875 s->info.channels =  1;
876 s->info.rate     = 1193180;
877 s->info.bits     =  8;
878
879 if ( streams_set_dir(g_midi_cb.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
880  ROAR_WARN("Error while initializing MIDI subsystem component CB");
881  midi_cb_free();
882  return -1;
883 }
884
885 streams_set_name(g_midi_cb.stream, "Console speaker bridge");
886
887 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_OUTPUT);
888 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_PRIMARY);
889 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_HWMIXER);
890 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_MUTE);
891
892 midi_config.inited |= MIDI_INITED_CB;
893
894 return 0;
895#else
896 g_midi_cb.console  = -1;
897 g_midi_cb.stream   = -1;
898
899 return -1;
900#endif
901}
902
903int midi_cb_free (void) {
904#ifdef _HAVE_CONSOLE
905
906 midi_cb_stop();
907
908 if ( g_midi_cb.stream != -1 )
909  streams_delete(g_midi_cb.stream);
910
911 if ( g_midi_cb.console != -1 )
912  close(g_midi_cb.console);
913
914 return 0;
915#else
916 return -1;
917#endif
918}
919
920int midi_cb_play(float t, float freq, int override) {
921 float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels;
922
923/*
924#define MIDI_CB_NOOVERRIDE 0
925#define MIDI_CB_OVERRIDE   1
926*/
927 if ( g_midi_cb.playing && override != MIDI_CB_OVERRIDE )
928  return -1;
929
930 g_midi_cb.stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t);
931 midi_cb_start(freq);
932
933 return 0;
934}
935
936int midi_cb_update (void) {
937
938 if ( !streams_get_flag(g_midi_cb.stream, ROAR_FLAG_MUTE) ) {
939  if ( midi_cb_readbuf() == -1 )
940   return -1;
941 } else if ( g_midi_cb.playing ) {
942  midi_cb_stop();
943 }
944
945 if ( !g_midi_cb.playing )
946  return 0;
947
948/*
949 if ( g_midi_cb.stoptime <= g_pos )
950  midi_cb_stop();
951*/
952
953 ROAR_DBG("midi_cb_update(void) = ?");
954
955 return 0;
956}
957
958int midi_cb_start(float freq) {
959// On linux this uses ioctl KIOCSOUND
960#ifdef __linux__
961 if ( g_midi_cb.console == -1 )
962  return -1;
963
964 if ( ioctl(g_midi_cb.console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 )
965  return -1;
966
967 g_midi_cb.playing = 1;
968
969 return 0;
970#else
971 return -1;
972#endif
973}
974
975int midi_cb_stop (void) {
976#ifdef __linux__
977 int ret;
978
979 ret = midi_cb_start(0);
980 g_midi_cb.playing = 0;
981
982 return ret;
983#else
984 return -1;
985#endif
986}
987
988int midi_cb_readbuf(void) {
989 struct roar_buffer        * buf = g_midi_mess.buf;
990 struct midi_message       * mes = NULL;
991 void                      * bufdata;
992
993 ROAR_DBG("midi_cb_readbuf(void) = ?");
994
995 while (buf != NULL) {
996  ROAR_DBG("midi_cb_readbuf(void): buf=%p", buf);
997
998  if ( roar_buffer_get_data(buf, &bufdata) == -1 ) {
999   return -1;
1000  }
1001
1002  mes = bufdata;
1003
1004  switch (mes->type) {
1005   case MIDI_TYPE_NOTE_ON:
1006     midi_cb_start(mes->d.note.freq);
1007    break;
1008   case MIDI_TYPE_NOTE_OFF:
1009     midi_cb_stop();
1010    break;
1011   case MIDI_TYPE_CONTROLER:
1012     if ( mes->kk == MIDI_CCE_ALL_NOTE_OFF ) /* all note off */
1013      midi_cb_stop();
1014    break;
1015  }
1016
1017  if ( roar_buffer_get_next(buf, &buf) == -1 )
1018   buf = NULL;
1019 }
1020
1021 return 0;
1022}
1023#endif
1024
1025// VIO:
1026
1027int     midi_vio_set_dummy(int stream) {
1028 struct roar_stream_server * ss;
1029
1030 if ( streams_get(stream, &ss) == -1 )
1031  return -1;
1032
1033 ss->vio.read     = NULL;
1034 ss->vio.write    = NULL;
1035 ss->vio.lseek    = NULL;
1036 ss->vio.nonblock = (int (*)(struct roar_vio_calls * vio, int state))midi_vio_ok;
1037 ss->vio.sync     = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
1038 ss->vio.ctl      = NULL;
1039 ss->vio.close    = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
1040
1041 return 0;
1042}
1043
1044int     midi_vio_ok(struct roar_vio_calls * vio, ...) {
1045 return 0;
1046}
1047
1048#endif
1049
1050//ll
Note: See TracBrowser for help on using the repository browser.