Fix the UHJ decoder parameters in uhjdecoder
[openal-soft.git] / examples / allatency.c
blob3b141ac0e22f69be725fd90350b0c60cc59e7c6f
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 <assert.h>
28 #include <inttypes.h>
29 #include <limits.h>
30 #include <stdio.h>
31 #include <stdlib.h>
33 #include "sndfile.h"
35 #include "AL/al.h"
36 #include "AL/alext.h"
38 #include "common/alhelpers.h"
41 static LPALSOURCEDSOFT alSourcedSOFT;
42 static LPALSOURCE3DSOFT alSource3dSOFT;
43 static LPALSOURCEDVSOFT alSourcedvSOFT;
44 static LPALGETSOURCEDSOFT alGetSourcedSOFT;
45 static LPALGETSOURCE3DSOFT alGetSource3dSOFT;
46 static LPALGETSOURCEDVSOFT alGetSourcedvSOFT;
47 static LPALSOURCEI64SOFT alSourcei64SOFT;
48 static LPALSOURCE3I64SOFT alSource3i64SOFT;
49 static LPALSOURCEI64VSOFT alSourcei64vSOFT;
50 static LPALGETSOURCEI64SOFT alGetSourcei64SOFT;
51 static LPALGETSOURCE3I64SOFT alGetSource3i64SOFT;
52 static LPALGETSOURCEI64VSOFT alGetSourcei64vSOFT;
54 /* C doesn't allow casting between function and non-function pointer types, so
55 * with C99 we need to use a union to reinterpret the pointer type. Pre-C99
56 * still needs to use a normal cast and live with the warning (C++ is fine with
57 * a regular reinterpret_cast).
59 #if __STDC_VERSION__ >= 199901L
60 #define FUNCTION_CAST(T, ptr) (union{void *p; T f;}){ptr}.f
61 #else
62 #define FUNCTION_CAST(T, ptr) (T)(ptr)
63 #endif
65 /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
66 * returns the new buffer ID.
68 static ALuint LoadSound(const char *filename)
70 ALenum err, format;
71 ALuint buffer;
72 SNDFILE *sndfile;
73 SF_INFO sfinfo;
74 short *membuf;
75 sf_count_t num_frames;
76 ALsizei num_bytes;
78 /* Open the audio file and check that it's usable. */
79 sndfile = sf_open(filename, SFM_READ, &sfinfo);
80 if(!sndfile)
82 fprintf(stderr, "Could not open audio in %s: %s\n", filename, sf_strerror(sndfile));
83 return 0;
85 if(sfinfo.frames < 1 || sfinfo.frames > (sf_count_t)(INT_MAX/sizeof(short))/sfinfo.channels)
87 fprintf(stderr, "Bad sample count in %s (%" PRId64 ")\n", filename, sfinfo.frames);
88 sf_close(sndfile);
89 return 0;
92 /* Get the sound format, and figure out the OpenAL format */
93 format = AL_NONE;
94 if(sfinfo.channels == 1)
95 format = AL_FORMAT_MONO16;
96 else if(sfinfo.channels == 2)
97 format = AL_FORMAT_STEREO16;
98 else if(sfinfo.channels == 3)
100 if(sf_command(sndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT)
101 format = AL_FORMAT_BFORMAT2D_16;
103 else if(sfinfo.channels == 4)
105 if(sf_command(sndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT)
106 format = AL_FORMAT_BFORMAT3D_16;
108 if(!format)
110 fprintf(stderr, "Unsupported channel count: %d\n", sfinfo.channels);
111 sf_close(sndfile);
112 return 0;
115 /* Decode the whole audio file to a buffer. */
116 membuf = malloc((size_t)(sfinfo.frames * sfinfo.channels) * sizeof(short));
118 num_frames = sf_readf_short(sndfile, membuf, sfinfo.frames);
119 if(num_frames < 1)
121 free(membuf);
122 sf_close(sndfile);
123 fprintf(stderr, "Failed to read samples in %s (%" PRId64 ")\n", filename, num_frames);
124 return 0;
126 num_bytes = (ALsizei)(num_frames * sfinfo.channels) * (ALsizei)sizeof(short);
128 /* Buffer the audio data into a new buffer object, then free the data and
129 * close the file.
131 buffer = 0;
132 alGenBuffers(1, &buffer);
133 alBufferData(buffer, format, membuf, num_bytes, sfinfo.samplerate);
135 free(membuf);
136 sf_close(sndfile);
138 /* Check if an error occured, and clean up if so. */
139 err = alGetError();
140 if(err != AL_NO_ERROR)
142 fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
143 if(buffer && alIsBuffer(buffer))
144 alDeleteBuffers(1, &buffer);
145 return 0;
148 return buffer;
152 int main(int argc, char **argv)
154 ALuint source, buffer;
155 ALdouble offsets[2];
156 ALenum state;
158 /* Print out usage if no arguments were specified */
159 if(argc < 2)
161 fprintf(stderr, "Usage: %s [-device <name>] <filename>\n", argv[0]);
162 return 1;
165 /* Initialize OpenAL, and check for source_latency support. */
166 argv++; argc--;
167 if(InitAL(&argv, &argc) != 0)
168 return 1;
170 if(!alIsExtensionPresent("AL_SOFT_source_latency"))
172 fprintf(stderr, "Error: AL_SOFT_source_latency not supported\n");
173 CloseAL();
174 return 1;
177 /* Define a macro to help load the function pointers. */
178 #define LOAD_PROC(T, x) ((x) = FUNCTION_CAST(T, alGetProcAddress(#x)))
179 LOAD_PROC(LPALSOURCEDSOFT, alSourcedSOFT);
180 LOAD_PROC(LPALSOURCE3DSOFT, alSource3dSOFT);
181 LOAD_PROC(LPALSOURCEDVSOFT, alSourcedvSOFT);
182 LOAD_PROC(LPALGETSOURCEDSOFT, alGetSourcedSOFT);
183 LOAD_PROC(LPALGETSOURCE3DSOFT, alGetSource3dSOFT);
184 LOAD_PROC(LPALGETSOURCEDVSOFT, alGetSourcedvSOFT);
185 LOAD_PROC(LPALSOURCEI64SOFT, alSourcei64SOFT);
186 LOAD_PROC(LPALSOURCE3I64SOFT, alSource3i64SOFT);
187 LOAD_PROC(LPALSOURCEI64VSOFT, alSourcei64vSOFT);
188 LOAD_PROC(LPALGETSOURCEI64SOFT, alGetSourcei64SOFT);
189 LOAD_PROC(LPALGETSOURCE3I64SOFT, alGetSource3i64SOFT);
190 LOAD_PROC(LPALGETSOURCEI64VSOFT, alGetSourcei64vSOFT);
191 #undef LOAD_PROC
193 /* Load the sound into a buffer. */
194 buffer = LoadSound(argv[0]);
195 if(!buffer)
197 CloseAL();
198 return 1;
201 /* Create the source to play the sound with. */
202 source = 0;
203 alGenSources(1, &source);
204 alSourcei(source, AL_BUFFER, (ALint)buffer);
205 assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
207 /* Play the sound until it finishes. */
208 alSourcePlay(source);
209 do {
210 al_nssleep(10000000);
211 alGetSourcei(source, AL_SOURCE_STATE, &state);
213 /* Get the source offset and latency. AL_SEC_OFFSET_LATENCY_SOFT will
214 * place the offset (in seconds) in offsets[0], and the time until that
215 * offset will be heard (in seconds) in offsets[1]. */
216 alGetSourcedvSOFT(source, AL_SEC_OFFSET_LATENCY_SOFT, offsets);
217 printf("\rOffset: %f - Latency:%3u ms ", offsets[0], (ALuint)(offsets[1]*1000));
218 fflush(stdout);
219 } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
220 printf("\n");
222 /* All done. Delete resources, and close down OpenAL. */
223 alDeleteSources(1, &source);
224 alDeleteBuffers(1, &buffer);
225 CloseAL();
227 return 0;