source: roaraudio/roard/midi.c @ 5053:4542bb21d38d

Last change on this file since 5053:4542bb21d38d was 5053:4542bb21d38d, checked in by phi, 13 years ago

add some debug msgs

File size: 18.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 ) {
[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
[1858]215int midi_conv_midi2mes (int id) {
[4521]216 void          * bufdata;
[1863]217 unsigned char * data;
218 struct roar_stream        *   s;
219 struct roar_stream_server *  ss;
220 struct roar_buffer        * buf = NULL;
221 struct midi_message       * mes = NULL;
[1879]222 size_t need = 0, have = 0, last_have = 0, old_have = 0;
[1863]223 int alive = 1;
224
225 if ( g_streams[id] == NULL )
226  return -1;
227
228 ROAR_DBG("midi_conv_midi2mes(id=%i) = ?", id);
229
230 s = ROAR_STREAM(ss = g_streams[id]);
231
232 while (ss->buffer != NULL && alive) {
[5053]233  ROAR_DBG("midi_conv_midi2mes(id=%i): ss->buffer=%p, alive=%i", id, ss->buffer, alive);
234
[4521]235  if ( roar_buffer_get_data(ss->buffer, &bufdata) == -1 ) {
[1863]236   alive = 0;
237   continue;
238  }
239
[4521]240  data = bufdata;
241
[1863]242  if ( roar_buffer_get_len(ss->buffer, &have) == -1 ) {
243   alive = 0;
244   continue;
245  }
[1879]246  old_have = have;
[1863]247
248  while (have && alive) {
[5053]249   ROAR_DBG("midi_conv_midi2mes(id=%i): have=%llu, alive=%i", id, (long long unsigned int)have, alive);
250
[1863]251   if ( *data & 0x80 ) {
[1865]252    if ( buf != NULL ) {
[1866]253     midi_add_buf(id, buf);
[1865]254     buf = NULL;
255    }
256
[1879]257    last_have = have;
258
[1863]259    need = 0;
260    if ( midi_new_bufmes(&buf, &mes) == -1 ) {
261     alive = 0;
262     continue;
263    }
[1865]264
265    if (*data == MIDI_TYPE_CLOCK_TICK || *data == MIDI_TYPE_CLOCK_START || *data == MIDI_TYPE_CLOCK_STOP ) {
266     mes->type = *data;
[1891]267
268     if ( *data == MIDI_TYPE_CLOCK_TICK ) {
269      s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1);
270     }
[1865]271    } else {
272     mes->type    = *data & 0xF0;
273     mes->channel = *data & 0x0F;
274     switch (*data & 0xF0) {
275      case MIDI_TYPE_NOTE_ON:
276      case MIDI_TYPE_NOTE_OFF:
277        need = 2;
278       break;
279      case MIDI_TYPE_PA:
280        need = 2;
281       break;
282      case MIDI_TYPE_CONTROLER:
283        need = 2;
284       break;
285      case MIDI_TYPE_PROGRAM:
286        need = 1;
287       break;
288      case MIDI_TYPE_MA:
289        need = 1;
290       break;
291      case MIDI_TYPE_PB:
292      case MIDI_TYPE_SYSEX:
293        need = 1;
294       break;
295     }
296    }
297
[1863]298   } else {
[1871]299
300    if ( mes == NULL ) {
301     ROAR_WARN("midi_conv_midi2mes(id=%i): Lost sync.", id);
302     data++;
303     have--;
304     continue;
305    }
306
307    switch (mes->type) {
[1873]308      case MIDI_TYPE_NOTE_ON:
309      case MIDI_TYPE_NOTE_OFF:
[1881]310       if ( need == 2 ) {
311        roar_midi_note_from_midiid(&(mes->d.note), *data);
[2409]312
313        // if velocity is zero we have a NOTE OFF event
314        if ( data[1] == 0 )
315         mes->type = MIDI_TYPE_NOTE_OFF;
[1881]316       }
[1874]317      case MIDI_TYPE_PA:
318      case MIDI_TYPE_CONTROLER:
[1873]319        if ( need == 2 ) {
320         mes->kk = *data;
321        } else {
322         mes->vv = *data;
323        }
324       break;
325      case MIDI_TYPE_PROGRAM:
326      case MIDI_TYPE_MA:
[1874]327        mes->vv = *data;
328       break;
[1873]329      case MIDI_TYPE_PB:
330      case MIDI_TYPE_SYSEX:
[1874]331        ROAR_WARN("midi_conv_midi2mes(id=%i): Message of Type 0x%.2X (PB or SysEx) not yet supported", id, mes->type);
[1873]332       break;
[1871]333    }
334
[1863]335    if ( need )
336     need--;
[1893]337
338    if ( !need ) {
339     switch (mes->type) {
340      case MIDI_TYPE_CONTROLER:
341       switch (mes->kk) {
342        case MIDI_CCE_MAIN_VOL:
343         if ( 516 * mes->vv > 65100 ) { // max volume
344          ss->mixer.mixer[mes->channel] = 65535;
345         } else {
346          ss->mixer.mixer[mes->channel] = 516 * mes->vv;
347         }
348         break;
349       }
350      break;
351     }
352    }
353
[1863]354   }
355   data++;
356   have--;
357  }
358
[1865]359  if ( need ) {
[5053]360   ROAR_DBG("midi_conv_midi2mes(id=%i) = ?", id);
[1879]361   if ( roar_buffer_set_offset(ss->buffer, old_have - last_have) == -1 ) {
362    ROAR_ERR("midi_conv_midi2mes(*): FIXME: BUG!!! Need to restore buffer here with corrected length");
363   }
[1865]364  } else if ( !have ) {
[1863]365   roar_buffer_next(&(ss->buffer));
366  }
367
368  if ( need && buf != NULL ) {
369   roar_buffer_free(buf);
370   buf = NULL;
371  } else if ( buf != NULL ) {
[1866]372   midi_add_buf(id, buf);
[1863]373   buf = NULL;
374  }
375 }
376
377 return 0;
[1858]378}
379
[1877]380#define _nb  len++; *(d++)
[1858]381int midi_conv_mes2midi (int id) {
[1877]382 struct roar_stream        *   s;
383 struct roar_stream_server *  ss;
384 struct roar_buffer        * buf = g_midi_mess.buf;
385 struct midi_message       * mes = NULL;
[4521]386 void                      * bufdata;
[1877]387 unsigned char               data[3];
388 unsigned char             * d;
389 int                         len;
[1909]390 int                         send_clock;
[1877]391
392 if ( g_streams[id] == NULL )
393  return -1;
394
395 ROAR_DBG("midi_conv_mes2midi(id=%i) = ?", id);
396
397 s = ROAR_STREAM(ss = g_streams[id]);
398
[1909]399 send_clock = streams_get_flag(id, ROAR_FLAG_SYNC);
400
[1877]401 while (buf != NULL) {
[4521]402  if ( roar_buffer_get_data(buf, &bufdata) == -1 ) {
[1877]403   return -1;
404  }
405
[4521]406  mes = bufdata;
407
[1877]408  if (mes->type == MIDI_TYPE_CLOCK_TICK || mes->type == MIDI_TYPE_CLOCK_START || mes->type == MIDI_TYPE_CLOCK_STOP ) {
[1910]409   if ( send_clock ) {
[1909]410    if ( stream_vio_s_write(ss, &(mes->type), 1) != 1 ) {
411     streams_delete(id);
412     return -1;
413    }
[1877]414   }
[1896]415
416   if ( mes->type == MIDI_TYPE_CLOCK_TICK ) {
417    s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1);
418   }
[1877]419  } else {
420   len = 0;
421   d   = data;
422
423   _nb = (mes->type & 0xF0) | (mes->channel & 0x0F);
424
425   switch (mes->type) {
[1898]426    case MIDI_TYPE_CONTROLER:
427      switch (mes->kk) {
428       case MIDI_CCE_MAIN_VOL:
429         if ( 516 * mes->vv > 65100 ) { // max volume
430          ss->mixer.mixer[mes->channel] = 65535;
431         } else {
432          ss->mixer.mixer[mes->channel] = 516 * mes->vv;
433         }
434        break;
435      }
[1877]436    case MIDI_TYPE_NOTE_ON:
437    case MIDI_TYPE_NOTE_OFF:
438    case MIDI_TYPE_PA:
439      _nb = mes->kk;
440      _nb = mes->vv;
441     break;
442    case MIDI_TYPE_PROGRAM:
443    case MIDI_TYPE_MA:
444      _nb = mes->vv;
445     break;
446    case MIDI_TYPE_PB:
447    case MIDI_TYPE_SYSEX:
448     break;
449   }
450
451   if ( stream_vio_s_write(ss, data, len) != len ) {
452    streams_delete(id);
453    return -1;
454   }
455  }
456
457  if ( roar_buffer_get_next(buf, &buf) == -1 )
458   buf = NULL;
459 }
460
461 return 0;
[1858]462}
[1877]463#undef _nb
[1858]464
[2489]465#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2461]466int midi_conv_mes2ssynth(void) {
[2462]467 struct roar_buffer        * buf = g_midi_mess.buf;
468 struct midi_message       * mes = NULL;
[4521]469 void                      * bufdata;
[2462]470
471 while (buf != NULL) {
[4521]472  if ( roar_buffer_get_data(buf, &bufdata) == -1 ) {
[2462]473   return -1;
474  }
475
[4521]476  mes = bufdata;
477
[2462]478  if ( ssynth_eval_message(mes) == -1 )
479   return -1;
480
481  if ( roar_buffer_get_next(buf, &buf) == -1 )
482   buf = NULL;
483 }
484
485 return 0;
[2461]486}
[2489]487#endif
[2461]488
[1863]489int midi_new_bufmes    (struct roar_buffer ** buf, struct midi_message ** mes) {
490 if ( buf == NULL || mes == NULL )
491  return -1;
492
[1867]493 *buf = (void*)(*mes = NULL);
[1863]494
[3764]495 if ( roar_buffer_new_data(buf, sizeof(struct midi_message), (void**)mes) == -1 ) {
[1867]496  *buf = (void*)(*mes = NULL);
[1863]497  return -1;
498 }
499
500 memset((void*)*mes, 0, sizeof(struct midi_message));
501
502 (*mes)->type = MIDI_TYPE_NONE;
503
504 return 0;
505}
506
[1866]507int midi_add_buf       (int id, struct roar_buffer * buf) {
[1869]508 struct midi_message * mes;
[4521]509 void * bufdata;
[1869]510
[1866]511 if ( id == -1 || buf == NULL )
512  return -1;
513
[4521]514 if ( roar_buffer_get_data(buf, &bufdata) == -1 )
[1869]515  return -1;
516
[4521]517 mes = bufdata;
518
[1878]519 ROAR_DBG("midi_add_buf(id=%i, buf=%p) = ?", id, buf);
520 ROAR_DBG("midi_add_buf(*): MIDI Message of Type 0x%.2X", mes->type);
521 ROAR_DBG("midi_add_buf(*): Channel: %i", mes->channel);
522 ROAR_DBG("midi_add_buf(*): flags=0x%.2X", mes->flags);
523 ROAR_DBG("midi_add_buf(*): kk=0x%.2X, vv=0x%.2X", mes->kk, mes->vv);
[1869]524
[1866]525 if ( g_midi_mess.buf == NULL ) {
526  g_midi_mess.buf = buf;
527 } else {
528  roar_buffer_add(g_midi_mess.buf, buf);
529 }
530
[5053]531 ROAR_DBG("midi_add_buf(*) = 0");
[1866]532 return 0;
533}
534
[1848]535// bridges:
536int midi_check_bridge  (int id) {
[1851]537
[1860]538 ROAR_DBG("midi_check_bridge(id=%i) = ?", id);
[1851]539
540 if ( id == g_midi_clock.stream ) {
541  midi_clock_tick();
542
543  return 0;
544 }
545
[1848]546 return -1;
547}
548
549// clock:
550
551int midi_clock_init (void) {
552 struct roar_stream * s;
553 struct roar_stream_server * ss;
554
[1850]555 if ( (g_midi_clock.stream = streams_new()) == -1 ) {
[1848]556  ROAR_WARN("Error while initializing MIDI subsystem component clock");
557  return -1;
558 }
559
[2915]560 client_stream_add(g_self_client, g_midi_clock.stream);
561
[1850]562 midi_vio_set_dummy(g_midi_clock.stream);
[1848]563
[1850]564 streams_get(g_midi_clock.stream, &ss);
[1848]565 s = ROAR_STREAM(ss);
566
567 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
568
[1850]569 s->pos_rel_id    =  g_midi_clock.stream;
[1848]570
571 s->info.codec    =  ROAR_CODEC_MIDI;
572 ss->codec_orgi   =  ROAR_CODEC_MIDI;
573
[2401]574 s->info.channels = 1; // we have only one channel, ticking on channel 0
[2400]575 s->info.rate     = ROAR_MIDI_TICKS_PER_BEAT; // one beat per sec
576 s->info.bits     = ROAR_MIDI_BITS;
[1848]577
[1850]578 if ( streams_set_dir(g_midi_clock.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
[1848]579  ROAR_WARN("Error while initializing MIDI subsystem component clock");
580  return -1;
581 }
582
[1850]583 streams_set_name(g_midi_clock.stream, "MIDI Clock");
[1848]584
[1850]585 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_PRIMARY);
586 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_SYNC);
[1848]587
[2400]588 midi_clock_set_bph(3600); // one beat per sec
[1851]589
[2943]590 ss->state = ROAR_STREAMSTATE_NEW;
591
[1924]592 midi_config.inited |= MIDI_INITED_CLOCK;
593
[1851]594 return 0;
595}
596
597int midi_clock_set_bph (uint_least32_t bph) {
598 uint_least32_t sph = g_sa->rate/2 * 75 * g_sa->channels; // samples per houre
599
600 g_midi_clock.bph  = bph;
601
602 g_midi_clock.spt  = sph/bph;
603
604 g_midi_clock.nt   = ROAR_MATH_OVERFLOW_ADD(g_pos, g_midi_clock.spt);
605
606 return 0;
607}
608
609int midi_clock_tick (void) {
[1875]610 struct roar_buffer  * buf;
611 struct midi_message * mes;
[1888]612 struct roar_stream_server * ss;
[1851]613 unsigned int diff;
614
[1888]615 if ( streams_get(g_midi_clock.stream, &ss) == -1 ) {
616  ROAR_ERR("midi_clock_tick(void): Something very BAD happend: can not get stream object of my own stream!");
617  g_midi_clock.stream = -1;
618  ROAR_WARN("midi_clock_tick(void): Disabled MIDI Clock");
619  return -1;
620 }
621
[1851]622 while ( g_pos >= g_midi_clock.nt ) {
623  diff = g_pos - g_midi_clock.nt;
[1911]624  ROAR_DBG("midi_clock_tick(void): g_pos is %u samples (%5.2f%%) after of nt.", diff, (float)diff/g_midi_clock.spt);
[1851]625
626  g_midi_clock.nt   = ROAR_MATH_OVERFLOW_ADD(g_midi_clock.nt, g_midi_clock.spt);
627
628  if ( streams_get_flag(g_midi_clock.stream, ROAR_FLAG_SYNC) ) {
[1860]629   ROAR_DBG("midi_clock_tick(void): TICK! (nt=%lu)", g_midi_clock.nt);
[1875]630   if ( midi_new_bufmes(&buf, &mes) == -1 ) {
631    ROAR_ERR("midi_clock_tick(void): Can not create Clock-Tick-Message");
632   }
633
634   mes->type = MIDI_TYPE_CLOCK_TICK;
635
636   if ( midi_add_buf(g_midi_clock.stream, buf) == -1 ) {
637    ROAR_ERR("midi_clock_tick(void): Can not add Clock-Tick-Message");
638    roar_buffer_free(buf);
639    return -1;
640   }
[1888]641
642   ROAR_STREAM(ss)->pos = ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(ss)->pos, 1);
[2943]643   ss->state = ROAR_STREAMSTATE_OLD;
[1851]644  } else {
[1860]645   ROAR_DBG("midi_clock_tick(void): silent tick. (nt=%lu)", g_midi_clock.nt);
[1851]646  }
647 }
648
[1848]649 return 0;
650}
651
[1845]652// CB:
[1843]653
[2487]654#ifndef ROAR_WITHOUT_DCOMP_CB
[1843]655int midi_cb_init (void) {
[1751]656#ifdef _HAVE_CONSOLE
[1843]657 struct roar_stream * s;
658 struct roar_stream_server * ss;
[185]659 int i;
660 char * files[] = {
[1924]661                   "/NX",
662                   "/dev/roarconsole",
[185]663                   "/dev/console",
664#ifdef __linux__
665                   "/dev/tty0",
666                   "/dev/vc/0",
667#endif
668                   NULL
669                  };
670
[1853]671 g_midi_cb.console  = -1;
672 g_midi_cb.stream   = -1;
673 g_midi_cb.stoptime =  0;
674 g_midi_cb.playing  =  0;
[185]675
[1924]676 if ( midi_config.console_dev != NULL ) {
677  files[0] = midi_config.console_dev;
678 }
679
[185]680 for (i = 0; files[i] != NULL; i++) {
[1853]681  if ( (g_midi_cb.console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 )
[185]682   break;
683 }
684
[1853]685 if ( g_midi_cb.console == -1 )
[1843]686  return -1;
687
[1853]688 if ( (g_midi_cb.stream = streams_new()) == -1 ) {
[1843]689  ROAR_WARN("Error while initializing MIDI subsystem component CB");
690  midi_cb_free();
[187]691  return -1;
692 }
[1843]693
[2915]694 client_stream_add(g_self_client, g_midi_clock.stream);
695
[1853]696 midi_vio_set_dummy(g_midi_cb.stream);
[1848]697
[1853]698 streams_get(g_midi_cb.stream, &ss);
[1843]699 s = ROAR_STREAM(ss);
700
701 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
702
703 s->pos_rel_id    = -1;
704
705 s->info.codec    =  0;
706 ss->codec_orgi   =  0;
707
708 s->info.channels =  1;
709 s->info.rate     = 1193180;
710 s->info.bits     =  8;
711
[1853]712 if ( streams_set_dir(g_midi_cb.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
[1843]713  ROAR_WARN("Error while initializing MIDI subsystem component CB");
714  midi_cb_free();
715  return -1;
716 }
717
[1853]718 streams_set_name(g_midi_cb.stream, "Console speaker bridge");
[1843]719
[1853]720 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_OUTPUT);
721 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_PRIMARY);
722 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_HWMIXER);
[1885]723 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_MUTE);
[1843]724
[1924]725 midi_config.inited |= MIDI_INITED_CB;
726
[1843]727 return 0;
[1485]728#else
[1853]729 g_midi_cb.console  = -1;
730 g_midi_cb.stream   = -1;
[1843]731
[1485]732 return -1;
733#endif
[185]734}
735
[1843]736int midi_cb_free (void) {
[1751]737#ifdef _HAVE_CONSOLE
[1843]738
739 midi_cb_stop();
740
[1853]741 if ( g_midi_cb.stream != -1 )
742  streams_delete(g_midi_cb.stream);
[1843]743
[1853]744 if ( g_midi_cb.console != -1 )
745  close(g_midi_cb.console);
[1843]746
[185]747 return 0;
[1485]748#else
749 return -1;
750#endif
[185]751}
752
753int midi_cb_play(float t, float freq, int override) {
[191]754 float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels;
755
[189]756/*
757#define MIDI_CB_NOOVERRIDE 0
758#define MIDI_CB_OVERRIDE   1
759*/
[1853]760 if ( g_midi_cb.playing && override != MIDI_CB_OVERRIDE )
[191]761  return -1;
762
[1853]763 g_midi_cb.stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t);
[191]764 midi_cb_start(freq);
765
766 return 0;
[185]767}
768
[190]769int midi_cb_update (void) {
[1884]770
771 if ( !streams_get_flag(g_midi_cb.stream, ROAR_FLAG_MUTE) ) {
772  if ( midi_cb_readbuf() == -1 )
773   return -1;
774 } else if ( g_midi_cb.playing ) {
775  midi_cb_stop();
776 }
[1882]777
[1853]778 if ( !g_midi_cb.playing )
[192]779  return 0;
780
[1884]781/*
[1853]782 if ( g_midi_cb.stoptime <= g_pos )
[189]783  midi_cb_stop();
[1884]784*/
[189]785
[1882]786 ROAR_DBG("midi_cb_update(void) = ?");
787
[189]788 return 0;
789}
790
[185]791int midi_cb_start(float freq) {
792// On linux this uses ioctl KIOCSOUND
[188]793#ifdef __linux__
[1853]794 if ( g_midi_cb.console == -1 )
[188]795  return -1;
796
[1853]797 if ( ioctl(g_midi_cb.console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 )
[188]798  return -1;
799
[1884]800 g_midi_cb.playing = 1;
801
[188]802 return 0;
803#else
[185]804 return -1;
[188]805#endif
[185]806}
807
808int midi_cb_stop (void) {
809#ifdef __linux__
[1884]810 int ret;
811
812 ret = midi_cb_start(0);
[1853]813 g_midi_cb.playing = 0;
[1884]814
815 return ret;
[185]816#else
817 return -1;
818#endif
819}
820
[1882]821int midi_cb_readbuf(void) {
822 struct roar_buffer        * buf = g_midi_mess.buf;
823 struct midi_message       * mes = NULL;
[4521]824 void                      * bufdata;
[1882]825
826 ROAR_DBG("midi_cb_readbuf(void) = ?");
827
828 while (buf != NULL) {
829  ROAR_DBG("midi_cb_readbuf(void): buf=%p", buf);
830
[4521]831  if ( roar_buffer_get_data(buf, &bufdata) == -1 ) {
[1882]832   return -1;
833  }
834
[4521]835  mes = bufdata;
836
[1882]837  switch (mes->type) {
838   case MIDI_TYPE_NOTE_ON:
839     midi_cb_start(mes->d.note.freq);
840    break;
841   case MIDI_TYPE_NOTE_OFF:
842     midi_cb_stop();
843    break;
[1890]844   case MIDI_TYPE_CONTROLER:
[1894]845     if ( mes->kk == MIDI_CCE_ALL_NOTE_OFF ) /* all note off */
[1890]846      midi_cb_stop();
847    break;
[1882]848  }
849
850  if ( roar_buffer_get_next(buf, &buf) == -1 )
851   buf = NULL;
852 }
853
854 return 0;
855}
[2487]856#endif
[1882]857
[1848]858// VIO:
859
860int     midi_vio_set_dummy(int stream) {
861 struct roar_stream_server * ss;
862
863 if ( streams_get(stream, &ss) == -1 )
864  return -1;
865
866 ss->vio.read     = NULL;
867 ss->vio.write    = NULL;
868 ss->vio.lseek    = NULL;
[1849]869 ss->vio.nonblock = (int (*)(struct roar_vio_calls * vio, int state))midi_vio_ok;
870 ss->vio.sync     = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
[1848]871 ss->vio.ctl      = NULL;
[1849]872 ss->vio.close    = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
873
874 return 0;
[1848]875}
876
877int     midi_vio_ok(struct roar_vio_calls * vio, ...) {
878 return 0;
879}
880
[2500]881#endif
882
[169]883//ll
Note: See TracBrowser for help on using the repository browser.