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

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