2 * OpenAL Source Latency Example
4 * Copyright (c) 2012 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 checking the latency of a sound. */
38 #include "common/alhelpers.h"
40 #include "win_main_utf8.h"
43 static LPALSOURCEDSOFT alSourcedSOFT
;
44 static LPALSOURCE3DSOFT alSource3dSOFT
;
45 static LPALSOURCEDVSOFT alSourcedvSOFT
;
46 static LPALGETSOURCEDSOFT alGetSourcedSOFT
;
47 static LPALGETSOURCE3DSOFT alGetSource3dSOFT
;
48 static LPALGETSOURCEDVSOFT alGetSourcedvSOFT
;
49 static LPALSOURCEI64SOFT alSourcei64SOFT
;
50 static LPALSOURCE3I64SOFT alSource3i64SOFT
;
51 static LPALSOURCEI64VSOFT alSourcei64vSOFT
;
52 static LPALGETSOURCEI64SOFT alGetSourcei64SOFT
;
53 static LPALGETSOURCE3I64SOFT alGetSource3i64SOFT
;
54 static LPALGETSOURCEI64VSOFT alGetSourcei64vSOFT
;
56 /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
57 * returns the new buffer ID.
59 static ALuint
LoadSound(const char *filename
)
66 sf_count_t num_frames
;
69 /* Open the audio file and check that it's usable. */
70 sndfile
= sf_open(filename
, SFM_READ
, &sfinfo
);
73 fprintf(stderr
, "Could not open audio in %s: %s\n", filename
, sf_strerror(sndfile
));
76 if(sfinfo
.frames
< 1 || sfinfo
.frames
> (sf_count_t
)(INT_MAX
/sizeof(short))/sfinfo
.channels
)
78 fprintf(stderr
, "Bad sample count in %s (%" PRId64
")\n", filename
, sfinfo
.frames
);
83 /* Get the sound format, and figure out the OpenAL format */
85 if(sfinfo
.channels
== 1)
86 format
= AL_FORMAT_MONO16
;
87 else if(sfinfo
.channels
== 2)
88 format
= AL_FORMAT_STEREO16
;
89 else if(sfinfo
.channels
== 3)
91 if(sf_command(sndfile
, SFC_WAVEX_GET_AMBISONIC
, NULL
, 0) == SF_AMBISONIC_B_FORMAT
)
92 format
= AL_FORMAT_BFORMAT2D_16
;
94 else if(sfinfo
.channels
== 4)
96 if(sf_command(sndfile
, SFC_WAVEX_GET_AMBISONIC
, NULL
, 0) == SF_AMBISONIC_B_FORMAT
)
97 format
= AL_FORMAT_BFORMAT3D_16
;
101 fprintf(stderr
, "Unsupported channel count: %d\n", sfinfo
.channels
);
106 /* Decode the whole audio file to a buffer. */
107 membuf
= malloc((size_t)(sfinfo
.frames
* sfinfo
.channels
) * sizeof(short));
109 num_frames
= sf_readf_short(sndfile
, membuf
, sfinfo
.frames
);
114 fprintf(stderr
, "Failed to read samples in %s (%" PRId64
")\n", filename
, num_frames
);
117 num_bytes
= (ALsizei
)(num_frames
* sfinfo
.channels
) * (ALsizei
)sizeof(short);
119 /* Buffer the audio data into a new buffer object, then free the data and
123 alGenBuffers(1, &buffer
);
124 alBufferData(buffer
, format
, membuf
, num_bytes
, sfinfo
.samplerate
);
129 /* Check if an error occurred, and clean up if so. */
131 if(err
!= AL_NO_ERROR
)
133 fprintf(stderr
, "OpenAL Error: %s\n", alGetString(err
));
134 if(buffer
&& alIsBuffer(buffer
))
135 alDeleteBuffers(1, &buffer
);
143 int main(int argc
, char **argv
)
145 ALuint source
, buffer
;
149 /* Print out usage if no arguments were specified */
152 fprintf(stderr
, "Usage: %s [-device <name>] <filename>\n", argv
[0]);
156 /* Initialize OpenAL, and check for source_latency support. */
158 if(InitAL(&argv
, &argc
) != 0)
161 if(!alIsExtensionPresent("AL_SOFT_source_latency"))
163 fprintf(stderr
, "Error: AL_SOFT_source_latency not supported\n");
168 /* Define a macro to help load the function pointers. */
169 #define LOAD_PROC(T, x) ((x) = FUNCTION_CAST(T, alGetProcAddress(#x)))
170 LOAD_PROC(LPALSOURCEDSOFT
, alSourcedSOFT
);
171 LOAD_PROC(LPALSOURCE3DSOFT
, alSource3dSOFT
);
172 LOAD_PROC(LPALSOURCEDVSOFT
, alSourcedvSOFT
);
173 LOAD_PROC(LPALGETSOURCEDSOFT
, alGetSourcedSOFT
);
174 LOAD_PROC(LPALGETSOURCE3DSOFT
, alGetSource3dSOFT
);
175 LOAD_PROC(LPALGETSOURCEDVSOFT
, alGetSourcedvSOFT
);
176 LOAD_PROC(LPALSOURCEI64SOFT
, alSourcei64SOFT
);
177 LOAD_PROC(LPALSOURCE3I64SOFT
, alSource3i64SOFT
);
178 LOAD_PROC(LPALSOURCEI64VSOFT
, alSourcei64vSOFT
);
179 LOAD_PROC(LPALGETSOURCEI64SOFT
, alGetSourcei64SOFT
);
180 LOAD_PROC(LPALGETSOURCE3I64SOFT
, alGetSource3i64SOFT
);
181 LOAD_PROC(LPALGETSOURCEI64VSOFT
, alGetSourcei64vSOFT
);
184 /* Load the sound into a buffer. */
185 buffer
= LoadSound(argv
[0]);
192 /* Create the source to play the sound with. */
194 alGenSources(1, &source
);
195 alSourcei(source
, AL_BUFFER
, (ALint
)buffer
);
196 assert(alGetError()==AL_NO_ERROR
&& "Failed to setup sound source");
198 /* Play the sound until it finishes. */
199 alSourcePlay(source
);
201 al_nssleep(10000000);
202 alGetSourcei(source
, AL_SOURCE_STATE
, &state
);
204 /* Get the source offset and latency. AL_SEC_OFFSET_LATENCY_SOFT will
205 * place the offset (in seconds) in offsets[0], and the time until that
206 * offset will be heard (in seconds) in offsets[1]. */
207 alGetSourcedvSOFT(source
, AL_SEC_OFFSET_LATENCY_SOFT
, offsets
);
208 printf("\rOffset: %f - Latency:%3u ms ", offsets
[0], (ALuint
)(offsets
[1]*1000));
210 } while(alGetError() == AL_NO_ERROR
&& state
== AL_PLAYING
);
213 /* All done. Delete resources, and close down OpenAL. */
214 alDeleteSources(1, &source
);
215 alDeleteBuffers(1, &buffer
);