Combine two function calls into one
[openal-soft.git] / examples / alhrtf.c
blob2be28a91270513a1979edcb3496a6d614592e1ff
1 /*
2 * OpenAL HRTF Example
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
22 * THE SOFTWARE.
25 /* This file contains an example for selecting an HRTF. */
27 #include <assert.h>
28 #include <math.h>
29 #include <stdio.h>
30 #include <string.h>
32 #include "SDL_sound.h"
33 #include "SDL_audio.h"
34 #include "SDL_stdinc.h"
36 #include "AL/al.h"
37 #include "AL/alc.h"
38 #include "AL/alext.h"
40 #include "common/alhelpers.h"
43 #ifndef M_PI
44 #define M_PI (3.14159265358979323846)
45 #endif
47 static LPALCGETSTRINGISOFT alcGetStringiSOFT;
48 static LPALCRESETDEVICESOFT alcResetDeviceSOFT;
50 /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
51 * returns the new buffer ID.
53 static ALuint LoadSound(const char *filename)
55 Sound_Sample *sample;
56 ALenum err, format;
57 ALuint buffer;
58 Uint32 slen;
60 /* Open the audio file */
61 sample = Sound_NewSampleFromFile(filename, NULL, 65536);
62 if(!sample)
64 fprintf(stderr, "Could not open audio in %s\n", filename);
65 return 0;
68 /* Get the sound format, and figure out the OpenAL format */
69 if(sample->actual.channels == 1)
71 if(sample->actual.format == AUDIO_U8)
72 format = AL_FORMAT_MONO8;
73 else if(sample->actual.format == AUDIO_S16SYS)
74 format = AL_FORMAT_MONO16;
75 else
77 fprintf(stderr, "Unsupported sample format: 0x%04x\n", sample->actual.format);
78 Sound_FreeSample(sample);
79 return 0;
82 else if(sample->actual.channels == 2)
84 if(sample->actual.format == AUDIO_U8)
85 format = AL_FORMAT_STEREO8;
86 else if(sample->actual.format == AUDIO_S16SYS)
87 format = AL_FORMAT_STEREO16;
88 else
90 fprintf(stderr, "Unsupported sample format: 0x%04x\n", sample->actual.format);
91 Sound_FreeSample(sample);
92 return 0;
95 else
97 fprintf(stderr, "Unsupported channel count: %d\n", sample->actual.channels);
98 Sound_FreeSample(sample);
99 return 0;
102 /* Decode the whole audio stream to a buffer. */
103 slen = Sound_DecodeAll(sample);
104 if(!sample->buffer || slen == 0)
106 fprintf(stderr, "Failed to read audio from %s\n", filename);
107 Sound_FreeSample(sample);
108 return 0;
111 /* Buffer the audio data into a new buffer object, then free the data and
112 * close the file. */
113 buffer = 0;
114 alGenBuffers(1, &buffer);
115 alBufferData(buffer, format, sample->buffer, (ALsizei)slen, (ALsizei)sample->actual.rate);
116 Sound_FreeSample(sample);
118 /* Check if an error occured, and clean up if so. */
119 err = alGetError();
120 if(err != AL_NO_ERROR)
122 fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
123 if(buffer && alIsBuffer(buffer))
124 alDeleteBuffers(1, &buffer);
125 return 0;
128 return buffer;
132 int main(int argc, char **argv)
134 ALCdevice *device;
135 ALCcontext *context;
136 ALboolean has_angle_ext;
137 ALuint source, buffer;
138 const char *soundname;
139 const char *hrtfname;
140 ALCint hrtf_state;
141 ALCint num_hrtf;
142 ALdouble angle;
143 ALenum state;
145 /* Print out usage if no arguments were specified */
146 if(argc < 2)
148 fprintf(stderr, "Usage: %s [-device <name>] [-hrtf <name>] <soundfile>\n", argv[0]);
149 return 1;
152 /* Initialize OpenAL, and check for HRTF support. */
153 argv++; argc--;
154 if(InitAL(&argv, &argc) != 0)
155 return 1;
157 context = alcGetCurrentContext();
158 device = alcGetContextsDevice(context);
159 if(!alcIsExtensionPresent(device, "ALC_SOFT_HRTF"))
161 fprintf(stderr, "Error: ALC_SOFT_HRTF not supported\n");
162 CloseAL();
163 return 1;
166 /* Define a macro to help load the function pointers. */
167 #define LOAD_PROC(d, T, x) ((x) = (T)alcGetProcAddress((d), #x))
168 LOAD_PROC(device, LPALCGETSTRINGISOFT, alcGetStringiSOFT);
169 LOAD_PROC(device, LPALCRESETDEVICESOFT, alcResetDeviceSOFT);
170 #undef LOAD_PROC
172 /* Check for the AL_EXT_STEREO_ANGLES extension to be able to also rotate
173 * stereo sources.
175 has_angle_ext = alIsExtensionPresent("AL_EXT_STEREO_ANGLES");
176 printf("AL_EXT_STEREO_ANGLES %sfound\n", has_angle_ext?"":"not ");
178 /* Check for user-preferred HRTF */
179 if(strcmp(argv[0], "-hrtf") == 0)
181 hrtfname = argv[1];
182 soundname = argv[2];
184 else
186 hrtfname = NULL;
187 soundname = argv[0];
190 /* Enumerate available HRTFs, and reset the device using one. */
191 alcGetIntegerv(device, ALC_NUM_HRTF_SPECIFIERS_SOFT, 1, &num_hrtf);
192 if(!num_hrtf)
193 printf("No HRTFs found\n");
194 else
196 ALCint attr[5];
197 ALCint index = -1;
198 ALCint i;
200 printf("Available HRTFs:\n");
201 for(i = 0;i < num_hrtf;i++)
203 const ALCchar *name = alcGetStringiSOFT(device, ALC_HRTF_SPECIFIER_SOFT, i);
204 printf(" %d: %s\n", i, name);
206 /* Check if this is the HRTF the user requested. */
207 if(hrtfname && strcmp(name, hrtfname) == 0)
208 index = i;
211 i = 0;
212 attr[i++] = ALC_HRTF_SOFT;
213 attr[i++] = ALC_TRUE;
214 if(index == -1)
216 if(hrtfname)
217 printf("HRTF \"%s\" not found\n", hrtfname);
218 printf("Using default HRTF...\n");
220 else
222 printf("Selecting HRTF %d...\n", index);
223 attr[i++] = ALC_HRTF_ID_SOFT;
224 attr[i++] = index;
226 attr[i] = 0;
228 if(!alcResetDeviceSOFT(device, attr))
229 printf("Failed to reset device: %s\n", alcGetString(device, alcGetError(device)));
232 /* Check if HRTF is enabled, and show which is being used. */
233 alcGetIntegerv(device, ALC_HRTF_SOFT, 1, &hrtf_state);
234 if(!hrtf_state)
235 printf("HRTF not enabled!\n");
236 else
238 const ALchar *name = alcGetString(device, ALC_HRTF_SPECIFIER_SOFT);
239 printf("HRTF enabled, using %s\n", name);
241 fflush(stdout);
243 /* Initialize SDL_sound. */
244 Sound_Init();
246 /* Load the sound into a buffer. */
247 buffer = LoadSound(soundname);
248 if(!buffer)
250 Sound_Quit();
251 CloseAL();
252 return 1;
255 /* Create the source to play the sound with. */
256 source = 0;
257 alGenSources(1, &source);
258 alSourcei(source, AL_SOURCE_RELATIVE, AL_TRUE);
259 alSource3f(source, AL_POSITION, 0.0f, 0.0f, -1.0f);
260 alSourcei(source, AL_BUFFER, (ALint)buffer);
261 assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
263 /* Play the sound until it finishes. */
264 angle = 0.0;
265 alSourcePlay(source);
266 do {
267 al_nssleep(10000000);
269 alcSuspendContext(context);
271 /* Rotate the source around the listener by about 1/4 cycle per second,
272 * and keep it within -pi...+pi.
274 angle += 0.01 * M_PI * 0.5;
275 if(angle > M_PI)
276 angle -= M_PI*2.0;
278 /* This only rotates mono sounds. */
279 alSource3f(source, AL_POSITION, (ALfloat)sin(angle), 0.0f, -(ALfloat)cos(angle));
281 if(has_angle_ext)
283 /* This rotates stereo sounds with the AL_EXT_STEREO_ANGLES
284 * extension. Angles are specified counter-clockwise in radians.
286 ALfloat angles[2] = { (ALfloat)(M_PI/6.0 - angle), (ALfloat)(-M_PI/6.0 - angle) };
287 alSourcefv(source, AL_STEREO_ANGLES, angles);
289 alcProcessContext(context);
291 alGetSourcei(source, AL_SOURCE_STATE, &state);
292 } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
294 /* All done. Delete resources, and close down SDL_sound and OpenAL. */
295 alDeleteSources(1, &source);
296 alDeleteBuffers(1, &buffer);
298 Sound_Quit();
299 CloseAL();
301 return 0;