source: roaraudio/roard/midi.c @ 1863:76178443318a

Last change on this file since 1863:76178443318a was 1863:76178443318a, checked in by phi, 15 years ago

do very ruimentary MIDI analysis, need to implement the protocol next

File size: 10.1 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 ) {
184    need = 0;
185    printf("S\n");
186    if ( midi_new_bufmes(&buf, &mes) == -1 ) {
187     alive = 0;
188     continue;
189    }
190   } else {
191    printf("D\n");
192    if ( need )
193     need--;
194   }
195   data++;
196   have--;
197  }
198
199  if ( !have ) {
200   roar_buffer_next(&(ss->buffer));
201  }
202
203  if ( need && buf != NULL ) {
204   roar_buffer_free(buf);
205   buf = NULL;
206  } else if ( buf != NULL ) {
207   if ( g_midi_mess.buf == NULL ) {
208    g_midi_mess.buf = buf;
209   } else {
210    roar_buffer_add(g_midi_mess.buf, buf);
211   }
212
213   buf = NULL;
214  }
215 }
216
217 return 0;
[1858]218}
219
220int midi_conv_mes2midi (int id) {
221 return -1;
222}
223
[1863]224int midi_new_bufmes    (struct roar_buffer ** buf, struct midi_message ** mes) {
225 if ( buf == NULL || mes == NULL )
226  return -1;
227
228 *buf = *mes = NULL;
229
230 if ( roar_buffer_new(buf, sizeof(struct midi_message)) == -1 )
231  return -1;
232
233 if ( roar_buffer_get_data(*buf, (void**)mes) == -1 ) {
234  roar_buffer_free(*buf);
235  *buf = *mes = NULL;
236  return -1;
237 }
238
239 memset((void*)*mes, 0, sizeof(struct midi_message));
240
241 (*mes)->type = MIDI_TYPE_NONE;
242
243 return 0;
244}
245
[1848]246// bridges:
247int midi_check_bridge  (int id) {
[1851]248
[1860]249 ROAR_DBG("midi_check_bridge(id=%i) = ?", id);
[1851]250
251 if ( id == g_midi_clock.stream ) {
252  midi_clock_tick();
253
254  return 0;
255 }
256
[1848]257 return -1;
258}
259
260// clock:
261
262int midi_clock_init (void) {
263 struct roar_stream * s;
264 struct roar_stream_server * ss;
265
[1850]266 if ( (g_midi_clock.stream = streams_new()) == -1 ) {
[1848]267  ROAR_WARN("Error while initializing MIDI subsystem component clock");
268  return -1;
269 }
270
[1850]271 midi_vio_set_dummy(g_midi_clock.stream);
[1848]272
[1850]273 streams_get(g_midi_clock.stream, &ss);
[1848]274 s = ROAR_STREAM(ss);
275
276 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
277
[1850]278 s->pos_rel_id    =  g_midi_clock.stream;
[1848]279
280 s->info.codec    =  ROAR_CODEC_MIDI;
281 ss->codec_orgi   =  ROAR_CODEC_MIDI;
282
283 s->info.channels =  0;
284 s->info.rate     = MIDI_RATE;
285 s->info.bits     =  8;
286
[1850]287 if ( streams_set_dir(g_midi_clock.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
[1848]288  ROAR_WARN("Error while initializing MIDI subsystem component clock");
289  return -1;
290 }
291
[1850]292 streams_set_name(g_midi_clock.stream, "MIDI Clock");
[1848]293
[1850]294 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_PRIMARY);
295 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_SYNC);
[1848]296
[1851]297 midi_clock_set_bph(3600); // one tick per sec
298
299 return 0;
300}
301
302int midi_clock_set_bph (uint_least32_t bph) {
303 uint_least32_t sph = g_sa->rate/2 * 75 * g_sa->channels; // samples per houre
304
305 g_midi_clock.bph  = bph;
306
307 g_midi_clock.spt  = sph/bph;
308
309 g_midi_clock.nt   = ROAR_MATH_OVERFLOW_ADD(g_pos, g_midi_clock.spt);
310
311 return 0;
312}
313
314int midi_clock_tick (void) {
315 unsigned int diff;
316
317 while ( g_pos >= g_midi_clock.nt ) {
318  diff = g_pos - g_midi_clock.nt;
[1860]319  ROAR_DBG("midi_clock_tick(void): g_pos is %u samples (%5.2f%%) ahead of nt.", diff, (float)diff/g_midi_clock.spt);
[1851]320
321  g_midi_clock.nt   = ROAR_MATH_OVERFLOW_ADD(g_midi_clock.nt, g_midi_clock.spt);
322
323  if ( streams_get_flag(g_midi_clock.stream, ROAR_FLAG_SYNC) ) {
[1860]324   ROAR_DBG("midi_clock_tick(void): TICK! (nt=%lu)", g_midi_clock.nt);
[1851]325  } else {
[1860]326   ROAR_DBG("midi_clock_tick(void): silent tick. (nt=%lu)", g_midi_clock.nt);
[1851]327  }
328 }
329
[1848]330 return 0;
331}
332
[1845]333// CB:
[1843]334
335int midi_cb_init (void) {
[1751]336#ifdef _HAVE_CONSOLE
[1843]337 struct roar_stream * s;
338 struct roar_stream_server * ss;
[185]339 int i;
340 char * files[] = {
341                   "/dev/console",
342#ifdef __linux__
343                   "/dev/tty0",
344                   "/dev/vc/0",
345#endif
346                   NULL
347                  };
348
[1853]349 g_midi_cb.console  = -1;
350 g_midi_cb.stream   = -1;
351 g_midi_cb.stoptime =  0;
352 g_midi_cb.playing  =  0;
[185]353
354 for (i = 0; files[i] != NULL; i++) {
[1853]355  if ( (g_midi_cb.console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 )
[185]356   break;
357 }
358
[1853]359 if ( g_midi_cb.console == -1 )
[1843]360  return -1;
361
[1853]362 if ( (g_midi_cb.stream = streams_new()) == -1 ) {
[1843]363  ROAR_WARN("Error while initializing MIDI subsystem component CB");
364  midi_cb_free();
[187]365  return -1;
366 }
[1843]367
[1853]368 midi_vio_set_dummy(g_midi_cb.stream);
[1848]369
[1853]370 streams_get(g_midi_cb.stream, &ss);
[1843]371 s = ROAR_STREAM(ss);
372
373 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
374
375 s->pos_rel_id    = -1;
376
377 s->info.codec    =  0;
378 ss->codec_orgi   =  0;
379
380 s->info.channels =  1;
381 s->info.rate     = 1193180;
382 s->info.bits     =  8;
383
[1853]384 if ( streams_set_dir(g_midi_cb.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
[1843]385  ROAR_WARN("Error while initializing MIDI subsystem component CB");
386  midi_cb_free();
387  return -1;
388 }
389
[1853]390 streams_set_name(g_midi_cb.stream, "Console speaker bridge");
[1843]391
[1853]392 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_OUTPUT);
393 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_PRIMARY);
394 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_HWMIXER);
[1843]395
396 return 0;
[1485]397#else
[1853]398 g_midi_cb.console  = -1;
399 g_midi_cb.stream   = -1;
[1843]400
[1485]401 return -1;
402#endif
[185]403}
404
[1843]405int midi_cb_free (void) {
[1751]406#ifdef _HAVE_CONSOLE
[1843]407
408 midi_cb_stop();
409
[1853]410 if ( g_midi_cb.stream != -1 )
411  streams_delete(g_midi_cb.stream);
[1843]412
[1853]413 if ( g_midi_cb.console != -1 )
414  close(g_midi_cb.console);
[1843]415
[185]416 return 0;
[1485]417#else
418 return -1;
419#endif
[185]420}
421
422int midi_cb_play(float t, float freq, int override) {
[191]423 float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels;
424
[189]425/*
426#define MIDI_CB_NOOVERRIDE 0
427#define MIDI_CB_OVERRIDE   1
428*/
[1853]429 if ( g_midi_cb.playing && override != MIDI_CB_OVERRIDE )
[191]430  return -1;
431
[1853]432 g_midi_cb.stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t);
[191]433 midi_cb_start(freq);
[1853]434 g_midi_cb.playing = 1;
[191]435
436 return 0;
[185]437}
438
[190]439int midi_cb_update (void) {
[1853]440 if ( !g_midi_cb.playing )
[192]441  return 0;
442
[1853]443 if ( g_midi_cb.stoptime <= g_pos )
[189]444  midi_cb_stop();
445
446 return 0;
447}
448
[185]449int midi_cb_start(float freq) {
450// On linux this uses ioctl KIOCSOUND
[188]451#ifdef __linux__
[1853]452 if ( g_midi_cb.console == -1 )
[188]453  return -1;
454
[1853]455 if ( ioctl(g_midi_cb.console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 )
[188]456  return -1;
457
458 return 0;
459#else
[185]460 return -1;
[188]461#endif
[185]462}
463
464int midi_cb_stop (void) {
465#ifdef __linux__
[1853]466 g_midi_cb.playing = 0;
[185]467 return midi_cb_start(0);
468#else
469 return -1;
470#endif
471}
472
[1848]473// VIO:
474
475int     midi_vio_set_dummy(int stream) {
476 struct roar_stream_server * ss;
477
478 if ( streams_get(stream, &ss) == -1 )
479  return -1;
480
481 ss->vio.read     = NULL;
482 ss->vio.write    = NULL;
483 ss->vio.lseek    = NULL;
[1849]484 ss->vio.nonblock = (int (*)(struct roar_vio_calls * vio, int state))midi_vio_ok;
485 ss->vio.sync     = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
[1848]486 ss->vio.ctl      = NULL;
[1849]487 ss->vio.close    = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
488
489 return 0;
[1848]490}
491
492int     midi_vio_ok(struct roar_vio_calls * vio, ...) {
493 return 0;
494}
495
[169]496//ll
Note: See TracBrowser for help on using the repository browser.