Line | |
---|
1 | //midi.c: |
---|
2 | |
---|
3 | #include "roard.h" |
---|
4 | |
---|
5 | int midi_init (void) { |
---|
6 | int i; |
---|
7 | char * files[] = { |
---|
8 | "/dev/console", |
---|
9 | #ifdef __linux__ |
---|
10 | "/dev/tty0", |
---|
11 | "/dev/vc/0", |
---|
12 | #endif |
---|
13 | NULL |
---|
14 | }; |
---|
15 | |
---|
16 | g_console = -1; |
---|
17 | g_midi_cb_stoptime = 0; |
---|
18 | g_midi_cb_playing = 0; |
---|
19 | |
---|
20 | for (i = 0; files[i] != NULL; i++) { |
---|
21 | if ( (g_console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 ) |
---|
22 | break; |
---|
23 | } |
---|
24 | |
---|
25 | if ( g_console != -1 ) { |
---|
26 | return 0; |
---|
27 | } else { |
---|
28 | return -1; |
---|
29 | } |
---|
30 | } |
---|
31 | |
---|
32 | int midi_free (void) { |
---|
33 | if ( g_console != -1 ) |
---|
34 | close(g_console); |
---|
35 | return 0; |
---|
36 | } |
---|
37 | |
---|
38 | int midi_cb_play(float t, float freq, int override) { |
---|
39 | float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels; |
---|
40 | |
---|
41 | /* |
---|
42 | #define MIDI_CB_NOOVERRIDE 0 |
---|
43 | #define MIDI_CB_OVERRIDE 1 |
---|
44 | */ |
---|
45 | if ( g_midi_cb_playing && override != MIDI_CB_OVERRIDE ) |
---|
46 | return -1; |
---|
47 | |
---|
48 | g_midi_cb_stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t); |
---|
49 | midi_cb_start(freq); |
---|
50 | g_midi_cb_playing = 1; |
---|
51 | |
---|
52 | return 0; |
---|
53 | } |
---|
54 | |
---|
55 | int midi_cb_update (void) { |
---|
56 | if ( !g_midi_cb_playing ) |
---|
57 | return 0; |
---|
58 | |
---|
59 | if ( g_midi_cb_stoptime <= g_pos ) |
---|
60 | midi_cb_stop(); |
---|
61 | |
---|
62 | return 0; |
---|
63 | } |
---|
64 | |
---|
65 | int midi_cb_start(float freq) { |
---|
66 | // On linux this uses ioctl KIOCSOUND |
---|
67 | #ifdef __linux__ |
---|
68 | if ( g_console == -1 ) |
---|
69 | return -1; |
---|
70 | |
---|
71 | if ( ioctl(g_console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 ) |
---|
72 | return -1; |
---|
73 | |
---|
74 | return 0; |
---|
75 | #else |
---|
76 | return -1; |
---|
77 | #endif |
---|
78 | } |
---|
79 | |
---|
80 | int midi_cb_stop (void) { |
---|
81 | #ifdef __linux__ |
---|
82 | g_midi_cb_playing = 0; |
---|
83 | return midi_cb_start(0); |
---|
84 | #else |
---|
85 | return -1; |
---|
86 | #endif |
---|
87 | } |
---|
88 | |
---|
89 | //ll |
---|
Note: See
TracBrowser
for help on using the repository browser.