source: roaraudio/roard/midi.c @ 5194:b40015b16ff2

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

make more use of /extern/

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