source: roaraudio/roard/midi.c @ 1831:dc25362e22ae

Last change on this file since 1831:dc25362e22ae was 1751:73aa7bba2551, checked in by phi, 15 years ago

only use a console device if we have one

File size: 2.5 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#ifdef _HAVE_CONSOLE
33 int i;
34 char * files[] = {
35                   "/dev/console",
36#ifdef __linux__
37                   "/dev/tty0",
38                   "/dev/vc/0",
39#endif
40                   NULL
41                  };
42
43 g_console          = -1;
44 g_midi_cb_stoptime = 0;
45 g_midi_cb_playing  = 0;
46
47 for (i = 0; files[i] != NULL; i++) {
48  if ( (g_console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 )
49   break;
50 }
51
52 if ( g_console != -1 ) {
53  return 0;
54 } else {
55  return -1;
56 }
57#else
58 return -1;
59#endif
60}
61
62int midi_free (void) {
63#ifdef _HAVE_CONSOLE
64 if ( g_console != -1 )
65  close(g_console);
66 return 0;
67#else
68 return -1;
69#endif
70}
71
72int midi_cb_play(float t, float freq, int override) {
73 float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels;
74
75/*
76#define MIDI_CB_NOOVERRIDE 0
77#define MIDI_CB_OVERRIDE   1
78*/
79 if ( g_midi_cb_playing && override != MIDI_CB_OVERRIDE )
80  return -1;
81
82 g_midi_cb_stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t);
83 midi_cb_start(freq);
84 g_midi_cb_playing = 1;
85
86 return 0;
87}
88
89int midi_cb_update (void) {
90 if ( !g_midi_cb_playing )
91  return 0;
92
93 if ( g_midi_cb_stoptime <= g_pos )
94  midi_cb_stop();
95
96 return 0;
97}
98
99int midi_cb_start(float freq) {
100// On linux this uses ioctl KIOCSOUND
101#ifdef __linux__
102 if ( g_console == -1 )
103  return -1;
104
105 if ( ioctl(g_console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 )
106  return -1;
107
108 return 0;
109#else
110 return -1;
111#endif
112}
113
114int midi_cb_stop (void) {
115#ifdef __linux__
116 g_midi_cb_playing = 0;
117 return midi_cb_start(0);
118#else
119 return -1;
120#endif
121}
122
123//ll
Note: See TracBrowser for help on using the repository browser.