-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsound.c
308 lines (253 loc) · 8.24 KB
/
sound.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include "textmode.h"
static SDL_AudioSpec spec;
static SDL_AudioDeviceID device;
static uint8_t volume = 5;
static bool is_muted = false;
static double NoteNumberToFrequency(int note_num)
{
static const int frequencies[] = { // in octave 6
4186, // C
4435, // C#
4699, // D
4978, // D#
5274, // E
5588, // F
5920, // F#
6272, // G
6645, // G#
7040, // A
7459, // A#
7902, // B
};
int octave = (note_num - 1) / 12;
int note = (note_num - 1) % 12;
int freq = frequencies[note];
int octaves_down = 6 - octave;
while ( octaves_down-- )
freq /= 2;
return (double)freq * 2.0; // basic notes sound 1 octave higher
}
static void DOS_QuitSound(void) {
DOS_StopSound();
SDL_PauseAudioDevice(device, SDL_TRUE);
SDL_CloseAudioDevice(device);
}
void DOS_Mute(bool muted)
{
is_muted = muted;
}
bool DOS_SoundIsPlaying(void)
{
return SDL_GetQueuedAudioSize(device) > 0;
}
void DOS_AddSound(unsigned frequency, unsigned milliseconds)
{
if ( is_muted ) {
return;
}
float period = (float)spec.freq / (float)frequency;
int len = (float)spec.freq * ((float)milliseconds / 1000.0f);
for ( int i = 0; i < len; i++ ) {
int8_t sample;
if ( frequency == 0 ) {
sample = spec.silence;
} else {
sample = (int)((float)i / period) % 2 ? volume : -volume;
}
SDL_QueueAudio(device, &sample, sizeof(sample));
}
}
void DOS_InitSound(void)
{
if ( SDL_WasInit(SDL_INIT_AUDIO) == 0 ) {
int result = SDL_InitSubSystem(SDL_INIT_AUDIO);
if ( result < 0 )
fprintf(stderr, "error: failed to init SDL audio subsystem: %s", SDL_GetError());
}
SDL_AudioSpec want = {
.freq = 44100,
.format = AUDIO_S8,
.channels = 1,
.samples = 4096,
};
device = SDL_OpenAudioDevice(NULL, 0, &want, &spec, 0);
if ( device == 0 ) {
fprintf(stderr, "error: failed to open audio: %s\n", SDL_GetError());
}
SDL_PauseAudioDevice(device, 0);
atexit(DOS_QuitSound);
}
void DOS_SetVolume(unsigned value)
{
if ( value > 15 || value <= 0 ) {
fprintf(stderr, "bad volume, expected value in range 1-15\n");
return;
}
volume = value;
}
void DOS_Sound(unsigned frequency, unsigned milliseconds)
{
SDL_ClearQueuedAudio(device);
DOS_AddSound(frequency, milliseconds);
}
void DOS_StopSound(void)
{
SDL_ClearQueuedAudio(device);
}
void DOS_Beep(void)
{
DOS_Sound(800, 200);
}
// Play
// L[1,2,4,8,16,32,64] default: 4
// O[0...6] default: 4
// T[32...255] default: 120
// [A...G]([+,#,-][1,2,4,8,16,32,64][.])
// N[0...84](.)
// P[v]
static void PlayError(const char * msg, int line_position)
{
printf("Play syntax error: %s (position %d)\n.", msg, line_position);
}
#define PLAY_STRING_MAX 255
void DOS_Play(const char * string, ...)
{
if ( strlen(string) > PLAY_STRING_MAX ) {
printf("Play error: string too long (max %d)\n", PLAY_STRING_MAX);
return;
}
va_list args;
va_start(args, string);
char buffer[PLAY_STRING_MAX + 1] = { 0 };
vsnprintf(buffer, PLAY_STRING_MAX, string, args);
va_end(args);
// default settings
int bmp = 120;
int oct = 4;
int len = 4;
// A-G
static const int note_offsets[7] = { 9, 11, 0, 2, 4, 5, 7 };
enum {
mode_staccato = 6, // 6/8
mode_normal = 7, // 7/8
mode_legato = 8 // 8/8
} mode = mode_normal;
SDL_ClearQueuedAudio(device);
// queue up whatever's in the string:
// TODO: could use a big clean up.
// TODO: handle tuplet handling
// TODO: nice if more articulate choice beyond s, n, and l - specify fraction?
const char * str = buffer;
while ( *str != '\0') {
char c = toupper(*str++);
switch ( c ) {
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
case 'G': case 'N': case 'P':
{
// get note:
int note = 0;
switch ( c ) {
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
case 'G':
note = 1 + (oct) * 12 + note_offsets[c - 'A'];
break;
case 'P':
note = 0;
break;
case 'N': {
int number = (int)strtol(str, (char **)&str, 10);
if ( number < 0 || number > 84 )
return PlayError("bad note number", (int)(str - string));
if ( number > 0 )
note = number;
break;
}
default:
break;
}
// adjust note per accidental:
if ( c >= 'A' && c <= 'G' ) {
if ( *str == '+' || *str == '#' ) {
if ( note < 84 )
note++;
str++;
} else if ( *str == '-' ) {
if ( note > 1 )
note--;
str++;
}
}
int d = len;
// check if there's a note length following a note A-G, set d
if ( c != 'N' ) {
int number = (int)strtol(str, (char **)&str, 10);
// strtol got a number, but it was a bad one
if ( number < 0 || number > 64 )
return PlayError("bad note value", (int)(str - string));
// strtol found a value number
if ( number > 0 )
d = number;
}
// TODO: this should only happen when after a note length
// count dots:
int dot_count = 0;
while ( *str == '.' ) {
dot_count++;
str++;
}
// adjust duration if there are dots:
float total_ms = (60.0f / (float)bmp) * 1000.0f * (4.0f / (float)d);
float prolongation = total_ms / 2.0f;
while ( dot_count-- ) {
total_ms += prolongation;
prolongation /= 2;
}
// calculate articulation silence:
int note_ms = total_ms * ((float)mode / 8.0f);
int silence_ms = total_ms * ((8.0f - (float)mode) / 8.0f);
// and finally, queue it
DOS_AddSound(NoteNumberToFrequency(note), note_ms);
DOS_AddSound(0, silence_ms);
break;
} // A-G, N, and P
case 'T':
bmp = (int)strtol(str, (char **)&str, 10);
if ( bmp == 0 )
return PlayError("bad tempo", (int)(str - string));
break;
case 'O':
if ( *str < '0' || *str > '6' )
return PlayError("bad octave", (int)(str - string));
oct = (int)strtol(str, (char **)&str, 10);
break;
// TODO: dots not handled
case 'L':
len = (int)strtol(str, (char **)&str, 10);
if ( len < 1 || len > 64 )
return PlayError("bad length", (int)(str - string));
break;
case '>':
if ( oct < 6 )
oct++;
break;
case '<':
if ( oct > 0 )
oct--;
break;
case 'M': {
char option = toupper(*str++);
switch ( option ) {
case 'L': mode = mode_legato; break;
case 'N': mode = mode_normal; break;
case 'S': mode = mode_staccato; break;
default:
return PlayError("bad music option", (int)(str - string));
break;
}
break;
}
default:
break;
}
}
}