aboutsummaryrefslogtreecommitdiffstats
path: root/examples/alreverb.c
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2020-03-24 15:46:47 -0700
committerChris Robinson <[email protected]>2020-03-24 15:46:47 -0700
commitcae78e79e81afbc47a9a5802c4cfcc62dbc07f8e (patch)
tree521ff02314133b3508bb3701a4bd898b376e4834 /examples/alreverb.c
parent586bc94d513125a63e61922ac0805a71c6ef1950 (diff)
Convert the examples from SDL_sound to libsndfile
Diffstat (limited to 'examples/alreverb.c')
-rw-r--r--examples/alreverb.c97
1 files changed, 43 insertions, 54 deletions
diff --git a/examples/alreverb.c b/examples/alreverb.c
index d789dffe..1f30a450 100644
--- a/examples/alreverb.c
+++ b/examples/alreverb.c
@@ -24,12 +24,13 @@
/* This file contains an example for applying reverb to a sound. */
-#include <stdio.h>
#include <assert.h>
+#include <inttypes.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
-#include "SDL_sound.h"
-#include "SDL_audio.h"
-#include "SDL_stdinc.h"
+#include "sndfile.h"
#include "AL/al.h"
#include "AL/alc.h"
@@ -149,68 +150,62 @@ static ALuint LoadEffect(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();
@@ -278,15 +273,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;
}
@@ -295,7 +286,6 @@ int main(int argc, char **argv)
if(!effect)
{
alDeleteBuffers(1, &buffer);
- Sound_Quit();
CloseAL();
return 1;
}
@@ -330,13 +320,12 @@ int main(int argc, char **argv)
alGetSourcei(source, AL_SOURCE_STATE, &state);
} while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
- /* All done. Delete resources, and close down SDL_sound and OpenAL. */
+ /* All done. Delete resources, and close down OpenAL. */
alDeleteSources(1, &source);
alDeleteAuxiliaryEffectSlots(1, &slot);
alDeleteEffects(1, &effect);
alDeleteBuffers(1, &buffer);
- Sound_Quit();
CloseAL();
return 0;