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>
28 /*---------------------------------------------------------------------------*/
30 #define AUDIO_RATE 44100
45 static int audio_state = 0;
46 static float sound_vol = 1.0f;
47 static float music_vol = 1.0f;
49 static SDL_AudioSpec spec;
51 static struct voice *music = NULL;
52 static struct voice *queue = NULL;
53 static struct voice *voices = NULL;
54 static short *buffer = NULL;
56 /*---------------------------------------------------------------------------*/
59 int n = (int) (d) + (int) (s); \
60 if (n > 32767) (d) = 32767; \
61 else if (n < -32768) (d) = -32768; \
62 else (d) = (short) n; \
65 static int voice_step(struct voice *V, float volume, Uint8 *stream, int length)
67 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
73 short *obuf = (short *) stream;
74 char *ibuf = (char *) buffer;
76 int i, b = 0, n = 1, c = 0, r = 0;
78 /* Compute the total request size for the current stream. */
80 if (V->chan == 1) r = length / 2;
81 if (V->chan == 2) r = length ;
83 /* While data is coming in and data is still needed... */
85 while (n > 0 && r > 0)
87 /* Read audio from the stream. */
89 if ((n = (int) ov_read(&V->vf, ibuf, r, order, 2, 1, &b)) > 0)
94 for (i = 0; i < n / 2; i += 1)
96 short M = (short) (V->amp * volume * buffer[i]);
103 if (V->amp < 0.0) V->amp = 0.0;
104 if (V->amp > 1.0) V->amp = 1.0;
107 /* Mix stereo audio. */
110 for (i = 0; i < n / 2; i += 2)
112 short L = (short) (V->amp * volume * buffer[i + 0]);
113 short R = (short) (V->amp * volume * buffer[i + 1]);
115 MIX(obuf[c], L); c++;
116 MIX(obuf[c], R); c++;
120 if (V->amp < 0.0) V->amp = 0.0;
121 if (V->amp > 1.0) V->amp = 1.0;
128 /* We're at EOF. Loop or end the voice. */
132 ov_raw_seek(&V->vf, 0);
141 static struct voice *voice_init(const char *filename, float a)
146 /* Allocate and initialize a new voice structure. */
148 if ((V = (struct voice *) calloc(1, sizeof (struct voice))))
152 V->name = strdup(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, float t)
353 if ((queue = voice_init(filename, 0.0f)))
358 queue->damp = +1.0f / (AUDIO_RATE * t);
365 void audio_music_stop(void)
381 /*---------------------------------------------------------------------------*/
383 void audio_music_fade_out(float t)
387 if (music) music->damp = -1.0f / (AUDIO_RATE * t);
392 void audio_music_fade_in(float t)
396 if (music) music->damp = +1.0f / (AUDIO_RATE * t);
401 void audio_music_fade_to(float t, const char *filename)
405 if (strcmp(filename, music->name) != 0)
407 audio_music_fade_out(t);
408 audio_music_queue(filename, t);
413 * We're fading to the current track. Chances are,
414 * whatever track is still in the queue, we don't want to
424 audio_music_fade_in(t);
429 audio_music_play(filename);
430 audio_music_fade_in(t);
434 void audio_volume(int s, int m)
436 sound_vol = (float) s / 10.0f;
437 music_vol = (float) m / 10.0f;
440 /*---------------------------------------------------------------------------*/