aboutsummaryrefslogtreecommitdiffstats
path: root/examples/alhrtf.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/alhrtf.c
parent586bc94d513125a63e61922ac0805a71c6ef1950 (diff)
Convert the examples from SDL_sound to libsndfile
Diffstat (limited to 'examples/alhrtf.c')
-rw-r--r--examples/alhrtf.c95
1 files changed, 42 insertions, 53 deletions
diff --git a/examples/alhrtf.c b/examples/alhrtf.c
index 2be28a91..bea87f56 100644
--- a/examples/alhrtf.c
+++ b/examples/alhrtf.c
@@ -25,13 +25,14 @@
/* This file contains an example for selecting an HRTF. */
#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"
@@ -52,68 +53,62 @@ static LPALCRESETDEVICESOFT alcResetDeviceSOFT;
*/
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();
@@ -240,14 +235,10 @@ int main(int argc, char **argv)
}
fflush(stdout);
- /* Initialize SDL_sound. */
- Sound_Init();
-
/* Load the sound into a buffer. */
buffer = LoadSound(soundname);
if(!buffer)
{
- Sound_Quit();
CloseAL();
return 1;
}
@@ -291,11 +282,9 @@ 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);
alDeleteBuffers(1, &buffer);
-
- Sound_Quit();
CloseAL();
return 0;