source: roaraudio/roard/midi.c @ 3358:7f9d211148e0

Last change on this file since 3358:7f9d211148e0 was 3358:7f9d211148e0, checked in by phi, 14 years ago

updated (C) statements

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