allatency.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 <assert.h>
  26. #include <inttypes.h>
  27. #include <limits.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include "sndfile.h"
  31. #include "AL/al.h"
  32. #include "AL/alext.h"
  33. #include "common/alhelpers.h"
  34. #include "win_main_utf8.h"
  35. static LPALSOURCEDSOFT alSourcedSOFT;
  36. static LPALSOURCE3DSOFT alSource3dSOFT;
  37. static LPALSOURCEDVSOFT alSourcedvSOFT;
  38. static LPALGETSOURCEDSOFT alGetSourcedSOFT;
  39. static LPALGETSOURCE3DSOFT alGetSource3dSOFT;
  40. static LPALGETSOURCEDVSOFT alGetSourcedvSOFT;
  41. static LPALSOURCEI64SOFT alSourcei64SOFT;
  42. static LPALSOURCE3I64SOFT alSource3i64SOFT;
  43. static LPALSOURCEI64VSOFT alSourcei64vSOFT;
  44. static LPALGETSOURCEI64SOFT alGetSourcei64SOFT;
  45. static LPALGETSOURCE3I64SOFT alGetSource3i64SOFT;
  46. static LPALGETSOURCEI64VSOFT alGetSourcei64vSOFT;
  47. /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
  48. * returns the new buffer ID.
  49. */
  50. static ALuint LoadSound(const char *filename)
  51. {
  52. ALenum err, format;
  53. ALuint buffer;
  54. SNDFILE *sndfile;
  55. SF_INFO sfinfo;
  56. short *membuf;
  57. sf_count_t num_frames;
  58. ALsizei num_bytes;
  59. /* Open the audio file and check that it's usable. */
  60. sndfile = sf_open(filename, SFM_READ, &sfinfo);
  61. if(!sndfile)
  62. {
  63. fprintf(stderr, "Could not open audio in %s: %s\n", filename, sf_strerror(sndfile));
  64. return 0;
  65. }
  66. if(sfinfo.frames < 1 || sfinfo.frames > (sf_count_t)(INT_MAX/sizeof(short))/sfinfo.channels)
  67. {
  68. fprintf(stderr, "Bad sample count in %s (%" PRId64 ")\n", filename, sfinfo.frames);
  69. sf_close(sndfile);
  70. return 0;
  71. }
  72. /* Get the sound format, and figure out the OpenAL format */
  73. format = AL_NONE;
  74. if(sfinfo.channels == 1)
  75. format = AL_FORMAT_MONO16;
  76. else if(sfinfo.channels == 2)
  77. format = AL_FORMAT_STEREO16;
  78. else if(sfinfo.channels == 3)
  79. {
  80. if(sf_command(sndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT)
  81. format = AL_FORMAT_BFORMAT2D_16;
  82. }
  83. else if(sfinfo.channels == 4)
  84. {
  85. if(sf_command(sndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT)
  86. format = AL_FORMAT_BFORMAT3D_16;
  87. }
  88. if(!format)
  89. {
  90. fprintf(stderr, "Unsupported channel count: %d\n", sfinfo.channels);
  91. sf_close(sndfile);
  92. return 0;
  93. }
  94. /* Decode the whole audio file to a buffer. */
  95. 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)
  98. {
  99. free(membuf);
  100. sf_close(sndfile);
  101. fprintf(stderr, "Failed to read samples in %s (%" PRId64 ")\n", filename, num_frames);
  102. return 0;
  103. }
  104. num_bytes = (ALsizei)(num_frames * sfinfo.channels) * (ALsizei)sizeof(short);
  105. /* Buffer the audio data into a new buffer object, then free the data and
  106. * close the file.
  107. */
  108. buffer = 0;
  109. alGenBuffers(1, &buffer);
  110. alBufferData(buffer, format, membuf, num_bytes, sfinfo.samplerate);
  111. free(membuf);
  112. sf_close(sndfile);
  113. /* Check if an error occurred, and clean up if so. */
  114. err = alGetError();
  115. if(err != AL_NO_ERROR)
  116. {
  117. fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
  118. if(buffer && alIsBuffer(buffer))
  119. alDeleteBuffers(1, &buffer);
  120. return 0;
  121. }
  122. return buffer;
  123. }
  124. int main(int argc, char **argv)
  125. {
  126. ALuint source, buffer;
  127. ALdouble offsets[2];
  128. ALenum state;
  129. /* Print out usage if no arguments were specified */
  130. if(argc < 2)
  131. {
  132. fprintf(stderr, "Usage: %s [-device <name>] <filename>\n", argv[0]);
  133. return 1;
  134. }
  135. /* Initialize OpenAL, and check for source_latency support. */
  136. argv++; argc--;
  137. if(InitAL(&argv, &argc) != 0)
  138. return 1;
  139. if(!alIsExtensionPresent("AL_SOFT_source_latency"))
  140. {
  141. fprintf(stderr, "Error: AL_SOFT_source_latency not supported\n");
  142. CloseAL();
  143. return 1;
  144. }
  145. /* Define a macro to help load the function pointers. */
  146. #define LOAD_PROC(T, x) ((x) = FUNCTION_CAST(T, alGetProcAddress(#x)))
  147. LOAD_PROC(LPALSOURCEDSOFT, alSourcedSOFT);
  148. LOAD_PROC(LPALSOURCE3DSOFT, alSource3dSOFT);
  149. LOAD_PROC(LPALSOURCEDVSOFT, alSourcedvSOFT);
  150. LOAD_PROC(LPALGETSOURCEDSOFT, alGetSourcedSOFT);
  151. LOAD_PROC(LPALGETSOURCE3DSOFT, alGetSource3dSOFT);
  152. LOAD_PROC(LPALGETSOURCEDVSOFT, alGetSourcedvSOFT);
  153. LOAD_PROC(LPALSOURCEI64SOFT, alSourcei64SOFT);
  154. LOAD_PROC(LPALSOURCE3I64SOFT, alSource3i64SOFT);
  155. LOAD_PROC(LPALSOURCEI64VSOFT, alSourcei64vSOFT);
  156. LOAD_PROC(LPALGETSOURCEI64SOFT, alGetSourcei64SOFT);
  157. LOAD_PROC(LPALGETSOURCE3I64SOFT, alGetSource3i64SOFT);
  158. LOAD_PROC(LPALGETSOURCEI64VSOFT, alGetSourcei64vSOFT);
  159. #undef LOAD_PROC
  160. /* Load the sound into a buffer. */
  161. buffer = LoadSound(argv[0]);
  162. if(!buffer)
  163. {
  164. CloseAL();
  165. return 1;
  166. }
  167. /* Create the source to play the sound with. */
  168. source = 0;
  169. alGenSources(1, &source);
  170. alSourcei(source, AL_BUFFER, (ALint)buffer);
  171. assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
  172. /* Play the sound until it finishes. */
  173. alSourcePlay(source);
  174. do {
  175. al_nssleep(10000000);
  176. alGetSourcei(source, AL_SOURCE_STATE, &state);
  177. /* Get the source offset and latency. AL_SEC_OFFSET_LATENCY_SOFT will
  178. * place the offset (in seconds) in offsets[0], and the time until that
  179. * offset will be heard (in seconds) in offsets[1]. */
  180. alGetSourcedvSOFT(source, AL_SEC_OFFSET_LATENCY_SOFT, offsets);
  181. printf("\rOffset: %f - Latency:%3u ms ", offsets[0], (ALuint)(offsets[1]*1000));
  182. fflush(stdout);
  183. } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
  184. printf("\n");
  185. /* All done. Delete resources, and close down OpenAL. */
  186. alDeleteSources(1, &source);
  187. alDeleteBuffers(1, &buffer);
  188. CloseAL();
  189. return 0;
  190. }