aboutsummaryrefslogtreecommitdiffstats
path: root/examples/almultireverb.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/almultireverb.c')
-rw-r--r--examples/almultireverb.c96
1 files changed, 43 insertions, 53 deletions
diff --git a/examples/almultireverb.c b/examples/almultireverb.c
index a90b3368..eb874061 100644
--- a/examples/almultireverb.c
+++ b/examples/almultireverb.c
@@ -29,14 +29,16 @@
* listener.
*/
+
#include <assert.h>
+#include <inttypes.h>
+#include <limits.h>
#include <math.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
-#include "SDL_sound.h"
-#include "SDL_audio.h"
-#include "SDL_stdinc.h"
+#include "sndfile.h"
#include "AL/al.h"
#include "AL/alc.h"
@@ -151,68 +153,62 @@ static int LoadEffect(ALuint effect, const EFXEAXREVERBPROPERTIES *reverb)
*/
static ALuint LoadSound(const char *filename)
{
- Sound_Sample *sample;
ALenum err, format;
ALuint buffer;
- Uint32 slen;
-
- /* Open the audio file */
- sample = Sound_NewSampleFromFile(filename, NULL, 65536);
- if(!sample)
+ SNDFILE *sndfile;
+ SF_INFO sfinfo;
+ short *membuf;
+ sf_count_t num_frames;
+ ALsizei num_bytes;
+
+ /* Open the audio file and check that it's usable. */
+ sndfile = sf_open(filename, SFM_READ, &sfinfo);
+ if(!sndfile)
{
- fprintf(stderr, "Could not open audio in %s\n", filename);
+ fprintf(stderr, "Could not open audio in %s: %s\n", filename, sf_strerror(sndfile));
return 0;
}
-
- /* Get the sound format, and figure out the OpenAL format */
- if(sample->actual.channels == 1)
- {
- if(sample->actual.format == AUDIO_U8)
- format = AL_FORMAT_MONO8;
- else if(sample->actual.format == AUDIO_S16SYS)
- format = AL_FORMAT_MONO16;
- else
- {
- fprintf(stderr, "Unsupported sample format: 0x%04x\n", sample->actual.format);
- Sound_FreeSample(sample);
- return 0;
- }
- }
- else if(sample->actual.channels == 2)
+ if(sfinfo.frames < 1 || sfinfo.frames > (sf_count_t)(INT_MAX/sizeof(short))/sfinfo.channels)
{
- if(sample->actual.format == AUDIO_U8)
- format = AL_FORMAT_STEREO8;
- else if(sample->actual.format == AUDIO_S16SYS)
- format = AL_FORMAT_STEREO16;
- else
- {
- fprintf(stderr, "Unsupported sample format: 0x%04x\n", sample->actual.format);
- Sound_FreeSample(sample);
- return 0;
- }
+ fprintf(stderr, "Bad sample count in %s (%" PRId64 ")\n", filename, sfinfo.frames);
+ sf_close(sndfile);
+ return 0;
}
+
+ /* Get the sound format, and figure out the OpenAL format */
+ if(sfinfo.channels == 1)
+ format = AL_FORMAT_MONO16;
+ else if(sfinfo.channels == 2)
+ format = AL_FORMAT_STEREO16;
else
{
- fprintf(stderr, "Unsupported channel count: %d\n", sample->actual.channels);
- Sound_FreeSample(sample);
+ fprintf(stderr, "Unsupported channel count: %d\n", sfinfo.channels);
+ sf_close(sndfile);
return 0;
}
- /* Decode the whole audio stream to a buffer. */
- slen = Sound_DecodeAll(sample);
- if(!sample->buffer || slen == 0)
+ /* Decode the whole audio file to a buffer. */
+ membuf = malloc((size_t)(sfinfo.frames * sfinfo.channels) * sizeof(short));
+
+ num_frames = sf_readf_short(sndfile, membuf, sfinfo.frames);
+ if(num_frames < 1)
{
- fprintf(stderr, "Failed to read audio from %s\n", filename);
- Sound_FreeSample(sample);
+ free(membuf);
+ sf_close(sndfile);
+ fprintf(stderr, "Failed to read samples in %s (%" PRId64 ")\n", filename, num_frames);
return 0;
}
+ num_bytes = (ALsizei)(num_frames * sfinfo.channels) * (ALsizei)sizeof(short);
/* Buffer the audio data into a new buffer object, then free the data and
- * close the file. */
+ * close the file.
+ */
buffer = 0;
alGenBuffers(1, &buffer);
- alBufferData(buffer, format, sample->buffer, (ALsizei)slen, (ALsizei)sample->actual.rate);
- Sound_FreeSample(sample);
+ alBufferData(buffer, format, membuf, num_bytes, sfinfo.samplerate);
+
+ free(membuf);
+ sf_close(sndfile);
/* Check if an error occured, and clean up if so. */
err = alGetError();
@@ -561,15 +557,11 @@ int main(int argc, char **argv)
LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTFV, alGetAuxiliaryEffectSlotfv);
#undef LOAD_PROC
- /* Initialize SDL_sound. */
- Sound_Init();
-
/* Load the sound into a buffer. */
buffer = LoadSound(argv[0]);
if(!buffer)
{
CloseAL();
- Sound_Quit();
return 1;
}
@@ -585,7 +577,6 @@ int main(int argc, char **argv)
{
alDeleteEffects(2, effects);
alDeleteBuffers(1, &buffer);
- Sound_Quit();
CloseAL();
return 1;
}
@@ -684,14 +675,13 @@ int main(int argc, char **argv)
alGetSourcei(source, AL_SOURCE_STATE, &state);
} while(alGetError() == AL_NO_ERROR && state == AL_PLAYING && loops < MaxTransitions);
- /* All done. Delete resources, and close down SDL_sound and OpenAL. */
+ /* All done. Delete resources, and close down OpenAL. */
alDeleteSources(1, &source);
alDeleteAuxiliaryEffectSlots(2, slots);
alDeleteEffects(2, effects);
alDeleteFilters(1, &direct_filter);
alDeleteBuffers(1, &buffer);
- Sound_Quit();
CloseAL();
return 0;