🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
7.0 KiB

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