source: roaraudio/roard/midi.c @ 5012:b263759832f1

Last change on this file since 5012:b263759832f1 was 4708:c9d40761088a, checked in by phi, 13 years ago

updated copyright statements

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