alreverb.c 12 KB

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