source: roaraudio/roard/midi.c @ 5152:6de4ec1639d3

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

removed incorrect warning

File size: 22.5 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_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
[1858]380int midi_conv_midi2mes (int id) {
[4521]381 void          * bufdata;
[1863]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;
[1879]387 size_t need = 0, have = 0, last_have = 0, old_have = 0;
[1863]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) {
[5053]398  ROAR_DBG("midi_conv_midi2mes(id=%i): ss->buffer=%p, alive=%i", id, ss->buffer, alive);
399
[4521]400  if ( roar_buffer_get_data(ss->buffer, &bufdata) == -1 ) {
[1863]401   alive = 0;
402   continue;
403  }
404
[4521]405  data = bufdata;
406
[1863]407  if ( roar_buffer_get_len(ss->buffer, &have) == -1 ) {
408   alive = 0;
409   continue;
410  }
[1879]411  old_have = have;
[1863]412
413  while (have && alive) {
[5053]414   ROAR_DBG("midi_conv_midi2mes(id=%i): have=%llu, alive=%i", id, (long long unsigned int)have, alive);
415
[1863]416   if ( *data & 0x80 ) {
[1865]417    if ( buf != NULL ) {
[1866]418     midi_add_buf(id, buf);
[1865]419     buf = NULL;
420    }
421
[1879]422    last_have = have;
423
[1863]424    need = 0;
425    if ( midi_new_bufmes(&buf, &mes) == -1 ) {
426     alive = 0;
427     continue;
428    }
[1865]429
430    if (*data == MIDI_TYPE_CLOCK_TICK || *data == MIDI_TYPE_CLOCK_START || *data == MIDI_TYPE_CLOCK_STOP ) {
431     mes->type = *data;
[1891]432
433     if ( *data == MIDI_TYPE_CLOCK_TICK ) {
434      s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1);
435     }
[1865]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
[1863]463   } else {
[1871]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) {
[1873]473      case MIDI_TYPE_NOTE_ON:
474      case MIDI_TYPE_NOTE_OFF:
[1881]475       if ( need == 2 ) {
476        roar_midi_note_from_midiid(&(mes->d.note), *data);
[2409]477
478        // if velocity is zero we have a NOTE OFF event
479        if ( data[1] == 0 )
480         mes->type = MIDI_TYPE_NOTE_OFF;
[1881]481       }
[1874]482      case MIDI_TYPE_PA:
483      case MIDI_TYPE_CONTROLER:
[1873]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:
[1874]492        mes->vv = *data;
493       break;
[1873]494      case MIDI_TYPE_PB:
495      case MIDI_TYPE_SYSEX:
[1874]496        ROAR_WARN("midi_conv_midi2mes(id=%i): Message of Type 0x%.2X (PB or SysEx) not yet supported", id, mes->type);
[1873]497       break;
[1871]498    }
499
[1863]500    if ( need )
501     need--;
[1893]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
[1863]519   }
520   data++;
521   have--;
522  }
523
[1865]524  if ( need ) {
[5053]525   ROAR_DBG("midi_conv_midi2mes(id=%i) = ?", id);
[1879]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   }
[1865]529  } else if ( !have ) {
[1863]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 ) {
[1866]537   midi_add_buf(id, buf);
[1863]538   buf = NULL;
539  }
540 }
541
542 return 0;
[1858]543}
[5089]544#endif
[1858]545
[1877]546#define _nb  len++; *(d++)
[1858]547int midi_conv_mes2midi (int id) {
[1877]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;
[4521]552 void                      * bufdata;
[1877]553 unsigned char               data[3];
554 unsigned char             * d;
555 int                         len;
[1909]556 int                         send_clock;
[1877]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
[1909]565 send_clock = streams_get_flag(id, ROAR_FLAG_SYNC);
566
[1877]567 while (buf != NULL) {
[4521]568  if ( roar_buffer_get_data(buf, &bufdata) == -1 ) {
[1877]569   return -1;
570  }
571
[4521]572  mes = bufdata;
573
[1877]574  if (mes->type == MIDI_TYPE_CLOCK_TICK || mes->type == MIDI_TYPE_CLOCK_START || mes->type == MIDI_TYPE_CLOCK_STOP ) {
[1910]575   if ( send_clock ) {
[1909]576    if ( stream_vio_s_write(ss, &(mes->type), 1) != 1 ) {
577     streams_delete(id);
578     return -1;
579    }
[1877]580   }
[1896]581
582   if ( mes->type == MIDI_TYPE_CLOCK_TICK ) {
583    s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1);
584   }
[1877]585  } else {
586   len = 0;
587   d   = data;
588
589   _nb = (mes->type & 0xF0) | (mes->channel & 0x0F);
590
591   switch (mes->type) {
[1898]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      }
[1877]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;
[1858]628}
[1877]629#undef _nb
[1858]630
[2489]631#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2461]632int midi_conv_mes2ssynth(void) {
[2462]633 struct roar_buffer        * buf = g_midi_mess.buf;
634 struct midi_message       * mes = NULL;
[4521]635 void                      * bufdata;
[2462]636
637 while (buf != NULL) {
[4521]638  if ( roar_buffer_get_data(buf, &bufdata) == -1 ) {
[2462]639   return -1;
640  }
641
[4521]642  mes = bufdata;
643
[2462]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;
[2461]652}
[2489]653#endif
[2461]654
[1863]655int midi_new_bufmes    (struct roar_buffer ** buf, struct midi_message ** mes) {
656 if ( buf == NULL || mes == NULL )
657  return -1;
658
[1867]659 *buf = (void*)(*mes = NULL);
[1863]660
[3764]661 if ( roar_buffer_new_data(buf, sizeof(struct midi_message), (void**)mes) == -1 ) {
[1867]662  *buf = (void*)(*mes = NULL);
[1863]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
[1866]673int midi_add_buf       (int id, struct roar_buffer * buf) {
[1869]674 struct midi_message * mes;
[4521]675 void * bufdata;
[1869]676
[1866]677 if ( id == -1 || buf == NULL )
678  return -1;
679
[4521]680 if ( roar_buffer_get_data(buf, &bufdata) == -1 )
[1869]681  return -1;
682
[4521]683 mes = bufdata;
684
[1878]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);
[1869]690
[1866]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
[5053]697 ROAR_DBG("midi_add_buf(*) = 0");
[1866]698 return 0;
699}
700
[1848]701// bridges:
702int midi_check_bridge  (int id) {
[1851]703
[1860]704 ROAR_DBG("midi_check_bridge(id=%i) = ?", id);
[1851]705
706 if ( id == g_midi_clock.stream ) {
707  midi_clock_tick();
708
709  return 0;
710 }
711
[1848]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
[1850]721 if ( (g_midi_clock.stream = streams_new()) == -1 ) {
[1848]722  ROAR_WARN("Error while initializing MIDI subsystem component clock");
723  return -1;
724 }
725
[2915]726 client_stream_add(g_self_client, g_midi_clock.stream);
727
[1850]728 midi_vio_set_dummy(g_midi_clock.stream);
[1848]729
[1850]730 streams_get(g_midi_clock.stream, &ss);
[1848]731 s = ROAR_STREAM(ss);
732
733 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
734
[1850]735 s->pos_rel_id    =  g_midi_clock.stream;
[1848]736
737 s->info.codec    =  ROAR_CODEC_MIDI;
738 ss->codec_orgi   =  ROAR_CODEC_MIDI;
739
[2401]740 s->info.channels = 1; // we have only one channel, ticking on channel 0
[2400]741 s->info.rate     = ROAR_MIDI_TICKS_PER_BEAT; // one beat per sec
742 s->info.bits     = ROAR_MIDI_BITS;
[1848]743
[1850]744 if ( streams_set_dir(g_midi_clock.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
[1848]745  ROAR_WARN("Error while initializing MIDI subsystem component clock");
746  return -1;
747 }
748
[1850]749 streams_set_name(g_midi_clock.stream, "MIDI Clock");
[1848]750
[1850]751 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_PRIMARY);
752 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_SYNC);
[1848]753
[2400]754 midi_clock_set_bph(3600); // one beat per sec
[1851]755
[2943]756 ss->state = ROAR_STREAMSTATE_NEW;
757
[1924]758 midi_config.inited |= MIDI_INITED_CLOCK;
759
[1851]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) {
[1875]776 struct roar_buffer  * buf;
777 struct midi_message * mes;
[1888]778 struct roar_stream_server * ss;
[1851]779 unsigned int diff;
780
[1888]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
[1851]788 while ( g_pos >= g_midi_clock.nt ) {
789  diff = g_pos - g_midi_clock.nt;
[1911]790  ROAR_DBG("midi_clock_tick(void): g_pos is %u samples (%5.2f%%) after of nt.", diff, (float)diff/g_midi_clock.spt);
[1851]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) ) {
[1860]795   ROAR_DBG("midi_clock_tick(void): TICK! (nt=%lu)", g_midi_clock.nt);
[1875]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   }
[1888]807
808   ROAR_STREAM(ss)->pos = ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(ss)->pos, 1);
[2943]809   ss->state = ROAR_STREAMSTATE_OLD;
[1851]810  } else {
[1860]811   ROAR_DBG("midi_clock_tick(void): silent tick. (nt=%lu)", g_midi_clock.nt);
[1851]812  }
813 }
814
[1848]815 return 0;
816}
817
[1845]818// CB:
[1843]819
[2487]820#ifndef ROAR_WITHOUT_DCOMP_CB
[1843]821int midi_cb_init (void) {
[1751]822#ifdef _HAVE_CONSOLE
[1843]823 struct roar_stream * s;
824 struct roar_stream_server * ss;
[185]825 int i;
826 char * files[] = {
[1924]827                   "/NX",
828                   "/dev/roarconsole",
[185]829                   "/dev/console",
830#ifdef __linux__
831                   "/dev/tty0",
832                   "/dev/vc/0",
833#endif
834                   NULL
835                  };
836
[1853]837 g_midi_cb.console  = -1;
838 g_midi_cb.stream   = -1;
839 g_midi_cb.stoptime =  0;
840 g_midi_cb.playing  =  0;
[185]841
[1924]842 if ( midi_config.console_dev != NULL ) {
843  files[0] = midi_config.console_dev;
844 }
845
[185]846 for (i = 0; files[i] != NULL; i++) {
[1853]847  if ( (g_midi_cb.console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 )
[185]848   break;
849 }
850
[1853]851 if ( g_midi_cb.console == -1 )
[1843]852  return -1;
853
[1853]854 if ( (g_midi_cb.stream = streams_new()) == -1 ) {
[1843]855  ROAR_WARN("Error while initializing MIDI subsystem component CB");
856  midi_cb_free();
[187]857  return -1;
858 }
[1843]859
[2915]860 client_stream_add(g_self_client, g_midi_clock.stream);
861
[1853]862 midi_vio_set_dummy(g_midi_cb.stream);
[1848]863
[1853]864 streams_get(g_midi_cb.stream, &ss);
[1843]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
[1853]878 if ( streams_set_dir(g_midi_cb.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
[1843]879  ROAR_WARN("Error while initializing MIDI subsystem component CB");
880  midi_cb_free();
881  return -1;
882 }
883
[1853]884 streams_set_name(g_midi_cb.stream, "Console speaker bridge");
[1843]885
[1853]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);
[1885]889 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_MUTE);
[1843]890
[1924]891 midi_config.inited |= MIDI_INITED_CB;
892
[1843]893 return 0;
[1485]894#else
[1853]895 g_midi_cb.console  = -1;
896 g_midi_cb.stream   = -1;
[1843]897
[1485]898 return -1;
899#endif
[185]900}
901
[1843]902int midi_cb_free (void) {
[1751]903#ifdef _HAVE_CONSOLE
[1843]904
905 midi_cb_stop();
906
[1853]907 if ( g_midi_cb.stream != -1 )
908  streams_delete(g_midi_cb.stream);
[1843]909
[1853]910 if ( g_midi_cb.console != -1 )
911  close(g_midi_cb.console);
[1843]912
[185]913 return 0;
[1485]914#else
915 return -1;
916#endif
[185]917}
918
919int midi_cb_play(float t, float freq, int override) {
[191]920 float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels;
921
[189]922/*
923#define MIDI_CB_NOOVERRIDE 0
924#define MIDI_CB_OVERRIDE   1
925*/
[1853]926 if ( g_midi_cb.playing && override != MIDI_CB_OVERRIDE )
[191]927  return -1;
928
[1853]929 g_midi_cb.stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t);
[191]930 midi_cb_start(freq);
931
932 return 0;
[185]933}
934
[190]935int midi_cb_update (void) {
[1884]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 }
[1882]943
[1853]944 if ( !g_midi_cb.playing )
[192]945  return 0;
946
[1884]947/*
[1853]948 if ( g_midi_cb.stoptime <= g_pos )
[189]949  midi_cb_stop();
[1884]950*/
[189]951
[1882]952 ROAR_DBG("midi_cb_update(void) = ?");
953
[189]954 return 0;
955}
956
[185]957int midi_cb_start(float freq) {
958// On linux this uses ioctl KIOCSOUND
[188]959#ifdef __linux__
[1853]960 if ( g_midi_cb.console == -1 )
[188]961  return -1;
962
[1853]963 if ( ioctl(g_midi_cb.console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 )
[188]964  return -1;
965
[1884]966 g_midi_cb.playing = 1;
967
[188]968 return 0;
969#else
[185]970 return -1;
[188]971#endif
[185]972}
973
974int midi_cb_stop (void) {
975#ifdef __linux__
[1884]976 int ret;
977
978 ret = midi_cb_start(0);
[1853]979 g_midi_cb.playing = 0;
[1884]980
981 return ret;
[185]982#else
983 return -1;
984#endif
985}
986
[1882]987int midi_cb_readbuf(void) {
988 struct roar_buffer        * buf = g_midi_mess.buf;
989 struct midi_message       * mes = NULL;
[4521]990 void                      * bufdata;
[1882]991
992 ROAR_DBG("midi_cb_readbuf(void) = ?");
993
994 while (buf != NULL) {
995  ROAR_DBG("midi_cb_readbuf(void): buf=%p", buf);
996
[4521]997  if ( roar_buffer_get_data(buf, &bufdata) == -1 ) {
[1882]998   return -1;
999  }
1000
[4521]1001  mes = bufdata;
1002
[1882]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;
[1890]1010   case MIDI_TYPE_CONTROLER:
[1894]1011     if ( mes->kk == MIDI_CCE_ALL_NOTE_OFF ) /* all note off */
[1890]1012      midi_cb_stop();
1013    break;
[1882]1014  }
1015
1016  if ( roar_buffer_get_next(buf, &buf) == -1 )
1017   buf = NULL;
1018 }
1019
1020 return 0;
1021}
[2487]1022#endif
[1882]1023
[1848]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;
[1849]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;
[1848]1037 ss->vio.ctl      = NULL;
[1849]1038 ss->vio.close    = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
1039
1040 return 0;
[1848]1041}
1042
1043int     midi_vio_ok(struct roar_vio_calls * vio, ...) {
1044 return 0;
1045}
1046
[2500]1047#endif
1048
[169]1049//ll
Note: See TracBrowser for help on using the repository browser.