Remove deprecated, performance, and error event types
[openal-soft.git] / examples / allatency.c
blob7ece01ebfafa16cbf9627db44bf54580c036f805
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 /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
55 * returns the new buffer ID.
57 static ALuint LoadSound(const char *filename)
59 ALenum err, format;
60 ALuint buffer;
61 SNDFILE *sndfile;
62 SF_INFO sfinfo;
63 short *membuf;
64 sf_count_t num_frames;
65 ALsizei num_bytes;
67 /* Open the audio file and check that it's usable. */
68 sndfile = sf_open(filename, SFM_READ, &sfinfo);
69 if(!sndfile)
71 fprintf(stderr, "Could not open audio in %s: %s\n", filename, sf_strerror(sndfile));
72 return 0;
74 if(sfinfo.frames < 1 || sfinfo.frames > (sf_count_t)(INT_MAX/sizeof(short))/sfinfo.channels)
76 fprintf(stderr, "Bad sample count in %s (%" PRId64 ")\n", filename, sfinfo.frames);
77 sf_close(sndfile);
78 return 0;
81 /* Get the sound format, and figure out the OpenAL format */
82 if(sfinfo.channels == 1)
83 format = AL_FORMAT_MONO16;
84 else if(sfinfo.channels == 2)
85 format = AL_FORMAT_STEREO16;
86 else
88 fprintf(stderr, "Unsupported channel count: %d\n", sfinfo.channels);
89 sf_close(sndfile);
90 return 0;
93 /* Decode the whole audio file to a buffer. */
94 membuf = malloc((size_t)(sfinfo.frames * sfinfo.channels) * sizeof(short));
96 num_frames = sf_readf_short(sndfile, membuf, sfinfo.frames);
97 if(num_frames < 1)
99 free(membuf);
100 sf_close(sndfile);
101 fprintf(stderr, "Failed to read samples in %s (%" PRId64 ")\n", filename, num_frames);
102 return 0;
104 num_bytes = (ALsizei)(num_frames * sfinfo.channels) * (ALsizei)sizeof(short);
106 /* Buffer the audio data into a new buffer object, then free the data and
107 * close the file.
109 buffer = 0;
110 alGenBuffers(1, &buffer);
111 alBufferData(buffer, format, membuf, num_bytes, sfinfo.samplerate);
113 free(membuf);
114 sf_close(sndfile);
116 /* Check if an error occured, and clean up if so. */
117 err = alGetError();
118 if(err != AL_NO_ERROR)
120 fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
121 if(buffer && alIsBuffer(buffer))
122 alDeleteBuffers(1, &buffer);
123 return 0;
126 return buffer;
130 int main(int argc, char **argv)
132 ALuint source, buffer;
133 ALdouble offsets[2];
134 ALenum state;
136 /* Print out usage if no arguments were specified */
137 if(argc < 2)
139 fprintf(stderr, "Usage: %s [-device <name>] <filename>\n", argv[0]);
140 return 1;
143 /* Initialize OpenAL, and check for source_latency support. */
144 argv++; argc--;
145 if(InitAL(&argv, &argc) != 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(T, x) ((x) = (T)alGetProcAddress(#x))
157 LOAD_PROC(LPALSOURCEDSOFT, alSourcedSOFT);
158 LOAD_PROC(LPALSOURCE3DSOFT, alSource3dSOFT);
159 LOAD_PROC(LPALSOURCEDVSOFT, alSourcedvSOFT);
160 LOAD_PROC(LPALGETSOURCEDSOFT, alGetSourcedSOFT);
161 LOAD_PROC(LPALGETSOURCE3DSOFT, alGetSource3dSOFT);
162 LOAD_PROC(LPALGETSOURCEDVSOFT, alGetSourcedvSOFT);
163 LOAD_PROC(LPALSOURCEI64SOFT, alSourcei64SOFT);
164 LOAD_PROC(LPALSOURCE3I64SOFT, alSource3i64SOFT);
165 LOAD_PROC(LPALSOURCEI64VSOFT, alSourcei64vSOFT);
166 LOAD_PROC(LPALGETSOURCEI64SOFT, alGetSourcei64SOFT);
167 LOAD_PROC(LPALGETSOURCE3I64SOFT, alGetSource3i64SOFT);
168 LOAD_PROC(LPALGETSOURCEI64VSOFT, alGetSourcei64vSOFT);
169 #undef LOAD_PROC
171 /* Load the sound into a buffer. */
172 buffer = LoadSound(argv[0]);
173 if(!buffer)
175 CloseAL();
176 return 1;
179 /* Create the source to play the sound with. */
180 source = 0;
181 alGenSources(1, &source);
182 alSourcei(source, AL_BUFFER, (ALint)buffer);
183 assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
185 /* Play the sound until it finishes. */
186 alSourcePlay(source);
187 do {
188 al_nssleep(10000000);
189 alGetSourcei(source, AL_SOURCE_STATE, &state);
191 /* Get the source offset and latency. AL_SEC_OFFSET_LATENCY_SOFT will
192 * place the offset (in seconds) in offsets[0], and the time until that
193 * offset will be heard (in seconds) in offsets[1]. */
194 alGetSourcedvSOFT(source, AL_SEC_OFFSET_LATENCY_SOFT, offsets);
195 printf("\rOffset: %f - Latency:%3u ms ", offsets[0], (ALuint)(offsets[1]*1000));
196 fflush(stdout);
197 } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
198 printf("\n");
200 /* All done. Delete resources, and close down OpenAL. */
201 alDeleteSources(1, &source);
202 alDeleteBuffers(1, &buffer);
203 CloseAL();
205 return 0;