zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
allatency.c
Go to the documentation of this file.
1 /*
2  * OpenAL Source Latency Example
3  *
4  * Copyright (c) 2012 by Chris Robinson <chris.kcat@gmail.com>
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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.
23  */
24 
25 /* This file contains an example for checking the latency of a sound. */
26 
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
36 
37 #include "AL/al.h"
38 #include "AL/alc.h"
39 #include "AL/alext.h"
40 
41 #include "common/alhelpers.h"
42 #include "common/alffmpeg.h"
43 
44 
47 
60 
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)
64 {
65  ALenum err, format, type, channels;
66  ALuint rate, buffer;
67  size_t datalen;
68  void *data;
69  FilePtr audiofile;
70  StreamPtr sound;
71 
72  /* Open the file and get the first stream from it */
73  audiofile = openAVFile(filename);
74  sound = getAVAudioStream(audiofile, 0);
75  if(!sound)
76  {
77  fprintf(stderr, "Could not open audio in %s\n", filename);
78  closeAVFile(audiofile);
79  return 0;
80  }
81 
82  /* Get the sound format, and figure out the OpenAL format */
83  if(getAVAudioInfo(sound, &rate, &channels, &type) != 0)
84  {
85  fprintf(stderr, "Error getting audio info for %s\n", filename);
86  closeAVFile(audiofile);
87  return 0;
88  }
89 
90  format = GetFormat(channels, type, alIsBufferFormatSupportedSOFT);
91  if(format == AL_NONE)
92  {
93  fprintf(stderr, "Unsupported format (%s, %s) for %s\n",
94  ChannelsName(channels), TypeName(type), filename);
95  closeAVFile(audiofile);
96  return 0;
97  }
98 
99  /* Decode the whole audio stream to a buffer. */
100  data = decodeAVAudioStream(sound, &datalen);
101  if(!data)
102  {
103  fprintf(stderr, "Failed to read audio from %s\n", filename);
104  closeAVFile(audiofile);
105  return 0;
106  }
107 
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);
116 
117  /* Check if an error occured, and clean up if so. */
118  err = alGetError();
119  if(err != AL_NO_ERROR)
120  {
121  fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
122  if(alIsBuffer(buffer))
123  alDeleteBuffers(1, &buffer);
124  return 0;
125  }
126 
127  return buffer;
128 }
129 
130 
131 int main(int argc, char **argv)
132 {
134  ALdouble offsets[2];
135  ALenum state;
136 
137  /* Print out usage if no file was specified */
138  if(argc < 2)
139  {
140  fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
141  return 1;
142  }
143 
144  /* Initialize OpenAL with the default device, and check for EFX support. */
145  if(InitAL() != 0)
146  return 1;
147 
148  if(!alIsExtensionPresent("AL_SOFT_source_latency"))
149  {
150  fprintf(stderr, "Error: AL_SOFT_source_latency not supported\n");
151  CloseAL();
152  return 1;
153  }
154 
155  /* Define a macro to help load the function pointers. */
156 #define LOAD_PROC(x) ((x) = alGetProcAddress(#x))
169 
170  if(alIsExtensionPresent("AL_SOFT_buffer_samples"))
171  {
174  }
175 #undef LOAD_PROC
176 
177  /* Load the sound into a buffer. */
178  buffer = LoadSound(argv[1]);
179  if(!buffer)
180  {
181  CloseAL();
182  return 1;
183  }
184 
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");
190 
191  /* Play the sound until it finishes. */
192  alSourcePlay(source);
193  do {
194  Sleep(10);
195  alGetSourcei(source, AL_SOURCE_STATE, &state);
196 
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]. */
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");
205 
206  /* All done. Delete resources, and close OpenAL. */
207  alDeleteSources(1, &source);
208  alDeleteBuffers(1, &buffer);
209 
210  CloseAL();
211 
212  return 0;
213 }
AL_API void AL_APIENTRY alSourcePlay(ALuint source)
Definition: alSource.c:1894
LPALBUFFERSAMPLESSOFT alBufferSamplesSOFT
Definition: allatency.c:45
struct MyFile * FilePtr
Definition: alffmpeg.h:13
#define AL_NONE
Definition: al.h:80
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum GLenum type
Definition: gl2ext.h:845
void(AL_APIENTRY * LPALSOURCEDSOFT)(ALuint, ALenum, ALdouble)
Definition: alext.h:323
int main(int argc, char **argv)
Definition: bootstrap.cpp:102
FilePtr openAVFile(const char *fname)
Definition: alffmpeg.c:161
void CloseAL(void)
Definition: alhelpers.c:71
void(AL_APIENTRY * LPALGETSOURCE3I64SOFT)(ALuint, ALenum, ALint64SOFT *, ALint64SOFT *, ALint64SOFT *)
Definition: alext.h:333
void(AL_APIENTRY * LPALSOURCEI64SOFT)(ALuint, ALenum, ALint64SOFT)
Definition: alext.h:329
void * decodeAVAudioStream(StreamPtr stream, size_t *length)
Definition: alffmpeg.c:611
AL_API ALenum AL_APIENTRY alGetError(void)
Definition: alError.c:46
SDL_EventEntry * free
Definition: SDL_events.c:80
GLuint GLsizei const GLuint const GLintptr * offsets
Definition: glew.h:4750
AL_API void AL_APIENTRY alGetSourcei(ALuint source, ALenum param, ALint *value)
Definition: alSource.c:1754
#define AL_SOURCE_STATE
Definition: al.h:235
LPALGETSOURCEI64SOFT alGetSourcei64SOFT
Definition: allatency.c:57
void(AL_APIENTRY * LPALGETSOURCEI64SOFT)(ALuint, ALenum, ALint64SOFT *)
Definition: alext.h:332
void AL_APIENTRY wrap_BufferSamples(ALuint buffer, ALuint samplerate, ALenum internalformat, ALsizei samples, ALenum channels, ALenum type, const ALvoid *data)
Definition: alhelpers.c:253
LPALGETSOURCEDVSOFT alGetSourcedvSOFT
Definition: allatency.c:53
#define assert(x)
Definition: SDL_malloc.c:1234
LPALGETSOURCE3DSOFT alGetSource3dSOFT
Definition: allatency.c:52
void(AL_APIENTRY * LPALGETSOURCE3DSOFT)(ALuint, ALenum, ALdouble *, ALdouble *, ALdouble *)
Definition: alext.h:327
AL_API void AL_APIENTRY alGenSources(ALsizei n, ALuint *sources)
Definition: alSource.c:1216
#define AL_SEC_OFFSET_LATENCY_SOFT
Definition: alext.h:320
StreamPtr getAVAudioStream(FilePtr file, int streamnum)
Definition: alffmpeg.c:318
LPALSOURCE3DSOFT alSource3dSOFT
Definition: allatency.c:49
#define Sleep(x)
Definition: allatency.c:34
ALboolean(AL_APIENTRY * LPALISBUFFERFORMATSUPPORTEDSOFT)(ALenum)
Definition: alext.h:261
LPALGETSOURCEDSOFT alGetSourcedSOFT
Definition: allatency.c:51
EGLContext EGLenum EGLClientBuffer buffer
Definition: eglext.h:87
LPALSOURCE3I64SOFT alSource3i64SOFT
Definition: allatency.c:55
void(AL_APIENTRY * LPALSOURCE3DSOFT)(ALuint, ALenum, ALdouble, ALdouble, ALdouble)
Definition: alext.h:324
void(AL_APIENTRY * LPALGETSOURCEDVSOFT)(ALuint, ALenum, ALdouble *)
Definition: alext.h:328
double ALdouble
Definition: al.h:71
const char * TypeName(ALenum type)
Definition: alhelpers.c:279
void(AL_APIENTRY * LPALBUFFERSAMPLESSOFT)(ALuint, ALuint, ALenum, ALsizei, ALenum, ALenum, const ALvoid *)
Definition: alext.h:258
#define AL_PLAYING
Definition: al.h:239
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl2ext.h:848
#define LOAD_PROC(x)
LPALSOURCEDSOFT alSourcedSOFT
Definition: allatency.c:48
struct MyStream * StreamPtr
Definition: alffmpeg.h:14
void(AL_APIENTRY * LPALGETSOURCEI64VSOFT)(ALuint, ALenum, ALint64SOFT *)
Definition: alext.h:334
LPALSOURCEI64VSOFT alSourcei64vSOFT
Definition: allatency.c:56
int ALenum
Definition: al.h:65
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum format
Definition: gl2ext.h:845
unsigned int ALuint
Definition: al.h:59
AL_API void AL_APIENTRY alGenBuffers(ALsizei n, ALuint *buffers)
Definition: alBuffer.c:183
#define AL_BUFFER
Definition: al.h:182
LPALGETSOURCE3I64SOFT alGetSource3i64SOFT
Definition: allatency.c:58
ALsizei BytesToFrames(ALsizei size, ALenum channels, ALenum type)
Definition: alhelpers.c:324
LPALSOURCEI64SOFT alSourcei64SOFT
Definition: allatency.c:54
ALenum GetFormat(ALenum channels, ALenum type, LPALISBUFFERFORMATSUPPORTEDSOFT palIsBufferFormatSupportedSOFT)
Definition: alhelpers.c:93
static ALuint LoadSound(const char *filename)
Definition: allatency.c:63
const char * ChannelsName(ALenum chans)
Definition: alhelpers.c:264
AL_API ALboolean AL_APIENTRY alIsExtensionPresent(const ALchar *extname)
Definition: alExtension.c:49
void(AL_APIENTRY * LPALGETSOURCEDSOFT)(ALuint, ALenum, ALdouble *)
Definition: alext.h:326
LPALSOURCEDVSOFT alSourcedvSOFT
Definition: allatency.c:50
AL_API const ALchar *AL_APIENTRY alGetString(ALenum param)
Definition: alState.c:437
void(AL_APIENTRY * LPALSOURCE3I64SOFT)(ALuint, ALenum, ALint64SOFT, ALint64SOFT, ALint64SOFT)
Definition: alext.h:330
int InitAL(void)
Definition: alhelpers.c:42
void(AL_APIENTRY * LPALSOURCEDVSOFT)(ALuint, ALenum, const ALdouble *)
Definition: alext.h:325
LPALGETSOURCEI64VSOFT alGetSourcei64vSOFT
Definition: allatency.c:59
void(AL_APIENTRY * LPALSOURCEI64VSOFT)(ALuint, ALenum, const ALint64SOFT *)
Definition: alext.h:331
AL_API void AL_APIENTRY alDeleteBuffers(ALsizei n, const ALuint *buffers)
Definition: alBuffer.c:229
void closeAVFile(FilePtr file)
Definition: alffmpeg.c:271
AL_API void AL_APIENTRY alSourcei(ALuint source, ALenum param, ALint value)
Definition: alSource.c:1481
int getAVAudioInfo(StreamPtr stream, ALuint *rate, ALenum *channels, ALenum *type)
Definition: alffmpeg.c:392
GLsizei GLsizei GLchar * source
Definition: gl2ext.h:994
#define AL_NO_ERROR
Definition: al.h:364
LPALISBUFFERFORMATSUPPORTEDSOFT alIsBufferFormatSupportedSOFT
Definition: allatency.c:46
AL_API ALboolean AL_APIENTRY alIsBuffer(ALuint buffer)
Definition: alBuffer.c:272
AL_API void AL_APIENTRY alDeleteSources(ALsizei n, const ALuint *sources)
Definition: alSource.c:1262