source: roaraudio/roard/midi.c @ 1891:aec50ee986d3

Last change on this file since 1891:aec50ee986d3 was 1891:aec50ee986d3, checked in by phi, 15 years ago

cont clock tics on normal streams

File size: 15.6 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  case ROAR_CODEC_MIDI:
153    return midi_conv_mes2midi(id);
154   break;
155  default:
156    streams_delete(id);
157    return -1;
158 }
159
160 return 0;
161}
162
163int midi_conv_midi2mes (int id) {
164 unsigned char * data;
165 struct roar_stream        *   s;
166 struct roar_stream_server *  ss;
167 struct roar_buffer        * buf = NULL;
168 struct midi_message       * mes = NULL;
169 size_t need = 0, have = 0, last_have = 0, old_have = 0;
170 int alive = 1;
171
172 if ( g_streams[id] == NULL )
173  return -1;
174
175 ROAR_DBG("midi_conv_midi2mes(id=%i) = ?", id);
176
177 s = ROAR_STREAM(ss = g_streams[id]);
178
179 while (ss->buffer != NULL && alive) {
180  if ( roar_buffer_get_data(ss->buffer, (void**)&data) == -1 ) {
181   alive = 0;
182   continue;
183  }
184
185  if ( roar_buffer_get_len(ss->buffer, &have) == -1 ) {
186   alive = 0;
187   continue;
188  }
189  old_have = have;
190
191  while (have && alive) {
192   if ( *data & 0x80 ) {
193    if ( buf != NULL ) {
194     midi_add_buf(id, buf);
195     buf = NULL;
196    }
197
198    last_have = have;
199
200    need = 0;
201    if ( midi_new_bufmes(&buf, &mes) == -1 ) {
202     alive = 0;
203     continue;
204    }
205
206    if (*data == MIDI_TYPE_CLOCK_TICK || *data == MIDI_TYPE_CLOCK_START || *data == MIDI_TYPE_CLOCK_STOP ) {
207     mes->type = *data;
208
209     if ( *data == MIDI_TYPE_CLOCK_TICK ) {
210      s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1);
211     }
212    } else {
213     mes->type    = *data & 0xF0;
214     mes->channel = *data & 0x0F;
215     switch (*data & 0xF0) {
216      case MIDI_TYPE_NOTE_ON:
217      case MIDI_TYPE_NOTE_OFF:
218        need = 2;
219       break;
220      case MIDI_TYPE_PA:
221        need = 2;
222       break;
223      case MIDI_TYPE_CONTROLER:
224        need = 2;
225       break;
226      case MIDI_TYPE_PROGRAM:
227        need = 1;
228       break;
229      case MIDI_TYPE_MA:
230        need = 1;
231       break;
232      case MIDI_TYPE_PB:
233      case MIDI_TYPE_SYSEX:
234        need = 1;
235       break;
236     }
237    }
238
239   } else {
240
241    if ( mes == NULL ) {
242     ROAR_WARN("midi_conv_midi2mes(id=%i): Lost sync.", id);
243     data++;
244     have--;
245     continue;
246    }
247
248    switch (mes->type) {
249      case MIDI_TYPE_NOTE_ON:
250      case MIDI_TYPE_NOTE_OFF:
251       if ( need == 2 ) {
252        roar_midi_note_from_midiid(&(mes->d.note), *data);
253       }
254      case MIDI_TYPE_PA:
255      case MIDI_TYPE_CONTROLER:
256        if ( need == 2 ) {
257         mes->kk = *data;
258        } else {
259         mes->vv = *data;
260        }
261       break;
262      case MIDI_TYPE_PROGRAM:
263      case MIDI_TYPE_MA:
264        mes->vv = *data;
265       break;
266      case MIDI_TYPE_PB:
267      case MIDI_TYPE_SYSEX:
268        ROAR_WARN("midi_conv_midi2mes(id=%i): Message of Type 0x%.2X (PB or SysEx) not yet supported", id, mes->type);
269       break;
270    }
271
272    if ( need )
273     need--;
274   }
275   data++;
276   have--;
277  }
278
279  if ( need ) {
280   if ( roar_buffer_set_offset(ss->buffer, old_have - last_have) == -1 ) {
281    ROAR_ERR("midi_conv_midi2mes(*): FIXME: BUG!!! Need to restore buffer here with corrected length");
282   }
283  } else if ( !have ) {
284   roar_buffer_next(&(ss->buffer));
285  }
286
287  if ( need && buf != NULL ) {
288   roar_buffer_free(buf);
289   buf = NULL;
290  } else if ( buf != NULL ) {
291   midi_add_buf(id, buf);
292   buf = NULL;
293  }
294 }
295
296 return 0;
297}
298
299#define _nb  len++; *(d++)
300int midi_conv_mes2midi (int id) {
301 struct roar_stream        *   s;
302 struct roar_stream_server *  ss;
303 struct roar_buffer        * buf = g_midi_mess.buf;
304 struct midi_message       * mes = NULL;
305 unsigned char               data[3];
306 unsigned char             * d;
307 int                         len;
308
309 if ( g_streams[id] == NULL )
310  return -1;
311
312 ROAR_DBG("midi_conv_mes2midi(id=%i) = ?", id);
313
314 s = ROAR_STREAM(ss = g_streams[id]);
315
316 while (buf != NULL) {
317  if ( roar_buffer_get_data(buf, (void**)&mes) == -1 ) {
318   return -1;
319  }
320
321  if (mes->type == MIDI_TYPE_CLOCK_TICK || mes->type == MIDI_TYPE_CLOCK_START || mes->type == MIDI_TYPE_CLOCK_STOP ) {
322   if ( stream_vio_s_write(ss, &(mes->type), 1) != 1 ) {
323    streams_delete(id);
324    return -1;
325   }
326  } else {
327   len = 0;
328   d   = data;
329
330   _nb = (mes->type & 0xF0) | (mes->channel & 0x0F);
331
332   switch (mes->type) {
333    case MIDI_TYPE_NOTE_ON:
334    case MIDI_TYPE_NOTE_OFF:
335    case MIDI_TYPE_PA:
336    case MIDI_TYPE_CONTROLER:
337      _nb = mes->kk;
338      _nb = mes->vv;
339     break;
340    case MIDI_TYPE_PROGRAM:
341    case MIDI_TYPE_MA:
342      _nb = mes->vv;
343     break;
344    case MIDI_TYPE_PB:
345    case MIDI_TYPE_SYSEX:
346     break;
347   }
348
349   if ( stream_vio_s_write(ss, data, len) != len ) {
350    streams_delete(id);
351    return -1;
352   }
353  }
354
355  if ( roar_buffer_get_next(buf, &buf) == -1 )
356   buf = NULL;
357 }
358
359 return 0;
360}
361#undef _nb
362
363int midi_new_bufmes    (struct roar_buffer ** buf, struct midi_message ** mes) {
364 if ( buf == NULL || mes == NULL )
365  return -1;
366
367 *buf = (void*)(*mes = NULL);
368
369 if ( roar_buffer_new(buf, sizeof(struct midi_message)) == -1 )
370  return -1;
371
372 if ( roar_buffer_get_data(*buf, (void**)mes) == -1 ) {
373  roar_buffer_free(*buf);
374  *buf = (void*)(*mes = NULL);
375  return -1;
376 }
377
378 memset((void*)*mes, 0, sizeof(struct midi_message));
379
380 (*mes)->type = MIDI_TYPE_NONE;
381
382 return 0;
383}
384
385int midi_add_buf       (int id, struct roar_buffer * buf) {
386 struct midi_message * mes;
387
388 if ( id == -1 || buf == NULL )
389  return -1;
390
391 if ( roar_buffer_get_data(buf, (void**)&mes) == -1 )
392  return -1;
393
394 ROAR_DBG("midi_add_buf(id=%i, buf=%p) = ?", id, buf);
395 ROAR_DBG("midi_add_buf(*): MIDI Message of Type 0x%.2X", mes->type);
396 ROAR_DBG("midi_add_buf(*): Channel: %i", mes->channel);
397 ROAR_DBG("midi_add_buf(*): flags=0x%.2X", mes->flags);
398 ROAR_DBG("midi_add_buf(*): kk=0x%.2X, vv=0x%.2X", mes->kk, mes->vv);
399
400 if ( g_midi_mess.buf == NULL ) {
401  g_midi_mess.buf = buf;
402 } else {
403  roar_buffer_add(g_midi_mess.buf, buf);
404 }
405
406 return 0;
407}
408
409// bridges:
410int midi_check_bridge  (int id) {
411
412 ROAR_DBG("midi_check_bridge(id=%i) = ?", id);
413
414 if ( id == g_midi_clock.stream ) {
415  midi_clock_tick();
416
417  return 0;
418 }
419
420 return -1;
421}
422
423// clock:
424
425int midi_clock_init (void) {
426 struct roar_stream * s;
427 struct roar_stream_server * ss;
428
429 if ( (g_midi_clock.stream = streams_new()) == -1 ) {
430  ROAR_WARN("Error while initializing MIDI subsystem component clock");
431  return -1;
432 }
433
434 midi_vio_set_dummy(g_midi_clock.stream);
435
436 streams_get(g_midi_clock.stream, &ss);
437 s = ROAR_STREAM(ss);
438
439 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
440
441 s->pos_rel_id    =  g_midi_clock.stream;
442
443 s->info.codec    =  ROAR_CODEC_MIDI;
444 ss->codec_orgi   =  ROAR_CODEC_MIDI;
445
446 s->info.channels =  0;
447 s->info.rate     = MIDI_RATE;
448 s->info.bits     =  8;
449
450 if ( streams_set_dir(g_midi_clock.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
451  ROAR_WARN("Error while initializing MIDI subsystem component clock");
452  return -1;
453 }
454
455 streams_set_name(g_midi_clock.stream, "MIDI Clock");
456
457 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_PRIMARY);
458 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_SYNC);
459
460 midi_clock_set_bph(3600); // one tick per sec
461
462 return 0;
463}
464
465int midi_clock_set_bph (uint_least32_t bph) {
466 uint_least32_t sph = g_sa->rate/2 * 75 * g_sa->channels; // samples per houre
467
468 g_midi_clock.bph  = bph;
469
470 g_midi_clock.spt  = sph/bph;
471
472 g_midi_clock.nt   = ROAR_MATH_OVERFLOW_ADD(g_pos, g_midi_clock.spt);
473
474 return 0;
475}
476
477int midi_clock_tick (void) {
478 struct roar_buffer  * buf;
479 struct midi_message * mes;
480 struct roar_stream_server * ss;
481 unsigned int diff;
482
483 if ( streams_get(g_midi_clock.stream, &ss) == -1 ) {
484  ROAR_ERR("midi_clock_tick(void): Something very BAD happend: can not get stream object of my own stream!");
485  g_midi_clock.stream = -1;
486  ROAR_WARN("midi_clock_tick(void): Disabled MIDI Clock");
487  return -1;
488 }
489
490 while ( g_pos >= g_midi_clock.nt ) {
491  diff = g_pos - g_midi_clock.nt;
492  ROAR_DBG("midi_clock_tick(void): g_pos is %u samples (%5.2f%%) ahead of nt.", diff, (float)diff/g_midi_clock.spt);
493
494  g_midi_clock.nt   = ROAR_MATH_OVERFLOW_ADD(g_midi_clock.nt, g_midi_clock.spt);
495
496  if ( streams_get_flag(g_midi_clock.stream, ROAR_FLAG_SYNC) ) {
497   ROAR_DBG("midi_clock_tick(void): TICK! (nt=%lu)", g_midi_clock.nt);
498   if ( midi_new_bufmes(&buf, &mes) == -1 ) {
499    ROAR_ERR("midi_clock_tick(void): Can not create Clock-Tick-Message");
500   }
501
502   mes->type = MIDI_TYPE_CLOCK_TICK;
503
504   if ( midi_add_buf(g_midi_clock.stream, buf) == -1 ) {
505    ROAR_ERR("midi_clock_tick(void): Can not add Clock-Tick-Message");
506    roar_buffer_free(buf);
507    return -1;
508   }
509
510   ROAR_STREAM(ss)->pos = ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(ss)->pos, 1);
511  } else {
512   ROAR_DBG("midi_clock_tick(void): silent tick. (nt=%lu)", g_midi_clock.nt);
513  }
514 }
515
516 return 0;
517}
518
519// CB:
520
521int midi_cb_init (void) {
522#ifdef _HAVE_CONSOLE
523 struct roar_stream * s;
524 struct roar_stream_server * ss;
525 int i;
526 char * files[] = {
527                   "/dev/console",
528#ifdef __linux__
529                   "/dev/tty0",
530                   "/dev/vc/0",
531#endif
532                   NULL
533                  };
534
535 g_midi_cb.console  = -1;
536 g_midi_cb.stream   = -1;
537 g_midi_cb.stoptime =  0;
538 g_midi_cb.playing  =  0;
539
540 for (i = 0; files[i] != NULL; i++) {
541  if ( (g_midi_cb.console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 )
542   break;
543 }
544
545 if ( g_midi_cb.console == -1 )
546  return -1;
547
548 if ( (g_midi_cb.stream = streams_new()) == -1 ) {
549  ROAR_WARN("Error while initializing MIDI subsystem component CB");
550  midi_cb_free();
551  return -1;
552 }
553
554 midi_vio_set_dummy(g_midi_cb.stream);
555
556 streams_get(g_midi_cb.stream, &ss);
557 s = ROAR_STREAM(ss);
558
559 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
560
561 s->pos_rel_id    = -1;
562
563 s->info.codec    =  0;
564 ss->codec_orgi   =  0;
565
566 s->info.channels =  1;
567 s->info.rate     = 1193180;
568 s->info.bits     =  8;
569
570 if ( streams_set_dir(g_midi_cb.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
571  ROAR_WARN("Error while initializing MIDI subsystem component CB");
572  midi_cb_free();
573  return -1;
574 }
575
576 streams_set_name(g_midi_cb.stream, "Console speaker bridge");
577
578 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_OUTPUT);
579 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_PRIMARY);
580 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_HWMIXER);
581 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_MUTE);
582
583 return 0;
584#else
585 g_midi_cb.console  = -1;
586 g_midi_cb.stream   = -1;
587
588 return -1;
589#endif
590}
591
592int midi_cb_free (void) {
593#ifdef _HAVE_CONSOLE
594
595 midi_cb_stop();
596
597 if ( g_midi_cb.stream != -1 )
598  streams_delete(g_midi_cb.stream);
599
600 if ( g_midi_cb.console != -1 )
601  close(g_midi_cb.console);
602
603 return 0;
604#else
605 return -1;
606#endif
607}
608
609int midi_cb_play(float t, float freq, int override) {
610 float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels;
611
612/*
613#define MIDI_CB_NOOVERRIDE 0
614#define MIDI_CB_OVERRIDE   1
615*/
616 if ( g_midi_cb.playing && override != MIDI_CB_OVERRIDE )
617  return -1;
618
619 g_midi_cb.stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t);
620 midi_cb_start(freq);
621
622 return 0;
623}
624
625int midi_cb_update (void) {
626
627 if ( !streams_get_flag(g_midi_cb.stream, ROAR_FLAG_MUTE) ) {
628  if ( midi_cb_readbuf() == -1 )
629   return -1;
630 } else if ( g_midi_cb.playing ) {
631  midi_cb_stop();
632 }
633
634 if ( !g_midi_cb.playing )
635  return 0;
636
637/*
638 if ( g_midi_cb.stoptime <= g_pos )
639  midi_cb_stop();
640*/
641
642 ROAR_DBG("midi_cb_update(void) = ?");
643
644 return 0;
645}
646
647int midi_cb_start(float freq) {
648// On linux this uses ioctl KIOCSOUND
649#ifdef __linux__
650 if ( g_midi_cb.console == -1 )
651  return -1;
652
653 if ( ioctl(g_midi_cb.console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 )
654  return -1;
655
656 g_midi_cb.playing = 1;
657
658 return 0;
659#else
660 return -1;
661#endif
662}
663
664int midi_cb_stop (void) {
665#ifdef __linux__
666 int ret;
667
668 ret = midi_cb_start(0);
669 g_midi_cb.playing = 0;
670
671 return ret;
672#else
673 return -1;
674#endif
675}
676
677int midi_cb_readbuf(void) {
678 struct roar_buffer        * buf = g_midi_mess.buf;
679 struct midi_message       * mes = NULL;
680
681 ROAR_DBG("midi_cb_readbuf(void) = ?");
682
683 while (buf != NULL) {
684  ROAR_DBG("midi_cb_readbuf(void): buf=%p", buf);
685
686  if ( roar_buffer_get_data(buf, (void**)&mes) == -1 ) {
687   return -1;
688  }
689
690  switch (mes->type) {
691   case MIDI_TYPE_NOTE_ON:
692     midi_cb_start(mes->d.note.freq);
693    break;
694   case MIDI_TYPE_NOTE_OFF:
695     midi_cb_stop();
696    break;
697   case MIDI_TYPE_CONTROLER:
698     if ( mes->kk == 123 ) /* all note off */
699      midi_cb_stop();
700    break;
701  }
702
703  if ( roar_buffer_get_next(buf, &buf) == -1 )
704   buf = NULL;
705 }
706
707 return 0;
708}
709
710// VIO:
711
712int     midi_vio_set_dummy(int stream) {
713 struct roar_stream_server * ss;
714
715 if ( streams_get(stream, &ss) == -1 )
716  return -1;
717
718 ss->vio.read     = NULL;
719 ss->vio.write    = NULL;
720 ss->vio.lseek    = NULL;
721 ss->vio.nonblock = (int (*)(struct roar_vio_calls * vio, int state))midi_vio_ok;
722 ss->vio.sync     = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
723 ss->vio.ctl      = NULL;
724 ss->vio.close    = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
725
726 return 0;
727}
728
729int     midi_vio_ok(struct roar_vio_calls * vio, ...) {
730 return 0;
731}
732
733//ll
Note: See TracBrowser for help on using the repository browser.