source: roaraudio/roard/midi.c @ 2500:5e481907a8c0

Last change on this file since 2500:5e481907a8c0 was 2500:5e481907a8c0, checked in by phi, 15 years ago

support to disable MIDI subsystem completly

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