allatency.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * OpenAL Source Latency Example
  3. *
  4. * Copyright (c) 2012 by Chris Robinson <[email protected]>
  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. /* This file contains an example for checking the latency of a sound. */
  25. #include <stdio.h>
  26. #include <assert.h>
  27. #include "AL/al.h"
  28. #include "AL/alc.h"
  29. #include "AL/alext.h"
  30. #include "common/alhelpers.h"
  31. #include "common/sdl_sound.h"
  32. static LPALBUFFERSAMPLESSOFT alBufferSamplesSOFT = wrap_BufferSamples;
  33. static LPALISBUFFERFORMATSUPPORTEDSOFT alIsBufferFormatSupportedSOFT;
  34. static LPALSOURCEDSOFT alSourcedSOFT;
  35. static LPALSOURCE3DSOFT alSource3dSOFT;
  36. static LPALSOURCEDVSOFT alSourcedvSOFT;
  37. static LPALGETSOURCEDSOFT alGetSourcedSOFT;
  38. static LPALGETSOURCE3DSOFT alGetSource3dSOFT;
  39. static LPALGETSOURCEDVSOFT alGetSourcedvSOFT;
  40. static LPALSOURCEI64SOFT alSourcei64SOFT;
  41. static LPALSOURCE3I64SOFT alSource3i64SOFT;
  42. static LPALSOURCEI64VSOFT alSourcei64vSOFT;
  43. static LPALGETSOURCEI64SOFT alGetSourcei64SOFT;
  44. static LPALGETSOURCE3I64SOFT alGetSource3i64SOFT;
  45. static LPALGETSOURCEI64VSOFT alGetSourcei64vSOFT;
  46. /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
  47. * returns the new buffer ID. */
  48. static ALuint LoadSound(const char *filename)
  49. {
  50. ALenum err, format, type, channels;
  51. ALuint rate, buffer;
  52. size_t datalen;
  53. void *data;
  54. FilePtr sound;
  55. /* Open the audio file */
  56. sound = openAudioFile(filename, 1000);
  57. if(!sound)
  58. {
  59. fprintf(stderr, "Could not open audio in %s\n", filename);
  60. closeAudioFile(sound);
  61. return 0;
  62. }
  63. /* Get the sound format, and figure out the OpenAL format */
  64. if(getAudioInfo(sound, &rate, &channels, &type) != 0)
  65. {
  66. fprintf(stderr, "Error getting audio info for %s\n", filename);
  67. closeAudioFile(sound);
  68. return 0;
  69. }
  70. format = GetFormat(channels, type, alIsBufferFormatSupportedSOFT);
  71. if(format == AL_NONE)
  72. {
  73. fprintf(stderr, "Unsupported format (%s, %s) for %s\n",
  74. ChannelsName(channels), TypeName(type), filename);
  75. closeAudioFile(sound);
  76. return 0;
  77. }
  78. /* Decode the whole audio stream to a buffer. */
  79. data = decodeAudioStream(sound, &datalen);
  80. if(!data)
  81. {
  82. fprintf(stderr, "Failed to read audio from %s\n", filename);
  83. closeAudioFile(sound);
  84. return 0;
  85. }
  86. /* Buffer the audio data into a new buffer object, then free the data and
  87. * close the file. */
  88. buffer = 0;
  89. alGenBuffers(1, &buffer);
  90. alBufferSamplesSOFT(buffer, rate, format, BytesToFrames(datalen, channels, type),
  91. channels, type, data);
  92. free(data);
  93. closeAudioFile(sound);
  94. /* Check if an error occured, and clean up if so. */
  95. err = alGetError();
  96. if(err != AL_NO_ERROR)
  97. {
  98. fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
  99. if(alIsBuffer(buffer))
  100. alDeleteBuffers(1, &buffer);
  101. return 0;
  102. }
  103. return buffer;
  104. }
  105. int main(int argc, char **argv)
  106. {
  107. ALuint source, buffer;
  108. ALdouble offsets[2];
  109. ALenum state;
  110. /* Print out usage if no file was specified */
  111. if(argc < 2)
  112. {
  113. fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
  114. return 1;
  115. }
  116. /* Initialize OpenAL with the default device, and check for EFX support. */
  117. if(InitAL() != 0)
  118. return 1;
  119. if(!alIsExtensionPresent("AL_SOFT_source_latency"))
  120. {
  121. fprintf(stderr, "Error: AL_SOFT_source_latency not supported\n");
  122. CloseAL();
  123. return 1;
  124. }
  125. /* Define a macro to help load the function pointers. */
  126. #define LOAD_PROC(x) ((x) = alGetProcAddress(#x))
  127. LOAD_PROC(alSourcedSOFT);
  128. LOAD_PROC(alSource3dSOFT);
  129. LOAD_PROC(alSourcedvSOFT);
  130. LOAD_PROC(alGetSourcedSOFT);
  131. LOAD_PROC(alGetSource3dSOFT);
  132. LOAD_PROC(alGetSourcedvSOFT);
  133. LOAD_PROC(alSourcei64SOFT);
  134. LOAD_PROC(alSource3i64SOFT);
  135. LOAD_PROC(alSourcei64vSOFT);
  136. LOAD_PROC(alGetSourcei64SOFT);
  137. LOAD_PROC(alGetSource3i64SOFT);
  138. LOAD_PROC(alGetSourcei64vSOFT);
  139. if(alIsExtensionPresent("AL_SOFT_buffer_samples"))
  140. {
  141. LOAD_PROC(alBufferSamplesSOFT);
  142. LOAD_PROC(alIsBufferFormatSupportedSOFT);
  143. }
  144. #undef LOAD_PROC
  145. /* Load the sound into a buffer. */
  146. buffer = LoadSound(argv[1]);
  147. if(!buffer)
  148. {
  149. CloseAL();
  150. return 1;
  151. }
  152. /* Create the source to play the sound with. */
  153. source = 0;
  154. alGenSources(1, &source);
  155. alSourcei(source, AL_BUFFER, buffer);
  156. assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
  157. /* Play the sound until it finishes. */
  158. alSourcePlay(source);
  159. do {
  160. Sleep(10);
  161. alGetSourcei(source, AL_SOURCE_STATE, &state);
  162. /* Get the source offset and latency. AL_SEC_OFFSET_LATENCY_SOFT will
  163. * place the offset (in seconds) in offsets[0], and the time until that
  164. * offset will be heard (in seconds) in offsets[1]. */
  165. alGetSourcedvSOFT(source, AL_SEC_OFFSET_LATENCY_SOFT, offsets);
  166. printf("\rOffset: %f - Latency:%3u ms ", offsets[0], (ALuint)(offsets[1]*1000));
  167. fflush(stdout);
  168. } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
  169. printf("\n");
  170. /* All done. Delete resources, and close OpenAL. */
  171. alDeleteSources(1, &source);
  172. alDeleteBuffers(1, &buffer);
  173. CloseAL();
  174. return 0;
  175. }