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

344 lines
13 KiB

  1. /*
  2. * OpenAL Reverb 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 applying reverb to 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/alc.h"
  33. #include "AL/alext.h"
  34. #include "AL/efx.h"
  35. #include "AL/efx-presets.h"
  36. #include "common/alhelpers.h"
  37. /* Effect object functions */
  38. static LPALGENEFFECTS alGenEffects;
  39. static LPALDELETEEFFECTS alDeleteEffects;
  40. static LPALISEFFECT alIsEffect;
  41. static LPALEFFECTI alEffecti;
  42. static LPALEFFECTIV alEffectiv;
  43. static LPALEFFECTF alEffectf;
  44. static LPALEFFECTFV alEffectfv;
  45. static LPALGETEFFECTI alGetEffecti;
  46. static LPALGETEFFECTIV alGetEffectiv;
  47. static LPALGETEFFECTF alGetEffectf;
  48. static LPALGETEFFECTFV alGetEffectfv;
  49. /* Auxiliary Effect Slot object functions */
  50. static LPALGENAUXILIARYEFFECTSLOTS alGenAuxiliaryEffectSlots;
  51. static LPALDELETEAUXILIARYEFFECTSLOTS alDeleteAuxiliaryEffectSlots;
  52. static LPALISAUXILIARYEFFECTSLOT alIsAuxiliaryEffectSlot;
  53. static LPALAUXILIARYEFFECTSLOTI alAuxiliaryEffectSloti;
  54. static LPALAUXILIARYEFFECTSLOTIV alAuxiliaryEffectSlotiv;
  55. static LPALAUXILIARYEFFECTSLOTF alAuxiliaryEffectSlotf;
  56. static LPALAUXILIARYEFFECTSLOTFV alAuxiliaryEffectSlotfv;
  57. static LPALGETAUXILIARYEFFECTSLOTI alGetAuxiliaryEffectSloti;
  58. static LPALGETAUXILIARYEFFECTSLOTIV alGetAuxiliaryEffectSlotiv;
  59. static LPALGETAUXILIARYEFFECTSLOTF alGetAuxiliaryEffectSlotf;
  60. static LPALGETAUXILIARYEFFECTSLOTFV alGetAuxiliaryEffectSlotfv;
  61. /* LoadEffect loads the given reverb properties into a new OpenAL effect
  62. * object, and returns the new effect ID. */
  63. static ALuint LoadEffect(const EFXEAXREVERBPROPERTIES *reverb)
  64. {
  65. ALuint effect = 0;
  66. ALenum err;
  67. /* Create the effect object and check if we can do EAX reverb. */
  68. alGenEffects(1, &effect);
  69. if(alGetEnumValue("AL_EFFECT_EAXREVERB") != 0)
  70. {
  71. printf("Using EAX Reverb\n");
  72. /* EAX Reverb is available. Set the EAX effect type then load the
  73. * reverb properties. */
  74. alEffecti(effect, AL_EFFECT_TYPE, AL_EFFECT_EAXREVERB);
  75. alEffectf(effect, AL_EAXREVERB_DENSITY, reverb->flDensity);
  76. alEffectf(effect, AL_EAXREVERB_DIFFUSION, reverb->flDiffusion);
  77. alEffectf(effect, AL_EAXREVERB_GAIN, reverb->flGain);
  78. alEffectf(effect, AL_EAXREVERB_GAINHF, reverb->flGainHF);
  79. alEffectf(effect, AL_EAXREVERB_GAINLF, reverb->flGainLF);
  80. alEffectf(effect, AL_EAXREVERB_DECAY_TIME, reverb->flDecayTime);
  81. alEffectf(effect, AL_EAXREVERB_DECAY_HFRATIO, reverb->flDecayHFRatio);
  82. alEffectf(effect, AL_EAXREVERB_DECAY_LFRATIO, reverb->flDecayLFRatio);
  83. alEffectf(effect, AL_EAXREVERB_REFLECTIONS_GAIN, reverb->flReflectionsGain);
  84. alEffectf(effect, AL_EAXREVERB_REFLECTIONS_DELAY, reverb->flReflectionsDelay);
  85. alEffectfv(effect, AL_EAXREVERB_REFLECTIONS_PAN, reverb->flReflectionsPan);
  86. alEffectf(effect, AL_EAXREVERB_LATE_REVERB_GAIN, reverb->flLateReverbGain);
  87. alEffectf(effect, AL_EAXREVERB_LATE_REVERB_DELAY, reverb->flLateReverbDelay);
  88. alEffectfv(effect, AL_EAXREVERB_LATE_REVERB_PAN, reverb->flLateReverbPan);
  89. alEffectf(effect, AL_EAXREVERB_ECHO_TIME, reverb->flEchoTime);
  90. alEffectf(effect, AL_EAXREVERB_ECHO_DEPTH, reverb->flEchoDepth);
  91. alEffectf(effect, AL_EAXREVERB_MODULATION_TIME, reverb->flModulationTime);
  92. alEffectf(effect, AL_EAXREVERB_MODULATION_DEPTH, reverb->flModulationDepth);
  93. alEffectf(effect, AL_EAXREVERB_AIR_ABSORPTION_GAINHF, reverb->flAirAbsorptionGainHF);
  94. alEffectf(effect, AL_EAXREVERB_HFREFERENCE, reverb->flHFReference);
  95. alEffectf(effect, AL_EAXREVERB_LFREFERENCE, reverb->flLFReference);
  96. alEffectf(effect, AL_EAXREVERB_ROOM_ROLLOFF_FACTOR, reverb->flRoomRolloffFactor);
  97. alEffecti(effect, AL_EAXREVERB_DECAY_HFLIMIT, reverb->iDecayHFLimit);
  98. }
  99. else
  100. {
  101. printf("Using Standard Reverb\n");
  102. /* No EAX Reverb. Set the standard reverb effect type then load the
  103. * available reverb properties. */
  104. alEffecti(effect, AL_EFFECT_TYPE, AL_EFFECT_REVERB);
  105. alEffectf(effect, AL_REVERB_DENSITY, reverb->flDensity);
  106. alEffectf(effect, AL_REVERB_DIFFUSION, reverb->flDiffusion);
  107. alEffectf(effect, AL_REVERB_GAIN, reverb->flGain);
  108. alEffectf(effect, AL_REVERB_GAINHF, reverb->flGainHF);
  109. alEffectf(effect, AL_REVERB_DECAY_TIME, reverb->flDecayTime);
  110. alEffectf(effect, AL_REVERB_DECAY_HFRATIO, reverb->flDecayHFRatio);
  111. alEffectf(effect, AL_REVERB_REFLECTIONS_GAIN, reverb->flReflectionsGain);
  112. alEffectf(effect, AL_REVERB_REFLECTIONS_DELAY, reverb->flReflectionsDelay);
  113. alEffectf(effect, AL_REVERB_LATE_REVERB_GAIN, reverb->flLateReverbGain);
  114. alEffectf(effect, AL_REVERB_LATE_REVERB_DELAY, reverb->flLateReverbDelay);
  115. alEffectf(effect, AL_REVERB_AIR_ABSORPTION_GAINHF, reverb->flAirAbsorptionGainHF);
  116. alEffectf(effect, AL_REVERB_ROOM_ROLLOFF_FACTOR, reverb->flRoomRolloffFactor);
  117. alEffecti(effect, AL_REVERB_DECAY_HFLIMIT, reverb->iDecayHFLimit);
  118. }
  119. /* Check if an error occured, and clean up if so. */
  120. err = alGetError();
  121. if(err != AL_NO_ERROR)
  122. {
  123. fprintf(stderr, "OpenAL error: %s\n", alGetString(err));
  124. if(alIsEffect(effect))
  125. alDeleteEffects(1, &effect);
  126. return 0;
  127. }
  128. return effect;
  129. }
  130. /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
  131. * returns the new buffer ID.
  132. */
  133. static ALuint LoadSound(const char *filename)
  134. {
  135. ALenum err, format;
  136. ALuint buffer;
  137. SNDFILE *sndfile;
  138. SF_INFO sfinfo;
  139. short *membuf;
  140. sf_count_t num_frames;
  141. ALsizei num_bytes;
  142. /* Open the audio file and check that it's usable. */
  143. sndfile = sf_open(filename, SFM_READ, &sfinfo);
  144. if(!sndfile)
  145. {
  146. fprintf(stderr, "Could not open audio in %s: %s\n", filename, sf_strerror(sndfile));
  147. return 0;
  148. }
  149. if(sfinfo.frames < 1 || sfinfo.frames > (sf_count_t)(INT_MAX/sizeof(short))/sfinfo.channels)
  150. {
  151. fprintf(stderr, "Bad sample count in %s (%" PRId64 ")\n", filename, sfinfo.frames);
  152. sf_close(sndfile);
  153. return 0;
  154. }
  155. /* Get the sound format, and figure out the OpenAL format */
  156. format = AL_NONE;
  157. if(sfinfo.channels == 1)
  158. format = AL_FORMAT_MONO16;
  159. else if(sfinfo.channels == 2)
  160. format = AL_FORMAT_STEREO16;
  161. else if(sfinfo.channels == 3)
  162. {
  163. if(sf_command(sndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT)
  164. format = AL_FORMAT_BFORMAT2D_16;
  165. }
  166. else if(sfinfo.channels == 4)
  167. {
  168. if(sf_command(sndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT)
  169. format = AL_FORMAT_BFORMAT3D_16;
  170. }
  171. if(!format)
  172. {
  173. fprintf(stderr, "Unsupported channel count: %d\n", sfinfo.channels);
  174. sf_close(sndfile);
  175. return 0;
  176. }
  177. /* Decode the whole audio file to a buffer. */
  178. membuf = malloc((size_t)(sfinfo.frames * sfinfo.channels) * sizeof(short));
  179. num_frames = sf_readf_short(sndfile, membuf, sfinfo.frames);
  180. if(num_frames < 1)
  181. {
  182. free(membuf);
  183. sf_close(sndfile);
  184. fprintf(stderr, "Failed to read samples in %s (%" PRId64 ")\n", filename, num_frames);
  185. return 0;
  186. }
  187. num_bytes = (ALsizei)(num_frames * sfinfo.channels) * (ALsizei)sizeof(short);
  188. /* Buffer the audio data into a new buffer object, then free the data and
  189. * close the file.
  190. */
  191. buffer = 0;
  192. alGenBuffers(1, &buffer);
  193. alBufferData(buffer, format, membuf, num_bytes, sfinfo.samplerate);
  194. free(membuf);
  195. sf_close(sndfile);
  196. /* Check if an error occured, and clean up if so. */
  197. err = alGetError();
  198. if(err != AL_NO_ERROR)
  199. {
  200. fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
  201. if(buffer && alIsBuffer(buffer))
  202. alDeleteBuffers(1, &buffer);
  203. return 0;
  204. }
  205. return buffer;
  206. }
  207. int main(int argc, char **argv)
  208. {
  209. EFXEAXREVERBPROPERTIES reverb = EFX_REVERB_PRESET_GENERIC;
  210. ALuint source, buffer, effect, slot;
  211. ALenum state;
  212. /* Print out usage if no arguments were specified */
  213. if(argc < 2)
  214. {
  215. fprintf(stderr, "Usage: %s [-device <name] <filename>\n", argv[0]);
  216. return 1;
  217. }
  218. /* Initialize OpenAL, and check for EFX support. */
  219. argv++; argc--;
  220. if(InitAL(&argv, &argc) != 0)
  221. return 1;
  222. if(!alcIsExtensionPresent(alcGetContextsDevice(alcGetCurrentContext()), "ALC_EXT_EFX"))
  223. {
  224. fprintf(stderr, "Error: EFX not supported\n");
  225. CloseAL();
  226. return 1;
  227. }
  228. /* Define a macro to help load the function pointers. */
  229. #define LOAD_PROC(T, x) ((x) = FUNCTION_CAST(T, alGetProcAddress(#x)))
  230. LOAD_PROC(LPALGENEFFECTS, alGenEffects);
  231. LOAD_PROC(LPALDELETEEFFECTS, alDeleteEffects);
  232. LOAD_PROC(LPALISEFFECT, alIsEffect);
  233. LOAD_PROC(LPALEFFECTI, alEffecti);
  234. LOAD_PROC(LPALEFFECTIV, alEffectiv);
  235. LOAD_PROC(LPALEFFECTF, alEffectf);
  236. LOAD_PROC(LPALEFFECTFV, alEffectfv);
  237. LOAD_PROC(LPALGETEFFECTI, alGetEffecti);
  238. LOAD_PROC(LPALGETEFFECTIV, alGetEffectiv);
  239. LOAD_PROC(LPALGETEFFECTF, alGetEffectf);
  240. LOAD_PROC(LPALGETEFFECTFV, alGetEffectfv);
  241. LOAD_PROC(LPALGENAUXILIARYEFFECTSLOTS, alGenAuxiliaryEffectSlots);
  242. LOAD_PROC(LPALDELETEAUXILIARYEFFECTSLOTS, alDeleteAuxiliaryEffectSlots);
  243. LOAD_PROC(LPALISAUXILIARYEFFECTSLOT, alIsAuxiliaryEffectSlot);
  244. LOAD_PROC(LPALAUXILIARYEFFECTSLOTI, alAuxiliaryEffectSloti);
  245. LOAD_PROC(LPALAUXILIARYEFFECTSLOTIV, alAuxiliaryEffectSlotiv);
  246. LOAD_PROC(LPALAUXILIARYEFFECTSLOTF, alAuxiliaryEffectSlotf);
  247. LOAD_PROC(LPALAUXILIARYEFFECTSLOTFV, alAuxiliaryEffectSlotfv);
  248. LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTI, alGetAuxiliaryEffectSloti);
  249. LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTIV, alGetAuxiliaryEffectSlotiv);
  250. LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTF, alGetAuxiliaryEffectSlotf);
  251. LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTFV, alGetAuxiliaryEffectSlotfv);
  252. #undef LOAD_PROC
  253. /* Load the sound into a buffer. */
  254. buffer = LoadSound(argv[0]);
  255. if(!buffer)
  256. {
  257. CloseAL();
  258. return 1;
  259. }
  260. /* Load the reverb into an effect. */
  261. effect = LoadEffect(&reverb);
  262. if(!effect)
  263. {
  264. alDeleteBuffers(1, &buffer);
  265. CloseAL();
  266. return 1;
  267. }
  268. /* Create the effect slot object. This is what "plays" an effect on sources
  269. * that connect to it. */
  270. slot = 0;
  271. alGenAuxiliaryEffectSlots(1, &slot);
  272. /* Tell the effect slot to use the loaded effect object. Note that the this
  273. * effectively copies the effect properties. You can modify or delete the
  274. * effect object afterward without affecting the effect slot.
  275. */
  276. alAuxiliaryEffectSloti(slot, AL_EFFECTSLOT_EFFECT, (ALint)effect);
  277. assert(alGetError()==AL_NO_ERROR && "Failed to set effect slot");
  278. /* Create the source to play the sound with. */
  279. source = 0;
  280. alGenSources(1, &source);
  281. alSourcei(source, AL_BUFFER, (ALint)buffer);
  282. /* Connect the source to the effect slot. This tells the source to use the
  283. * effect slot 'slot', on send #0 with the AL_FILTER_NULL filter object.
  284. */
  285. alSource3i(source, AL_AUXILIARY_SEND_FILTER, (ALint)slot, 0, AL_FILTER_NULL);
  286. assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
  287. /* Play the sound until it finishes. */
  288. alSourcePlay(source);
  289. do {
  290. al_nssleep(10000000);
  291. alGetSourcei(source, AL_SOURCE_STATE, &state);
  292. } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
  293. /* All done. Delete resources, and close down OpenAL. */
  294. alDeleteSources(1, &source);
  295. alDeleteAuxiliaryEffectSlots(1, &slot);
  296. alDeleteEffects(1, &effect);
  297. alDeleteBuffers(1, &buffer);
  298. CloseAL();
  299. return 0;
  300. }