4 * Copyright (c) 2015 by Chris Robinson <chris.kcat@gmail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 /* This file contains an example for selecting an HRTF. */
31 #include <SDL_sound.h>
37 #include "common/alhelpers.h"
41 #define M_PI (3.14159265358979323846)
44 static LPALCGETSTRINGISOFT alcGetStringiSOFT
;
45 static LPALCRESETDEVICESOFT alcResetDeviceSOFT
;
47 /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
48 * returns the new buffer ID.
50 static ALuint
LoadSound(const char *filename
)
57 /* Open the audio file */
58 sample
= Sound_NewSampleFromFile(filename
, NULL
, 65536);
61 fprintf(stderr
, "Could not open audio in %s\n", filename
);
65 /* Get the sound format, and figure out the OpenAL format */
66 if(sample
->actual
.channels
== 1)
68 if(sample
->actual
.format
== AUDIO_U8
)
69 format
= AL_FORMAT_MONO8
;
70 else if(sample
->actual
.format
== AUDIO_S16SYS
)
71 format
= AL_FORMAT_MONO16
;
74 fprintf(stderr
, "Unsupported sample format: 0x%04x\n", sample
->actual
.format
);
75 Sound_FreeSample(sample
);
79 else if(sample
->actual
.channels
== 2)
81 if(sample
->actual
.format
== AUDIO_U8
)
82 format
= AL_FORMAT_STEREO8
;
83 else if(sample
->actual
.format
== AUDIO_S16SYS
)
84 format
= AL_FORMAT_STEREO16
;
87 fprintf(stderr
, "Unsupported sample format: 0x%04x\n", sample
->actual
.format
);
88 Sound_FreeSample(sample
);
94 fprintf(stderr
, "Unsupported channel count: %d\n", sample
->actual
.channels
);
95 Sound_FreeSample(sample
);
99 /* Decode the whole audio stream to a buffer. */
100 slen
= Sound_DecodeAll(sample
);
101 if(!sample
->buffer
|| slen
== 0)
103 fprintf(stderr
, "Failed to read audio from %s\n", filename
);
104 Sound_FreeSample(sample
);
108 /* Buffer the audio data into a new buffer object, then free the data and
111 alGenBuffers(1, &buffer
);
112 alBufferData(buffer
, format
, sample
->buffer
, slen
, sample
->actual
.rate
);
113 Sound_FreeSample(sample
);
115 /* Check if an error occured, and clean up if so. */
117 if(err
!= AL_NO_ERROR
)
119 fprintf(stderr
, "OpenAL Error: %s\n", alGetString(err
));
120 if(buffer
&& alIsBuffer(buffer
))
121 alDeleteBuffers(1, &buffer
);
129 int main(int argc
, char **argv
)
132 ALboolean has_angle_ext
;
133 ALuint source
, buffer
;
134 const char *soundname
;
135 const char *hrtfname
;
141 /* Print out usage if no arguments were specified */
144 fprintf(stderr
, "Usage: %s [-device <name>] [-hrtf <name>] <soundfile>\n", argv
[0]);
148 /* Initialize OpenAL, and check for HRTF support. */
150 if(InitAL(&argv
, &argc
) != 0)
153 device
= alcGetContextsDevice(alcGetCurrentContext());
154 if(!alcIsExtensionPresent(device
, "ALC_SOFT_HRTF"))
156 fprintf(stderr
, "Error: ALC_SOFT_HRTF not supported\n");
161 /* Define a macro to help load the function pointers. */
162 #define LOAD_PROC(d, x) ((x) = alcGetProcAddress((d), #x))
163 LOAD_PROC(device
, alcGetStringiSOFT
);
164 LOAD_PROC(device
, alcResetDeviceSOFT
);
167 /* Check for the AL_EXT_STEREO_ANGLES extension to be able to also rotate
170 has_angle_ext
= alIsExtensionPresent("AL_EXT_STEREO_ANGLES");
171 printf("AL_EXT_STEREO_ANGLES%s found\n", has_angle_ext
?"":" not");
173 /* Check for user-preferred HRTF */
174 if(strcmp(argv
[0], "-hrtf") == 0)
185 /* Enumerate available HRTFs, and reset the device using one. */
186 alcGetIntegerv(device
, ALC_NUM_HRTF_SPECIFIERS_SOFT
, 1, &num_hrtf
);
188 printf("No HRTFs found\n");
195 printf("Available HRTFs:\n");
196 for(i
= 0;i
< num_hrtf
;i
++)
198 const ALCchar
*name
= alcGetStringiSOFT(device
, ALC_HRTF_SPECIFIER_SOFT
, i
);
199 printf(" %d: %s\n", i
, name
);
201 /* Check if this is the HRTF the user requested. */
202 if(hrtfname
&& strcmp(name
, hrtfname
) == 0)
207 attr
[i
++] = ALC_HRTF_SOFT
;
208 attr
[i
++] = ALC_TRUE
;
212 printf("HRTF \"%s\" not found\n", hrtfname
);
213 printf("Using default HRTF...\n");
217 printf("Selecting HRTF %d...\n", index
);
218 attr
[i
++] = ALC_HRTF_ID_SOFT
;
223 if(!alcResetDeviceSOFT(device
, attr
))
224 printf("Failed to reset device: %s\n", alcGetString(device
, alcGetError(device
)));
227 /* Check if HRTF is enabled, and show which is being used. */
228 alcGetIntegerv(device
, ALC_HRTF_SOFT
, 1, &hrtf_state
);
230 printf("HRTF not enabled!\n");
233 const ALchar
*name
= alcGetString(device
, ALC_HRTF_SPECIFIER_SOFT
);
234 printf("HRTF enabled, using %s\n", name
);
238 /* Initialize SDL_sound. */
241 /* Load the sound into a buffer. */
242 buffer
= LoadSound(soundname
);
250 /* Create the source to play the sound with. */
252 alGenSources(1, &source
);
253 alSourcei(source
, AL_SOURCE_RELATIVE
, AL_TRUE
);
254 alSource3f(source
, AL_POSITION
, 0.0f
, 0.0f
, -1.0f
);
255 alSourcei(source
, AL_BUFFER
, buffer
);
256 assert(alGetError()==AL_NO_ERROR
&& "Failed to setup sound source");
258 /* Play the sound until it finishes. */
260 alSourcePlay(source
);
262 al_nssleep(10000000);
264 /* Rotate the source around the listener by about 1/4 cycle per second,
265 * and keep it within -pi...+pi.
267 angle
+= 0.01 * M_PI
* 0.5;
271 /* This only rotates mono sounds. */
272 alSource3f(source
, AL_POSITION
, (ALfloat
)sin(angle
), 0.0f
, -(ALfloat
)cos(angle
));
276 /* This rotates stereo sounds with the AL_EXT_STEREO_ANGLES
277 * extension. Angles are specified counter-clockwise in radians.
279 ALfloat angles
[2] = { (ALfloat
)(M_PI
/6.0 - angle
), (ALfloat
)(-M_PI
/6.0 - angle
) };
280 alSourcefv(source
, AL_STEREO_ANGLES
, angles
);
283 alGetSourcei(source
, AL_SOURCE_STATE
, &state
);
284 } while(alGetError() == AL_NO_ERROR
&& state
== AL_PLAYING
);
286 /* All done. Delete resources, and close down SDL_sound and OpenAL. */
287 alDeleteSources(1, &source
);
288 alDeleteBuffers(1, &buffer
);