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. */
35 #include "common/alhelpers.h"
36 #include "common/sdl_sound.h"
40 #define M_PI (3.14159265358979323846)
43 static LPALCGETSTRINGISOFT alcGetStringiSOFT
;
44 static LPALCRESETDEVICESOFT alcResetDeviceSOFT
;
46 /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
47 * returns the new buffer ID. */
48 static ALuint
LoadSound(const char *filename
)
50 ALenum err
, format
, type
, channels
;
56 /* Open the audio file */
57 sound
= openAudioFile(filename
, 1000);
60 fprintf(stderr
, "Could not open audio in %s\n", filename
);
61 closeAudioFile(sound
);
65 /* Get the sound format, and figure out the OpenAL format */
66 if(getAudioInfo(sound
, &rate
, &channels
, &type
) != 0)
68 fprintf(stderr
, "Error getting audio info for %s\n", filename
);
69 closeAudioFile(sound
);
73 format
= GetFormat(channels
, type
, NULL
);
76 fprintf(stderr
, "Unsupported format (%s, %s) for %s\n",
77 ChannelsName(channels
), TypeName(type
), filename
);
78 closeAudioFile(sound
);
82 /* Decode the whole audio stream to a buffer. */
83 data
= decodeAudioStream(sound
, &datalen
);
86 fprintf(stderr
, "Failed to read audio from %s\n", filename
);
87 closeAudioFile(sound
);
91 /* Buffer the audio data into a new buffer object, then free the data and
94 alGenBuffers(1, &buffer
);
95 alBufferData(buffer
, format
, data
, datalen
, rate
);
97 closeAudioFile(sound
);
99 /* Check if an error occured, and clean up if so. */
101 if(err
!= AL_NO_ERROR
)
103 fprintf(stderr
, "OpenAL Error: %s\n", alGetString(err
));
104 if(buffer
&& alIsBuffer(buffer
))
105 alDeleteBuffers(1, &buffer
);
113 int main(int argc
, char **argv
)
116 ALuint source
, buffer
;
117 const char *soundname
;
118 const char *hrtfname
;
124 /* Print out usage if no file was specified */
125 if(argc
< 2 || (strcmp(argv
[1], "-hrtf") == 0 && argc
< 4))
127 fprintf(stderr
, "Usage: %s [-hrtf <name>] <soundfile>\n", argv
[0]);
131 /* Initialize OpenAL with the default device, and check for HRTF support. */
135 if(strcmp(argv
[1], "-hrtf") == 0)
146 device
= alcGetContextsDevice(alcGetCurrentContext());
147 if(!alcIsExtensionPresent(device
, "ALC_SOFT_HRTF"))
149 fprintf(stderr
, "Error: ALC_SOFT_HRTF not supported\n");
154 /* Define a macro to help load the function pointers. */
155 #define LOAD_PROC(d, x) ((x) = alcGetProcAddress((d), #x))
156 LOAD_PROC(device
, alcGetStringiSOFT
);
157 LOAD_PROC(device
, alcResetDeviceSOFT
);
160 /* Enumerate available HRTFs, and reset the device using one. */
161 alcGetIntegerv(device
, ALC_NUM_HRTF_SPECIFIERS_SOFT
, 1, &num_hrtf
);
163 printf("No HRTFs found\n");
170 printf("Available HRTFs:\n");
171 for(i
= 0;i
< num_hrtf
;i
++)
173 const ALCchar
*name
= alcGetStringiSOFT(device
, ALC_HRTF_SPECIFIER_SOFT
, i
);
174 printf(" %d: %s\n", i
, name
);
176 /* Check if this is the HRTF the user requested. */
177 if(hrtfname
&& strcmp(name
, hrtfname
) == 0)
182 attr
[i
++] = ALC_HRTF_SOFT
;
183 attr
[i
++] = ALC_TRUE
;
187 printf("HRTF \"%s\" not found\n", hrtfname
);
188 printf("Using default HRTF...\n");
192 printf("Selecting HRTF %d...\n", index
);
193 attr
[i
++] = ALC_HRTF_ID_SOFT
;
198 if(!alcResetDeviceSOFT(device
, attr
))
199 printf("Failed to reset device: %s\n", alcGetString(device
, alcGetError(device
)));
202 /* Check if HRTF is enabled, and show which is being used. */
203 alcGetIntegerv(device
, ALC_HRTF_SOFT
, 1, &hrtf_state
);
205 printf("HRTF not enabled!\n");
208 const ALchar
*name
= alcGetString(device
, ALC_HRTF_SPECIFIER_SOFT
);
209 printf("HRTF enabled, using %s\n", name
);
213 /* Load the sound into a buffer. */
214 buffer
= LoadSound(soundname
);
221 /* Create the source to play the sound with. */
223 alGenSources(1, &source
);
224 alSourcei(source
, AL_SOURCE_RELATIVE
, AL_TRUE
);
225 alSource3f(source
, AL_POSITION
, 0.0f
, 0.0f
, -1.0f
);
226 alSourcei(source
, AL_BUFFER
, buffer
);
227 assert(alGetError()==AL_NO_ERROR
&& "Failed to setup sound source");
229 /* Play the sound until it finishes. */
231 alSourcePlay(source
);
233 al_nssleep(10000000);
235 /* Rotate the source around the listener by about 1/4 cycle per second.
236 * Only affects mono sounds.
238 angle
+= 0.01 * M_PI
* 0.5;
239 alSource3f(source
, AL_POSITION
, (ALfloat
)sin(angle
), 0.0f
, -(ALfloat
)cos(angle
));
241 alGetSourcei(source
, AL_SOURCE_STATE
, &state
);
242 } while(alGetError() == AL_NO_ERROR
&& state
== AL_PLAYING
);
244 /* All done. Delete resources, and close OpenAL. */
245 alDeleteSources(1, &source
);
246 alDeleteBuffers(1, &buffer
);