source: roaraudio/roard/midi.c @ 3215:13678c1774be

Last change on this file since 3215:13678c1774be was 3215:13678c1774be, checked in by phi, 14 years ago

reset mixer stream in case a mixer comes up

File size: 18.2 KB
Line 
1//midi.c:
2
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
25#include "roard.h"
26
27#ifndef ROAR_WITHOUT_DCOMP_MIDI
28
29#if defined(ROAR_HAVE_IO_POSIX) && !defined(ROAR_TARGET_WIN32)
30#define _HAVE_CONSOLE
31#endif
32
33int midi_init_config(void) {
34 midi_config.init        = 1;
35 midi_config.inited      = 0;
36
37#ifndef ROAR_WITHOUT_DCOMP_CB
38 midi_config.init_cb     = 0;
39 midi_config.console_dev = NULL;
40#endif
41
42 return 0;
43}
44
45int midi_init (void) {
46 struct roar_stream_server * ss;
47 int i;
48
49 midi_config.inited = 0;
50
51 if ( midi_config.init ) {
52#ifndef ROAR_WITHOUT_DCOMP_CB
53  if ( midi_config.init_cb ) {
54   if ( midi_cb_init() == -1 ) {
55    ROAR_WARN("Can not initialize MIDI subsystem component CB");
56   }
57  }
58#endif
59
60  if ( midi_clock_init() == -1 ) {
61   ROAR_WARN("Can not initialize MIDI subsystem component clock");
62  }
63
64 }
65
66 g_midi_mess.buf = NULL;
67
68 if ( (g_midi_mixer.stream = add_mixer(ROAR_SUBSYS_MIDI, _MIXER_NAME("MIDI"), &ss)) == -1 ) {
69  ROAR_WARN("Can not create MIDI mixer");
70 }
71 ss->state = ROAR_STREAMSTATE_OLD;
72
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
81 midi_config.inited |= MIDI_INITED_MAIN;
82
83 return 0;
84}
85
86int midi_free (void) {
87 if ( midi_reinit() == -1 )
88  return -1;
89
90#ifndef ROAR_WITHOUT_DCOMP_CB
91 if ( midi_cb_free() == -1 )
92  return -1;
93#endif
94
95 return 0;
96}
97
98int midi_update(void) {
99
100 if ( g_midi_clock.stream != -1 )
101  midi_check_bridge(g_midi_clock.stream);
102
103#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
104 midi_conv_mes2ssynth();
105#endif
106
107#ifndef ROAR_WITHOUT_DCOMP_CB
108 return midi_cb_update();
109#else
110 return 0;
111#endif
112}
113
114int midi_reinit(void) {
115
116 if ( g_midi_mess.buf != NULL )
117  roar_buffer_free(g_midi_mess.buf);
118
119 g_midi_mess.buf = NULL;
120
121 return 0;
122}
123
124// STREAMS:
125
126int midi_check_stream  (int id) {
127 struct roar_stream        *   s;
128 struct roar_stream_server *  ss;
129 struct roar_buffer        *   b;
130 char                      * buf;
131 ssize_t                     len;
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
140 if ( s->dir == ROAR_DIR_BRIDGE )
141  return midi_check_bridge(id);
142
143 switch (s->info.codec) {
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);
161  roar_buffer_free(b);
162  return -1;
163 }
164
165 roar_buffer_set_len(b, len);
166
167 if ( stream_add_buffer(id, b) == -1 ) {
168  roar_buffer_free(b);
169  streams_delete(id);
170  return -1;
171 }
172
173 switch (s->info.codec) {
174  case ROAR_CODEC_MIDI:
175    return midi_conv_midi2mes(id);
176   break;
177  default:
178    streams_delete(id);
179    return -1;
180 }
181
182 return 0;
183}
184
185int midi_send_stream   (int id) {
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) {
197  case ROAR_CODEC_MIDI:
198    return midi_conv_mes2midi(id);
199   break;
200  default:
201    streams_delete(id);
202    return -1;
203 }
204
205 return 0;
206}
207
208int midi_conv_midi2mes (int id) {
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;
214 size_t need = 0, have = 0, last_have = 0, old_have = 0;
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  }
234  old_have = have;
235
236  while (have && alive) {
237   if ( *data & 0x80 ) {
238    if ( buf != NULL ) {
239     midi_add_buf(id, buf);
240     buf = NULL;
241    }
242
243    last_have = have;
244
245    need = 0;
246    if ( midi_new_bufmes(&buf, &mes) == -1 ) {
247     alive = 0;
248     continue;
249    }
250
251    if (*data == MIDI_TYPE_CLOCK_TICK || *data == MIDI_TYPE_CLOCK_START || *data == MIDI_TYPE_CLOCK_STOP ) {
252     mes->type = *data;
253
254     if ( *data == MIDI_TYPE_CLOCK_TICK ) {
255      s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1);
256     }
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
284   } else {
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) {
294      case MIDI_TYPE_NOTE_ON:
295      case MIDI_TYPE_NOTE_OFF:
296       if ( need == 2 ) {
297        roar_midi_note_from_midiid(&(mes->d.note), *data);
298
299        // if velocity is zero we have a NOTE OFF event
300        if ( data[1] == 0 )
301         mes->type = MIDI_TYPE_NOTE_OFF;
302       }
303      case MIDI_TYPE_PA:
304      case MIDI_TYPE_CONTROLER:
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:
313        mes->vv = *data;
314       break;
315      case MIDI_TYPE_PB:
316      case MIDI_TYPE_SYSEX:
317        ROAR_WARN("midi_conv_midi2mes(id=%i): Message of Type 0x%.2X (PB or SysEx) not yet supported", id, mes->type);
318       break;
319    }
320
321    if ( need )
322     need--;
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
340   }
341   data++;
342   have--;
343  }
344
345  if ( need ) {
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   }
349  } else if ( !have ) {
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 ) {
357   midi_add_buf(id, buf);
358   buf = NULL;
359  }
360 }
361
362 return 0;
363}
364
365#define _nb  len++; *(d++)
366int midi_conv_mes2midi (int id) {
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;
374 int                         send_clock;
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
383 send_clock = streams_get_flag(id, ROAR_FLAG_SYNC);
384
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 ) {
391   if ( send_clock ) {
392    if ( stream_vio_s_write(ss, &(mes->type), 1) != 1 ) {
393     streams_delete(id);
394     return -1;
395    }
396   }
397
398   if ( mes->type == MIDI_TYPE_CLOCK_TICK ) {
399    s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1);
400   }
401  } else {
402   len = 0;
403   d   = data;
404
405   _nb = (mes->type & 0xF0) | (mes->channel & 0x0F);
406
407   switch (mes->type) {
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      }
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;
444}
445#undef _nb
446
447#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
448int midi_conv_mes2ssynth(void) {
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;
465}
466#endif
467
468int midi_new_bufmes    (struct roar_buffer ** buf, struct midi_message ** mes) {
469 if ( buf == NULL || mes == NULL )
470  return -1;
471
472 *buf = (void*)(*mes = NULL);
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);
479  *buf = (void*)(*mes = NULL);
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
490int midi_add_buf       (int id, struct roar_buffer * buf) {
491 struct midi_message * mes;
492
493 if ( id == -1 || buf == NULL )
494  return -1;
495
496 if ( roar_buffer_get_data(buf, (void**)&mes) == -1 )
497  return -1;
498
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);
504
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
514// bridges:
515int midi_check_bridge  (int id) {
516
517 ROAR_DBG("midi_check_bridge(id=%i) = ?", id);
518
519 if ( id == g_midi_clock.stream ) {
520  midi_clock_tick();
521
522  return 0;
523 }
524
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
534 if ( (g_midi_clock.stream = streams_new()) == -1 ) {
535  ROAR_WARN("Error while initializing MIDI subsystem component clock");
536  return -1;
537 }
538
539 client_stream_add(g_self_client, g_midi_clock.stream);
540
541 midi_vio_set_dummy(g_midi_clock.stream);
542
543 streams_get(g_midi_clock.stream, &ss);
544 s = ROAR_STREAM(ss);
545
546 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
547
548 s->pos_rel_id    =  g_midi_clock.stream;
549
550 s->info.codec    =  ROAR_CODEC_MIDI;
551 ss->codec_orgi   =  ROAR_CODEC_MIDI;
552
553 s->info.channels = 1; // we have only one channel, ticking on channel 0
554 s->info.rate     = ROAR_MIDI_TICKS_PER_BEAT; // one beat per sec
555 s->info.bits     = ROAR_MIDI_BITS;
556
557 if ( streams_set_dir(g_midi_clock.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
558  ROAR_WARN("Error while initializing MIDI subsystem component clock");
559  return -1;
560 }
561
562 streams_set_name(g_midi_clock.stream, "MIDI Clock");
563
564 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_PRIMARY);
565 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_SYNC);
566
567 midi_clock_set_bph(3600); // one beat per sec
568
569 ss->state = ROAR_STREAMSTATE_NEW;
570
571 midi_config.inited |= MIDI_INITED_CLOCK;
572
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) {
589 struct roar_buffer  * buf;
590 struct midi_message * mes;
591 struct roar_stream_server * ss;
592 unsigned int diff;
593
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
601 while ( g_pos >= g_midi_clock.nt ) {
602  diff = g_pos - g_midi_clock.nt;
603  ROAR_DBG("midi_clock_tick(void): g_pos is %u samples (%5.2f%%) after of nt.", diff, (float)diff/g_midi_clock.spt);
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) ) {
608   ROAR_DBG("midi_clock_tick(void): TICK! (nt=%lu)", g_midi_clock.nt);
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   }
620
621   ROAR_STREAM(ss)->pos = ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(ss)->pos, 1);
622   ss->state = ROAR_STREAMSTATE_OLD;
623  } else {
624   ROAR_DBG("midi_clock_tick(void): silent tick. (nt=%lu)", g_midi_clock.nt);
625  }
626 }
627
628 return 0;
629}
630
631// CB:
632
633#ifndef ROAR_WITHOUT_DCOMP_CB
634int midi_cb_init (void) {
635#ifdef _HAVE_CONSOLE
636 struct roar_stream * s;
637 struct roar_stream_server * ss;
638 int i;
639 char * files[] = {
640                   "/NX",
641                   "/dev/roarconsole",
642                   "/dev/console",
643#ifdef __linux__
644                   "/dev/tty0",
645                   "/dev/vc/0",
646#endif
647                   NULL
648                  };
649
650 g_midi_cb.console  = -1;
651 g_midi_cb.stream   = -1;
652 g_midi_cb.stoptime =  0;
653 g_midi_cb.playing  =  0;
654
655 if ( midi_config.console_dev != NULL ) {
656  files[0] = midi_config.console_dev;
657 }
658
659 for (i = 0; files[i] != NULL; i++) {
660  if ( (g_midi_cb.console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 )
661   break;
662 }
663
664 if ( g_midi_cb.console == -1 )
665  return -1;
666
667 if ( (g_midi_cb.stream = streams_new()) == -1 ) {
668  ROAR_WARN("Error while initializing MIDI subsystem component CB");
669  midi_cb_free();
670  return -1;
671 }
672
673 client_stream_add(g_self_client, g_midi_clock.stream);
674
675 midi_vio_set_dummy(g_midi_cb.stream);
676
677 streams_get(g_midi_cb.stream, &ss);
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
691 if ( streams_set_dir(g_midi_cb.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
692  ROAR_WARN("Error while initializing MIDI subsystem component CB");
693  midi_cb_free();
694  return -1;
695 }
696
697 streams_set_name(g_midi_cb.stream, "Console speaker bridge");
698
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);
702 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_MUTE);
703
704 midi_config.inited |= MIDI_INITED_CB;
705
706 return 0;
707#else
708 g_midi_cb.console  = -1;
709 g_midi_cb.stream   = -1;
710
711 return -1;
712#endif
713}
714
715int midi_cb_free (void) {
716#ifdef _HAVE_CONSOLE
717
718 midi_cb_stop();
719
720 if ( g_midi_cb.stream != -1 )
721  streams_delete(g_midi_cb.stream);
722
723 if ( g_midi_cb.console != -1 )
724  close(g_midi_cb.console);
725
726 return 0;
727#else
728 return -1;
729#endif
730}
731
732int midi_cb_play(float t, float freq, int override) {
733 float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels;
734
735/*
736#define MIDI_CB_NOOVERRIDE 0
737#define MIDI_CB_OVERRIDE   1
738*/
739 if ( g_midi_cb.playing && override != MIDI_CB_OVERRIDE )
740  return -1;
741
742 g_midi_cb.stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t);
743 midi_cb_start(freq);
744
745 return 0;
746}
747
748int midi_cb_update (void) {
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 }
756
757 if ( !g_midi_cb.playing )
758  return 0;
759
760/*
761 if ( g_midi_cb.stoptime <= g_pos )
762  midi_cb_stop();
763*/
764
765 ROAR_DBG("midi_cb_update(void) = ?");
766
767 return 0;
768}
769
770int midi_cb_start(float freq) {
771// On linux this uses ioctl KIOCSOUND
772#ifdef __linux__
773 if ( g_midi_cb.console == -1 )
774  return -1;
775
776 if ( ioctl(g_midi_cb.console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 )
777  return -1;
778
779 g_midi_cb.playing = 1;
780
781 return 0;
782#else
783 return -1;
784#endif
785}
786
787int midi_cb_stop (void) {
788#ifdef __linux__
789 int ret;
790
791 ret = midi_cb_start(0);
792 g_midi_cb.playing = 0;
793
794 return ret;
795#else
796 return -1;
797#endif
798}
799
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;
820   case MIDI_TYPE_CONTROLER:
821     if ( mes->kk == MIDI_CCE_ALL_NOTE_OFF ) /* all note off */
822      midi_cb_stop();
823    break;
824  }
825
826  if ( roar_buffer_get_next(buf, &buf) == -1 )
827   buf = NULL;
828 }
829
830 return 0;
831}
832#endif
833
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;
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;
847 ss->vio.ctl      = NULL;
848 ss->vio.close    = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
849
850 return 0;
851}
852
853int     midi_vio_ok(struct roar_vio_calls * vio, ...) {
854 return 0;
855}
856
857#endif
858
859//ll
Note: See TracBrowser for help on using the repository browser.