source: roaraudio/roard/midi.c @ 2915:80026c7bf0b5

Last change on this file since 2915:80026c7bf0b5 was 2915:80026c7bf0b5, checked in by phi, 15 years ago

set client of stream

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