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. */
41 #include "common/alhelpers.h"
43 #include "win_main_utf8.h"
47 #define M_PI (3.14159265358979323846)
50 static LPALCGETSTRINGISOFT alcGetStringiSOFT
;
51 static LPALCRESETDEVICESOFT alcResetDeviceSOFT
;
53 /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
54 * returns the new buffer ID.
56 static ALuint
LoadSound(const char *filename
)
63 sf_count_t num_frames
;
66 /* Open the audio file and check that it's usable. */
67 sndfile
= sf_open(filename
, SFM_READ
, &sfinfo
);
70 fprintf(stderr
, "Could not open audio in %s: %s\n", filename
, sf_strerror(sndfile
));
73 if(sfinfo
.frames
< 1 || sfinfo
.frames
> (sf_count_t
)(INT_MAX
/sizeof(short))/sfinfo
.channels
)
75 fprintf(stderr
, "Bad sample count in %s (%" PRId64
")\n", filename
, sfinfo
.frames
);
80 /* Get the sound format, and figure out the OpenAL format */
82 if(sfinfo
.channels
== 1)
83 format
= AL_FORMAT_MONO16
;
84 else if(sfinfo
.channels
== 2)
85 format
= AL_FORMAT_STEREO16
;
86 else if(sfinfo
.channels
== 3)
88 if(sf_command(sndfile
, SFC_WAVEX_GET_AMBISONIC
, NULL
, 0) == SF_AMBISONIC_B_FORMAT
)
89 format
= AL_FORMAT_BFORMAT2D_16
;
91 else if(sfinfo
.channels
== 4)
93 if(sf_command(sndfile
, SFC_WAVEX_GET_AMBISONIC
, NULL
, 0) == SF_AMBISONIC_B_FORMAT
)
94 format
= AL_FORMAT_BFORMAT3D_16
;
98 fprintf(stderr
, "Unsupported channel count: %d\n", sfinfo
.channels
);
103 /* Decode the whole audio file to a buffer. */
104 membuf
= malloc((size_t)(sfinfo
.frames
* sfinfo
.channels
) * sizeof(short));
106 num_frames
= sf_readf_short(sndfile
, membuf
, sfinfo
.frames
);
111 fprintf(stderr
, "Failed to read samples in %s (%" PRId64
")\n", filename
, num_frames
);
114 num_bytes
= (ALsizei
)(num_frames
* sfinfo
.channels
) * (ALsizei
)sizeof(short);
116 /* Buffer the audio data into a new buffer object, then free the data and
120 alGenBuffers(1, &buffer
);
121 alBufferData(buffer
, format
, membuf
, num_bytes
, sfinfo
.samplerate
);
126 /* Check if an error occurred, and clean up if so. */
128 if(err
!= AL_NO_ERROR
)
130 fprintf(stderr
, "OpenAL Error: %s\n", alGetString(err
));
131 if(buffer
&& alIsBuffer(buffer
))
132 alDeleteBuffers(1, &buffer
);
140 int main(int argc
, char **argv
)
144 ALboolean has_angle_ext
;
145 ALuint source
, buffer
;
146 const char *soundname
;
147 const char *hrtfname
;
153 /* Print out usage if no arguments were specified */
156 fprintf(stderr
, "Usage: %s [-device <name>] [-hrtf <name>] <soundfile>\n", argv
[0]);
160 /* Initialize OpenAL, and check for HRTF support. */
162 if(InitAL(&argv
, &argc
) != 0)
165 context
= alcGetCurrentContext();
166 device
= alcGetContextsDevice(context
);
167 if(!alcIsExtensionPresent(device
, "ALC_SOFT_HRTF"))
169 fprintf(stderr
, "Error: ALC_SOFT_HRTF not supported\n");
174 /* Define a macro to help load the function pointers. */
175 #define LOAD_PROC(d, T, x) ((x) = FUNCTION_CAST(T, alcGetProcAddress((d), #x)))
176 LOAD_PROC(device
, LPALCGETSTRINGISOFT
, alcGetStringiSOFT
);
177 LOAD_PROC(device
, LPALCRESETDEVICESOFT
, alcResetDeviceSOFT
);
180 /* Check for the AL_EXT_STEREO_ANGLES extension to be able to also rotate
183 has_angle_ext
= alIsExtensionPresent("AL_EXT_STEREO_ANGLES");
184 printf("AL_EXT_STEREO_ANGLES %sfound\n", has_angle_ext
?"":"not ");
186 /* Check for user-preferred HRTF */
187 if(strcmp(argv
[0], "-hrtf") == 0)
198 /* Enumerate available HRTFs, and reset the device using one. */
199 alcGetIntegerv(device
, ALC_NUM_HRTF_SPECIFIERS_SOFT
, 1, &num_hrtf
);
201 printf("No HRTFs found\n");
208 printf("Available HRTFs:\n");
209 for(i
= 0;i
< num_hrtf
;i
++)
211 const ALCchar
*name
= alcGetStringiSOFT(device
, ALC_HRTF_SPECIFIER_SOFT
, i
);
212 printf(" %d: %s\n", i
, name
);
214 /* Check if this is the HRTF the user requested. */
215 if(hrtfname
&& strcmp(name
, hrtfname
) == 0)
220 attr
[i
++] = ALC_HRTF_SOFT
;
221 attr
[i
++] = ALC_TRUE
;
225 printf("HRTF \"%s\" not found\n", hrtfname
);
226 printf("Using default HRTF...\n");
230 printf("Selecting HRTF %d...\n", index
);
231 attr
[i
++] = ALC_HRTF_ID_SOFT
;
236 if(!alcResetDeviceSOFT(device
, attr
))
237 printf("Failed to reset device: %s\n", alcGetString(device
, alcGetError(device
)));
240 /* Check if HRTF is enabled, and show which is being used. */
241 alcGetIntegerv(device
, ALC_HRTF_SOFT
, 1, &hrtf_state
);
243 printf("HRTF not enabled!\n");
246 const ALchar
*name
= alcGetString(device
, ALC_HRTF_SPECIFIER_SOFT
);
247 printf("HRTF enabled, using %s\n", name
);
251 /* Load the sound into a buffer. */
252 buffer
= LoadSound(soundname
);
259 /* Create the source to play the sound with. */
261 alGenSources(1, &source
);
262 alSourcei(source
, AL_SOURCE_RELATIVE
, AL_TRUE
);
263 alSource3f(source
, AL_POSITION
, 0.0f
, 0.0f
, -1.0f
);
264 alSourcei(source
, AL_BUFFER
, (ALint
)buffer
);
265 assert(alGetError()==AL_NO_ERROR
&& "Failed to setup sound source");
267 /* Play the sound until it finishes. */
269 alSourcePlay(source
);
271 al_nssleep(10000000);
273 alcSuspendContext(context
);
275 /* Rotate the source around the listener by about 1/4 cycle per second,
276 * and keep it within -pi...+pi.
278 angle
+= 0.01 * M_PI
* 0.5;
282 /* This only rotates mono sounds. */
283 alSource3f(source
, AL_POSITION
, (ALfloat
)sin(angle
), 0.0f
, -(ALfloat
)cos(angle
));
287 /* This rotates stereo sounds with the AL_EXT_STEREO_ANGLES
288 * extension. Angles are specified counter-clockwise in radians.
290 ALfloat angles
[2] = { (ALfloat
)(M_PI
/6.0 - angle
), (ALfloat
)(-M_PI
/6.0 - angle
) };
291 alSourcefv(source
, AL_STEREO_ANGLES
, angles
);
293 alcProcessContext(context
);
295 alGetSourcei(source
, AL_SOURCE_STATE
, &state
);
296 } while(alGetError() == AL_NO_ERROR
&& state
== AL_PLAYING
);
298 /* All done. Delete resources, and close down OpenAL. */
299 alDeleteSources(1, &source
);
300 alDeleteBuffers(1, &buffer
);