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
RevLine 
[169]1//midi.c:
2
[668]3/*
[4708]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
[668]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
[3517]21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
[668]23 *
24 */
25
[169]26#include "roard.h"
27
[2500]28#ifndef ROAR_WITHOUT_DCOMP_MIDI
29
[1751]30#if defined(ROAR_HAVE_IO_POSIX) && !defined(ROAR_TARGET_WIN32)
31#define _HAVE_CONSOLE
32#endif
33
[1924]34int midi_init_config(void) {
35 midi_config.init        = 1;
36 midi_config.inited      = 0;
[2487]37
38#ifndef ROAR_WITHOUT_DCOMP_CB
[2445]39 midi_config.init_cb     = 0;
[1924]40 midi_config.console_dev = NULL;
[2487]41#endif
[1924]42
43 return 0;
44}
45
[185]46int midi_init (void) {
[2943]47 struct roar_stream_server * ss;
[3558]48 char cmap[16];
[3215]49 int i;
[1843]50
[1924]51 midi_config.inited = 0;
[1843]52
[1924]53 if ( midi_config.init ) {
[2487]54#ifndef ROAR_WITHOUT_DCOMP_CB
[1924]55  if ( midi_config.init_cb ) {
56   if ( midi_cb_init() == -1 ) {
57    ROAR_WARN("Can not initialize MIDI subsystem component CB");
58   }
[3594]59  } else {
60   g_midi_cb.console = -1;
[1924]61  }
[2487]62#endif
[1924]63
64  if ( midi_clock_init() == -1 ) {
65   ROAR_WARN("Can not initialize MIDI subsystem component clock");
66  }
67
[1848]68 }
69
[1863]70 g_midi_mess.buf = NULL;
71
[2943]72 if ( (g_midi_mixer.stream = add_mixer(ROAR_SUBSYS_MIDI, _MIXER_NAME("MIDI"), &ss)) == -1 ) {
[2941]73  ROAR_WARN("Can not create MIDI mixer");
74 }
[3547]75
[3558]76 roardsp_chanlist_init(cmap,   16, ROARDSP_CHANLIST_MAP_MIDI);
77 streams_set_map(g_midi_mixer.stream, cmap, 16);
[3547]78
[2943]79 ss->state = ROAR_STREAMSTATE_OLD;
[2941]80
[3215]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
[1924]89 midi_config.inited |= MIDI_INITED_MAIN;
90
[1843]91 return 0;
92}
[1848]93
[1843]94int midi_free (void) {
[1924]95 if ( midi_reinit() == -1 )
96  return -1;
97
[2487]98#ifndef ROAR_WITHOUT_DCOMP_CB
[1924]99 if ( midi_cb_free() == -1 )
100  return -1;
[2487]101#endif
[1924]102
103 return 0;
[1843]104}
105
106int midi_update(void) {
[1848]107
[1850]108 if ( g_midi_clock.stream != -1 )
109  midi_check_bridge(g_midi_clock.stream);
[1848]110
[2489]111#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2461]112 midi_conv_mes2ssynth();
[2489]113#endif
[2461]114
[2487]115#ifndef ROAR_WITHOUT_DCOMP_CB
[1843]116 return midi_cb_update();
[2487]117#else
118 return 0;
119#endif
[1843]120}
121
[1855]122int midi_reinit(void) {
[1868]123
124 if ( g_midi_mess.buf != NULL )
125  roar_buffer_free(g_midi_mess.buf);
126
127 g_midi_mess.buf = NULL;
128
[1855]129 return 0;
130}
131
[1845]132// STREAMS:
133
134int midi_check_stream  (int id) {
[1846]135 struct roar_stream        *   s;
136 struct roar_stream_server *  ss;
[1858]137 struct roar_buffer        *   b;
[4521]138 void                      * buf;
[1858]139 ssize_t                     len;
[1846]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
[1848]148 if ( s->dir == ROAR_DIR_BRIDGE )
149  return midi_check_bridge(id);
150
[1846]151 switch (s->info.codec) {
[1858]152  case ROAR_CODEC_MIDI:
153   break;
154  default:
155    streams_delete(id);
156    return -1;
157 }
158
[4521]159 if ( roar_buffer_new_data(&b, MIDI_READ_SIZE, &buf) == -1 ) {
[1858]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);
[1862]167  roar_buffer_free(b);
[1858]168  return -1;
169 }
170
171 roar_buffer_set_len(b, len);
172
[1859]173 if ( stream_add_buffer(id, b) == -1 ) {
174  roar_buffer_free(b);
175  streams_delete(id);
176  return -1;
177 }
178
[1858]179 switch (s->info.codec) {
180  case ROAR_CODEC_MIDI:
181    return midi_conv_midi2mes(id);
182   break;
[1846]183  default:
184    streams_delete(id);
185    return -1;
186 }
187
[5053]188 ROAR_DBG("midi_check_stream(id=%i) = 0", id);
[1846]189 return 0;
[1845]190}
[1846]191
[1845]192int midi_send_stream   (int id) {
[1846]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) {
[1877]204  case ROAR_CODEC_MIDI:
205    return midi_conv_mes2midi(id);
206   break;
[1846]207  default:
208    streams_delete(id);
209    return -1;
210 }
211
212 return 0;
[1845]213}
214
[5089]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
[1858]381int midi_conv_midi2mes (int id) {
[4521]382 void          * bufdata;
[1863]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;
[1879]388 size_t need = 0, have = 0, last_have = 0, old_have = 0;
[1863]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) {
[5053]399  ROAR_DBG("midi_conv_midi2mes(id=%i): ss->buffer=%p, alive=%i", id, ss->buffer, alive);
400
[4521]401  if ( roar_buffer_get_data(ss->buffer, &bufdata) == -1 ) {
[1863]402   alive = 0;
403   continue;
404  }
405
[4521]406  data = bufdata;
407
[1863]408  if ( roar_buffer_get_len(ss->buffer, &have) == -1 ) {
409   alive = 0;
410   continue;
411  }
[1879]412  old_have = have;
[1863]413
414  while (have && alive) {
[5053]415   ROAR_DBG("midi_conv_midi2mes(id=%i): have=%llu, alive=%i", id, (long long unsigned int)have, alive);
416
[1863]417   if ( *data & 0x80 ) {
[1865]418    if ( buf != NULL ) {
[1866]419     midi_add_buf(id, buf);
[1865]420     buf = NULL;
421    }
422
[1879]423    last_have = have;
424
[1863]425    need = 0;
426    if ( midi_new_bufmes(&buf, &mes) == -1 ) {
427     alive = 0;
428     continue;
429    }
[1865]430
431    if (*data == MIDI_TYPE_CLOCK_TICK || *data == MIDI_TYPE_CLOCK_START || *data == MIDI_TYPE_CLOCK_STOP ) {
432     mes->type = *data;
[1891]433
434     if ( *data == MIDI_TYPE_CLOCK_TICK ) {
435      s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1);
436     }
[1865]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
[1863]464   } else {
[1871]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) {
[1873]474      case MIDI_TYPE_NOTE_ON:
475      case MIDI_TYPE_NOTE_OFF:
[1881]476       if ( need == 2 ) {
477        roar_midi_note_from_midiid(&(mes->d.note), *data);
[2409]478
479        // if velocity is zero we have a NOTE OFF event
480        if ( data[1] == 0 )
481         mes->type = MIDI_TYPE_NOTE_OFF;
[1881]482       }
[1874]483      case MIDI_TYPE_PA:
484      case MIDI_TYPE_CONTROLER:
[1873]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:
[1874]493        mes->vv = *data;
494       break;
[1873]495      case MIDI_TYPE_PB:
496      case MIDI_TYPE_SYSEX:
[1874]497        ROAR_WARN("midi_conv_midi2mes(id=%i): Message of Type 0x%.2X (PB or SysEx) not yet supported", id, mes->type);
[1873]498       break;
[1871]499    }
500
[1863]501    if ( need )
502     need--;
[1893]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
[1863]520   }
521   data++;
522   have--;
523  }
524
[1865]525  if ( need ) {
[5053]526   ROAR_DBG("midi_conv_midi2mes(id=%i) = ?", id);
[1879]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   }
[1865]530  } else if ( !have ) {
[1863]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 ) {
[1866]538   midi_add_buf(id, buf);
[1863]539   buf = NULL;
540  }
541 }
542
543 return 0;
[1858]544}
[5089]545#endif
[1858]546
[1877]547#define _nb  len++; *(d++)
[1858]548int midi_conv_mes2midi (int id) {
[1877]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;
[4521]553 void                      * bufdata;
[1877]554 unsigned char               data[3];
555 unsigned char             * d;
556 int                         len;
[1909]557 int                         send_clock;
[1877]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
[1909]566 send_clock = streams_get_flag(id, ROAR_FLAG_SYNC);
567
[1877]568 while (buf != NULL) {
[4521]569  if ( roar_buffer_get_data(buf, &bufdata) == -1 ) {
[1877]570   return -1;
571  }
572
[4521]573  mes = bufdata;
574
[1877]575  if (mes->type == MIDI_TYPE_CLOCK_TICK || mes->type == MIDI_TYPE_CLOCK_START || mes->type == MIDI_TYPE_CLOCK_STOP ) {
[1910]576   if ( send_clock ) {
[1909]577    if ( stream_vio_s_write(ss, &(mes->type), 1) != 1 ) {
578     streams_delete(id);
579     return -1;
580    }
[1877]581   }
[1896]582
583   if ( mes->type == MIDI_TYPE_CLOCK_TICK ) {
584    s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1);
585   }
[1877]586  } else {
587   len = 0;
588   d   = data;
589
590   _nb = (mes->type & 0xF0) | (mes->channel & 0x0F);
591
592   switch (mes->type) {
[1898]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      }
[1877]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;
[1858]629}
[1877]630#undef _nb
[1858]631
[2489]632#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2461]633int midi_conv_mes2ssynth(void) {
[2462]634 struct roar_buffer        * buf = g_midi_mess.buf;
635 struct midi_message       * mes = NULL;
[4521]636 void                      * bufdata;
[2462]637
638 while (buf != NULL) {
[4521]639  if ( roar_buffer_get_data(buf, &bufdata) == -1 ) {
[2462]640   return -1;
641  }
642
[4521]643  mes = bufdata;
644
[2462]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;
[2461]653}
[2489]654#endif
[2461]655
[1863]656int midi_new_bufmes    (struct roar_buffer ** buf, struct midi_message ** mes) {
657 if ( buf == NULL || mes == NULL )
658  return -1;
659
[1867]660 *buf = (void*)(*mes = NULL);
[1863]661
[3764]662 if ( roar_buffer_new_data(buf, sizeof(struct midi_message), (void**)mes) == -1 ) {
[1867]663  *buf = (void*)(*mes = NULL);
[1863]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
[1866]674int midi_add_buf       (int id, struct roar_buffer * buf) {
[1869]675 struct midi_message * mes;
[4521]676 void * bufdata;
[1869]677
[1866]678 if ( id == -1 || buf == NULL )
679  return -1;
680
[4521]681 if ( roar_buffer_get_data(buf, &bufdata) == -1 )
[1869]682  return -1;
683
[4521]684 mes = bufdata;
685
[1878]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);
[1869]691
[1866]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
[5053]698 ROAR_DBG("midi_add_buf(*) = 0");
[1866]699 return 0;
700}
701
[1848]702// bridges:
703int midi_check_bridge  (int id) {
[1851]704
[1860]705 ROAR_DBG("midi_check_bridge(id=%i) = ?", id);
[1851]706
707 if ( id == g_midi_clock.stream ) {
708  midi_clock_tick();
709
710  return 0;
711 }
712
[1848]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
[1850]722 if ( (g_midi_clock.stream = streams_new()) == -1 ) {
[1848]723  ROAR_WARN("Error while initializing MIDI subsystem component clock");
724  return -1;
725 }
726
[2915]727 client_stream_add(g_self_client, g_midi_clock.stream);
728
[1850]729 midi_vio_set_dummy(g_midi_clock.stream);
[1848]730
[1850]731 streams_get(g_midi_clock.stream, &ss);
[1848]732 s = ROAR_STREAM(ss);
733
734 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
735
[1850]736 s->pos_rel_id    =  g_midi_clock.stream;
[1848]737
738 s->info.codec    =  ROAR_CODEC_MIDI;
739 ss->codec_orgi   =  ROAR_CODEC_MIDI;
740
[2401]741 s->info.channels = 1; // we have only one channel, ticking on channel 0
[2400]742 s->info.rate     = ROAR_MIDI_TICKS_PER_BEAT; // one beat per sec
743 s->info.bits     = ROAR_MIDI_BITS;
[1848]744
[1850]745 if ( streams_set_dir(g_midi_clock.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
[1848]746  ROAR_WARN("Error while initializing MIDI subsystem component clock");
747  return -1;
748 }
749
[1850]750 streams_set_name(g_midi_clock.stream, "MIDI Clock");
[1848]751
[1850]752 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_PRIMARY);
753 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_SYNC);
[1848]754
[2400]755 midi_clock_set_bph(3600); // one beat per sec
[1851]756
[2943]757 ss->state = ROAR_STREAMSTATE_NEW;
758
[1924]759 midi_config.inited |= MIDI_INITED_CLOCK;
760
[1851]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) {
[1875]777 struct roar_buffer  * buf;
778 struct midi_message * mes;
[1888]779 struct roar_stream_server * ss;
[1851]780 unsigned int diff;
781
[1888]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
[1851]789 while ( g_pos >= g_midi_clock.nt ) {
790  diff = g_pos - g_midi_clock.nt;
[1911]791  ROAR_DBG("midi_clock_tick(void): g_pos is %u samples (%5.2f%%) after of nt.", diff, (float)diff/g_midi_clock.spt);
[1851]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) ) {
[1860]796   ROAR_DBG("midi_clock_tick(void): TICK! (nt=%lu)", g_midi_clock.nt);
[1875]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   }
[1888]808
809   ROAR_STREAM(ss)->pos = ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(ss)->pos, 1);
[2943]810   ss->state = ROAR_STREAMSTATE_OLD;
[1851]811  } else {
[1860]812   ROAR_DBG("midi_clock_tick(void): silent tick. (nt=%lu)", g_midi_clock.nt);
[1851]813  }
814 }
815
[1848]816 return 0;
817}
818
[1845]819// CB:
[1843]820
[2487]821#ifndef ROAR_WITHOUT_DCOMP_CB
[1843]822int midi_cb_init (void) {
[1751]823#ifdef _HAVE_CONSOLE
[1843]824 struct roar_stream * s;
825 struct roar_stream_server * ss;
[185]826 int i;
827 char * files[] = {
[1924]828                   "/NX",
829                   "/dev/roarconsole",
[185]830                   "/dev/console",
831#ifdef __linux__
832                   "/dev/tty0",
833                   "/dev/vc/0",
834#endif
835                   NULL
836                  };
837
[1853]838 g_midi_cb.console  = -1;
839 g_midi_cb.stream   = -1;
840 g_midi_cb.stoptime =  0;
841 g_midi_cb.playing  =  0;
[185]842
[1924]843 if ( midi_config.console_dev != NULL ) {
844  files[0] = midi_config.console_dev;
845 }
846
[185]847 for (i = 0; files[i] != NULL; i++) {
[1853]848  if ( (g_midi_cb.console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 )
[185]849   break;
850 }
851
[1853]852 if ( g_midi_cb.console == -1 )
[1843]853  return -1;
854
[1853]855 if ( (g_midi_cb.stream = streams_new()) == -1 ) {
[1843]856  ROAR_WARN("Error while initializing MIDI subsystem component CB");
857  midi_cb_free();
[187]858  return -1;
859 }
[1843]860
[2915]861 client_stream_add(g_self_client, g_midi_clock.stream);
862
[1853]863 midi_vio_set_dummy(g_midi_cb.stream);
[1848]864
[1853]865 streams_get(g_midi_cb.stream, &ss);
[1843]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
[1853]879 if ( streams_set_dir(g_midi_cb.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
[1843]880  ROAR_WARN("Error while initializing MIDI subsystem component CB");
881  midi_cb_free();
882  return -1;
883 }
884
[1853]885 streams_set_name(g_midi_cb.stream, "Console speaker bridge");
[1843]886
[1853]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);
[1885]890 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_MUTE);
[1843]891
[1924]892 midi_config.inited |= MIDI_INITED_CB;
893
[1843]894 return 0;
[1485]895#else
[1853]896 g_midi_cb.console  = -1;
897 g_midi_cb.stream   = -1;
[1843]898
[1485]899 return -1;
900#endif
[185]901}
902
[1843]903int midi_cb_free (void) {
[1751]904#ifdef _HAVE_CONSOLE
[1843]905
906 midi_cb_stop();
907
[1853]908 if ( g_midi_cb.stream != -1 )
909  streams_delete(g_midi_cb.stream);
[1843]910
[1853]911 if ( g_midi_cb.console != -1 )
912  close(g_midi_cb.console);
[1843]913
[185]914 return 0;
[1485]915#else
916 return -1;
917#endif
[185]918}
919
920int midi_cb_play(float t, float freq, int override) {
[191]921 float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels;
922
[189]923/*
924#define MIDI_CB_NOOVERRIDE 0
925#define MIDI_CB_OVERRIDE   1
926*/
[1853]927 if ( g_midi_cb.playing && override != MIDI_CB_OVERRIDE )
[191]928  return -1;
929
[1853]930 g_midi_cb.stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t);
[191]931 midi_cb_start(freq);
932
933 return 0;
[185]934}
935
[190]936int midi_cb_update (void) {
[1884]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 }
[1882]944
[1853]945 if ( !g_midi_cb.playing )
[192]946  return 0;
947
[1884]948/*
[1853]949 if ( g_midi_cb.stoptime <= g_pos )
[189]950  midi_cb_stop();
[1884]951*/
[189]952
[1882]953 ROAR_DBG("midi_cb_update(void) = ?");
954
[189]955 return 0;
956}
957
[185]958int midi_cb_start(float freq) {
959// On linux this uses ioctl KIOCSOUND
[188]960#ifdef __linux__
[1853]961 if ( g_midi_cb.console == -1 )
[188]962  return -1;
963
[1853]964 if ( ioctl(g_midi_cb.console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 )
[188]965  return -1;
966
[1884]967 g_midi_cb.playing = 1;
968
[188]969 return 0;
970#else
[185]971 return -1;
[188]972#endif
[185]973}
974
975int midi_cb_stop (void) {
976#ifdef __linux__
[1884]977 int ret;
978
979 ret = midi_cb_start(0);
[1853]980 g_midi_cb.playing = 0;
[1884]981
982 return ret;
[185]983#else
984 return -1;
985#endif
986}
987
[1882]988int midi_cb_readbuf(void) {
989 struct roar_buffer        * buf = g_midi_mess.buf;
990 struct midi_message       * mes = NULL;
[4521]991 void                      * bufdata;
[1882]992
993 ROAR_DBG("midi_cb_readbuf(void) = ?");
994
995 while (buf != NULL) {
996  ROAR_DBG("midi_cb_readbuf(void): buf=%p", buf);
997
[4521]998  if ( roar_buffer_get_data(buf, &bufdata) == -1 ) {
[1882]999   return -1;
1000  }
1001
[4521]1002  mes = bufdata;
1003
[1882]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;
[1890]1011   case MIDI_TYPE_CONTROLER:
[1894]1012     if ( mes->kk == MIDI_CCE_ALL_NOTE_OFF ) /* all note off */
[1890]1013      midi_cb_stop();
1014    break;
[1882]1015  }
1016
1017  if ( roar_buffer_get_next(buf, &buf) == -1 )
1018   buf = NULL;
1019 }
1020
1021 return 0;
1022}
[2487]1023#endif
[1882]1024
[1848]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;
[1849]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;
[1848]1038 ss->vio.ctl      = NULL;
[1849]1039 ss->vio.close    = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
1040
1041 return 0;
[1848]1042}
1043
1044int     midi_vio_ok(struct roar_vio_calls * vio, ...) {
1045 return 0;
1046}
1047
[2500]1048#endif
1049
[169]1050//ll
Note: See TracBrowser for help on using the repository browser.