2 (C) Copyright 2007,2008, Stephen M. Cameron.
4 This file is part of wordwarvi.
6 wordwarvi is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 wordwarvi is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with wordwarvi; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #ifndef WWVIAUDIO_STUBS_ONLY
27 #include <sys/types.h>
34 #define WWVIAUDIO_DEFINE_GLOBALS
35 #include "wwviaudio.h"
36 #undef WWVIAUDIO_DEFINE_GLOBALS
38 #include <portaudio.h>
39 #include <ccan/ogg_to_pcm/ogg_to_pcm.h>
41 #define FRAMES_PER_BUFFER (1024)
43 static PaStream *stream = NULL;
44 static int audio_paused = 0;
45 static int music_playing = 1;
46 static int sound_working = 0;
47 static int nomusic = 0;
48 static int sound_effects_on = 1;
49 static int sound_device = -1; /* default sound device for port audio. */
50 static int max_concurrent_sounds = 0;
51 static int max_sound_clips = 0;
53 /* Pause all audio output, output silence. */
54 void wwviaudio_pause_audio(void)
59 /* Resume playing audio previously paused. */
60 void wwviaudio_resume_audio(void)
65 /* Silence the music channel */
66 void wwviaudio_silence_music(void)
71 /* Resume volume on the music channel. */
72 void wwviaudio_resume_music(void)
77 void wwviaudio_toggle_music(void)
79 music_playing = !music_playing;
82 /* Silence the sound effects. */
83 void wwviaudio_silence_sound_effects(void)
88 /* Resume volume on the sound effects. */
89 void wwviaudio_resume_sound_effects(void)
94 void wwviaudio_toggle_sound_effects(void)
96 sound_effects_on = !sound_effects_on;
99 void wwviaudio_set_nomusic(void)
104 static struct sound_clip {
111 static struct sound_clip *audio_queue = NULL;
113 int wwviaudio_read_ogg_clip(int clipnum, char *filename)
116 char filebuf[PATH_MAX];
118 int samplesize, sample_rate;
122 if (clipnum >= max_sound_clips || clipnum < 0)
125 strncpy(filebuf, filename, PATH_MAX);
126 rc = stat(filebuf, &statbuf);
128 snprintf(filebuf, PATH_MAX, "%s", filename);
129 rc = stat(filebuf, &statbuf);
131 fprintf(stderr, "stat('%s') failed.\n", filebuf);
136 printf("Reading sound file: '%s'\n", filebuf);
137 printf("frames = %lld\n", sfinfo.frames);
138 printf("samplerate = %d\n", sfinfo.samplerate);
139 printf("channels = %d\n", sfinfo.channels);
140 printf("format = %d\n", sfinfo.format);
141 printf("sections = %d\n", sfinfo.sections);
142 printf("seekable = %d\n", sfinfo.seekable);
144 if (clip[clipnum].sample != NULL)
145 /* overwriting a previously read clip... */
146 free(clip[clipnum].sample);
148 rc = ogg_to_pcm(filebuf, &clip[clipnum].sample, &samplesize,
149 &sample_rate, &nchannels, &nframes);
150 if (clip[clipnum].sample == NULL) {
151 printf("Can't get memory for sound data for %"PRIu64
152 " frames in %s\n", nframes, filebuf);
157 fprintf(stderr, "Error: ogg_to_pcm('%s') failed.\n",
162 clip[clipnum].nsamples = (int) nframes;
163 if (clip[clipnum].nsamples < 0)
164 clip[clipnum].nsamples = 0;
171 /* This routine will be called by the PortAudio engine when audio is needed.
172 ** It may called at interrupt level on some machines so don't do anything
173 ** that could mess up the system like calling malloc() or free().
175 static int patestCallback(const void *inputBuffer, void *outputBuffer,
176 unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo,
177 PaStreamCallbackFlags statusFlags, __attribute__ ((unused)) void *userData )
179 int i, j, sample, count;
182 out = (float*) outputBuffer;
187 /* output silence when paused and
188 * don't advance any sound slot pointers
190 for (i = 0; i < framesPerBuffer; i++)
195 for (i = 0; i < framesPerBuffer; i++) {
198 for (j = 0; j < max_concurrent_sounds; j++) {
199 if (!audio_queue[j].active ||
200 audio_queue[j].sample == NULL)
202 sample = i + audio_queue[j].pos;
204 if (sample >= audio_queue[j].nsamples) {
205 audio_queue[j].active = 0;
208 if (j != WWVIAUDIO_MUSIC_SLOT && sound_effects_on)
209 output += (float) audio_queue[j].sample[sample] * 0.5 / (float) (INT16_MAX);
210 else if (j == WWVIAUDIO_MUSIC_SLOT && music_playing)
211 output += (float) audio_queue[j].sample[sample] / (float) (INT16_MAX);
213 *out++ = (float) output / 2.0;
215 for (i = 0; i < max_concurrent_sounds; i++) {
216 if (!audio_queue[i].active)
218 audio_queue[i].pos += framesPerBuffer;
219 if (audio_queue[i].pos >= audio_queue[i].nsamples)
220 audio_queue[i].active = 0;
222 return 0; /* we're never finished */
226 static void decode_paerror(PaError rc)
230 fprintf(stderr, "An error occurred while using the portaudio stream\n");
231 fprintf(stderr, "Error number: %d\n", rc);
232 fprintf(stderr, "Error message: %s\n", Pa_GetErrorText(rc));
235 static void wwviaudio_terminate_portaudio(PaError rc)
241 int wwviaudio_initialize_portaudio(int maximum_concurrent_sounds, int maximum_sound_clips)
243 PaStreamParameters outparams;
245 PaDeviceIndex device_count;
247 max_concurrent_sounds = maximum_concurrent_sounds;
248 max_sound_clips = maximum_sound_clips;
250 audio_queue = malloc(max_concurrent_sounds * sizeof(audio_queue[0]));
251 clip = malloc(max_sound_clips * sizeof(clip[0]));
252 if (audio_queue == NULL || clip == NULL)
255 memset(audio_queue, 0, sizeof(audio_queue[0]) * max_concurrent_sounds);
256 memset(clip, 0, sizeof(clip[0]) * max_sound_clips);
258 rc = Pa_Initialize();
262 device_count = Pa_GetDeviceCount();
263 printf("Portaudio reports %d sound devices.\n", device_count);
265 if (device_count == 0) {
266 printf("There will be no audio.\n");
272 outparams.device = Pa_GetDefaultOutputDevice(); /* default output device */
274 printf("Portaudio says the default device is: %d\n", outparams.device);
276 if (sound_device != -1) {
277 if (sound_device >= device_count)
278 fprintf(stderr, "wordwarvi: Invalid sound device "
279 "%d specified, ignoring.\n", sound_device);
281 outparams.device = sound_device;
282 printf("Using sound device %d\n", outparams.device);
285 if (outparams.device < 0 && device_count > 0) {
286 printf("Hmm, that's strange, portaudio says the default device is %d,\n"
287 " but there are %d devices\n",
288 outparams.device, device_count);
289 printf("I think we'll just skip sound for now.\n");
290 printf("You might try the '--sounddevice' option and see if that helps.\n");
295 outparams.channelCount = 1; /* mono output */
296 outparams.sampleFormat = paFloat32; /* 32 bit floating point output */
297 outparams.suggestedLatency =
298 Pa_GetDeviceInfo(outparams.device)->defaultLowOutputLatency;
299 outparams.hostApiSpecificStreamInfo = NULL;
301 rc = Pa_OpenStream(&stream,
303 &outparams, WWVIAUDIO_SAMPLE_RATE, FRAMES_PER_BUFFER,
304 paNoFlag, /* paClipOff, */ /* we won't output out of range samples so don't bother clipping them */
305 patestCallback, NULL /* cookie */);
308 if ((rc = Pa_StartStream(stream)) != paNoError)
312 wwviaudio_terminate_portaudio(rc);
317 void wwviaudio_stop_portaudio(void)
323 if ((rc = Pa_StopStream(stream)) != paNoError)
325 rc = Pa_CloseStream(stream);
327 wwviaudio_terminate_portaudio(rc);
331 max_concurrent_sounds = 0;
334 for (i = 0; i < max_sound_clips; i++) {
336 free(clip[i].sample);
345 static int wwviaudio_add_sound_to_slot(int which_sound, int which_slot)
352 if (nomusic && which_slot == WWVIAUDIO_MUSIC_SLOT)
355 if (which_slot != WWVIAUDIO_ANY_SLOT) {
356 if (audio_queue[which_slot].active)
357 audio_queue[which_slot].active = 0;
358 audio_queue[which_slot].pos = 0;
359 audio_queue[which_slot].nsamples = 0;
360 /* would like to put a memory barrier here. */
361 audio_queue[which_slot].sample = clip[which_sound].sample;
362 audio_queue[which_slot].nsamples = clip[which_sound].nsamples;
363 /* would like to put a memory barrier here. */
364 audio_queue[which_slot].active = 1;
367 for (i=1;i<max_concurrent_sounds;i++) {
368 if (audio_queue[i].active == 0) {
369 audio_queue[i].nsamples = clip[which_sound].nsamples;
370 audio_queue[i].pos = 0;
371 audio_queue[i].sample = clip[which_sound].sample;
372 audio_queue[i].active = 1;
376 return (i >= max_concurrent_sounds) ? -1 : i;
379 int wwviaudio_add_sound(int which_sound)
381 return wwviaudio_add_sound_to_slot(which_sound, WWVIAUDIO_ANY_SLOT);
384 int wwviaudio_play_music(int which_sound)
386 return wwviaudio_add_sound_to_slot(which_sound, WWVIAUDIO_MUSIC_SLOT);
390 void wwviaudio_add_sound_low_priority(int which_sound)
393 /* adds a sound if there are at least 5 empty sound slots. */
401 for (i = 1; i < max_concurrent_sounds; i++)
402 if (audio_queue[i].active == 0) {
405 if (empty_slots >= 5)
414 if (audio_queue[i].active == 0) {
415 audio_queue[i].nsamples = clip[which_sound].nsamples;
416 audio_queue[i].pos = 0;
417 audio_queue[i].sample = clip[which_sound].sample;
418 audio_queue[i].active = 1;
424 void wwviaudio_cancel_sound(int queue_entry)
428 audio_queue[queue_entry].active = 0;
431 void wwviaudio_cancel_music(void)
433 wwviaudio_cancel_sound(WWVIAUDIO_MUSIC_SLOT);
436 void wwviaudio_cancel_all_sounds(void)
441 for (i = 0; i < max_concurrent_sounds; i++)
442 audio_queue[i].active = 0;
445 int wwviaudio_set_sound_device(int device)
447 sound_device = device;
451 #else /* stubs only... */
453 int wwviaudio_initialize_portaudio() { return 0; }
454 void wwviaudio_stop_portaudio() { return; }
455 void wwviaudio_set_nomusic() { return; }
456 int wwviaudio_read_ogg_clip(int clipnum, char *filename) { return 0; }
458 void wwviaudio_pause_audio() { return; }
459 void wwviaudio_resume_audio() { return; }
461 void wwviaudio_silence_music() { return; }
462 void wwviaudio_resume_music() { return; }
463 void wwviaudio_silence_sound_effects() { return; }
464 void wwviaudio_resume_sound_effects() { return; }
465 void wwviaudio_toggle_sound_effects() { return; }
467 int wwviaudio_play_music(int which_sound) { return 0; }
468 void wwviaudio_cancel_music() { return; }
469 void wwviaudio_toggle_music() { return; }
470 int wwviaudio_add_sound(int which_sound) { return 0; }
471 void wwviaudio_add_sound_low_priority(int which_sound) { return; }
472 void wwviaudio_cancel_sound(int queue_entry) { return; }
473 void wwviaudio_cancel_all_sounds() { return; }
474 int wwviaudio_set_sound_device(int device) { return 0; }