source: roaraudio/roard/midi.c @ 1874:8ae3287023ff

Last change on this file since 1874:8ae3287023ff was 1874:8ae3287023ff, checked in by phi, 15 years ago

additinal parameter mapping

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