source: roaraudio/roard/midi.c @ 5162:85c55ccd12a9

Last change on this file since 5162:85c55ccd12a9 was 5162:85c55ccd12a9, checked in by phi, 13 years ago

fixed some compiler warnings

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