source: roaraudio/roard/midi.c @ 5053:4542bb21d38d

Last change on this file since 5053:4542bb21d38d was 5053:4542bb21d38d, checked in by phi, 13 years ago

add some debug msgs

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