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>
27 /*---------------------------------------------------------------------------*/
29 #define AUDIO_RATE 44100
44 static int audio_state = 0;
45 static float sound_vol = 1.0f;
46 static float music_vol = 1.0f;
48 static SDL_AudioSpec spec;
50 static struct voice *music = NULL;
51 static struct voice *queue = NULL;
52 static struct voice *voices = NULL;
53 static short *buffer = NULL;
55 /*---------------------------------------------------------------------------*/
58 int n = (int) (d) + (int) (s); \
59 if (n > 32767) (d) = 32767; \
60 else if (n < -32768) (d) = -32768; \
61 else (d) = (short) n; \
64 static int voice_step(struct voice *V, float volume, Uint8 *stream, int length)
66 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
72 short *obuf = (short *) stream;
73 char *ibuf = (char *) buffer;
75 int i, b = 0, n = 1, c = 0, r = 0;
77 /* Compute the total request size for the current stream. */
79 if (V->chan == 1) r = length / 2;
80 if (V->chan == 2) r = length ;
82 /* While data is coming in and data is still needed... */
84 while (n > 0 && r > 0)
86 /* Read audio from the stream. */
88 if ((n = (int) ov_read(&V->vf, ibuf, r, order, 2, 1, &b)) > 0)
93 for (i = 0; i < n / 2; i += 1)
95 short M = (short) (V->amp * volume * buffer[i]);
102 if (V->amp < 0.0) V->amp = 0.0;
103 if (V->amp > 1.0) V->amp = 1.0;
106 /* Mix stereo audio. */
109 for (i = 0; i < n / 2; i += 2)
111 short L = (short) (V->amp * volume * buffer[i + 0]);
112 short R = (short) (V->amp * volume * buffer[i + 1]);
114 MIX(obuf[c], L); c++;
115 MIX(obuf[c], R); c++;
119 if (V->amp < 0.0) V->amp = 0.0;
120 if (V->amp > 1.0) V->amp = 1.0;
127 /* We're at EOF. Loop or end the voice. */
131 ov_raw_seek(&V->vf, 0);
140 static struct voice *voice_init(const char *filename, float a)
145 /* Allocate and initialize a new voice structure. */
147 if ((V = (struct voice *) calloc(1, sizeof (struct voice))))
151 V->name = (char *) malloc(strlen(filename) + 1);
153 strcpy(V->name, filename);
155 /* Attempt to open the named Ogg stream. */
157 if ((fp = fopen(config_data(filename), FMODE_RB)))
159 if (ov_open(fp, &V->vf, NULL, 0) == 0)
161 vorbis_info *info = ov_info(&V->vf, -1);
163 /* On success, configure the voice. */
167 V->chan = info->channels;
171 if (V->amp > 1.0) V->amp = 1.0;
172 if (V->amp < 0.0) V->amp = 0.0;
174 /* The file will be closed when the Ogg is cleared. */
182 static void voice_free(struct voice *V)
190 /*---------------------------------------------------------------------------*/
192 static void audio_step(void *data, Uint8 *stream, int length)
194 struct voice *V = voices;
195 struct voice *P = NULL;
197 /* Zero the output buffer. */
199 memset(stream, 0, length);
201 /* Mix the background music. */
205 voice_step(music, music_vol, stream, length);
207 /* If the track has faded out, move to a queued track. */
209 if (music->amp <= 0.0f && music->damp < 0.0f && queue)
217 /* Iterate over all active voices. */
221 /* Mix this voice. */
223 if (V->play && voice_step(V, sound_vol, stream, length))
225 /* Delete a finished voice... */
230 V = P->next = V->next;
232 V = voices = V->next;
238 /* ... or continue to the next. */
246 /*---------------------------------------------------------------------------*/
248 void audio_init(void)
252 /* Configure the audio. */
254 spec.format = AUDIO_S16SYS;
255 spec.channels = AUDIO_CHAN;
256 spec.samples = config_get_d(CONFIG_AUDIO_BUFF);
257 spec.freq = AUDIO_RATE;
258 spec.callback = audio_step;
260 /* Allocate an input buffer. */
262 if ((buffer = (short *) malloc(spec.samples * 4)))
264 /* Start the audio thread. */
266 if (SDL_OpenAudio(&spec, NULL) == 0)
271 else fprintf(stderr, "%s\n", SDL_GetError());
274 /* Set the initial volumes. */
276 audio_volume(config_get_d(CONFIG_SOUND_VOLUME),
277 config_get_d(CONFIG_MUSIC_VOLUME));
280 void audio_free(void)
282 /* Halt the audio thread. */
286 /* Release the input buffer. */
290 /* Ogg streams and voice structure remain open to allow quality setting. */
293 void audio_play(const char *filename, float a)
299 /* If we're already playing this sound, preempt the running copy. */
301 for (V = voices; V; V = V->next)
302 if (strcmp(V->name, filename) == 0)
304 ov_raw_seek(&V->vf, 0);
308 if (V->amp > 1.0) V->amp = 1.0;
309 if (V->amp < 0.0) V->amp = 0.0;
314 /* Create a new voice structure. */
316 V = voice_init(filename, a);
318 /* Add it to the list of sounding voices. */
329 /*---------------------------------------------------------------------------*/
331 void audio_music_play(const char *filename)
339 if ((music = voice_init(filename, 0.0f)))
348 void audio_music_queue(const char *filename, float t)
354 if ((queue = voice_init(filename, 0.0f)))
359 queue->damp = +1.0f / (AUDIO_RATE * t);
366 void audio_music_stop(void)
382 /*---------------------------------------------------------------------------*/
384 void audio_music_fade_out(float t)
388 if (music) music->damp = -1.0f / (AUDIO_RATE * t);
393 void audio_music_fade_in(float t)
397 if (music) music->damp = +1.0f / (AUDIO_RATE * t);
402 void audio_music_fade_to(float t, const char *filename)
406 if (strcmp(filename, music->name) != 0)
408 audio_music_fade_out(t);
409 audio_music_queue(filename, t);
411 else audio_music_fade_in(t);
415 audio_music_play(filename);
416 audio_music_fade_in(t);
420 void audio_volume(int s, int m)
422 sound_vol = (float) s / 10.0f;
423 music_vol = (float) m / 10.0f;
426 /*---------------------------------------------------------------------------*/