source: roaraudio/roard/midi.c @ 2487:10020ba41793

Last change on this file since 2487:10020ba41793 was 2487:10020ba41793, checked in by phi, 15 years ago

support to disable CB completly

File size: 17.5 KB
RevLine 
[169]1//midi.c:
2
[668]3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
5 *
6 *  This file is part of roard a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
[169]25#include "roard.h"
26
[1751]27#if defined(ROAR_HAVE_IO_POSIX) && !defined(ROAR_TARGET_WIN32)
28#define _HAVE_CONSOLE
29#endif
30
[1924]31int midi_init_config(void) {
32 midi_config.init        = 1;
33 midi_config.inited      = 0;
[2487]34
35#ifndef ROAR_WITHOUT_DCOMP_CB
[2445]36 midi_config.init_cb     = 0;
[1924]37 midi_config.console_dev = NULL;
[2487]38#endif
[1924]39
40 return 0;
41}
42
[185]43int midi_init (void) {
[1843]44
[1924]45 midi_config.inited = 0;
[1843]46
[1924]47 if ( midi_config.init ) {
[2487]48#ifndef ROAR_WITHOUT_DCOMP_CB
[1924]49  if ( midi_config.init_cb ) {
50   if ( midi_cb_init() == -1 ) {
51    ROAR_WARN("Can not initialize MIDI subsystem component CB");
52   }
53  }
[2487]54#endif
[1924]55
56  if ( midi_clock_init() == -1 ) {
57   ROAR_WARN("Can not initialize MIDI subsystem component clock");
58  }
59
[1848]60 }
61
[1863]62 g_midi_mess.buf = NULL;
63
[1924]64 midi_config.inited |= MIDI_INITED_MAIN;
65
[1843]66 return 0;
67}
[1848]68
[1843]69int midi_free (void) {
[1924]70 if ( midi_reinit() == -1 )
71  return -1;
72
[2487]73#ifndef ROAR_WITHOUT_DCOMP_CB
[1924]74 if ( midi_cb_free() == -1 )
75  return -1;
[2487]76#endif
[1924]77
78 return 0;
[1843]79}
80
81int midi_update(void) {
[1848]82
[1850]83 if ( g_midi_clock.stream != -1 )
84  midi_check_bridge(g_midi_clock.stream);
[1848]85
[2461]86 midi_conv_mes2ssynth();
87
[2487]88#ifndef ROAR_WITHOUT_DCOMP_CB
[1843]89 return midi_cb_update();
[2487]90#else
91 return 0;
92#endif
[1843]93}
94
[1855]95int midi_reinit(void) {
[1868]96
97 if ( g_midi_mess.buf != NULL )
98  roar_buffer_free(g_midi_mess.buf);
99
100 g_midi_mess.buf = NULL;
101
[1855]102 return 0;
103}
104
[1845]105// STREAMS:
106
107int midi_check_stream  (int id) {
[1846]108 struct roar_stream        *   s;
109 struct roar_stream_server *  ss;
[1858]110 struct roar_buffer        *   b;
111 char                      * buf;
112 ssize_t                     len;
[1846]113
114 if ( g_streams[id] == NULL )
115  return -1;
116
117 ROAR_DBG("midi_check_stream(id=%i) = ?", id);
118
119 s = ROAR_STREAM(ss = g_streams[id]);
120
[1848]121 if ( s->dir == ROAR_DIR_BRIDGE )
122  return midi_check_bridge(id);
123
[1846]124 switch (s->info.codec) {
[1858]125  case ROAR_CODEC_MIDI:
126   break;
127  default:
128    streams_delete(id);
129    return -1;
130 }
131
132 if ( roar_buffer_new(&b, MIDI_READ_SIZE) == -1 ) {
133  ROAR_ERR("midi_check_stream(*): Can not alloc buffer space!");
134  ROAR_DBG("midi_check_stream(*) = -1");
135  return -1;
136 }
137
138 roar_buffer_get_data(b, (void **)&buf);
139
140 if ( (len = stream_vio_s_read(ss, buf, MIDI_READ_SIZE)) < 1 ) {
141  streams_delete(id);
[1862]142  roar_buffer_free(b);
[1858]143  return -1;
144 }
145
146 roar_buffer_set_len(b, len);
147
[1859]148 if ( stream_add_buffer(id, b) == -1 ) {
149  roar_buffer_free(b);
150  streams_delete(id);
151  return -1;
152 }
153
[1858]154 switch (s->info.codec) {
155  case ROAR_CODEC_MIDI:
156    return midi_conv_midi2mes(id);
157   break;
[1846]158  default:
159    streams_delete(id);
160    return -1;
161 }
162
163 return 0;
[1845]164}
[1846]165
[1845]166int midi_send_stream   (int id) {
[1846]167 struct roar_stream        *   s;
168 struct roar_stream_server *  ss;
169
170 if ( g_streams[id] == NULL )
171  return -1;
172
173 ROAR_DBG("midi_send_stream(id=%i) = ?", id);
174
175 s = ROAR_STREAM(ss = g_streams[id]);
176
177 switch (s->info.codec) {
[1877]178  case ROAR_CODEC_MIDI:
179    return midi_conv_mes2midi(id);
180   break;
[1846]181  default:
182    streams_delete(id);
183    return -1;
184 }
185
186 return 0;
[1845]187}
188
[1858]189int midi_conv_midi2mes (int id) {
[1863]190 unsigned char * data;
191 struct roar_stream        *   s;
192 struct roar_stream_server *  ss;
193 struct roar_buffer        * buf = NULL;
194 struct midi_message       * mes = NULL;
[1879]195 size_t need = 0, have = 0, last_have = 0, old_have = 0;
[1863]196 int alive = 1;
197
198 if ( g_streams[id] == NULL )
199  return -1;
200
201 ROAR_DBG("midi_conv_midi2mes(id=%i) = ?", id);
202
203 s = ROAR_STREAM(ss = g_streams[id]);
204
205 while (ss->buffer != NULL && alive) {
206  if ( roar_buffer_get_data(ss->buffer, (void**)&data) == -1 ) {
207   alive = 0;
208   continue;
209  }
210
211  if ( roar_buffer_get_len(ss->buffer, &have) == -1 ) {
212   alive = 0;
213   continue;
214  }
[1879]215  old_have = have;
[1863]216
217  while (have && alive) {
218   if ( *data & 0x80 ) {
[1865]219    if ( buf != NULL ) {
[1866]220     midi_add_buf(id, buf);
[1865]221     buf = NULL;
222    }
223
[1879]224    last_have = have;
225
[1863]226    need = 0;
227    if ( midi_new_bufmes(&buf, &mes) == -1 ) {
228     alive = 0;
229     continue;
230    }
[1865]231
232    if (*data == MIDI_TYPE_CLOCK_TICK || *data == MIDI_TYPE_CLOCK_START || *data == MIDI_TYPE_CLOCK_STOP ) {
233     mes->type = *data;
[1891]234
235     if ( *data == MIDI_TYPE_CLOCK_TICK ) {
236      s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1);
237     }
[1865]238    } else {
239     mes->type    = *data & 0xF0;
240     mes->channel = *data & 0x0F;
241     switch (*data & 0xF0) {
242      case MIDI_TYPE_NOTE_ON:
243      case MIDI_TYPE_NOTE_OFF:
244        need = 2;
245       break;
246      case MIDI_TYPE_PA:
247        need = 2;
248       break;
249      case MIDI_TYPE_CONTROLER:
250        need = 2;
251       break;
252      case MIDI_TYPE_PROGRAM:
253        need = 1;
254       break;
255      case MIDI_TYPE_MA:
256        need = 1;
257       break;
258      case MIDI_TYPE_PB:
259      case MIDI_TYPE_SYSEX:
260        need = 1;
261       break;
262     }
263    }
264
[1863]265   } else {
[1871]266
267    if ( mes == NULL ) {
268     ROAR_WARN("midi_conv_midi2mes(id=%i): Lost sync.", id);
269     data++;
270     have--;
271     continue;
272    }
273
274    switch (mes->type) {
[1873]275      case MIDI_TYPE_NOTE_ON:
276      case MIDI_TYPE_NOTE_OFF:
[1881]277       if ( need == 2 ) {
278        roar_midi_note_from_midiid(&(mes->d.note), *data);
[2409]279
280        // if velocity is zero we have a NOTE OFF event
281        if ( data[1] == 0 )
282         mes->type = MIDI_TYPE_NOTE_OFF;
[1881]283       }
[1874]284      case MIDI_TYPE_PA:
285      case MIDI_TYPE_CONTROLER:
[1873]286        if ( need == 2 ) {
287         mes->kk = *data;
288        } else {
289         mes->vv = *data;
290        }
291       break;
292      case MIDI_TYPE_PROGRAM:
293      case MIDI_TYPE_MA:
[1874]294        mes->vv = *data;
295       break;
[1873]296      case MIDI_TYPE_PB:
297      case MIDI_TYPE_SYSEX:
[1874]298        ROAR_WARN("midi_conv_midi2mes(id=%i): Message of Type 0x%.2X (PB or SysEx) not yet supported", id, mes->type);
[1873]299       break;
[1871]300    }
301
[1863]302    if ( need )
303     need--;
[1893]304
305    if ( !need ) {
306     switch (mes->type) {
307      case MIDI_TYPE_CONTROLER:
308       switch (mes->kk) {
309        case MIDI_CCE_MAIN_VOL:
310         if ( 516 * mes->vv > 65100 ) { // max volume
311          ss->mixer.mixer[mes->channel] = 65535;
312         } else {
313          ss->mixer.mixer[mes->channel] = 516 * mes->vv;
314         }
315         break;
316       }
317      break;
318     }
319    }
320
[1863]321   }
322   data++;
323   have--;
324  }
325
[1865]326  if ( need ) {
[1879]327   if ( roar_buffer_set_offset(ss->buffer, old_have - last_have) == -1 ) {
328    ROAR_ERR("midi_conv_midi2mes(*): FIXME: BUG!!! Need to restore buffer here with corrected length");
329   }
[1865]330  } else if ( !have ) {
[1863]331   roar_buffer_next(&(ss->buffer));
332  }
333
334  if ( need && buf != NULL ) {
335   roar_buffer_free(buf);
336   buf = NULL;
337  } else if ( buf != NULL ) {
[1866]338   midi_add_buf(id, buf);
[1863]339   buf = NULL;
340  }
341 }
342
343 return 0;
[1858]344}
345
[1877]346#define _nb  len++; *(d++)
[1858]347int midi_conv_mes2midi (int id) {
[1877]348 struct roar_stream        *   s;
349 struct roar_stream_server *  ss;
350 struct roar_buffer        * buf = g_midi_mess.buf;
351 struct midi_message       * mes = NULL;
352 unsigned char               data[3];
353 unsigned char             * d;
354 int                         len;
[1909]355 int                         send_clock;
[1877]356
357 if ( g_streams[id] == NULL )
358  return -1;
359
360 ROAR_DBG("midi_conv_mes2midi(id=%i) = ?", id);
361
362 s = ROAR_STREAM(ss = g_streams[id]);
363
[1909]364 send_clock = streams_get_flag(id, ROAR_FLAG_SYNC);
365
[1877]366 while (buf != NULL) {
367  if ( roar_buffer_get_data(buf, (void**)&mes) == -1 ) {
368   return -1;
369  }
370
371  if (mes->type == MIDI_TYPE_CLOCK_TICK || mes->type == MIDI_TYPE_CLOCK_START || mes->type == MIDI_TYPE_CLOCK_STOP ) {
[1910]372   if ( send_clock ) {
[1909]373    if ( stream_vio_s_write(ss, &(mes->type), 1) != 1 ) {
374     streams_delete(id);
375     return -1;
376    }
[1877]377   }
[1896]378
379   if ( mes->type == MIDI_TYPE_CLOCK_TICK ) {
380    s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1);
381   }
[1877]382  } else {
383   len = 0;
384   d   = data;
385
386   _nb = (mes->type & 0xF0) | (mes->channel & 0x0F);
387
388   switch (mes->type) {
[1898]389    case MIDI_TYPE_CONTROLER:
390      switch (mes->kk) {
391       case MIDI_CCE_MAIN_VOL:
392         if ( 516 * mes->vv > 65100 ) { // max volume
393          ss->mixer.mixer[mes->channel] = 65535;
394         } else {
395          ss->mixer.mixer[mes->channel] = 516 * mes->vv;
396         }
397        break;
398      }
[1877]399    case MIDI_TYPE_NOTE_ON:
400    case MIDI_TYPE_NOTE_OFF:
401    case MIDI_TYPE_PA:
402      _nb = mes->kk;
403      _nb = mes->vv;
404     break;
405    case MIDI_TYPE_PROGRAM:
406    case MIDI_TYPE_MA:
407      _nb = mes->vv;
408     break;
409    case MIDI_TYPE_PB:
410    case MIDI_TYPE_SYSEX:
411     break;
412   }
413
414   if ( stream_vio_s_write(ss, data, len) != len ) {
415    streams_delete(id);
416    return -1;
417   }
418  }
419
420  if ( roar_buffer_get_next(buf, &buf) == -1 )
421   buf = NULL;
422 }
423
424 return 0;
[1858]425}
[1877]426#undef _nb
[1858]427
[2461]428int midi_conv_mes2ssynth(void) {
[2462]429 struct roar_buffer        * buf = g_midi_mess.buf;
430 struct midi_message       * mes = NULL;
431
432 while (buf != NULL) {
433  if ( roar_buffer_get_data(buf, (void**)&mes) == -1 ) {
434   return -1;
435  }
436
437  if ( ssynth_eval_message(mes) == -1 )
438   return -1;
439
440  if ( roar_buffer_get_next(buf, &buf) == -1 )
441   buf = NULL;
442 }
443
444 return 0;
[2461]445}
446
[1863]447int midi_new_bufmes    (struct roar_buffer ** buf, struct midi_message ** mes) {
448 if ( buf == NULL || mes == NULL )
449  return -1;
450
[1867]451 *buf = (void*)(*mes = NULL);
[1863]452
453 if ( roar_buffer_new(buf, sizeof(struct midi_message)) == -1 )
454  return -1;
455
456 if ( roar_buffer_get_data(*buf, (void**)mes) == -1 ) {
457  roar_buffer_free(*buf);
[1867]458  *buf = (void*)(*mes = NULL);
[1863]459  return -1;
460 }
461
462 memset((void*)*mes, 0, sizeof(struct midi_message));
463
464 (*mes)->type = MIDI_TYPE_NONE;
465
466 return 0;
467}
468
[1866]469int midi_add_buf       (int id, struct roar_buffer * buf) {
[1869]470 struct midi_message * mes;
471
[1866]472 if ( id == -1 || buf == NULL )
473  return -1;
474
[1872]475 if ( roar_buffer_get_data(buf, (void**)&mes) == -1 )
[1869]476  return -1;
477
[1878]478 ROAR_DBG("midi_add_buf(id=%i, buf=%p) = ?", id, buf);
479 ROAR_DBG("midi_add_buf(*): MIDI Message of Type 0x%.2X", mes->type);
480 ROAR_DBG("midi_add_buf(*): Channel: %i", mes->channel);
481 ROAR_DBG("midi_add_buf(*): flags=0x%.2X", mes->flags);
482 ROAR_DBG("midi_add_buf(*): kk=0x%.2X, vv=0x%.2X", mes->kk, mes->vv);
[1869]483
[1866]484 if ( g_midi_mess.buf == NULL ) {
485  g_midi_mess.buf = buf;
486 } else {
487  roar_buffer_add(g_midi_mess.buf, buf);
488 }
489
490 return 0;
491}
492
[1848]493// bridges:
494int midi_check_bridge  (int id) {
[1851]495
[1860]496 ROAR_DBG("midi_check_bridge(id=%i) = ?", id);
[1851]497
498 if ( id == g_midi_clock.stream ) {
499  midi_clock_tick();
500
501  return 0;
502 }
503
[1848]504 return -1;
505}
506
507// clock:
508
509int midi_clock_init (void) {
510 struct roar_stream * s;
511 struct roar_stream_server * ss;
512
[1850]513 if ( (g_midi_clock.stream = streams_new()) == -1 ) {
[1848]514  ROAR_WARN("Error while initializing MIDI subsystem component clock");
515  return -1;
516 }
517
[1850]518 midi_vio_set_dummy(g_midi_clock.stream);
[1848]519
[1850]520 streams_get(g_midi_clock.stream, &ss);
[1848]521 s = ROAR_STREAM(ss);
522
523 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
524
[1850]525 s->pos_rel_id    =  g_midi_clock.stream;
[1848]526
527 s->info.codec    =  ROAR_CODEC_MIDI;
528 ss->codec_orgi   =  ROAR_CODEC_MIDI;
529
[2401]530 s->info.channels = 1; // we have only one channel, ticking on channel 0
[2400]531 s->info.rate     = ROAR_MIDI_TICKS_PER_BEAT; // one beat per sec
532 s->info.bits     = ROAR_MIDI_BITS;
[1848]533
[1850]534 if ( streams_set_dir(g_midi_clock.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
[1848]535  ROAR_WARN("Error while initializing MIDI subsystem component clock");
536  return -1;
537 }
538
[1850]539 streams_set_name(g_midi_clock.stream, "MIDI Clock");
[1848]540
[1850]541 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_PRIMARY);
542 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_SYNC);
[1848]543
[2400]544 midi_clock_set_bph(3600); // one beat per sec
[1851]545
[1924]546 midi_config.inited |= MIDI_INITED_CLOCK;
547
[1851]548 return 0;
549}
550
551int midi_clock_set_bph (uint_least32_t bph) {
552 uint_least32_t sph = g_sa->rate/2 * 75 * g_sa->channels; // samples per houre
553
554 g_midi_clock.bph  = bph;
555
556 g_midi_clock.spt  = sph/bph;
557
558 g_midi_clock.nt   = ROAR_MATH_OVERFLOW_ADD(g_pos, g_midi_clock.spt);
559
560 return 0;
561}
562
563int midi_clock_tick (void) {
[1875]564 struct roar_buffer  * buf;
565 struct midi_message * mes;
[1888]566 struct roar_stream_server * ss;
[1851]567 unsigned int diff;
568
[1888]569 if ( streams_get(g_midi_clock.stream, &ss) == -1 ) {
570  ROAR_ERR("midi_clock_tick(void): Something very BAD happend: can not get stream object of my own stream!");
571  g_midi_clock.stream = -1;
572  ROAR_WARN("midi_clock_tick(void): Disabled MIDI Clock");
573  return -1;
574 }
575
[1851]576 while ( g_pos >= g_midi_clock.nt ) {
577  diff = g_pos - g_midi_clock.nt;
[1911]578  ROAR_DBG("midi_clock_tick(void): g_pos is %u samples (%5.2f%%) after of nt.", diff, (float)diff/g_midi_clock.spt);
[1851]579
580  g_midi_clock.nt   = ROAR_MATH_OVERFLOW_ADD(g_midi_clock.nt, g_midi_clock.spt);
581
582  if ( streams_get_flag(g_midi_clock.stream, ROAR_FLAG_SYNC) ) {
[1860]583   ROAR_DBG("midi_clock_tick(void): TICK! (nt=%lu)", g_midi_clock.nt);
[1875]584   if ( midi_new_bufmes(&buf, &mes) == -1 ) {
585    ROAR_ERR("midi_clock_tick(void): Can not create Clock-Tick-Message");
586   }
587
588   mes->type = MIDI_TYPE_CLOCK_TICK;
589
590   if ( midi_add_buf(g_midi_clock.stream, buf) == -1 ) {
591    ROAR_ERR("midi_clock_tick(void): Can not add Clock-Tick-Message");
592    roar_buffer_free(buf);
593    return -1;
594   }
[1888]595
596   ROAR_STREAM(ss)->pos = ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(ss)->pos, 1);
[1851]597  } else {
[1860]598   ROAR_DBG("midi_clock_tick(void): silent tick. (nt=%lu)", g_midi_clock.nt);
[1851]599  }
600 }
601
[1848]602 return 0;
603}
604
[1845]605// CB:
[1843]606
[2487]607#ifndef ROAR_WITHOUT_DCOMP_CB
[1843]608int midi_cb_init (void) {
[1751]609#ifdef _HAVE_CONSOLE
[1843]610 struct roar_stream * s;
611 struct roar_stream_server * ss;
[185]612 int i;
613 char * files[] = {
[1924]614                   "/NX",
615                   "/dev/roarconsole",
[185]616                   "/dev/console",
617#ifdef __linux__
618                   "/dev/tty0",
619                   "/dev/vc/0",
620#endif
621                   NULL
622                  };
623
[1853]624 g_midi_cb.console  = -1;
625 g_midi_cb.stream   = -1;
626 g_midi_cb.stoptime =  0;
627 g_midi_cb.playing  =  0;
[185]628
[1924]629 if ( midi_config.console_dev != NULL ) {
630  files[0] = midi_config.console_dev;
631 }
632
[185]633 for (i = 0; files[i] != NULL; i++) {
[1853]634  if ( (g_midi_cb.console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 )
[185]635   break;
636 }
637
[1853]638 if ( g_midi_cb.console == -1 )
[1843]639  return -1;
640
[1853]641 if ( (g_midi_cb.stream = streams_new()) == -1 ) {
[1843]642  ROAR_WARN("Error while initializing MIDI subsystem component CB");
643  midi_cb_free();
[187]644  return -1;
645 }
[1843]646
[1853]647 midi_vio_set_dummy(g_midi_cb.stream);
[1848]648
[1853]649 streams_get(g_midi_cb.stream, &ss);
[1843]650 s = ROAR_STREAM(ss);
651
652 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
653
654 s->pos_rel_id    = -1;
655
656 s->info.codec    =  0;
657 ss->codec_orgi   =  0;
658
659 s->info.channels =  1;
660 s->info.rate     = 1193180;
661 s->info.bits     =  8;
662
[1853]663 if ( streams_set_dir(g_midi_cb.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
[1843]664  ROAR_WARN("Error while initializing MIDI subsystem component CB");
665  midi_cb_free();
666  return -1;
667 }
668
[1853]669 streams_set_name(g_midi_cb.stream, "Console speaker bridge");
[1843]670
[1853]671 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_OUTPUT);
672 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_PRIMARY);
673 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_HWMIXER);
[1885]674 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_MUTE);
[1843]675
[1924]676 midi_config.inited |= MIDI_INITED_CB;
677
[1843]678 return 0;
[1485]679#else
[1853]680 g_midi_cb.console  = -1;
681 g_midi_cb.stream   = -1;
[1843]682
[1485]683 return -1;
684#endif
[185]685}
686
[1843]687int midi_cb_free (void) {
[1751]688#ifdef _HAVE_CONSOLE
[1843]689
690 midi_cb_stop();
691
[1853]692 if ( g_midi_cb.stream != -1 )
693  streams_delete(g_midi_cb.stream);
[1843]694
[1853]695 if ( g_midi_cb.console != -1 )
696  close(g_midi_cb.console);
[1843]697
[185]698 return 0;
[1485]699#else
700 return -1;
701#endif
[185]702}
703
704int midi_cb_play(float t, float freq, int override) {
[191]705 float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels;
706
[189]707/*
708#define MIDI_CB_NOOVERRIDE 0
709#define MIDI_CB_OVERRIDE   1
710*/
[1853]711 if ( g_midi_cb.playing && override != MIDI_CB_OVERRIDE )
[191]712  return -1;
713
[1853]714 g_midi_cb.stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t);
[191]715 midi_cb_start(freq);
716
717 return 0;
[185]718}
719
[190]720int midi_cb_update (void) {
[1884]721
722 if ( !streams_get_flag(g_midi_cb.stream, ROAR_FLAG_MUTE) ) {
723  if ( midi_cb_readbuf() == -1 )
724   return -1;
725 } else if ( g_midi_cb.playing ) {
726  midi_cb_stop();
727 }
[1882]728
[1853]729 if ( !g_midi_cb.playing )
[192]730  return 0;
731
[1884]732/*
[1853]733 if ( g_midi_cb.stoptime <= g_pos )
[189]734  midi_cb_stop();
[1884]735*/
[189]736
[1882]737 ROAR_DBG("midi_cb_update(void) = ?");
738
[189]739 return 0;
740}
741
[185]742int midi_cb_start(float freq) {
743// On linux this uses ioctl KIOCSOUND
[188]744#ifdef __linux__
[1853]745 if ( g_midi_cb.console == -1 )
[188]746  return -1;
747
[1853]748 if ( ioctl(g_midi_cb.console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 )
[188]749  return -1;
750
[1884]751 g_midi_cb.playing = 1;
752
[188]753 return 0;
754#else
[185]755 return -1;
[188]756#endif
[185]757}
758
759int midi_cb_stop (void) {
760#ifdef __linux__
[1884]761 int ret;
762
763 ret = midi_cb_start(0);
[1853]764 g_midi_cb.playing = 0;
[1884]765
766 return ret;
[185]767#else
768 return -1;
769#endif
770}
771
[1882]772int midi_cb_readbuf(void) {
773 struct roar_buffer        * buf = g_midi_mess.buf;
774 struct midi_message       * mes = NULL;
775
776 ROAR_DBG("midi_cb_readbuf(void) = ?");
777
778 while (buf != NULL) {
779  ROAR_DBG("midi_cb_readbuf(void): buf=%p", buf);
780
781  if ( roar_buffer_get_data(buf, (void**)&mes) == -1 ) {
782   return -1;
783  }
784
785  switch (mes->type) {
786   case MIDI_TYPE_NOTE_ON:
787     midi_cb_start(mes->d.note.freq);
788    break;
789   case MIDI_TYPE_NOTE_OFF:
790     midi_cb_stop();
791    break;
[1890]792   case MIDI_TYPE_CONTROLER:
[1894]793     if ( mes->kk == MIDI_CCE_ALL_NOTE_OFF ) /* all note off */
[1890]794      midi_cb_stop();
795    break;
[1882]796  }
797
798  if ( roar_buffer_get_next(buf, &buf) == -1 )
799   buf = NULL;
800 }
801
802 return 0;
803}
[2487]804#endif
[1882]805
[1848]806// VIO:
807
808int     midi_vio_set_dummy(int stream) {
809 struct roar_stream_server * ss;
810
811 if ( streams_get(stream, &ss) == -1 )
812  return -1;
813
814 ss->vio.read     = NULL;
815 ss->vio.write    = NULL;
816 ss->vio.lseek    = NULL;
[1849]817 ss->vio.nonblock = (int (*)(struct roar_vio_calls * vio, int state))midi_vio_ok;
818 ss->vio.sync     = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
[1848]819 ss->vio.ctl      = NULL;
[1849]820 ss->vio.close    = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
821
822 return 0;
[1848]823}
824
825int     midi_vio_ok(struct roar_vio_calls * vio, ...) {
826 return 0;
827}
828
[169]829//ll
Note: See TracBrowser for help on using the repository browser.