source: roaraudio/roard/midi.c @ 2409:b3d510af4c5c

Last change on this file since 2409:b3d510af4c5c was 2409:b3d510af4c5c, checked in by phi, 15 years ago

if velocity is zero we have a NOTE OFF event

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