🛠️🐜 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.

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