]> git.ozlabs.org Git - ccan/blob - ccan/wwviaudio/wwviaudio.c
tal: allow notifiers on NULL.
[ccan] / ccan / wwviaudio / wwviaudio.c
1 /*
2     (C) Copyright 2007,2008, Stephen M. Cameron.
3
4     This file is part of wordwarvi.
5
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.
10
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.
15
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
19
20  */
21
22 #ifndef WWVIAUDIO_STUBS_ONLY
23
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <limits.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <inttypes.h>
33
34 #define WWVIAUDIO_DEFINE_GLOBALS
35 #include "wwviaudio.h"
36 #undef WWVIAUDIO_DEFINE_GLOBALS
37
38 #include <portaudio.h>
39 #include <ccan/ogg_to_pcm/ogg_to_pcm.h>
40
41 #define FRAMES_PER_BUFFER  (1024)
42
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;
52
53 /* Pause all audio output, output silence. */
54 void wwviaudio_pause_audio(void)
55 {
56         audio_paused = 1;
57 }
58
59 /* Resume playing audio previously paused. */
60 void wwviaudio_resume_audio(void)
61 {
62         audio_paused = 0;
63 }
64
65 /* Silence the music channel */
66 void wwviaudio_silence_music(void)
67 {
68         music_playing = 0;
69 }
70
71 /* Resume volume on the music channel. */
72 void wwviaudio_resume_music(void)
73 {
74         music_playing = 1;
75 }
76
77 void wwviaudio_toggle_music(void)
78 {
79         music_playing = !music_playing;
80 }
81
82 /* Silence the sound effects. */
83 void wwviaudio_silence_sound_effects(void)
84 {
85         sound_effects_on = 0;
86 }
87
88 /* Resume volume on the sound effects. */
89 void wwviaudio_resume_sound_effects(void)
90 {
91         sound_effects_on = 1;
92 }
93
94 void wwviaudio_toggle_sound_effects(void)
95 {
96         sound_effects_on = !sound_effects_on;
97 }
98
99 void wwviaudio_set_nomusic(void)
100 {
101         nomusic = 1;
102 }
103
104 static struct sound_clip {
105         int active;
106         int nsamples;
107         int pos;
108         int16_t *sample;
109 } *clip = NULL;
110
111 static struct sound_clip *audio_queue = NULL;
112
113 int wwviaudio_read_ogg_clip(int clipnum, char *filename)
114 {
115         uint64_t nframes;
116         char filebuf[PATH_MAX];
117         struct stat statbuf;
118         int samplesize, sample_rate;
119         int nchannels;
120         int rc;
121
122         if (clipnum >= max_sound_clips || clipnum < 0)
123                 return -1;
124
125         strncpy(filebuf, filename, PATH_MAX);
126         rc = stat(filebuf, &statbuf);
127         if (rc != 0) {
128                 snprintf(filebuf, PATH_MAX, "%s", filename);
129                 rc = stat(filebuf, &statbuf);
130                 if (rc != 0) {
131                         fprintf(stderr, "stat('%s') failed.\n", filebuf);
132                         return -1;
133                 }
134         }
135 /*
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);
143 */
144         if (clip[clipnum].sample != NULL)
145                 /* overwriting a previously read clip... */
146                 free(clip[clipnum].sample);
147
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);
153                 goto error;
154         }
155
156         if (rc != 0) {
157                 fprintf(stderr, "Error: ogg_to_pcm('%s') failed.\n",
158                         filebuf);
159                 goto error;
160         }
161
162         clip[clipnum].nsamples = (int) nframes;
163         if (clip[clipnum].nsamples < 0)
164                 clip[clipnum].nsamples = 0;
165
166         return 0;
167 error:
168         return -1;
169 }
170
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().
174 */
175 static int patestCallback(const void *inputBuffer, void *outputBuffer,
176         unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo,
177         PaStreamCallbackFlags statusFlags, __attribute__ ((unused)) void *userData )
178 {
179         int i, j, sample, count;
180         float *out = NULL;
181         float output;
182         out = (float*) outputBuffer;
183         output = 0.0;
184         count = 0;
185
186         if (audio_paused) {
187                 /* output silence when paused and
188                  * don't advance any sound slot pointers
189                  */
190                 for (i = 0; i < framesPerBuffer; i++)
191                         *out++ = (float) 0;
192                 return 0;
193         }
194
195         for (i = 0; i < framesPerBuffer; i++) {
196                 output = 0.0;
197                 count = 0;
198                 for (j = 0; j < max_concurrent_sounds; j++) {
199                         if (!audio_queue[j].active ||
200                                 audio_queue[j].sample == NULL)
201                                 continue;
202                         sample = i + audio_queue[j].pos;
203                         count++;
204                         if (sample >= audio_queue[j].nsamples) {
205                                 audio_queue[j].active = 0;
206                                 continue;
207                         }
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);
212                 }
213                 *out++ = (float) output / 2.0;
214         }
215         for (i = 0; i < max_concurrent_sounds; i++) {
216                 if (!audio_queue[i].active)
217                         continue;
218                 audio_queue[i].pos += framesPerBuffer;
219                 if (audio_queue[i].pos >= audio_queue[i].nsamples)
220                         audio_queue[i].active = 0;
221         }
222         return 0; /* we're never finished */
223 }
224
225
226 static void decode_paerror(PaError rc)
227 {
228         if (rc == paNoError)
229                 return;
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));
233 }
234
235 static void wwviaudio_terminate_portaudio(PaError rc)
236 {
237         Pa_Terminate();
238         decode_paerror(rc);
239 }
240
241 int wwviaudio_initialize_portaudio(int maximum_concurrent_sounds, int maximum_sound_clips)
242 {
243         PaStreamParameters outparams;
244         PaError rc;
245         PaDeviceIndex device_count;
246
247         max_concurrent_sounds = maximum_concurrent_sounds;
248         max_sound_clips = maximum_sound_clips;
249
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)
253                 return -1;
254
255         memset(audio_queue, 0, sizeof(audio_queue[0]) * max_concurrent_sounds);
256         memset(clip, 0, sizeof(clip[0]) * max_sound_clips);
257
258         rc = Pa_Initialize();
259         if (rc != paNoError)
260                 goto error;
261
262         device_count = Pa_GetDeviceCount();
263         printf("Portaudio reports %d sound devices.\n", device_count);
264
265         if (device_count == 0) {
266                 printf("There will be no audio.\n");
267                 goto error;
268                 rc = 0;
269         }
270         sound_working = 1;
271
272         outparams.device = Pa_GetDefaultOutputDevice();  /* default output device */
273
274         printf("Portaudio says the default device is: %d\n", outparams.device);
275
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);
280                 else
281                         outparams.device = sound_device;
282                 printf("Using sound device %d\n", outparams.device);
283         }
284
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");
291                 sound_working = 0;
292                 return -1;
293         }
294
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;
300
301         rc = Pa_OpenStream(&stream,
302                 NULL,         /* no input */
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 */);
306         if (rc != paNoError)
307                 goto error;
308         if ((rc = Pa_StartStream(stream)) != paNoError)
309                 goto error;
310         return rc;
311 error:
312         wwviaudio_terminate_portaudio(rc);
313         return rc;
314 }
315
316
317 void wwviaudio_stop_portaudio(void)
318 {
319         int i, rc;
320         
321         if (!sound_working)
322                 return;
323         if ((rc = Pa_StopStream(stream)) != paNoError)
324                 goto error;
325         rc = Pa_CloseStream(stream);
326 error:
327         wwviaudio_terminate_portaudio(rc);
328         if (audio_queue) {
329                 free(audio_queue);
330                 audio_queue = NULL;
331                 max_concurrent_sounds = 0;
332         }
333         if (clip) {
334                 for (i = 0; i < max_sound_clips; i++) {
335                         if (clip[i].sample)
336                                 free(clip[i].sample);
337                 }
338                 free(clip);
339                 clip = NULL;
340                 max_sound_clips = 0;
341         }
342         return;
343 }
344
345 static int wwviaudio_add_sound_to_slot(int which_sound, int which_slot)
346 {
347         int i;
348
349         if (!sound_working)
350                 return 0;
351
352         if (nomusic && which_slot == WWVIAUDIO_MUSIC_SLOT)
353                 return 0;
354
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;
365                 return which_slot;
366         }
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;
373                         break;
374                 }
375         }
376         return (i >= max_concurrent_sounds) ? -1 : i;
377 }
378
379 int wwviaudio_add_sound(int which_sound)
380 {
381         return wwviaudio_add_sound_to_slot(which_sound, WWVIAUDIO_ANY_SLOT);
382 }
383
384 int wwviaudio_play_music(int which_sound)
385 {
386         return wwviaudio_add_sound_to_slot(which_sound, WWVIAUDIO_MUSIC_SLOT);
387 }
388
389
390 void wwviaudio_add_sound_low_priority(int which_sound)
391 {
392
393         /* adds a sound if there are at least 5 empty sound slots. */
394         int i;
395         int empty_slots = 0;
396         int last_slot;
397
398         if (!sound_working)
399                 return;
400         last_slot = -1;
401         for (i = 1; i < max_concurrent_sounds; i++)
402                 if (audio_queue[i].active == 0) {
403                         last_slot = i;
404                         empty_slots++;
405                         if (empty_slots >= 5)
406                                 break;
407         }
408
409         if (empty_slots < 5)
410                 return;
411         
412         i = last_slot;
413
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;
419         }
420         return;
421 }
422
423
424 void wwviaudio_cancel_sound(int queue_entry)
425 {
426         if (!sound_working)
427                 return;
428         audio_queue[queue_entry].active = 0;
429 }
430
431 void wwviaudio_cancel_music(void)
432 {
433         wwviaudio_cancel_sound(WWVIAUDIO_MUSIC_SLOT);
434 }
435
436 void wwviaudio_cancel_all_sounds(void)
437 {
438         int i;
439         if (!sound_working)
440                 return;
441         for (i = 0; i < max_concurrent_sounds; i++)
442                 audio_queue[i].active = 0;
443 }
444
445 int wwviaudio_set_sound_device(int device)
446 {
447         sound_device = device;
448         return 0;
449 }
450
451 #else /* stubs only... */
452
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; }
457
458 void wwviaudio_pause_audio() { return; }
459 void wwviaudio_resume_audio() { return; }
460
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; }
466
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; }
475
476 #endif