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. */
30 #define WIN32_LEAN_AND_MEAN
34 #define Sleep(x) usleep((x)*1000)
41 #include "common/alhelpers.h"
42 #include "common/alffmpeg.h"
45 LPALBUFFERSAMPLESSOFT alBufferSamplesSOFT
= wrap_BufferSamples
;
46 LPALISBUFFERFORMATSUPPORTEDSOFT alIsBufferFormatSupportedSOFT
;
48 LPALSOURCEDSOFT alSourcedSOFT
;
49 LPALSOURCE3DSOFT alSource3dSOFT
;
50 LPALSOURCEDVSOFT alSourcedvSOFT
;
51 LPALGETSOURCEDSOFT alGetSourcedSOFT
;
52 LPALGETSOURCE3DSOFT alGetSource3dSOFT
;
53 LPALGETSOURCEDVSOFT alGetSourcedvSOFT
;
54 LPALSOURCEI64SOFT alSourcei64SOFT
;
55 LPALSOURCE3I64SOFT alSource3i64SOFT
;
56 LPALSOURCEI64VSOFT alSourcei64vSOFT
;
57 LPALGETSOURCEI64SOFT alGetSourcei64SOFT
;
58 LPALGETSOURCE3I64SOFT alGetSource3i64SOFT
;
59 LPALGETSOURCEI64VSOFT alGetSourcei64vSOFT
;
61 /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
62 * returns the new buffer ID. */
63 static ALuint
LoadSound(const char *filename
)
65 ALenum err
, format
, type
, channels
;
72 /* Open the file and get the first stream from it */
73 audiofile
= openAVFile(filename
);
74 sound
= getAVAudioStream(audiofile
, 0);
77 fprintf(stderr
, "Could not open audio in %s\n", filename
);
78 closeAVFile(audiofile
);
82 /* Get the sound format, and figure out the OpenAL format */
83 if(getAVAudioInfo(sound
, &rate
, &channels
, &type
) != 0)
85 fprintf(stderr
, "Error getting audio info for %s\n", filename
);
86 closeAVFile(audiofile
);
90 format
= GetFormat(channels
, type
, alIsBufferFormatSupportedSOFT
);
93 fprintf(stderr
, "Unsupported format (%s, %s) for %s\n",
94 ChannelsName(channels
), TypeName(type
), filename
);
95 closeAVFile(audiofile
);
99 /* Decode the whole audio stream to a buffer. */
100 data
= decodeAVAudioStream(sound
, &datalen
);
103 fprintf(stderr
, "Failed to read audio from %s\n", filename
);
104 closeAVFile(audiofile
);
108 /* Buffer the audio data into a new buffer object, then free the data and
111 alGenBuffers(1, &buffer
);
112 alBufferSamplesSOFT(buffer
, rate
, format
, BytesToFrames(datalen
, channels
, type
),
113 channels
, type
, data
);
115 closeAVFile(audiofile
);
117 /* Check if an error occured, and clean up if so. */
119 if(err
!= AL_NO_ERROR
)
121 fprintf(stderr
, "OpenAL Error: %s\n", alGetString(err
));
122 if(alIsBuffer(buffer
))
123 alDeleteBuffers(1, &buffer
);
131 int main(int argc
, char **argv
)
133 ALuint source
, buffer
;
137 /* Print out usage if no file was specified */
140 fprintf(stderr
, "Usage: %s <filename>\n", argv
[0]);
144 /* Initialize OpenAL with the default device, and check for EFX support. */
148 if(!alIsExtensionPresent("AL_SOFT_source_latency"))
150 fprintf(stderr
, "Error: AL_SOFT_source_latency not supported\n");
155 /* Define a macro to help load the function pointers. */
156 #define LOAD_PROC(x) ((x) = alGetProcAddress(#x))
157 LOAD_PROC(alSourcedSOFT
);
158 LOAD_PROC(alSource3dSOFT
);
159 LOAD_PROC(alSourcedvSOFT
);
160 LOAD_PROC(alGetSourcedSOFT
);
161 LOAD_PROC(alGetSource3dSOFT
);
162 LOAD_PROC(alGetSourcedvSOFT
);
163 LOAD_PROC(alSourcei64SOFT
);
164 LOAD_PROC(alSource3i64SOFT
);
165 LOAD_PROC(alSourcei64vSOFT
);
166 LOAD_PROC(alGetSourcei64SOFT
);
167 LOAD_PROC(alGetSource3i64SOFT
);
168 LOAD_PROC(alGetSourcei64vSOFT
);
170 if(alIsExtensionPresent("AL_SOFT_buffer_samples"))
172 LOAD_PROC(alBufferSamplesSOFT
);
173 LOAD_PROC(alIsBufferFormatSupportedSOFT
);
177 /* Load the sound into a buffer. */
178 buffer
= LoadSound(argv
[1]);
185 /* Create the source to play the sound with. */
187 alGenSources(1, &source
);
188 alSourcei(source
, AL_BUFFER
, buffer
);
189 assert(alGetError()==AL_NO_ERROR
&& "Failed to setup sound source");
191 /* Play the sound until it finishes. */
192 alSourcePlay(source
);
195 alGetSourcei(source
, AL_SOURCE_STATE
, &state
);
197 /* Get the source offset and latency. AL_SEC_OFFSET_LATENCY_SOFT will
198 * place the offset (in seconds) in offsets[0], and the time until that
199 * offset will be heard (in seconds) in offsets[1]. */
200 alGetSourcedvSOFT(source
, AL_SEC_OFFSET_LATENCY_SOFT
, offsets
);
201 printf("\rOffset: %f - Latency:%3u ms ", offsets
[0], (ALuint
)(offsets
[1]*1000));
203 } while(alGetError() == AL_NO_ERROR
&& state
== AL_PLAYING
);
206 /* All done. Delete resources, and close OpenAL. */
207 alDeleteSources(1, &source
);
208 alDeleteBuffers(1, &buffer
);