source: roaraudio/roard/midi.c @ 2476:6da5cf65271b

Last change on this file since 2476:6da5cf65271b was 2462:df57a22e074e, checked in by phi, 15 years ago

send data to ssynth_eval_message()

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