source: roaraudio/roard/midi.c @ 2495:61b6587c286a

Last change on this file since 2495:61b6587c286a was 2489:266b1a6c9683, checked in by phi, 15 years ago

support to disable ssynth

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