forked from smcameron/wordwarvi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wwviaudio.c
487 lines (411 loc) · 12.1 KB
/
wwviaudio.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*
(C) Copyright 2007,2008, Stephen M. Cameron.
This file is part of wordwarvi.
wordwarvi is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
wordwarvi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with wordwarvi; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef WWVIAUDIO_STUBS_ONLY
#include <stdio.h>
#include <stdint.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define WWVIAUDIO_DEFINE_GLOBALS
#include "wwviaudio.h"
#undef WWVIAUDIO_DEFINE_GLOBALS
#include "portaudio.h"
#include "ogg_to_pcm.h"
#define FRAMES_PER_BUFFER (1024)
static PaStream *stream = NULL;
static int audio_paused = 0;
static int music_playing = 1;
static int sound_working = 0;
static int nomusic = 0;
static int sound_effects_on = 1;
static int sound_device = -1; /* default sound device for port audio. */
static unsigned int max_concurrent_sounds = 0;
static int max_sound_clips = 0;
/* Pause all audio output, output silence. */
void wwviaudio_pause_audio(void)
{
audio_paused = 1;
}
/* Resume playing audio previously paused. */
void wwviaudio_resume_audio(void)
{
audio_paused = 0;
}
/* Silence the music channel */
void wwviaudio_silence_music(void)
{
music_playing = 0;
}
/* Resume volume on the music channel. */
void wwviaudio_resume_music(void)
{
music_playing = 1;
}
void wwviaudio_toggle_music(void)
{
music_playing = !music_playing;
}
/* Silence the sound effects. */
void wwviaudio_silence_sound_effects(void)
{
sound_effects_on = 0;
}
/* Resume volume on the sound effects. */
void wwviaudio_resume_sound_effects(void)
{
sound_effects_on = 1;
}
void wwviaudio_toggle_sound_effects(void)
{
sound_effects_on = !sound_effects_on;
}
void wwviaudio_set_nomusic(void)
{
nomusic = 1;
}
static struct sound_clip {
int active;
int nsamples;
int pos;
int16_t *sample;
} *clip = NULL;
static struct sound_clip *audio_queue = NULL;
int wwviaudio_read_ogg_clip(int clipnum, char *filename)
{
uint64_t nframes;
char filebuf[PATH_MAX];
struct stat statbuf;
int samplesize, sample_rate;
int nchannels;
int rc;
if (clipnum >= max_sound_clips || clipnum < 0)
return -1;
#ifndef __WIN32__
snprintf(filebuf, PATH_MAX, "%s/%s", DATADIR, filename);
rc = stat(filebuf, &statbuf);
if (rc != 0) {
strncpy(filebuf, filename, PATH_MAX);
rc = stat(filebuf, &statbuf);
if (rc != 0) {
fprintf(stderr, "stat('%s') failed.\n", filebuf);
return -1;
}
}
#else
/* use relative path on win32 */
snprintf(filebuf, PATH_MAX, "%s", filename);
#endif
/*
printf("Reading sound file: '%s'\n", filebuf);
printf("frames = %lld\n", sfinfo.frames);
printf("samplerate = %d\n", sfinfo.samplerate);
printf("channels = %d\n", sfinfo.channels);
printf("format = %d\n", sfinfo.format);
printf("sections = %d\n", sfinfo.sections);
printf("seekable = %d\n", sfinfo.seekable);
*/
if (clip[clipnum].sample != NULL)
/* overwriting a previously read clip... */
free(clip[clipnum].sample);
rc = ogg_to_pcm(filebuf, &clip[clipnum].sample, &samplesize,
&sample_rate, &nchannels, &nframes);
if (clip[clipnum].sample == NULL) {
printf("Can't get memory for sound data for %llu frames in %s\n",
nframes, filebuf);
goto error;
}
if (rc != 0) {
fprintf(stderr, "Error: ogg_to_pcm('%s') failed.\n",
filebuf);
goto error;
}
clip[clipnum].nsamples = (int) nframes;
if (clip[clipnum].nsamples < 0)
clip[clipnum].nsamples = 0;
return 0;
error:
return -1;
}
/* This routine will be called by the PortAudio engine when audio is needed.
** It may called at interrupt level on some machines so don't do anything
** that could mess up the system like calling malloc() or free().
*/
static int patestCallback(__attribute__ ((unused)) const void *inputBuffer,
void *outputBuffer,
unsigned long framesPerBuffer,
__attribute__ ((unused)) const PaStreamCallbackTimeInfo* timeInfo,
__attribute__ ((unused)) PaStreamCallbackFlags statusFlags,
__attribute__ ((unused)) void *userData )
{
unsigned int i, j;
int sample, count;
float *out = NULL;
float output;
out = (float*) outputBuffer;
output = 0.0;
count = 0;
if (audio_paused) {
/* output silence when paused and
* don't advance any sound slot pointers
*/
for (i = 0; i < framesPerBuffer; i++)
*out++ = (float) 0;
return 0;
}
for (i = 0; i < framesPerBuffer; i++) {
output = 0.0;
count = 0;
for (j = 0; j < max_concurrent_sounds; j++) {
if (!audio_queue[j].active ||
audio_queue[j].sample == NULL)
continue;
sample = i + audio_queue[j].pos;
count++;
if (sample >= audio_queue[j].nsamples) {
audio_queue[j].active = 0;
continue;
}
if (j != WWVIAUDIO_MUSIC_SLOT && sound_effects_on)
output += (float) audio_queue[j].sample[sample] * 0.5 / (float) (INT16_MAX);
else if (j == WWVIAUDIO_MUSIC_SLOT && music_playing)
output += (float) audio_queue[j].sample[sample] / (float) (INT16_MAX);
}
*out++ = (float) output / 2.0;
}
for (i = 0; i < max_concurrent_sounds; i++) {
if (!audio_queue[i].active)
continue;
audio_queue[i].pos += framesPerBuffer;
if (audio_queue[i].pos >= audio_queue[i].nsamples)
audio_queue[i].active = 0;
}
return 0; /* we're never finished */
}
static void decode_paerror(PaError rc)
{
if (rc == paNoError)
return;
fprintf(stderr, "An error occured while using the portaudio stream\n");
fprintf(stderr, "Error number: %d\n", rc);
fprintf(stderr, "Error message: %s\n", Pa_GetErrorText(rc));
}
static void wwviaudio_terminate_portaudio(PaError rc)
{
Pa_Terminate();
decode_paerror(rc);
}
int wwviaudio_initialize_portaudio(int maximum_concurrent_sounds, int maximum_sound_clips)
{
PaStreamParameters outparams;
PaError rc;
PaDeviceIndex device_count;
if (maximum_concurrent_sounds < 0)
return -1;
max_concurrent_sounds = (unsigned int) maximum_concurrent_sounds;
max_sound_clips = maximum_sound_clips;
audio_queue = malloc(max_concurrent_sounds * sizeof(audio_queue[0]));
clip = malloc(max_sound_clips * sizeof(clip[0]));
if (audio_queue == NULL || clip == NULL)
return -1;
memset(audio_queue, 0, sizeof(audio_queue[0]) * max_concurrent_sounds);
memset(clip, 0, sizeof(clip[0]) * max_sound_clips);
rc = Pa_Initialize();
if (rc != paNoError)
goto error;
device_count = Pa_GetDeviceCount();
printf("Portaudio reports %d sound devices.\n", device_count);
if (device_count == 0) {
printf("There will be no audio.\n");
goto error;
rc = 0;
}
sound_working = 1;
outparams.device = Pa_GetDefaultOutputDevice(); /* default output device */
printf("Portaudio says the default device is: %d\n", outparams.device);
if (sound_device != -1) {
if (sound_device >= device_count)
fprintf(stderr, "wordwarvi: Invalid sound device "
"%d specified, ignoring.\n", sound_device);
else
outparams.device = sound_device;
printf("Using sound device %d\n", outparams.device);
}
if (outparams.device < 0 && device_count > 0) {
printf("Hmm, that's strange, portaudio says the default device is %d,\n"
" but there are %d devices\n",
outparams.device, device_count);
printf("I think we'll just skip sound for now.\n");
printf("You might try the '--sounddevice' option and see if that helps.\n");
sound_working = 0;
return -1;
}
outparams.channelCount = 1; /* mono output */
outparams.sampleFormat = paFloat32; /* 32 bit floating point output */
outparams.suggestedLatency =
Pa_GetDeviceInfo(outparams.device)->defaultLowOutputLatency;
outparams.hostApiSpecificStreamInfo = NULL;
rc = Pa_OpenStream(&stream,
NULL, /* no input */
&outparams, WWVIAUDIO_SAMPLE_RATE, FRAMES_PER_BUFFER,
paNoFlag, /* paClipOff, */ /* we won't output out of range samples so don't bother clipping them */
patestCallback, NULL /* cookie */);
if (rc != paNoError)
goto error;
if ((rc = Pa_StartStream(stream)) != paNoError)
goto error;
return rc;
error:
wwviaudio_terminate_portaudio(rc);
return rc;
}
void wwviaudio_stop_portaudio(void)
{
int i, rc;
if (!sound_working)
return;
if ((rc = Pa_StopStream(stream)) != paNoError)
goto error;
rc = Pa_CloseStream(stream);
error:
wwviaudio_terminate_portaudio(rc);
if (audio_queue) {
free(audio_queue);
audio_queue = NULL;
max_concurrent_sounds = 0;
}
if (clip) {
for (i = 0; i < max_sound_clips; i++) {
if (clip[i].sample)
free(clip[i].sample);
}
free(clip);
clip = NULL;
max_sound_clips = 0;
}
return;
}
static int wwviaudio_add_sound_to_slot(int which_sound, int which_slot)
{
unsigned int i;
if (!sound_working)
return 0;
if (nomusic && which_slot == WWVIAUDIO_MUSIC_SLOT)
return 0;
if (which_slot != WWVIAUDIO_ANY_SLOT) {
if (audio_queue[which_slot].active)
audio_queue[which_slot].active = 0;
audio_queue[which_slot].pos = 0;
audio_queue[which_slot].nsamples = 0;
/* would like to put a memory barrier here. */
audio_queue[which_slot].sample = clip[which_sound].sample;
audio_queue[which_slot].nsamples = clip[which_sound].nsamples;
/* would like to put a memory barrier here. */
audio_queue[which_slot].active = 1;
return which_slot;
}
for (i=1;i<max_concurrent_sounds;i++) {
if (audio_queue[i].active == 0) {
audio_queue[i].nsamples = clip[which_sound].nsamples;
audio_queue[i].pos = 0;
audio_queue[i].sample = clip[which_sound].sample;
audio_queue[i].active = 1;
break;
}
}
return (i >= max_concurrent_sounds) ? -1 : (int) i;
}
int wwviaudio_add_sound(int which_sound)
{
return wwviaudio_add_sound_to_slot(which_sound, WWVIAUDIO_ANY_SLOT);
}
int wwviaudio_play_music(int which_sound)
{
return wwviaudio_add_sound_to_slot(which_sound, WWVIAUDIO_MUSIC_SLOT);
}
void wwviaudio_add_sound_low_priority(int which_sound)
{
/* adds a sound if there are at least 5 empty sound slots. */
unsigned int i;
int empty_slots = 0;
int last_slot;
if (!sound_working)
return;
last_slot = -1;
for (i = 1; i < max_concurrent_sounds; i++)
if (audio_queue[i].active == 0) {
last_slot = i;
empty_slots++;
if (empty_slots >= 5)
break;
}
if (empty_slots < 5)
return;
i = (unsigned int) last_slot;
if (audio_queue[i].active == 0) {
audio_queue[i].nsamples = clip[which_sound].nsamples;
audio_queue[i].pos = 0;
audio_queue[i].sample = clip[which_sound].sample;
audio_queue[i].active = 1;
}
return;
}
void wwviaudio_cancel_sound(int queue_entry)
{
if (!sound_working)
return;
audio_queue[queue_entry].active = 0;
}
void wwviaudio_cancel_music(void)
{
wwviaudio_cancel_sound(WWVIAUDIO_MUSIC_SLOT);
}
void wwviaudio_cancel_all_sounds(void)
{
unsigned int i;
if (!sound_working)
return;
for (i = 0; i < max_concurrent_sounds; i++)
audio_queue[i].active = 0;
}
int wwviaudio_set_sound_device(int device)
{
sound_device = device;
return 0;
}
#else /* stubs only... */
int wwviaudio_initialize_portaudio() { return 0; }
void wwviaudio_stop_portaudio() { return; }
void wwviaudio_set_nomusic() { return; }
int wwviaudio_read_ogg_clip(int clipnum, char *filename) { return 0; }
void wwviaudio_pause_audio() { return; }
void wwviaudio_resume_audio() { return; }
void wwviaudio_silence_music() { return; }
void wwviaudio_resume_music() { return; }
void wwviaudio_silence_sound_effects() { return; }
void wwviaudio_resume_sound_effects() { return; }
void wwviaudio_toggle_sound_effects() { return; }
int wwviaudio_play_music(int which_sound) { return 0; }
void wwviaudio_cancel_music() { return; }
void wwviaudio_toggle_music() { return; }
int wwviaudio_add_sound(int which_sound) { return 0; }
void wwviaudio_add_sound_low_priority(int which_sound) { return; }
void wwviaudio_cancel_sound(int queue_entry) { return; }
void wwviaudio_cancel_all_sounds() { return; }
int wwviaudio_set_sound_device(int device) { return 0; }
#endif