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
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 ) {
[5162]160  ROAR_ERR("midi_check_stream(id=%i): Can not alloc buffer space!", id);
161  ROAR_DBG("midi_check_stream(id=%i) = -1", id);
[1858]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
[5162]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 }
[1858]177
[1859]178 if ( stream_add_buffer(id, b) == -1 ) {
179  roar_buffer_free(b);
180  streams_delete(id);
181  return -1;
182 }
183
[1858]184 switch (s->info.codec) {
185  case ROAR_CODEC_MIDI:
186    return midi_conv_midi2mes(id);
187   break;
[1846]188  default:
189    streams_delete(id);
190    return -1;
191 }
192
[5053]193 ROAR_DBG("midi_check_stream(id=%i) = 0", id);
[1846]194 return 0;
[1845]195}
[1846]196
[1845]197int midi_send_stream   (int id) {
[1846]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) {
[1877]209  case ROAR_CODEC_MIDI:
210    return midi_conv_mes2midi(id);
211   break;
[1846]212  default:
213    streams_delete(id);
214    return -1;
215 }
216
217 return 0;
[1845]218}
219
[5089]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
[5162]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  }
[5089]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
[1858]388int midi_conv_midi2mes (int id) {
[4521]389 void          * bufdata;
[1863]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;
[1879]395 size_t need = 0, have = 0, last_have = 0, old_have = 0;
[1863]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) {
[5053]406  ROAR_DBG("midi_conv_midi2mes(id=%i): ss->buffer=%p, alive=%i", id, ss->buffer, alive);
407
[4521]408  if ( roar_buffer_get_data(ss->buffer, &bufdata) == -1 ) {
[1863]409   alive = 0;
410   continue;
411  }
412
[4521]413  data = bufdata;
414
[1863]415  if ( roar_buffer_get_len(ss->buffer, &have) == -1 ) {
416   alive = 0;
417   continue;
418  }
[1879]419  old_have = have;
[1863]420
421  while (have && alive) {
[5053]422   ROAR_DBG("midi_conv_midi2mes(id=%i): have=%llu, alive=%i", id, (long long unsigned int)have, alive);
423
[1863]424   if ( *data & 0x80 ) {
[1865]425    if ( buf != NULL ) {
[1866]426     midi_add_buf(id, buf);
[1865]427     buf = NULL;
428    }
429
[1879]430    last_have = have;
431
[1863]432    need = 0;
433    if ( midi_new_bufmes(&buf, &mes) == -1 ) {
434     alive = 0;
435     continue;
436    }
[1865]437
438    if (*data == MIDI_TYPE_CLOCK_TICK || *data == MIDI_TYPE_CLOCK_START || *data == MIDI_TYPE_CLOCK_STOP ) {
439     mes->type = *data;
[1891]440
441     if ( *data == MIDI_TYPE_CLOCK_TICK ) {
442      s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1);
443     }
[1865]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
[1863]471   } else {
[1871]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) {
[1873]481      case MIDI_TYPE_NOTE_ON:
482      case MIDI_TYPE_NOTE_OFF:
[1881]483       if ( need == 2 ) {
484        roar_midi_note_from_midiid(&(mes->d.note), *data);
[2409]485
486        // if velocity is zero we have a NOTE OFF event
487        if ( data[1] == 0 )
488         mes->type = MIDI_TYPE_NOTE_OFF;
[1881]489       }
[1874]490      case MIDI_TYPE_PA:
491      case MIDI_TYPE_CONTROLER:
[1873]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:
[1874]500        mes->vv = *data;
501       break;
[1873]502      case MIDI_TYPE_PB:
503      case MIDI_TYPE_SYSEX:
[1874]504        ROAR_WARN("midi_conv_midi2mes(id=%i): Message of Type 0x%.2X (PB or SysEx) not yet supported", id, mes->type);
[1873]505       break;
[1871]506    }
507
[1863]508    if ( need )
509     need--;
[1893]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
[1863]527   }
528   data++;
529   have--;
530  }
531
[1865]532  if ( need ) {
[5053]533   ROAR_DBG("midi_conv_midi2mes(id=%i) = ?", id);
[1879]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   }
[1865]537  } else if ( !have ) {
[1863]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 ) {
[1866]545   midi_add_buf(id, buf);
[1863]546   buf = NULL;
547  }
548 }
549
550 return 0;
[1858]551}
[5089]552#endif
[1858]553
[1877]554#define _nb  len++; *(d++)
[1858]555int midi_conv_mes2midi (int id) {
[1877]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;
[4521]560 void                      * bufdata;
[1877]561 unsigned char               data[3];
562 unsigned char             * d;
563 int                         len;
[1909]564 int                         send_clock;
[1877]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
[1909]573 send_clock = streams_get_flag(id, ROAR_FLAG_SYNC);
574
[1877]575 while (buf != NULL) {
[4521]576  if ( roar_buffer_get_data(buf, &bufdata) == -1 ) {
[1877]577   return -1;
578  }
579
[4521]580  mes = bufdata;
581
[1877]582  if (mes->type == MIDI_TYPE_CLOCK_TICK || mes->type == MIDI_TYPE_CLOCK_START || mes->type == MIDI_TYPE_CLOCK_STOP ) {
[1910]583   if ( send_clock ) {
[1909]584    if ( stream_vio_s_write(ss, &(mes->type), 1) != 1 ) {
585     streams_delete(id);
586     return -1;
587    }
[1877]588   }
[1896]589
590   if ( mes->type == MIDI_TYPE_CLOCK_TICK ) {
591    s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1);
592   }
[1877]593  } else {
594   len = 0;
595   d   = data;
596
597   _nb = (mes->type & 0xF0) | (mes->channel & 0x0F);
598
599   switch (mes->type) {
[1898]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      }
[1877]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;
[1858]636}
[1877]637#undef _nb
[1858]638
[2489]639#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2461]640int midi_conv_mes2ssynth(void) {
[2462]641 struct roar_buffer        * buf = g_midi_mess.buf;
642 struct midi_message       * mes = NULL;
[4521]643 void                      * bufdata;
[2462]644
645 while (buf != NULL) {
[4521]646  if ( roar_buffer_get_data(buf, &bufdata) == -1 ) {
[2462]647   return -1;
648  }
649
[4521]650  mes = bufdata;
651
[2462]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;
[2461]660}
[2489]661#endif
[2461]662
[1863]663int midi_new_bufmes    (struct roar_buffer ** buf, struct midi_message ** mes) {
664 if ( buf == NULL || mes == NULL )
665  return -1;
666
[1867]667 *buf = (void*)(*mes = NULL);
[1863]668
[3764]669 if ( roar_buffer_new_data(buf, sizeof(struct midi_message), (void**)mes) == -1 ) {
[1867]670  *buf = (void*)(*mes = NULL);
[1863]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
[1866]681int midi_add_buf       (int id, struct roar_buffer * buf) {
[5162]682#ifdef DEBUG
[1869]683 struct midi_message * mes;
[4521]684 void * bufdata;
[5162]685#endif
[1869]686
[1866]687 if ( id == -1 || buf == NULL )
688  return -1;
689
[5162]690#ifdef DEBUG
[4521]691 if ( roar_buffer_get_data(buf, &bufdata) == -1 )
[1869]692  return -1;
693
[4521]694 mes = bufdata;
695
[1878]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);
[5162]701#endif
[1869]702
[1866]703 if ( g_midi_mess.buf == NULL ) {
704  g_midi_mess.buf = buf;
705 } else {
[5162]706  if ( roar_buffer_add(g_midi_mess.buf, buf) == -1 ) {
707   ROAR_DBG("midi_add_buf(*) = -1");
708   return -1;
709  }
[1866]710 }
711
[5053]712 ROAR_DBG("midi_add_buf(*) = 0");
[1866]713 return 0;
714}
715
[1848]716// bridges:
717int midi_check_bridge  (int id) {
[1851]718
[1860]719 ROAR_DBG("midi_check_bridge(id=%i) = ?", id);
[1851]720
721 if ( id == g_midi_clock.stream ) {
722  midi_clock_tick();
723
724  return 0;
725 }
726
[1848]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
[1850]736 if ( (g_midi_clock.stream = streams_new()) == -1 ) {
[1848]737  ROAR_WARN("Error while initializing MIDI subsystem component clock");
738  return -1;
739 }
740
[2915]741 client_stream_add(g_self_client, g_midi_clock.stream);
742
[1850]743 midi_vio_set_dummy(g_midi_clock.stream);
[1848]744
[1850]745 streams_get(g_midi_clock.stream, &ss);
[1848]746 s = ROAR_STREAM(ss);
747
748 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
749
[1850]750 s->pos_rel_id    =  g_midi_clock.stream;
[1848]751
752 s->info.codec    =  ROAR_CODEC_MIDI;
753 ss->codec_orgi   =  ROAR_CODEC_MIDI;
754
[2401]755 s->info.channels = 1; // we have only one channel, ticking on channel 0
[2400]756 s->info.rate     = ROAR_MIDI_TICKS_PER_BEAT; // one beat per sec
757 s->info.bits     = ROAR_MIDI_BITS;
[1848]758
[1850]759 if ( streams_set_dir(g_midi_clock.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
[1848]760  ROAR_WARN("Error while initializing MIDI subsystem component clock");
761  return -1;
762 }
763
[1850]764 streams_set_name(g_midi_clock.stream, "MIDI Clock");
[1848]765
[1850]766 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_PRIMARY);
767 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_SYNC);
[1848]768
[2400]769 midi_clock_set_bph(3600); // one beat per sec
[1851]770
[2943]771 ss->state = ROAR_STREAMSTATE_NEW;
772
[1924]773 midi_config.inited |= MIDI_INITED_CLOCK;
774
[1851]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) {
[1875]791 struct roar_buffer  * buf;
792 struct midi_message * mes;
[1888]793 struct roar_stream_server * ss;
[1851]794 unsigned int diff;
795
[1888]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
[1851]803 while ( g_pos >= g_midi_clock.nt ) {
804  diff = g_pos - g_midi_clock.nt;
[1911]805  ROAR_DBG("midi_clock_tick(void): g_pos is %u samples (%5.2f%%) after of nt.", diff, (float)diff/g_midi_clock.spt);
[1851]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) ) {
[1860]810   ROAR_DBG("midi_clock_tick(void): TICK! (nt=%lu)", g_midi_clock.nt);
[1875]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   }
[1888]822
823   ROAR_STREAM(ss)->pos = ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(ss)->pos, 1);
[2943]824   ss->state = ROAR_STREAMSTATE_OLD;
[1851]825  } else {
[1860]826   ROAR_DBG("midi_clock_tick(void): silent tick. (nt=%lu)", g_midi_clock.nt);
[1851]827  }
828 }
829
[1848]830 return 0;
831}
832
[1845]833// CB:
[1843]834
[2487]835#ifndef ROAR_WITHOUT_DCOMP_CB
[1843]836int midi_cb_init (void) {
[1751]837#ifdef _HAVE_CONSOLE
[1843]838 struct roar_stream * s;
839 struct roar_stream_server * ss;
[185]840 int i;
841 char * files[] = {
[1924]842                   "/NX",
843                   "/dev/roarconsole",
[185]844                   "/dev/console",
845#ifdef __linux__
846                   "/dev/tty0",
847                   "/dev/vc/0",
848#endif
849                   NULL
850                  };
851
[1853]852 g_midi_cb.console  = -1;
853 g_midi_cb.stream   = -1;
854 g_midi_cb.stoptime =  0;
855 g_midi_cb.playing  =  0;
[185]856
[1924]857 if ( midi_config.console_dev != NULL ) {
858  files[0] = midi_config.console_dev;
859 }
860
[185]861 for (i = 0; files[i] != NULL; i++) {
[1853]862  if ( (g_midi_cb.console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 )
[185]863   break;
864 }
865
[1853]866 if ( g_midi_cb.console == -1 )
[1843]867  return -1;
868
[1853]869 if ( (g_midi_cb.stream = streams_new()) == -1 ) {
[1843]870  ROAR_WARN("Error while initializing MIDI subsystem component CB");
871  midi_cb_free();
[187]872  return -1;
873 }
[1843]874
[2915]875 client_stream_add(g_self_client, g_midi_clock.stream);
876
[1853]877 midi_vio_set_dummy(g_midi_cb.stream);
[1848]878
[1853]879 streams_get(g_midi_cb.stream, &ss);
[1843]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
[1853]893 if ( streams_set_dir(g_midi_cb.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
[1843]894  ROAR_WARN("Error while initializing MIDI subsystem component CB");
895  midi_cb_free();
896  return -1;
897 }
898
[1853]899 streams_set_name(g_midi_cb.stream, "Console speaker bridge");
[1843]900
[1853]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);
[1885]904 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_MUTE);
[1843]905
[1924]906 midi_config.inited |= MIDI_INITED_CB;
907
[1843]908 return 0;
[1485]909#else
[1853]910 g_midi_cb.console  = -1;
911 g_midi_cb.stream   = -1;
[1843]912
[1485]913 return -1;
914#endif
[185]915}
916
[1843]917int midi_cb_free (void) {
[1751]918#ifdef _HAVE_CONSOLE
[1843]919
920 midi_cb_stop();
921
[1853]922 if ( g_midi_cb.stream != -1 )
923  streams_delete(g_midi_cb.stream);
[1843]924
[1853]925 if ( g_midi_cb.console != -1 )
926  close(g_midi_cb.console);
[1843]927
[185]928 return 0;
[1485]929#else
930 return -1;
931#endif
[185]932}
933
934int midi_cb_play(float t, float freq, int override) {
[191]935 float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels;
936
[189]937/*
938#define MIDI_CB_NOOVERRIDE 0
939#define MIDI_CB_OVERRIDE   1
940*/
[1853]941 if ( g_midi_cb.playing && override != MIDI_CB_OVERRIDE )
[191]942  return -1;
943
[1853]944 g_midi_cb.stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t);
[191]945 midi_cb_start(freq);
946
947 return 0;
[185]948}
949
[190]950int midi_cb_update (void) {
[1884]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 }
[1882]958
[1853]959 if ( !g_midi_cb.playing )
[192]960  return 0;
961
[1884]962/*
[1853]963 if ( g_midi_cb.stoptime <= g_pos )
[189]964  midi_cb_stop();
[1884]965*/
[189]966
[1882]967 ROAR_DBG("midi_cb_update(void) = ?");
968
[189]969 return 0;
970}
971
[185]972int midi_cb_start(float freq) {
973// On linux this uses ioctl KIOCSOUND
[188]974#ifdef __linux__
[1853]975 if ( g_midi_cb.console == -1 )
[188]976  return -1;
977
[1853]978 if ( ioctl(g_midi_cb.console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 )
[188]979  return -1;
980
[1884]981 g_midi_cb.playing = 1;
982
[188]983 return 0;
984#else
[185]985 return -1;
[188]986#endif
[185]987}
988
989int midi_cb_stop (void) {
990#ifdef __linux__
[1884]991 int ret;
992
993 ret = midi_cb_start(0);
[1853]994 g_midi_cb.playing = 0;
[1884]995
996 return ret;
[185]997#else
998 return -1;
999#endif
1000}
1001
[1882]1002int midi_cb_readbuf(void) {
1003 struct roar_buffer        * buf = g_midi_mess.buf;
1004 struct midi_message       * mes = NULL;
[4521]1005 void                      * bufdata;
[1882]1006
1007 ROAR_DBG("midi_cb_readbuf(void) = ?");
1008
1009 while (buf != NULL) {
1010  ROAR_DBG("midi_cb_readbuf(void): buf=%p", buf);
1011
[4521]1012  if ( roar_buffer_get_data(buf, &bufdata) == -1 ) {
[1882]1013   return -1;
1014  }
1015
[4521]1016  mes = bufdata;
1017
[1882]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;
[1890]1025   case MIDI_TYPE_CONTROLER:
[1894]1026     if ( mes->kk == MIDI_CCE_ALL_NOTE_OFF ) /* all note off */
[1890]1027      midi_cb_stop();
1028    break;
[1882]1029  }
1030
1031  if ( roar_buffer_get_next(buf, &buf) == -1 )
1032   buf = NULL;
1033 }
1034
1035 return 0;
1036}
[2487]1037#endif
[1882]1038
[1848]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;
[1849]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;
[1848]1052 ss->vio.ctl      = NULL;
[1849]1053 ss->vio.close    = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
1054
1055 return 0;
[1848]1056}
1057
1058int     midi_vio_ok(struct roar_vio_calls * vio, ...) {
1059 return 0;
1060}
1061
[2500]1062#endif
1063
[169]1064//ll
Note: See TracBrowser for help on using the repository browser.