SetSourceiv should never get AL_SAMPLE_OFFSET_LATENCY_SOFT
[openal-soft.git] / examples / allatency.c
blob48bf5b51384c2b60b00d35374747eaac3daf51d5
1 /*
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
22 * THE SOFTWARE.
25 /* This file contains an example for checking the latency of a sound. */
27 #include <stdio.h>
28 #include <assert.h>
29 #ifdef _WIN32
30 #define WIN32_LEAN_AND_MEAN
31 #include <windows.h>
32 #else
33 #include <unistd.h>
34 #define Sleep(x) usleep((x)*1000)
35 #endif
37 #include "AL/al.h"
38 #include "AL/alc.h"
39 #include "AL/alext.h"
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;
66 ALuint rate, buffer;
67 size_t datalen;
68 void *data;
69 FilePtr audiofile;
70 StreamPtr sound;
72 /* Open the file and get the first stream from it */
73 audiofile = openAVFile(filename);
74 sound = getAVAudioStream(audiofile, 0);
75 if(!sound)
77 fprintf(stderr, "Could not open audio in %s\n", filename);
78 closeAVFile(audiofile);
79 return 0;
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);
87 return 0;
90 format = GetFormat(channels, type, alIsBufferFormatSupportedSOFT);
91 if(format == AL_NONE)
93 fprintf(stderr, "Unsupported format (%s, %s) for %s\n",
94 ChannelsName(channels), TypeName(type), filename);
95 closeAVFile(audiofile);
96 return 0;
99 /* Decode the whole audio stream to a buffer. */
100 data = decodeAVAudioStream(sound, &datalen);
101 if(!data)
103 fprintf(stderr, "Failed to read audio from %s\n", filename);
104 closeAVFile(audiofile);
105 return 0;
108 /* Buffer the audio data into a new buffer object, then free the data and
109 * close the file. */
110 buffer = 0;
111 alGenBuffers(1, &buffer);
112 alBufferSamplesSOFT(buffer, rate, format, BytesToFrames(datalen, channels, type),
113 channels, type, data);
114 free(data);
115 closeAVFile(audiofile);
117 /* Check if an error occured, and clean up if so. */
118 err = alGetError();
119 if(err != AL_NO_ERROR)
121 fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
122 if(alIsBuffer(buffer))
123 alDeleteBuffers(1, &buffer);
124 return 0;
127 return buffer;
131 int main(int argc, char **argv)
133 ALuint source, buffer;
134 ALdouble offsets[2];
135 ALenum state;
137 /* Print out usage if no file was specified */
138 if(argc < 2)
140 fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
141 return 1;
144 /* Initialize OpenAL with the default device, and check for EFX support. */
145 if(InitAL() != 0)
146 return 1;
148 if(!alIsExtensionPresent("AL_SOFT_source_latency"))
150 fprintf(stderr, "Error: AL_SOFT_source_latency not supported\n");
151 CloseAL();
152 return 1;
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);
175 #undef LOAD_PROC
177 /* Load the sound into a buffer. */
178 buffer = LoadSound(argv[1]);
179 if(!buffer)
181 CloseAL();
182 return 1;
185 /* Create the source to play the sound with. */
186 source = 0;
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);
193 do {
194 Sleep(10);
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));
202 fflush(stdout);
203 } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
204 printf("\n");
206 /* All done. Delete resources, and close OpenAL. */
207 alDeleteSources(1, &source);
208 alDeleteBuffers(1, &buffer);
210 CloseAL();
212 return 0;