alhrtf.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * OpenAL HRTF Example
  3. *
  4. * Copyright (c) 2015 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 selecting an HRTF. */
  25. #include <stdio.h>
  26. #include <assert.h>
  27. #include <math.h>
  28. #include "AL/al.h"
  29. #include "AL/alc.h"
  30. #include "AL/alext.h"
  31. #include "common/alhelpers.h"
  32. #include "common/sdl_sound.h"
  33. #ifndef M_PI
  34. #define M_PI (3.14159265358979323846)
  35. #endif
  36. static LPALCGETSTRINGISOFT alcGetStringiSOFT;
  37. static LPALCRESETDEVICESOFT alcResetDeviceSOFT;
  38. /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
  39. * returns the new buffer ID. */
  40. static ALuint LoadSound(const char *filename)
  41. {
  42. ALenum err, format, type, channels;
  43. ALuint rate, buffer;
  44. size_t datalen;
  45. void *data;
  46. FilePtr sound;
  47. /* Open the audio file */
  48. sound = openAudioFile(filename, 1000);
  49. if(!sound)
  50. {
  51. fprintf(stderr, "Could not open audio in %s\n", filename);
  52. closeAudioFile(sound);
  53. return 0;
  54. }
  55. /* Get the sound format, and figure out the OpenAL format */
  56. if(getAudioInfo(sound, &rate, &channels, &type) != 0)
  57. {
  58. fprintf(stderr, "Error getting audio info for %s\n", filename);
  59. closeAudioFile(sound);
  60. return 0;
  61. }
  62. format = GetFormat(channels, type, NULL);
  63. if(format == AL_NONE)
  64. {
  65. fprintf(stderr, "Unsupported format (%s, %s) for %s\n",
  66. ChannelsName(channels), TypeName(type), filename);
  67. closeAudioFile(sound);
  68. return 0;
  69. }
  70. /* Decode the whole audio stream to a buffer. */
  71. data = decodeAudioStream(sound, &datalen);
  72. if(!data)
  73. {
  74. fprintf(stderr, "Failed to read audio from %s\n", filename);
  75. closeAudioFile(sound);
  76. return 0;
  77. }
  78. /* Buffer the audio data into a new buffer object, then free the data and
  79. * close the file. */
  80. buffer = 0;
  81. alGenBuffers(1, &buffer);
  82. alBufferData(buffer, format, data, datalen, rate);
  83. free(data);
  84. closeAudioFile(sound);
  85. /* Check if an error occured, and clean up if so. */
  86. err = alGetError();
  87. if(err != AL_NO_ERROR)
  88. {
  89. fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
  90. if(buffer && alIsBuffer(buffer))
  91. alDeleteBuffers(1, &buffer);
  92. return 0;
  93. }
  94. return buffer;
  95. }
  96. int main(int argc, char **argv)
  97. {
  98. ALCdevice *device;
  99. ALuint source, buffer;
  100. const char *soundname;
  101. const char *hrtfname;
  102. ALCint hrtf_state;
  103. ALCint num_hrtf;
  104. ALdouble angle;
  105. ALenum state;
  106. /* Print out usage if no file was specified */
  107. if(argc < 2 || (strcmp(argv[1], "-hrtf") == 0 && argc < 4))
  108. {
  109. fprintf(stderr, "Usage: %s [-hrtf <name>] <soundfile>\n", argv[0]);
  110. return 1;
  111. }
  112. /* Initialize OpenAL with the default device, and check for HRTF support. */
  113. if(InitAL() != 0)
  114. return 1;
  115. if(strcmp(argv[1], "-hrtf") == 0)
  116. {
  117. hrtfname = argv[2];
  118. soundname = argv[3];
  119. }
  120. else
  121. {
  122. hrtfname = NULL;
  123. soundname = argv[1];
  124. }
  125. device = alcGetContextsDevice(alcGetCurrentContext());
  126. if(!alcIsExtensionPresent(device, "ALC_SOFT_HRTF"))
  127. {
  128. fprintf(stderr, "Error: ALC_SOFT_HRTF not supported\n");
  129. CloseAL();
  130. return 1;
  131. }
  132. /* Define a macro to help load the function pointers. */
  133. #define LOAD_PROC(d, x) ((x) = alcGetProcAddress((d), #x))
  134. LOAD_PROC(device, alcGetStringiSOFT);
  135. LOAD_PROC(device, alcResetDeviceSOFT);
  136. #undef LOAD_PROC
  137. /* Enumerate available HRTFs, and reset the device using one. */
  138. alcGetIntegerv(device, ALC_NUM_HRTF_SPECIFIERS_SOFT, 1, &num_hrtf);
  139. if(!num_hrtf)
  140. printf("No HRTFs found\n");
  141. else
  142. {
  143. ALCint attr[5];
  144. ALCint index = -1;
  145. ALCint i;
  146. printf("Available HRTFs:\n");
  147. for(i = 0;i < num_hrtf;i++)
  148. {
  149. const ALCchar *name = alcGetStringiSOFT(device, ALC_HRTF_SPECIFIER_SOFT, i);
  150. printf(" %d: %s\n", i, name);
  151. /* Check if this is the HRTF the user requested. */
  152. if(hrtfname && strcmp(name, hrtfname) == 0)
  153. index = i;
  154. }
  155. if(index == -1)
  156. {
  157. if(hrtfname)
  158. printf("HRTF \"%s\" not found\n", hrtfname);
  159. index = 0;
  160. }
  161. printf("Selecting HRTF %d...\n", index);
  162. attr[0] = ALC_HRTF_SOFT;
  163. attr[1] = ALC_TRUE;
  164. attr[2] = ALC_HRTF_ID_SOFT;
  165. attr[3] = index;
  166. attr[4] = 0;
  167. if(!alcResetDeviceSOFT(device, attr))
  168. printf("Failed to reset device: %s\n", alcGetString(device, alcGetError(device)));
  169. }
  170. /* Check if HRTF is enabled, and show which is being used. */
  171. alcGetIntegerv(device, ALC_HRTF_SOFT, 1, &hrtf_state);
  172. if(!hrtf_state)
  173. printf("HRTF not enabled!\n");
  174. else
  175. {
  176. const ALchar *name = alcGetString(device, ALC_HRTF_SPECIFIER_SOFT);
  177. printf("HRTF enabled, using %s\n", name);
  178. }
  179. fflush(stdout);
  180. /* Load the sound into a buffer. */
  181. buffer = LoadSound(soundname);
  182. if(!buffer)
  183. {
  184. CloseAL();
  185. return 1;
  186. }
  187. /* Create the source to play the sound with. */
  188. source = 0;
  189. alGenSources(1, &source);
  190. alSourcei(source, AL_SOURCE_RELATIVE, AL_TRUE);
  191. alSource3f(source, AL_POSITION, 0.0f, 0.0f, -1.0f);
  192. alSourcei(source, AL_BUFFER, buffer);
  193. assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
  194. /* Play the sound until it finishes. */
  195. angle = 0.0;
  196. alSourcePlay(source);
  197. do {
  198. al_nssleep(10000000);
  199. /* Rotate the source around the listener by about 1/4 cycle per second.
  200. * Only affects mono sounds.
  201. */
  202. angle += 0.01 * M_PI * 0.5;
  203. alSource3f(source, AL_POSITION, (ALfloat)sin(angle), 0.0f, -(ALfloat)cos(angle));
  204. alGetSourcei(source, AL_SOURCE_STATE, &state);
  205. } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
  206. /* All done. Delete resources, and close OpenAL. */
  207. alDeleteSources(1, &source);
  208. alDeleteBuffers(1, &buffer);
  209. CloseAL();
  210. return 0;
  211. }