source: roaraudio/roard/midi.c @ 5090:d51be1bd8cd5

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

removed incorrect warning

File size: 22.5 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_buffer_free(buf);
362  } else {
363   midi_add_buf(id, buf); // FIXME: don't we need error handling here?
364   cachepos = 0;
365  }
366 }
367
368 if ( cachepos ) {
369  if ( roar_buffer_new_data(&buf, cachepos, &bufdata) == -1 )
370   return -1;
371
372  memcpy(bufdata, cache, cachepos);
373
374  ss->buffer = buf;
375 }
376
377 return 0;
378}
379#else
380int midi_conv_midi2mes (int id) {
381 void          * bufdata;
382 unsigned char * data;
383 struct roar_stream        *   s;
384 struct roar_stream_server *  ss;
385 struct roar_buffer        * buf = NULL;
386 struct midi_message       * mes = NULL;
387 size_t need = 0, have = 0, last_have = 0, old_have = 0;
388 int alive = 1;
389
390 if ( g_streams[id] == NULL )
391  return -1;
392
393 ROAR_DBG("midi_conv_midi2mes(id=%i) = ?", id);
394
395 s = ROAR_STREAM(ss = g_streams[id]);
396
397 while (ss->buffer != NULL && alive) {
398  ROAR_DBG("midi_conv_midi2mes(id=%i): ss->buffer=%p, alive=%i", id, ss->buffer, alive);
399
400  if ( roar_buffer_get_data(ss->buffer, &bufdata) == -1 ) {
401   alive = 0;
402   continue;
403  }
404
405  data = bufdata;
406
407  if ( roar_buffer_get_len(ss->buffer, &have) == -1 ) {
408   alive = 0;
409   continue;
410  }
411  old_have = have;
412
413  while (have && alive) {
414   ROAR_DBG("midi_conv_midi2mes(id=%i): have=%llu, alive=%i", id, (long long unsigned int)have, alive);
415
416   if ( *data & 0x80 ) {
417    if ( buf != NULL ) {
418     midi_add_buf(id, buf);
419     buf = NULL;
420    }
421
422    last_have = have;
423
424    need = 0;
425    if ( midi_new_bufmes(&buf, &mes) == -1 ) {
426     alive = 0;
427     continue;
428    }
429
430    if (*data == MIDI_TYPE_CLOCK_TICK || *data == MIDI_TYPE_CLOCK_START || *data == MIDI_TYPE_CLOCK_STOP ) {
431     mes->type = *data;
432
433     if ( *data == MIDI_TYPE_CLOCK_TICK ) {
434      s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1);
435     }
436    } else {
437     mes->type    = *data & 0xF0;
438     mes->channel = *data & 0x0F;
439     switch (*data & 0xF0) {
440      case MIDI_TYPE_NOTE_ON:
441      case MIDI_TYPE_NOTE_OFF:
442        need = 2;
443       break;
444      case MIDI_TYPE_PA:
445        need = 2;
446       break;
447      case MIDI_TYPE_CONTROLER:
448        need = 2;
449       break;
450      case MIDI_TYPE_PROGRAM:
451        need = 1;
452       break;
453      case MIDI_TYPE_MA:
454        need = 1;
455       break;
456      case MIDI_TYPE_PB:
457      case MIDI_TYPE_SYSEX:
458        need = 1;
459       break;
460     }
461    }
462
463   } else {
464
465    if ( mes == NULL ) {
466     ROAR_WARN("midi_conv_midi2mes(id=%i): Lost sync.", id);
467     data++;
468     have--;
469     continue;
470    }
471
472    switch (mes->type) {
473      case MIDI_TYPE_NOTE_ON:
474      case MIDI_TYPE_NOTE_OFF:
475       if ( need == 2 ) {
476        roar_midi_note_from_midiid(&(mes->d.note), *data);
477
478        // if velocity is zero we have a NOTE OFF event
479        if ( data[1] == 0 )
480         mes->type = MIDI_TYPE_NOTE_OFF;
481       }
482      case MIDI_TYPE_PA:
483      case MIDI_TYPE_CONTROLER:
484        if ( need == 2 ) {
485         mes->kk = *data;
486        } else {
487         mes->vv = *data;
488        }
489       break;
490      case MIDI_TYPE_PROGRAM:
491      case MIDI_TYPE_MA:
492        mes->vv = *data;
493       break;
494      case MIDI_TYPE_PB:
495      case MIDI_TYPE_SYSEX:
496        ROAR_WARN("midi_conv_midi2mes(id=%i): Message of Type 0x%.2X (PB or SysEx) not yet supported", id, mes->type);
497       break;
498    }
499
500    if ( need )
501     need--;
502
503    if ( !need ) {
504     switch (mes->type) {
505      case MIDI_TYPE_CONTROLER:
506       switch (mes->kk) {
507        case MIDI_CCE_MAIN_VOL:
508         if ( 516 * mes->vv > 65100 ) { // max volume
509          ss->mixer.mixer[mes->channel] = 65535;
510         } else {
511          ss->mixer.mixer[mes->channel] = 516 * mes->vv;
512         }
513         break;
514       }
515      break;
516     }
517    }
518
519   }
520   data++;
521   have--;
522  }
523
524  if ( need ) {
525   ROAR_DBG("midi_conv_midi2mes(id=%i) = ?", id);
526   if ( roar_buffer_set_offset(ss->buffer, old_have - last_have) == -1 ) {
527    ROAR_ERR("midi_conv_midi2mes(*): FIXME: BUG!!! Need to restore buffer here with corrected length");
528   }
529  } else if ( !have ) {
530   roar_buffer_next(&(ss->buffer));
531  }
532
533  if ( need && buf != NULL ) {
534   roar_buffer_free(buf);
535   buf = NULL;
536  } else if ( buf != NULL ) {
537   midi_add_buf(id, buf);
538   buf = NULL;
539  }
540 }
541
542 return 0;
543}
544#endif
545
546#define _nb  len++; *(d++)
547int midi_conv_mes2midi (int id) {
548 struct roar_stream        *   s;
549 struct roar_stream_server *  ss;
550 struct roar_buffer        * buf = g_midi_mess.buf;
551 struct midi_message       * mes = NULL;
552 void                      * bufdata;
553 unsigned char               data[3];
554 unsigned char             * d;
555 int                         len;
556 int                         send_clock;
557
558 if ( g_streams[id] == NULL )
559  return -1;
560
561 ROAR_DBG("midi_conv_mes2midi(id=%i) = ?", id);
562
563 s = ROAR_STREAM(ss = g_streams[id]);
564
565 send_clock = streams_get_flag(id, ROAR_FLAG_SYNC);
566
567 while (buf != NULL) {
568  if ( roar_buffer_get_data(buf, &bufdata) == -1 ) {
569   return -1;
570  }
571
572  mes = bufdata;
573
574  if (mes->type == MIDI_TYPE_CLOCK_TICK || mes->type == MIDI_TYPE_CLOCK_START || mes->type == MIDI_TYPE_CLOCK_STOP ) {
575   if ( send_clock ) {
576    if ( stream_vio_s_write(ss, &(mes->type), 1) != 1 ) {
577     streams_delete(id);
578     return -1;
579    }
580   }
581
582   if ( mes->type == MIDI_TYPE_CLOCK_TICK ) {
583    s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1);
584   }
585  } else {
586   len = 0;
587   d   = data;
588
589   _nb = (mes->type & 0xF0) | (mes->channel & 0x0F);
590
591   switch (mes->type) {
592    case MIDI_TYPE_CONTROLER:
593      switch (mes->kk) {
594       case MIDI_CCE_MAIN_VOL:
595         if ( 516 * mes->vv > 65100 ) { // max volume
596          ss->mixer.mixer[mes->channel] = 65535;
597         } else {
598          ss->mixer.mixer[mes->channel] = 516 * mes->vv;
599         }
600        break;
601      }
602    case MIDI_TYPE_NOTE_ON:
603    case MIDI_TYPE_NOTE_OFF:
604    case MIDI_TYPE_PA:
605      _nb = mes->kk;
606      _nb = mes->vv;
607     break;
608    case MIDI_TYPE_PROGRAM:
609    case MIDI_TYPE_MA:
610      _nb = mes->vv;
611     break;
612    case MIDI_TYPE_PB:
613    case MIDI_TYPE_SYSEX:
614     break;
615   }
616
617   if ( stream_vio_s_write(ss, data, len) != len ) {
618    streams_delete(id);
619    return -1;
620   }
621  }
622
623  if ( roar_buffer_get_next(buf, &buf) == -1 )
624   buf = NULL;
625 }
626
627 return 0;
628}
629#undef _nb
630
631#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
632int midi_conv_mes2ssynth(void) {
633 struct roar_buffer        * buf = g_midi_mess.buf;
634 struct midi_message       * mes = NULL;
635 void                      * bufdata;
636
637 while (buf != NULL) {
638  if ( roar_buffer_get_data(buf, &bufdata) == -1 ) {
639   return -1;
640  }
641
642  mes = bufdata;
643
644  if ( ssynth_eval_message(mes) == -1 )
645   return -1;
646
647  if ( roar_buffer_get_next(buf, &buf) == -1 )
648   buf = NULL;
649 }
650
651 return 0;
652}
653#endif
654
655int midi_new_bufmes    (struct roar_buffer ** buf, struct midi_message ** mes) {
656 if ( buf == NULL || mes == NULL )
657  return -1;
658
659 *buf = (void*)(*mes = NULL);
660
661 if ( roar_buffer_new_data(buf, sizeof(struct midi_message), (void**)mes) == -1 ) {
662  *buf = (void*)(*mes = NULL);
663  return -1;
664 }
665
666 memset((void*)*mes, 0, sizeof(struct midi_message));
667
668 (*mes)->type = MIDI_TYPE_NONE;
669
670 return 0;
671}
672
673int midi_add_buf       (int id, struct roar_buffer * buf) {
674 struct midi_message * mes;
675 void * bufdata;
676
677 if ( id == -1 || buf == NULL )
678  return -1;
679
680 if ( roar_buffer_get_data(buf, &bufdata) == -1 )
681  return -1;
682
683 mes = bufdata;
684
685 ROAR_DBG("midi_add_buf(id=%i, buf=%p) = ?", id, buf);
686 ROAR_DBG("midi_add_buf(*): MIDI Message of Type 0x%.2X", mes->type);
687 ROAR_DBG("midi_add_buf(*): Channel: %i", mes->channel);
688 ROAR_DBG("midi_add_buf(*): flags=0x%.2X", mes->flags);
689 ROAR_DBG("midi_add_buf(*): kk=0x%.2X, vv=0x%.2X", mes->kk, mes->vv);
690
691 if ( g_midi_mess.buf == NULL ) {
692  g_midi_mess.buf = buf;
693 } else {
694  roar_buffer_add(g_midi_mess.buf, buf);
695 }
696
697 ROAR_DBG("midi_add_buf(*) = 0");
698 return 0;
699}
700
701// bridges:
702int midi_check_bridge  (int id) {
703
704 ROAR_DBG("midi_check_bridge(id=%i) = ?", id);
705
706 if ( id == g_midi_clock.stream ) {
707  midi_clock_tick();
708
709  return 0;
710 }
711
712 return -1;
713}
714
715// clock:
716
717int midi_clock_init (void) {
718 struct roar_stream * s;
719 struct roar_stream_server * ss;
720
721 if ( (g_midi_clock.stream = streams_new()) == -1 ) {
722  ROAR_WARN("Error while initializing MIDI subsystem component clock");
723  return -1;
724 }
725
726 client_stream_add(g_self_client, g_midi_clock.stream);
727
728 midi_vio_set_dummy(g_midi_clock.stream);
729
730 streams_get(g_midi_clock.stream, &ss);
731 s = ROAR_STREAM(ss);
732
733 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
734
735 s->pos_rel_id    =  g_midi_clock.stream;
736
737 s->info.codec    =  ROAR_CODEC_MIDI;
738 ss->codec_orgi   =  ROAR_CODEC_MIDI;
739
740 s->info.channels = 1; // we have only one channel, ticking on channel 0
741 s->info.rate     = ROAR_MIDI_TICKS_PER_BEAT; // one beat per sec
742 s->info.bits     = ROAR_MIDI_BITS;
743
744 if ( streams_set_dir(g_midi_clock.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
745  ROAR_WARN("Error while initializing MIDI subsystem component clock");
746  return -1;
747 }
748
749 streams_set_name(g_midi_clock.stream, "MIDI Clock");
750
751 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_PRIMARY);
752 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_SYNC);
753
754 midi_clock_set_bph(3600); // one beat per sec
755
756 ss->state = ROAR_STREAMSTATE_NEW;
757
758 midi_config.inited |= MIDI_INITED_CLOCK;
759
760 return 0;
761}
762
763int midi_clock_set_bph (uint_least32_t bph) {
764 uint_least32_t sph = g_sa->rate/2 * 75 * g_sa->channels; // samples per houre
765
766 g_midi_clock.bph  = bph;
767
768 g_midi_clock.spt  = sph/bph;
769
770 g_midi_clock.nt   = ROAR_MATH_OVERFLOW_ADD(g_pos, g_midi_clock.spt);
771
772 return 0;
773}
774
775int midi_clock_tick (void) {
776 struct roar_buffer  * buf;
777 struct midi_message * mes;
778 struct roar_stream_server * ss;
779 unsigned int diff;
780
781 if ( streams_get(g_midi_clock.stream, &ss) == -1 ) {
782  ROAR_ERR("midi_clock_tick(void): Something very BAD happend: can not get stream object of my own stream!");
783  g_midi_clock.stream = -1;
784  ROAR_WARN("midi_clock_tick(void): Disabled MIDI Clock");
785  return -1;
786 }
787
788 while ( g_pos >= g_midi_clock.nt ) {
789  diff = g_pos - g_midi_clock.nt;
790  ROAR_DBG("midi_clock_tick(void): g_pos is %u samples (%5.2f%%) after of nt.", diff, (float)diff/g_midi_clock.spt);
791
792  g_midi_clock.nt   = ROAR_MATH_OVERFLOW_ADD(g_midi_clock.nt, g_midi_clock.spt);
793
794  if ( streams_get_flag(g_midi_clock.stream, ROAR_FLAG_SYNC) ) {
795   ROAR_DBG("midi_clock_tick(void): TICK! (nt=%lu)", g_midi_clock.nt);
796   if ( midi_new_bufmes(&buf, &mes) == -1 ) {
797    ROAR_ERR("midi_clock_tick(void): Can not create Clock-Tick-Message");
798   }
799
800   mes->type = MIDI_TYPE_CLOCK_TICK;
801
802   if ( midi_add_buf(g_midi_clock.stream, buf) == -1 ) {
803    ROAR_ERR("midi_clock_tick(void): Can not add Clock-Tick-Message");
804    roar_buffer_free(buf);
805    return -1;
806   }
807
808   ROAR_STREAM(ss)->pos = ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(ss)->pos, 1);
809   ss->state = ROAR_STREAMSTATE_OLD;
810  } else {
811   ROAR_DBG("midi_clock_tick(void): silent tick. (nt=%lu)", g_midi_clock.nt);
812  }
813 }
814
815 return 0;
816}
817
818// CB:
819
820#ifndef ROAR_WITHOUT_DCOMP_CB
821int midi_cb_init (void) {
822#ifdef _HAVE_CONSOLE
823 struct roar_stream * s;
824 struct roar_stream_server * ss;
825 int i;
826 char * files[] = {
827                   "/NX",
828                   "/dev/roarconsole",
829                   "/dev/console",
830#ifdef __linux__
831                   "/dev/tty0",
832                   "/dev/vc/0",
833#endif
834                   NULL
835                  };
836
837 g_midi_cb.console  = -1;
838 g_midi_cb.stream   = -1;
839 g_midi_cb.stoptime =  0;
840 g_midi_cb.playing  =  0;
841
842 if ( midi_config.console_dev != NULL ) {
843  files[0] = midi_config.console_dev;
844 }
845
846 for (i = 0; files[i] != NULL; i++) {
847  if ( (g_midi_cb.console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 )
848   break;
849 }
850
851 if ( g_midi_cb.console == -1 )
852  return -1;
853
854 if ( (g_midi_cb.stream = streams_new()) == -1 ) {
855  ROAR_WARN("Error while initializing MIDI subsystem component CB");
856  midi_cb_free();
857  return -1;
858 }
859
860 client_stream_add(g_self_client, g_midi_clock.stream);
861
862 midi_vio_set_dummy(g_midi_cb.stream);
863
864 streams_get(g_midi_cb.stream, &ss);
865 s = ROAR_STREAM(ss);
866
867 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
868
869 s->pos_rel_id    = -1;
870
871 s->info.codec    =  0;
872 ss->codec_orgi   =  0;
873
874 s->info.channels =  1;
875 s->info.rate     = 1193180;
876 s->info.bits     =  8;
877
878 if ( streams_set_dir(g_midi_cb.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
879  ROAR_WARN("Error while initializing MIDI subsystem component CB");
880  midi_cb_free();
881  return -1;
882 }
883
884 streams_set_name(g_midi_cb.stream, "Console speaker bridge");
885
886 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_OUTPUT);
887 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_PRIMARY);
888 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_HWMIXER);
889 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_MUTE);
890
891 midi_config.inited |= MIDI_INITED_CB;
892
893 return 0;
894#else
895 g_midi_cb.console  = -1;
896 g_midi_cb.stream   = -1;
897
898 return -1;
899#endif
900}
901
902int midi_cb_free (void) {
903#ifdef _HAVE_CONSOLE
904
905 midi_cb_stop();
906
907 if ( g_midi_cb.stream != -1 )
908  streams_delete(g_midi_cb.stream);
909
910 if ( g_midi_cb.console != -1 )
911  close(g_midi_cb.console);
912
913 return 0;
914#else
915 return -1;
916#endif
917}
918
919int midi_cb_play(float t, float freq, int override) {
920 float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels;
921
922/*
923#define MIDI_CB_NOOVERRIDE 0
924#define MIDI_CB_OVERRIDE   1
925*/
926 if ( g_midi_cb.playing && override != MIDI_CB_OVERRIDE )
927  return -1;
928
929 g_midi_cb.stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t);
930 midi_cb_start(freq);
931
932 return 0;
933}
934
935int midi_cb_update (void) {
936
937 if ( !streams_get_flag(g_midi_cb.stream, ROAR_FLAG_MUTE) ) {
938  if ( midi_cb_readbuf() == -1 )
939   return -1;
940 } else if ( g_midi_cb.playing ) {
941  midi_cb_stop();
942 }
943
944 if ( !g_midi_cb.playing )
945  return 0;
946
947/*
948 if ( g_midi_cb.stoptime <= g_pos )
949  midi_cb_stop();
950*/
951
952 ROAR_DBG("midi_cb_update(void) = ?");
953
954 return 0;
955}
956
957int midi_cb_start(float freq) {
958// On linux this uses ioctl KIOCSOUND
959#ifdef __linux__
960 if ( g_midi_cb.console == -1 )
961  return -1;
962
963 if ( ioctl(g_midi_cb.console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 )
964  return -1;
965
966 g_midi_cb.playing = 1;
967
968 return 0;
969#else
970 return -1;
971#endif
972}
973
974int midi_cb_stop (void) {
975#ifdef __linux__
976 int ret;
977
978 ret = midi_cb_start(0);
979 g_midi_cb.playing = 0;
980
981 return ret;
982#else
983 return -1;
984#endif
985}
986
987int midi_cb_readbuf(void) {
988 struct roar_buffer        * buf = g_midi_mess.buf;
989 struct midi_message       * mes = NULL;
990 void                      * bufdata;
991
992 ROAR_DBG("midi_cb_readbuf(void) = ?");
993
994 while (buf != NULL) {
995  ROAR_DBG("midi_cb_readbuf(void): buf=%p", buf);
996
997  if ( roar_buffer_get_data(buf, &bufdata) == -1 ) {
998   return -1;
999  }
1000
1001  mes = bufdata;
1002
1003  switch (mes->type) {
1004   case MIDI_TYPE_NOTE_ON:
1005     midi_cb_start(mes->d.note.freq);
1006    break;
1007   case MIDI_TYPE_NOTE_OFF:
1008     midi_cb_stop();
1009    break;
1010   case MIDI_TYPE_CONTROLER:
1011     if ( mes->kk == MIDI_CCE_ALL_NOTE_OFF ) /* all note off */
1012      midi_cb_stop();
1013    break;
1014  }
1015
1016  if ( roar_buffer_get_next(buf, &buf) == -1 )
1017   buf = NULL;
1018 }
1019
1020 return 0;
1021}
1022#endif
1023
1024// VIO:
1025
1026int     midi_vio_set_dummy(int stream) {
1027 struct roar_stream_server * ss;
1028
1029 if ( streams_get(stream, &ss) == -1 )
1030  return -1;
1031
1032 ss->vio.read     = NULL;
1033 ss->vio.write    = NULL;
1034 ss->vio.lseek    = NULL;
1035 ss->vio.nonblock = (int (*)(struct roar_vio_calls * vio, int state))midi_vio_ok;
1036 ss->vio.sync     = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
1037 ss->vio.ctl      = NULL;
1038 ss->vio.close    = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
1039
1040 return 0;
1041}
1042
1043int     midi_vio_ok(struct roar_vio_calls * vio, ...) {
1044 return 0;
1045}
1046
1047#endif
1048
1049//ll
Note: See TracBrowser for help on using the repository browser.