source: roaraudio/roard/midi.c @ 3213:da8251c98c0a

Last change on this file since 3213:da8251c98c0a was 2943:da369368665c, checked in by phi, 15 years ago

corrected stream states

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