source: roaraudio/roard/midi.c @ 1865:80a9af2b423c

Last change on this file since 1865:80a9af2b423c was 1865:80a9af2b423c, checked in by phi, 15 years ago

parese status byte

File size: 11.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
[185]31int midi_init (void) {
[1843]32
33 if ( midi_cb_init() == -1 ) {
34  ROAR_WARN("Can not initialize MIDI subsystem component CB");
35 }
36
[1848]37 if ( midi_clock_init() == -1 ) {
38  ROAR_WARN("Can not initialize MIDI subsystem component clock");
39 }
40
[1863]41 g_midi_mess.buf = NULL;
42
[1843]43 return 0;
44}
[1848]45
[1843]46int midi_free (void) {
47 return midi_cb_free();
48}
49
50int midi_update(void) {
[1848]51
[1850]52 if ( g_midi_clock.stream != -1 )
53  midi_check_bridge(g_midi_clock.stream);
[1848]54
[1843]55 return midi_cb_update();
56}
57
[1855]58int midi_reinit(void) {
59 return 0;
60}
61
[1845]62// STREAMS:
63
64int midi_check_stream  (int id) {
[1846]65 struct roar_stream        *   s;
66 struct roar_stream_server *  ss;
[1858]67 struct roar_buffer        *   b;
68 char                      * buf;
69 ssize_t                     len;
[1859]70 int                           i;
[1846]71
72 if ( g_streams[id] == NULL )
73  return -1;
74
75 ROAR_DBG("midi_check_stream(id=%i) = ?", id);
76
77 s = ROAR_STREAM(ss = g_streams[id]);
78
[1848]79 if ( s->dir == ROAR_DIR_BRIDGE )
80  return midi_check_bridge(id);
81
[1846]82 switch (s->info.codec) {
[1858]83  case ROAR_CODEC_MIDI:
84   break;
85  default:
86    streams_delete(id);
87    return -1;
88 }
89
90 if ( roar_buffer_new(&b, MIDI_READ_SIZE) == -1 ) {
91  ROAR_ERR("midi_check_stream(*): Can not alloc buffer space!");
92  ROAR_DBG("midi_check_stream(*) = -1");
93  return -1;
94 }
95
96 roar_buffer_get_data(b, (void **)&buf);
97
98 if ( (len = stream_vio_s_read(ss, buf, MIDI_READ_SIZE)) < 1 ) {
99  streams_delete(id);
[1862]100  roar_buffer_free(b);
[1858]101  return -1;
102 }
103
104 roar_buffer_set_len(b, len);
105
[1859]106 if ( stream_add_buffer(id, b) == -1 ) {
107  roar_buffer_free(b);
108  streams_delete(id);
109  return -1;
110 }
111
112 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
113  if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) {
114   if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) {
115    if ( stream_vio_write(i, buf, len) != len ) {
116     streams_delete(i);
117    }
118   }
[1858]119  }
[1859]120 }
[1858]121
122 switch (s->info.codec) {
123  case ROAR_CODEC_MIDI:
124    return midi_conv_midi2mes(id);
125   break;
[1846]126  default:
127    streams_delete(id);
128    return -1;
129 }
130
131 return 0;
[1845]132}
[1846]133
[1845]134int midi_send_stream   (int id) {
[1846]135 struct roar_stream        *   s;
136 struct roar_stream_server *  ss;
137
138 if ( g_streams[id] == NULL )
139  return -1;
140
141 ROAR_DBG("midi_send_stream(id=%i) = ?", id);
142
143 s = ROAR_STREAM(ss = g_streams[id]);
144
145 switch (s->info.codec) {
146  default:
147    streams_delete(id);
148    return -1;
149 }
150
151 return 0;
[1845]152}
153
[1858]154int midi_conv_midi2mes (int id) {
[1863]155 unsigned char * data;
156 struct roar_stream        *   s;
157 struct roar_stream_server *  ss;
158 struct roar_buffer        * buf = NULL;
159 struct midi_message       * mes = NULL;
160 size_t need = 0, have = 0;
161 int alive = 1;
162
163 if ( g_streams[id] == NULL )
164  return -1;
165
166 ROAR_DBG("midi_conv_midi2mes(id=%i) = ?", id);
167
168 s = ROAR_STREAM(ss = g_streams[id]);
169
170 while (ss->buffer != NULL && alive) {
171  if ( roar_buffer_get_data(ss->buffer, (void**)&data) == -1 ) {
172   alive = 0;
173   continue;
174  }
175
176  if ( roar_buffer_get_len(ss->buffer, &have) == -1 ) {
177   alive = 0;
178   continue;
179  }
180
181  while (have && alive) {
182   printf("%.2x=", *data);
183   if ( *data & 0x80 ) {
[1865]184    if ( buf != NULL ) {
185     if ( g_midi_mess.buf == NULL ) {
186      g_midi_mess.buf = buf;
187     } else {
188      roar_buffer_add(g_midi_mess.buf, buf);
189     }
190     buf = NULL;
191    }
192
[1863]193    need = 0;
194    printf("S\n");
195    if ( midi_new_bufmes(&buf, &mes) == -1 ) {
196     alive = 0;
197     continue;
198    }
[1865]199
200    if (*data == MIDI_TYPE_CLOCK_TICK || *data == MIDI_TYPE_CLOCK_START || *data == MIDI_TYPE_CLOCK_STOP ) {
201     mes->type = *data;
202    } else {
203     mes->type    = *data & 0xF0;
204     mes->channel = *data & 0x0F;
205     switch (*data & 0xF0) {
206      case MIDI_TYPE_NOTE_ON:
207      case MIDI_TYPE_NOTE_OFF:
208        need = 2;
209       break;
210      case MIDI_TYPE_PA:
211        need = 2;
212       break;
213      case MIDI_TYPE_CONTROLER:
214        need = 2;
215       break;
216      case MIDI_TYPE_PROGRAM:
217        need = 1;
218       break;
219      case MIDI_TYPE_MA:
220        need = 1;
221       break;
222      case MIDI_TYPE_PB:
223      case MIDI_TYPE_SYSEX:
224        need = 1;
225       break;
226     }
227    }
228
[1863]229   } else {
230    printf("D\n");
231    if ( need )
232     need--;
233   }
234   data++;
235   have--;
236  }
237
[1865]238  if ( need ) {
239   ROAR_ERR("midi_conv_midi2mes(*): FIXME: BUG!!! Need to restore buffer here with corrected length");
240  } else if ( !have ) {
[1863]241   roar_buffer_next(&(ss->buffer));
242  }
243
244  if ( need && buf != NULL ) {
245   roar_buffer_free(buf);
246   buf = NULL;
247  } else if ( buf != NULL ) {
248   if ( g_midi_mess.buf == NULL ) {
249    g_midi_mess.buf = buf;
250   } else {
251    roar_buffer_add(g_midi_mess.buf, buf);
252   }
253
254   buf = NULL;
255  }
256 }
257
258 return 0;
[1858]259}
260
261int midi_conv_mes2midi (int id) {
262 return -1;
263}
264
[1863]265int midi_new_bufmes    (struct roar_buffer ** buf, struct midi_message ** mes) {
266 if ( buf == NULL || mes == NULL )
267  return -1;
268
269 *buf = *mes = NULL;
270
271 if ( roar_buffer_new(buf, sizeof(struct midi_message)) == -1 )
272  return -1;
273
274 if ( roar_buffer_get_data(*buf, (void**)mes) == -1 ) {
275  roar_buffer_free(*buf);
276  *buf = *mes = NULL;
277  return -1;
278 }
279
280 memset((void*)*mes, 0, sizeof(struct midi_message));
281
282 (*mes)->type = MIDI_TYPE_NONE;
283
284 return 0;
285}
286
[1848]287// bridges:
288int midi_check_bridge  (int id) {
[1851]289
[1860]290 ROAR_DBG("midi_check_bridge(id=%i) = ?", id);
[1851]291
292 if ( id == g_midi_clock.stream ) {
293  midi_clock_tick();
294
295  return 0;
296 }
297
[1848]298 return -1;
299}
300
301// clock:
302
303int midi_clock_init (void) {
304 struct roar_stream * s;
305 struct roar_stream_server * ss;
306
[1850]307 if ( (g_midi_clock.stream = streams_new()) == -1 ) {
[1848]308  ROAR_WARN("Error while initializing MIDI subsystem component clock");
309  return -1;
310 }
311
[1850]312 midi_vio_set_dummy(g_midi_clock.stream);
[1848]313
[1850]314 streams_get(g_midi_clock.stream, &ss);
[1848]315 s = ROAR_STREAM(ss);
316
317 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
318
[1850]319 s->pos_rel_id    =  g_midi_clock.stream;
[1848]320
321 s->info.codec    =  ROAR_CODEC_MIDI;
322 ss->codec_orgi   =  ROAR_CODEC_MIDI;
323
324 s->info.channels =  0;
325 s->info.rate     = MIDI_RATE;
326 s->info.bits     =  8;
327
[1850]328 if ( streams_set_dir(g_midi_clock.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
[1848]329  ROAR_WARN("Error while initializing MIDI subsystem component clock");
330  return -1;
331 }
332
[1850]333 streams_set_name(g_midi_clock.stream, "MIDI Clock");
[1848]334
[1850]335 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_PRIMARY);
336 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_SYNC);
[1848]337
[1851]338 midi_clock_set_bph(3600); // one tick per sec
339
340 return 0;
341}
342
343int midi_clock_set_bph (uint_least32_t bph) {
344 uint_least32_t sph = g_sa->rate/2 * 75 * g_sa->channels; // samples per houre
345
346 g_midi_clock.bph  = bph;
347
348 g_midi_clock.spt  = sph/bph;
349
350 g_midi_clock.nt   = ROAR_MATH_OVERFLOW_ADD(g_pos, g_midi_clock.spt);
351
352 return 0;
353}
354
355int midi_clock_tick (void) {
356 unsigned int diff;
357
358 while ( g_pos >= g_midi_clock.nt ) {
359  diff = g_pos - g_midi_clock.nt;
[1860]360  ROAR_DBG("midi_clock_tick(void): g_pos is %u samples (%5.2f%%) ahead of nt.", diff, (float)diff/g_midi_clock.spt);
[1851]361
362  g_midi_clock.nt   = ROAR_MATH_OVERFLOW_ADD(g_midi_clock.nt, g_midi_clock.spt);
363
364  if ( streams_get_flag(g_midi_clock.stream, ROAR_FLAG_SYNC) ) {
[1860]365   ROAR_DBG("midi_clock_tick(void): TICK! (nt=%lu)", g_midi_clock.nt);
[1851]366  } else {
[1860]367   ROAR_DBG("midi_clock_tick(void): silent tick. (nt=%lu)", g_midi_clock.nt);
[1851]368  }
369 }
370
[1848]371 return 0;
372}
373
[1845]374// CB:
[1843]375
376int midi_cb_init (void) {
[1751]377#ifdef _HAVE_CONSOLE
[1843]378 struct roar_stream * s;
379 struct roar_stream_server * ss;
[185]380 int i;
381 char * files[] = {
382                   "/dev/console",
383#ifdef __linux__
384                   "/dev/tty0",
385                   "/dev/vc/0",
386#endif
387                   NULL
388                  };
389
[1853]390 g_midi_cb.console  = -1;
391 g_midi_cb.stream   = -1;
392 g_midi_cb.stoptime =  0;
393 g_midi_cb.playing  =  0;
[185]394
395 for (i = 0; files[i] != NULL; i++) {
[1853]396  if ( (g_midi_cb.console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 )
[185]397   break;
398 }
399
[1853]400 if ( g_midi_cb.console == -1 )
[1843]401  return -1;
402
[1853]403 if ( (g_midi_cb.stream = streams_new()) == -1 ) {
[1843]404  ROAR_WARN("Error while initializing MIDI subsystem component CB");
405  midi_cb_free();
[187]406  return -1;
407 }
[1843]408
[1853]409 midi_vio_set_dummy(g_midi_cb.stream);
[1848]410
[1853]411 streams_get(g_midi_cb.stream, &ss);
[1843]412 s = ROAR_STREAM(ss);
413
414 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
415
416 s->pos_rel_id    = -1;
417
418 s->info.codec    =  0;
419 ss->codec_orgi   =  0;
420
421 s->info.channels =  1;
422 s->info.rate     = 1193180;
423 s->info.bits     =  8;
424
[1853]425 if ( streams_set_dir(g_midi_cb.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
[1843]426  ROAR_WARN("Error while initializing MIDI subsystem component CB");
427  midi_cb_free();
428  return -1;
429 }
430
[1853]431 streams_set_name(g_midi_cb.stream, "Console speaker bridge");
[1843]432
[1853]433 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_OUTPUT);
434 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_PRIMARY);
435 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_HWMIXER);
[1843]436
437 return 0;
[1485]438#else
[1853]439 g_midi_cb.console  = -1;
440 g_midi_cb.stream   = -1;
[1843]441
[1485]442 return -1;
443#endif
[185]444}
445
[1843]446int midi_cb_free (void) {
[1751]447#ifdef _HAVE_CONSOLE
[1843]448
449 midi_cb_stop();
450
[1853]451 if ( g_midi_cb.stream != -1 )
452  streams_delete(g_midi_cb.stream);
[1843]453
[1853]454 if ( g_midi_cb.console != -1 )
455  close(g_midi_cb.console);
[1843]456
[185]457 return 0;
[1485]458#else
459 return -1;
460#endif
[185]461}
462
463int midi_cb_play(float t, float freq, int override) {
[191]464 float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels;
465
[189]466/*
467#define MIDI_CB_NOOVERRIDE 0
468#define MIDI_CB_OVERRIDE   1
469*/
[1853]470 if ( g_midi_cb.playing && override != MIDI_CB_OVERRIDE )
[191]471  return -1;
472
[1853]473 g_midi_cb.stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t);
[191]474 midi_cb_start(freq);
[1853]475 g_midi_cb.playing = 1;
[191]476
477 return 0;
[185]478}
479
[190]480int midi_cb_update (void) {
[1853]481 if ( !g_midi_cb.playing )
[192]482  return 0;
483
[1853]484 if ( g_midi_cb.stoptime <= g_pos )
[189]485  midi_cb_stop();
486
487 return 0;
488}
489
[185]490int midi_cb_start(float freq) {
491// On linux this uses ioctl KIOCSOUND
[188]492#ifdef __linux__
[1853]493 if ( g_midi_cb.console == -1 )
[188]494  return -1;
495
[1853]496 if ( ioctl(g_midi_cb.console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 )
[188]497  return -1;
498
499 return 0;
500#else
[185]501 return -1;
[188]502#endif
[185]503}
504
505int midi_cb_stop (void) {
506#ifdef __linux__
[1853]507 g_midi_cb.playing = 0;
[185]508 return midi_cb_start(0);
509#else
510 return -1;
511#endif
512}
513
[1848]514// VIO:
515
516int     midi_vio_set_dummy(int stream) {
517 struct roar_stream_server * ss;
518
519 if ( streams_get(stream, &ss) == -1 )
520  return -1;
521
522 ss->vio.read     = NULL;
523 ss->vio.write    = NULL;
524 ss->vio.lseek    = NULL;
[1849]525 ss->vio.nonblock = (int (*)(struct roar_vio_calls * vio, int state))midi_vio_ok;
526 ss->vio.sync     = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
[1848]527 ss->vio.ctl      = NULL;
[1849]528 ss->vio.close    = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
529
530 return 0;
[1848]531}
532
533int     midi_vio_ok(struct roar_vio_calls * vio, ...) {
534 return 0;
535}
536
[169]537//ll
Note: See TracBrowser for help on using the repository browser.