2 * Copyright (C) 2003 Robert Kooima
4 * NEVERBALL is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
17 #include <vorbis/codec.h>
18 #include <vorbis/vorbisfile.h>
26 /*---------------------------------------------------------------------------*/
28 #define AUDIO_RATE 44100
43 static int audio_state = 0;
44 static float sound_vol = 1.0f;
45 static float music_vol = 1.0f;
47 static SDL_AudioSpec spec;
49 static struct voice *music = NULL;
50 static struct voice *queue = NULL;
51 static struct voice *voices = NULL;
52 static short *buffer = NULL;
54 /*---------------------------------------------------------------------------*/
57 int n = (int) (d) + (int) (s); \
58 if (n > 32767) (d) = 32767; \
59 else if (n < -32768) (d) = -32768; \
60 else (d) = (short) n; \
63 static int voice_step(struct voice *V, float volume, Uint8 *stream, int length)
65 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
71 short *obuf = (short *) stream;
72 char *ibuf = (char *) buffer;
74 int i, b = 0, n = 1, c = 0, r = 0;
76 /* Compute the total request size for the current stream. */
78 if (V->chan == 1) r = length / 2;
79 if (V->chan == 2) r = length ;
81 /* While data is coming in and data is still needed... */
83 while (n > 0 && r > 0)
85 /* Read audio from the stream. */
87 if ((n = (int) ov_read(&V->vf, ibuf, r, order, 2, 1, &b)) > 0)
92 for (i = 0; i < n / 2; i += 1)
94 short M = (short) (V->amp * volume * buffer[i]);
101 if (V->amp < 0.0) V->amp = 0.0;
102 if (V->amp > 1.0) V->amp = 1.0;
105 /* Mix stereo audio. */
108 for (i = 0; i < n / 2; i += 2)
110 short L = (short) (V->amp * volume * buffer[i + 0]);
111 short R = (short) (V->amp * volume * buffer[i + 1]);
113 MIX(obuf[c], L); c++;
114 MIX(obuf[c], R); c++;
118 if (V->amp < 0.0) V->amp = 0.0;
119 if (V->amp > 1.0) V->amp = 1.0;
126 /* We're at EOF. Loop or end the voice. */
130 ov_raw_seek(&V->vf, 0);
139 static struct voice *voice_init(const char *filename, float a)
144 /* Allocate and initialize a new voice structure. */
146 if ((V = (struct voice *) calloc(1, sizeof (struct voice))))
150 V->name = (char *) malloc(strlen(filename) + 1);
152 strcpy(V->name, filename);
154 /* Attempt to open the named Ogg stream. */
156 if ((fp = fopen(config_data(filename), FMODE_RB)))
158 if (ov_open(fp, &V->vf, NULL, 0) == 0)
160 vorbis_info *info = ov_info(&V->vf, -1);
162 /* On success, configure the voice. */
166 V->chan = info->channels;
170 if (V->amp > 1.0) V->amp = 1.0;
171 if (V->amp < 0.0) V->amp = 0.0;
173 /* The file will be closed when the Ogg is cleared. */
181 static void voice_free(struct voice *V)
189 /*---------------------------------------------------------------------------*/
191 static void audio_step(void *data, Uint8 *stream, int length)
193 struct voice *V = voices;
194 struct voice *P = NULL;
196 /* Zero the output buffer. */
198 memset(stream, 0, length);
200 /* Mix the background music. */
204 voice_step(music, music_vol, stream, length);
206 /* If the track has faded out, move to a queued track. */
208 if (music->amp <= 0.0f && music->damp < 0.0f && queue)
216 /* Iterate over all active voices. */
220 /* Mix this voice. */
222 if (V->play && voice_step(V, sound_vol, stream, length))
224 /* Delete a finished voice... */
229 V = P->next = V->next;
231 V = voices = V->next;
237 /* ... or continue to the next. */
245 /*---------------------------------------------------------------------------*/
247 void audio_init(void)
251 /* Configure the audio. */
253 spec.format = AUDIO_S16SYS;
254 spec.channels = AUDIO_CHAN;
255 spec.samples = config_get_d(CONFIG_AUDIO_BUFF);
256 spec.freq = AUDIO_RATE;
257 spec.callback = audio_step;
259 /* Allocate an input buffer. */
261 if ((buffer = (short *) malloc(spec.samples * 4)))
263 /* Start the audio thread. */
265 if (SDL_OpenAudio(&spec, NULL) == 0)
270 else fprintf(stderr, "%s\n", SDL_GetError());
273 /* Set the initial volumes. */
275 audio_volume(config_get_d(CONFIG_SOUND_VOLUME),
276 config_get_d(CONFIG_MUSIC_VOLUME));
279 void audio_free(void)
281 /* Halt the audio thread. */
285 /* Release the input buffer. */
289 /* Ogg streams and voice structure remain open to allow quality setting. */
292 void audio_play(const char *filename, float a)
298 /* If we're already playing this sound, preempt the running copy. */
300 for (V = voices; V; V = V->next)
301 if (strcmp(V->name, filename) == 0)
303 ov_raw_seek(&V->vf, 0);
307 if (V->amp > 1.0) V->amp = 1.0;
308 if (V->amp < 0.0) V->amp = 0.0;
313 /* Create a new voice structure. */
315 V = voice_init(filename, a);
317 /* Add it to the list of sounding voices. */
328 /*---------------------------------------------------------------------------*/
330 void audio_music_play(const char *filename)
338 if ((music = voice_init(filename, 0.0f)))
347 void audio_music_queue(const char *filename)
353 if ((queue = voice_init(filename, 0.0f)))
362 void audio_music_stop(void)
378 /*---------------------------------------------------------------------------*/
380 void audio_music_fade_out(float t)
384 if (music) music->damp = -1.0f / (AUDIO_RATE * t);
389 void audio_music_fade_in(float t)
393 if (music) music->damp = +1.0f / (AUDIO_RATE * t);
398 void audio_music_fade_to(float t, const char *filename)
402 if (strcmp(filename, music->name))
404 audio_music_queue(filename);
405 audio_music_fade_out(t);
407 if (queue) queue->damp = +1.0f / (AUDIO_RATE * t);
409 else audio_music_fade_in(t);
413 audio_music_play(filename);
414 audio_music_fade_in(t);
418 void audio_volume(int s, int m)
420 sound_vol = (float) s / 10.0f;
421 music_vol = (float) m / 10.0f;
424 /*---------------------------------------------------------------------------*/