Reduce LIMITER_VALUE_MAX
[openal-soft.git] / examples / allatency.c
blobd561373fae6a9a606c3f71f34884499255a6b186
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>
30 #include <SDL_sound.h>
32 #include "AL/al.h"
33 #include "AL/alc.h"
34 #include "AL/alext.h"
36 #include "common/alhelpers.h"
39 static LPALSOURCEDSOFT alSourcedSOFT;
40 static LPALSOURCE3DSOFT alSource3dSOFT;
41 static LPALSOURCEDVSOFT alSourcedvSOFT;
42 static LPALGETSOURCEDSOFT alGetSourcedSOFT;
43 static LPALGETSOURCE3DSOFT alGetSource3dSOFT;
44 static LPALGETSOURCEDVSOFT alGetSourcedvSOFT;
45 static LPALSOURCEI64SOFT alSourcei64SOFT;
46 static LPALSOURCE3I64SOFT alSource3i64SOFT;
47 static LPALSOURCEI64VSOFT alSourcei64vSOFT;
48 static LPALGETSOURCEI64SOFT alGetSourcei64SOFT;
49 static LPALGETSOURCE3I64SOFT alGetSource3i64SOFT;
50 static LPALGETSOURCEI64VSOFT alGetSourcei64vSOFT;
52 /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
53 * returns the new buffer ID.
55 static ALuint LoadSound(const char *filename)
57 Sound_Sample *sample;
58 ALenum err, format;
59 ALuint buffer;
60 Uint32 slen;
62 /* Open the audio file */
63 sample = Sound_NewSampleFromFile(filename, NULL, 65536);
64 if(!sample)
66 fprintf(stderr, "Could not open audio in %s\n", filename);
67 return 0;
70 /* Get the sound format, and figure out the OpenAL format */
71 if(sample->actual.channels == 1)
73 if(sample->actual.format == AUDIO_U8)
74 format = AL_FORMAT_MONO8;
75 else if(sample->actual.format == AUDIO_S16SYS)
76 format = AL_FORMAT_MONO16;
77 else
79 fprintf(stderr, "Unsupported sample format: 0x%04x\n", sample->actual.format);
80 Sound_FreeSample(sample);
81 return 0;
84 else if(sample->actual.channels == 2)
86 if(sample->actual.format == AUDIO_U8)
87 format = AL_FORMAT_STEREO8;
88 else if(sample->actual.format == AUDIO_S16SYS)
89 format = AL_FORMAT_STEREO16;
90 else
92 fprintf(stderr, "Unsupported sample format: 0x%04x\n", sample->actual.format);
93 Sound_FreeSample(sample);
94 return 0;
97 else
99 fprintf(stderr, "Unsupported channel count: %d\n", sample->actual.channels);
100 Sound_FreeSample(sample);
101 return 0;
104 /* Decode the whole audio stream to a buffer. */
105 slen = Sound_DecodeAll(sample);
106 if(!sample->buffer || slen == 0)
108 fprintf(stderr, "Failed to read audio from %s\n", filename);
109 Sound_FreeSample(sample);
110 return 0;
113 /* Buffer the audio data into a new buffer object, then free the data and
114 * close the file. */
115 buffer = 0;
116 alGenBuffers(1, &buffer);
117 alBufferData(buffer, format, sample->buffer, slen, sample->actual.rate);
118 Sound_FreeSample(sample);
120 /* Check if an error occured, and clean up if so. */
121 err = alGetError();
122 if(err != AL_NO_ERROR)
124 fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
125 if(buffer && alIsBuffer(buffer))
126 alDeleteBuffers(1, &buffer);
127 return 0;
130 return buffer;
134 int main(int argc, char **argv)
136 ALuint source, buffer;
137 ALdouble offsets[2];
138 ALenum state;
140 /* Print out usage if no arguments were specified */
141 if(argc < 2)
143 fprintf(stderr, "Usage: %s [-device <name>] <filename>\n", argv[0]);
144 return 1;
147 /* Initialize OpenAL, and check for source_latency support. */
148 argv++; argc--;
149 if(InitAL(&argv, &argc) != 0)
150 return 1;
152 if(!alIsExtensionPresent("AL_SOFT_source_latency"))
154 fprintf(stderr, "Error: AL_SOFT_source_latency not supported\n");
155 CloseAL();
156 return 1;
159 /* Define a macro to help load the function pointers. */
160 #define LOAD_PROC(x) ((x) = alGetProcAddress(#x))
161 LOAD_PROC(alSourcedSOFT);
162 LOAD_PROC(alSource3dSOFT);
163 LOAD_PROC(alSourcedvSOFT);
164 LOAD_PROC(alGetSourcedSOFT);
165 LOAD_PROC(alGetSource3dSOFT);
166 LOAD_PROC(alGetSourcedvSOFT);
167 LOAD_PROC(alSourcei64SOFT);
168 LOAD_PROC(alSource3i64SOFT);
169 LOAD_PROC(alSourcei64vSOFT);
170 LOAD_PROC(alGetSourcei64SOFT);
171 LOAD_PROC(alGetSource3i64SOFT);
172 LOAD_PROC(alGetSourcei64vSOFT);
173 #undef LOAD_PROC
175 /* Initialize SDL_sound. */
176 Sound_Init();
178 /* Load the sound into a buffer. */
179 buffer = LoadSound(argv[0]);
180 if(!buffer)
182 Sound_Quit();
183 CloseAL();
184 return 1;
187 /* Create the source to play the sound with. */
188 source = 0;
189 alGenSources(1, &source);
190 alSourcei(source, AL_BUFFER, buffer);
191 assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
193 /* Play the sound until it finishes. */
194 alSourcePlay(source);
195 do {
196 al_nssleep(10000000);
197 alGetSourcei(source, AL_SOURCE_STATE, &state);
199 /* Get the source offset and latency. AL_SEC_OFFSET_LATENCY_SOFT will
200 * place the offset (in seconds) in offsets[0], and the time until that
201 * offset will be heard (in seconds) in offsets[1]. */
202 alGetSourcedvSOFT(source, AL_SEC_OFFSET_LATENCY_SOFT, offsets);
203 printf("\rOffset: %f - Latency:%3u ms ", offsets[0], (ALuint)(offsets[1]*1000));
204 fflush(stdout);
205 } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
206 printf("\n");
208 /* All done. Delete resources, and close down SDL_sound and OpenAL. */
209 alDeleteSources(1, &source);
210 alDeleteBuffers(1, &buffer);
212 Sound_Quit();
213 CloseAL();
215 return 0;