source: roaraudio/roard/midi.c @ 1859:5bae46f45f79

Last change on this file since 1859:5bae46f45f79 was 1859:5bae46f45f79, checked in by phi, 15 years ago

write to midi thru

File size: 8.4 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
[1843]41 return 0;
42}
[1848]43
[1843]44int midi_free (void) {
45 return midi_cb_free();
46}
47
48int midi_update(void) {
[1848]49
[1850]50 if ( g_midi_clock.stream != -1 )
51  midi_check_bridge(g_midi_clock.stream);
[1848]52
[1843]53 return midi_cb_update();
54}
55
[1855]56int midi_reinit(void) {
57 return 0;
58}
59
[1845]60// STREAMS:
61
62int midi_check_stream  (int id) {
[1846]63 struct roar_stream        *   s;
64 struct roar_stream_server *  ss;
[1858]65 struct roar_buffer        *   b;
66 char                      * buf;
67 ssize_t                     len;
[1859]68 int                           i;
[1846]69
70 if ( g_streams[id] == NULL )
71  return -1;
72
73 ROAR_DBG("midi_check_stream(id=%i) = ?", id);
74
75 s = ROAR_STREAM(ss = g_streams[id]);
76
[1848]77 if ( s->dir == ROAR_DIR_BRIDGE )
78  return midi_check_bridge(id);
79
[1846]80 switch (s->info.codec) {
[1858]81  case ROAR_CODEC_MIDI:
82   break;
83  default:
84    streams_delete(id);
85    return -1;
86 }
87
88 if ( roar_buffer_new(&b, MIDI_READ_SIZE) == -1 ) {
89  ROAR_ERR("midi_check_stream(*): Can not alloc buffer space!");
90  ROAR_DBG("midi_check_stream(*) = -1");
91  return -1;
92 }
93
94 roar_buffer_get_data(b, (void **)&buf);
95
96 if ( (len = stream_vio_s_read(ss, buf, MIDI_READ_SIZE)) < 1 ) {
97  streams_delete(id);
98  return -1;
99 }
100
101 roar_buffer_set_len(b, len);
102
[1859]103 if ( stream_add_buffer(id, b) == -1 ) {
104  roar_buffer_free(b);
105  streams_delete(id);
106  return -1;
107 }
108
109 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
110  if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) {
111   if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) {
112    if ( stream_vio_write(i, buf, len) != len ) {
113     streams_delete(i);
114    }
115   }
[1858]116  }
[1859]117 }
[1858]118
119 switch (s->info.codec) {
120  case ROAR_CODEC_MIDI:
121    return midi_conv_midi2mes(id);
122   break;
[1846]123  default:
124    streams_delete(id);
125    return -1;
126 }
127
128 return 0;
[1845]129}
[1846]130
[1845]131int midi_send_stream   (int id) {
[1846]132 struct roar_stream        *   s;
133 struct roar_stream_server *  ss;
134
135 if ( g_streams[id] == NULL )
136  return -1;
137
138 ROAR_DBG("midi_send_stream(id=%i) = ?", id);
139
140 s = ROAR_STREAM(ss = g_streams[id]);
141
142 switch (s->info.codec) {
143  default:
144    streams_delete(id);
145    return -1;
146 }
147
148 return 0;
[1845]149}
150
[1858]151int midi_conv_midi2mes (int id) {
152 return -1;
153}
154
155int midi_conv_mes2midi (int id) {
156 return -1;
157}
158
[1848]159// bridges:
160int midi_check_bridge  (int id) {
[1851]161
162 ROAR_WARN("midi_check_bridge(id=%i) = ?", id);
163
164 if ( id == g_midi_clock.stream ) {
165  midi_clock_tick();
166
167  return 0;
168 }
169
[1848]170 return -1;
171}
172
173// clock:
174
175int midi_clock_init (void) {
176 struct roar_stream * s;
177 struct roar_stream_server * ss;
178
[1850]179 if ( (g_midi_clock.stream = streams_new()) == -1 ) {
[1848]180  ROAR_WARN("Error while initializing MIDI subsystem component clock");
181  return -1;
182 }
183
[1850]184 midi_vio_set_dummy(g_midi_clock.stream);
[1848]185
[1850]186 streams_get(g_midi_clock.stream, &ss);
[1848]187 s = ROAR_STREAM(ss);
188
189 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
190
[1850]191 s->pos_rel_id    =  g_midi_clock.stream;
[1848]192
193 s->info.codec    =  ROAR_CODEC_MIDI;
194 ss->codec_orgi   =  ROAR_CODEC_MIDI;
195
196 s->info.channels =  0;
197 s->info.rate     = MIDI_RATE;
198 s->info.bits     =  8;
199
[1850]200 if ( streams_set_dir(g_midi_clock.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
[1848]201  ROAR_WARN("Error while initializing MIDI subsystem component clock");
202  return -1;
203 }
204
[1850]205 streams_set_name(g_midi_clock.stream, "MIDI Clock");
[1848]206
[1850]207 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_PRIMARY);
208 streams_set_flag(g_midi_clock.stream, ROAR_FLAG_SYNC);
[1848]209
[1851]210 midi_clock_set_bph(3600); // one tick per sec
211
212 return 0;
213}
214
215int midi_clock_set_bph (uint_least32_t bph) {
216 uint_least32_t sph = g_sa->rate/2 * 75 * g_sa->channels; // samples per houre
217
218 g_midi_clock.bph  = bph;
219
220 g_midi_clock.spt  = sph/bph;
221
222 g_midi_clock.nt   = ROAR_MATH_OVERFLOW_ADD(g_pos, g_midi_clock.spt);
223
224 return 0;
225}
226
227int midi_clock_tick (void) {
228 unsigned int diff;
229
230 while ( g_pos >= g_midi_clock.nt ) {
231  diff = g_pos - g_midi_clock.nt;
232  ROAR_WARN("midi_clock_tick(void): g_pos is %u samples (%5.2f%%) ahead of nt.", diff, (float)diff/g_midi_clock.spt);
233
234  g_midi_clock.nt   = ROAR_MATH_OVERFLOW_ADD(g_midi_clock.nt, g_midi_clock.spt);
235
236  if ( streams_get_flag(g_midi_clock.stream, ROAR_FLAG_SYNC) ) {
237   ROAR_WARN("midi_clock_tick(void): TICK! (nt=%lu)", g_midi_clock.nt);
238  } else {
239   ROAR_WARN("midi_clock_tick(void): silent tick. (nt=%lu)", g_midi_clock.nt);
240  }
241 }
242
[1848]243 return 0;
244}
245
[1845]246// CB:
[1843]247
248int midi_cb_init (void) {
[1751]249#ifdef _HAVE_CONSOLE
[1843]250 struct roar_stream * s;
251 struct roar_stream_server * ss;
[185]252 int i;
253 char * files[] = {
254                   "/dev/console",
255#ifdef __linux__
256                   "/dev/tty0",
257                   "/dev/vc/0",
258#endif
259                   NULL
260                  };
261
[1853]262 g_midi_cb.console  = -1;
263 g_midi_cb.stream   = -1;
264 g_midi_cb.stoptime =  0;
265 g_midi_cb.playing  =  0;
[185]266
267 for (i = 0; files[i] != NULL; i++) {
[1853]268  if ( (g_midi_cb.console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 )
[185]269   break;
270 }
271
[1853]272 if ( g_midi_cb.console == -1 )
[1843]273  return -1;
274
[1853]275 if ( (g_midi_cb.stream = streams_new()) == -1 ) {
[1843]276  ROAR_WARN("Error while initializing MIDI subsystem component CB");
277  midi_cb_free();
[187]278  return -1;
279 }
[1843]280
[1853]281 midi_vio_set_dummy(g_midi_cb.stream);
[1848]282
[1853]283 streams_get(g_midi_cb.stream, &ss);
[1843]284 s = ROAR_STREAM(ss);
285
286 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
287
288 s->pos_rel_id    = -1;
289
290 s->info.codec    =  0;
291 ss->codec_orgi   =  0;
292
293 s->info.channels =  1;
294 s->info.rate     = 1193180;
295 s->info.bits     =  8;
296
[1853]297 if ( streams_set_dir(g_midi_cb.stream, ROAR_DIR_BRIDGE, 1) == -1 ) {
[1843]298  ROAR_WARN("Error while initializing MIDI subsystem component CB");
299  midi_cb_free();
300  return -1;
301 }
302
[1853]303 streams_set_name(g_midi_cb.stream, "Console speaker bridge");
[1843]304
[1853]305 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_OUTPUT);
306 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_PRIMARY);
307 streams_set_flag(g_midi_cb.stream, ROAR_FLAG_HWMIXER);
[1843]308
309 return 0;
[1485]310#else
[1853]311 g_midi_cb.console  = -1;
312 g_midi_cb.stream   = -1;
[1843]313
[1485]314 return -1;
315#endif
[185]316}
317
[1843]318int midi_cb_free (void) {
[1751]319#ifdef _HAVE_CONSOLE
[1843]320
321 midi_cb_stop();
322
[1853]323 if ( g_midi_cb.stream != -1 )
324  streams_delete(g_midi_cb.stream);
[1843]325
[1853]326 if ( g_midi_cb.console != -1 )
327  close(g_midi_cb.console);
[1843]328
[185]329 return 0;
[1485]330#else
331 return -1;
332#endif
[185]333}
334
335int midi_cb_play(float t, float freq, int override) {
[191]336 float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels;
337
[189]338/*
339#define MIDI_CB_NOOVERRIDE 0
340#define MIDI_CB_OVERRIDE   1
341*/
[1853]342 if ( g_midi_cb.playing && override != MIDI_CB_OVERRIDE )
[191]343  return -1;
344
[1853]345 g_midi_cb.stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t);
[191]346 midi_cb_start(freq);
[1853]347 g_midi_cb.playing = 1;
[191]348
349 return 0;
[185]350}
351
[190]352int midi_cb_update (void) {
[1853]353 if ( !g_midi_cb.playing )
[192]354  return 0;
355
[1853]356 if ( g_midi_cb.stoptime <= g_pos )
[189]357  midi_cb_stop();
358
359 return 0;
360}
361
[185]362int midi_cb_start(float freq) {
363// On linux this uses ioctl KIOCSOUND
[188]364#ifdef __linux__
[1853]365 if ( g_midi_cb.console == -1 )
[188]366  return -1;
367
[1853]368 if ( ioctl(g_midi_cb.console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 )
[188]369  return -1;
370
371 return 0;
372#else
[185]373 return -1;
[188]374#endif
[185]375}
376
377int midi_cb_stop (void) {
378#ifdef __linux__
[1853]379 g_midi_cb.playing = 0;
[185]380 return midi_cb_start(0);
381#else
382 return -1;
383#endif
384}
385
[1848]386// VIO:
387
388int     midi_vio_set_dummy(int stream) {
389 struct roar_stream_server * ss;
390
391 if ( streams_get(stream, &ss) == -1 )
392  return -1;
393
394 ss->vio.read     = NULL;
395 ss->vio.write    = NULL;
396 ss->vio.lseek    = NULL;
[1849]397 ss->vio.nonblock = (int (*)(struct roar_vio_calls * vio, int state))midi_vio_ok;
398 ss->vio.sync     = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
[1848]399 ss->vio.ctl      = NULL;
[1849]400 ss->vio.close    = (int (*)(struct roar_vio_calls * vio))midi_vio_ok;
401
402 return 0;
[1848]403}
404
405int     midi_vio_ok(struct roar_vio_calls * vio, ...) {
406 return 0;
407}
408
[169]409//ll
Note: See TracBrowser for help on using the repository browser.