OpenAL is a free cross-platform 3D audio API appropriate for use with gaming applications and many other types of audio applications.
http://www.leadwerks.com/post/openal.zip
http://www.leadwerks.com/post/openal.zip
Strict Import pub.openal Global ALDevice = alcOpenDevice (Null) Global ALContext = alcCreateContext (ALDevice, Null) alcMakeContextCurrent (ALContext) alinit() ' auto cleanup OnEnd OpenALRelease Function OpenALRelease() alcDestroyContext (ALContext) alcCloseDevice (ALDevice) EndFunction 'Create buffers Local buffer alGenBuffers(1,Varptr(buffer)) If Not buffer RuntimeError "Failed to create buffer." 'Create buffers Local source alGenSources(1,Varptr(source)) If Not source RuntimeError "Failed to create source." Local bbsound:TAudioSample = LoadAudioSample(RequestFile("")) If Not bbsound RuntimeError "Failed to load sound "+Chr(34)+AppDir$+"/online.wav"+Chr(34)+"." Local format Local samples = Int(bbsound.samples) Local length = bbsound.length Local freq = bbsound.hertz Select bbsound.format Case SF_MONO8 ; format = AL_FORMAT_MONO8 Case SF_MONO16LE ; format = AL_FORMAT_MONO16 ; length:*2 Case SF_MONO16BE ; format = AL_FORMAT_MONO16 ; length:*2 Case SF_STEREO8 ; format = AL_FORMAT_STEREO8 ; length:*2 Case SF_STEREO16LE ; format = AL_FORMAT_STEREO16 ; length:*4 Case SF_STEREO16BE ; format = AL_FORMAT_STEREO16 ; length:*4 EndSelect alBufferData buffer,format,samples,length,freq alSourcei(source,AL_BUFFER,buffer) alSourcePlay source Delay 10000 Function alError$() Select alGetError() Case AL_INVALID_NAME Return "AL_INVALID_NAME" Case AL_ILLEGAL_ENUM Return "AL_ILLEGAL_ENUM" Case AL_INVALID_ENUM Return "AL_INVALID_ENUM" Case AL_INVALID_VALUE Return "AL_INVALID_VALUE" Case AL_ILLEGAL_COMMAND Return "AL_ILLEGAL_COMMAND" Case AL_INVALID_OPERATION Return "AL_INVALID_OPERATION" Case AL_OUT_OF_MEMORY Return "AL_OUT_OF_MEMORY" EndSelect EndFunction
Strict Import pub.openal Global ALDevice = alcOpenDevice (Null) Global ALContext = alcCreateContext (ALDevice, [ALC_MAX_AUXILIARY_SENDS,4]) alcMakeContextCurrent (ALContext) alinit() ' auto cleanup OnEnd OpenALRelease Function OpenALRelease() alcDestroyContext (ALContext) alcCloseDevice (ALDevice) EndFunction If Not alcIsExtensionPresent(ALDevice, "ALC_EXT_EFX") Then RuntimeError "EFX not supported" 'Create buffers Local buffer alGenBuffers(1,Varptr(buffer)) If Not buffer RuntimeError "Failed to create buffer." 'Create sources Local source alGenSources(1,Varptr(source)) If Not source RuntimeError "Failed to create source." Local bbsound:TAudioSample = LoadAudioSample(RequestFile("")) If Not bbsound RuntimeError "Failed to load sound" Local format Local samples = Int(bbsound.samples) Local length = bbsound.length Local freq = bbsound.hertz Select bbsound.format Case SF_MONO8 ; format = AL_FORMAT_MONO8 Case SF_MONO16LE ; format = AL_FORMAT_MONO16 ; length:*2 Case SF_MONO16BE ; format = AL_FORMAT_MONO16 ; length:*2 Case SF_STEREO8 ; format = AL_FORMAT_STEREO8 ; length:*2 Case SF_STEREO16LE ; format = AL_FORMAT_STEREO16 ; length:*4 Case SF_STEREO16BE ; format = AL_FORMAT_STEREO16 ; length:*4 EndSelect alBufferData buffer,format,samples,length,freq alSourcei(source,AL_BUFFER,buffer) Local efxReverb:EFXEAXREVERBPROPERTIES = New EFXEAXREVERBPROPERTIES Local uiEffectSlot, uiEffect Print("~n- EFX Reverb Application -~n"); ' Clear AL Error State alGetError(); ' PLAY WITH NO EFFECT Print("Playing Source without Effects~n") PlaySource(Source) ' CREATE EAXREVERB EFFECT SLOT If CreateAuxEffectSlot(uiEffectSlot) If Not CreateEffect(uiEffect, AL_EFFECT_EAXREVERB) Then Print("Failed to Create an EAX Reverb Effect~n") Else Print("Failed to generate an Auxilary Effect Slot~n") EndIf ' Enable (non-filtered) Send from Source To Auxiliary Effect Slot alSource3i(Source, AL_AUXILIARY_SEND_FILTER, uiEffectSlot, 0, AL_FILTER_NULL) Print("Playing Source with Send to 'BATHROOM' Reverb Effect~n") ' Reverb Preset is stored in legacy format, use helper Function To convert To EFX EAX Reverb 'ConvertReverbParameters(Varptr(REVERB_PRESET_BATHROOM), &efxReverb); ' <-- cant find this! must have something to do with EFX-Util.lib? ' Set the Effect parameters 'If Not SetEFXEAXReverbProperties(efxReverb, uiEffect) Then Print("Failed to set Reverb Parameters~n") If Not SetEFXEAXReverbProperties2(REVERB_PRESET_BATHROOM, uiEffect) Then Print("Failed to set Reverb Parameters~n") ' Load Effect into Auxiliary Effect Slot alAuxiliaryEffectSloti(uiEffectSlot, AL_EFFECTSLOT_EFFECT, uiEffect); ' Play Source again PlaySource(Source) Print("Playing Source with Send to 'STONECORRIDOR' Reverb Effect~n") 'If Not SetEFXEAXReverbProperties(efxReverb, uiEffect) Then Print("Failed to set Reverb Parameters~n") If Not SetEFXEAXReverbProperties2(REVERB_PRESET_STONECORRIDOR, uiEffect) Then Print("Failed to set Reverb Parameters~n") alAuxiliaryEffectSloti(uiEffectSlot, AL_EFFECTSLOT_EFFECT, uiEffect) PlaySource(Source) Print("Playing Source with Send to 'HANGAR' Reverb Effect~n") 'If Not SetEFXEAXReverbProperties(efxReverb, uiEffect) Then Print("Failed to set Reverb Parameters~n") If Not SetEFXEAXReverbProperties2(REVERB_PRESET_HANGAR, uiEffect) Then Print("Failed to set Reverb Parameters~n") alAuxiliaryEffectSloti(uiEffectSlot, AL_EFFECTSLOT_EFFECT, uiEffect) PlaySource(Source) Delay 2000 ' let reverb settle ' Clean-up ... ' Remove Effect Send from Source alSource3i(Source, AL_AUXILIARY_SEND_FILTER, AL_EFFECTSLOT_NULL, 0, AL_FILTER_NULL); ' Load Null Effect into Effect Slot alAuxiliaryEffectSloti(uiEffectSlot, AL_EFFECTSLOT_EFFECT, AL_EFFECT_NULL); ' Delete Effect alDeleteEffects(1, Varptr(uiEffect)) ' Delete Auxiliary Effect Slot alDeleteAuxiliaryEffectSlots(1, Varptr(uiEffectSlot)) ' Delete Source And Buffer alSourceStop(Source) alDeleteSources(1, Varptr(Source)) alDeleteBuffers(1, Varptr(Buffer)) Function CreateAuxEffectSlot:Int(puiAuxEffectSlot:Int Var) alGetError(); alGenAuxiliaryEffectSlots(1, Varptr(puiAuxEffectSlot)); If alGetError()=AL_NO_ERROR Then Return True Return False EndFunction Function CreateEffect:Int(puiEffect:Int Var, eEffectType:Int) alGetError(); alGenEffects(1, Varptr(puiEffect)); alEffecti(puiEffect, AL_EFFECT_TYPE, eEffectType); If alGetError() = AL_NO_ERROR Return True Else alDeleteEffects(1, Varptr(puiEffect)); EndIf EndFunction Function PlaySource(uiSource:Int) Local iState alGetError(); alSourcePlay(uiSource); Repeat Delay 10 alGetSourcei(uiSource, AL_SOURCE_STATE, Varptr(iState)); Until iState <> AL_PLAYING EndFunction Function SetEFXEAXReverbProperties:Int(pEFXEAXReverb:EFXEAXREVERBPROPERTIES, uiEffect:Int Var) ' Clear AL Error code alGetError(); alEffectf(uiEffect, AL_EAXREVERB_DENSITY, pEFXEAXReverb.flDensity); alEffectf(uiEffect, AL_EAXREVERB_DIFFUSION, pEFXEAXReverb.flDiffusion); alEffectf(uiEffect, AL_EAXREVERB_GAIN, pEFXEAXReverb.flGain); alEffectf(uiEffect, AL_EAXREVERB_GAINHF, pEFXEAXReverb.flGainHF); alEffectf(uiEffect, AL_EAXREVERB_GAINLF, pEFXEAXReverb.flGainLF); alEffectf(uiEffect, AL_EAXREVERB_DECAY_TIME, pEFXEAXReverb.flDecayTime); alEffectf(uiEffect, AL_EAXREVERB_DECAY_HFRATIO, pEFXEAXReverb.flDecayHFRatio); alEffectf(uiEffect, AL_EAXREVERB_DECAY_LFRATIO, pEFXEAXReverb.flDecayLFRatio); alEffectf(uiEffect, AL_EAXREVERB_REFLECTIONS_GAIN, pEFXEAXReverb.flReflectionsGain); alEffectf(uiEffect, AL_EAXREVERB_REFLECTIONS_DELAY, pEFXEAXReverb.flReflectionsDelay); alEffectfv(uiEffect, AL_EAXREVERB_REFLECTIONS_PAN, pEFXEAXReverb.flReflectionsPan); alEffectf(uiEffect, AL_EAXREVERB_LATE_REVERB_GAIN, pEFXEAXReverb.flLateReverbGain); alEffectf(uiEffect, AL_EAXREVERB_LATE_REVERB_DELAY, pEFXEAXReverb.flLateReverbDelay); alEffectfv(uiEffect, AL_EAXREVERB_LATE_REVERB_PAN, pEFXEAXReverb.flLateReverbPan); alEffectf(uiEffect, AL_EAXREVERB_ECHO_TIME, pEFXEAXReverb.flEchoTime); alEffectf(uiEffect, AL_EAXREVERB_ECHO_DEPTH, pEFXEAXReverb.flEchoDepth); alEffectf(uiEffect, AL_EAXREVERB_MODULATION_TIME, pEFXEAXReverb.flModulationTime); alEffectf(uiEffect, AL_EAXREVERB_MODULATION_DEPTH, pEFXEAXReverb.flModulationDepth); alEffectf(uiEffect, AL_EAXREVERB_AIR_ABSORPTION_GAINHF, pEFXEAXReverb.flAirAbsorptionGainHF); alEffectf(uiEffect, AL_EAXREVERB_HFREFERENCE, pEFXEAXReverb.flHFReference); alEffectf(uiEffect, AL_EAXREVERB_LFREFERENCE, pEFXEAXReverb.flLFReference); alEffectf(uiEffect, AL_EAXREVERB_ROOM_ROLLOFF_FACTOR, pEFXEAXReverb.flRoomRolloffFactor); alEffecti(uiEffect, AL_EAXREVERB_DECAY_HFLIMIT, pEFXEAXReverb.iDecayHFLimit); If alGetError()=AL_NO_ERROR Then Return True Return EndFunction Rem Field ulEnvironment:Int ' unsigned Field flEnvironmentSize:Float Field lRoom:Int Field lRoomHF:Int Field lRoomLF:Int Field lReflections:Int Field lReverb:Int Field flAirAbsorptionHF:Float Field ulFlags:Int EndRem Function SetEFXEAXReverbProperties2:Int(pEFXEAXReverb:EAXREVERBPROPERTIES, uiEffect:Int Var) ' Clear AL Error code alGetError(); ' alEffectf (uiEffect, AL_EAXREVERB_DENSITY, pEFXEAXReverb.) alEffectf (uiEffect, AL_EAXREVERB_DIFFUSION, pEFXEAXReverb.flEnvironmentDiffusion) ' alEffectf (uiEffect, AL_EAXREVERB_GAIN, pEFXEAXReverb.lRoom) ' alEffectf (uiEffect, AL_EAXREVERB_GAINHF, pEFXEAXReverb.lRoomHF) ' alEffectf (uiEffect, AL_EAXREVERB_GAINLF, pEFXEAXReverb.lRoomLF) alEffectf (uiEffect, AL_EAXREVERB_DECAY_TIME, pEFXEAXReverb.flDecayTime) alEffectf (uiEffect, AL_EAXREVERB_DECAY_HFRATIO, pEFXEAXReverb.flDecayHFRatio) alEffectf (uiEffect, AL_EAXREVERB_DECAY_LFRATIO, pEFXEAXReverb.flDecayLFRatio) ' alEffectf (uiEffect, AL_EAXREVERB_REFLECTIONS_GAIN, pEFXEAXReverb.lReflections) alEffectf (uiEffect, AL_EAXREVERB_REFLECTIONS_DELAY, pEFXEAXReverb.flReflectionsDelay) alEffectfv(uiEffect, AL_EAXREVERB_REFLECTIONS_PAN, pEFXEAXReverb.vReflectionsPan) ' alEffectf (uiEffect, AL_EAXREVERB_LATE_REVERB_GAIN, pEFXEAXReverb.lReverb) alEffectf (uiEffect, AL_EAXREVERB_LATE_REVERB_DELAY, pEFXEAXReverb.flReverbDelay) alEffectfv(uiEffect, AL_EAXREVERB_LATE_REVERB_PAN, pEFXEAXReverb.vReverbPan) alEffectf (uiEffect, AL_EAXREVERB_ECHO_TIME, pEFXEAXReverb.flEchoTime) alEffectf (uiEffect, AL_EAXREVERB_ECHO_DEPTH, pEFXEAXReverb.flEchoDepth) alEffectf (uiEffect, AL_EAXREVERB_MODULATION_TIME, pEFXEAXReverb.flModulationTime) alEffectf (uiEffect, AL_EAXREVERB_MODULATION_DEPTH, pEFXEAXReverb.flModulationDepth) ' alEffectf (uiEffect, AL_EAXREVERB_AIR_ABSORPTION_GAINHF,pEFXEAXReverb.flAirAbsorptionHF) alEffectf (uiEffect, AL_EAXREVERB_HFREFERENCE, pEFXEAXReverb.flHFReference) alEffectf (uiEffect, AL_EAXREVERB_LFREFERENCE, pEFXEAXReverb.flLFReference) alEffectf (uiEffect, AL_EAXREVERB_ROOM_ROLLOFF_FACTOR, pEFXEAXReverb.flRoomRolloffFactor) ' alEffecti (uiEffect, AL_EAXREVERB_DECAY_HFLIMIT, pEFXEAXReverb.) If alGetError()=AL_NO_ERROR Then Return True Return EndFunction
Strict Import Pub.OpenAL Global ALDevice = alcOpenDevice (Null) ; alError("alcOpenDevice") Global ALContext = alcCreateContext (ALDevice, Null) ; alError("alcCreateContext") alcMakeContextCurrent (ALContext) ; alError("alcMakeContextCurrent") alinit() ; alError("alinit") ' auto cleanup OnEnd OpenALRelease Function OpenALRelease() alcDestroyContext (ALContext) ; alError("alcDestroyContext") alcCloseDevice (ALDevice) ; alError("alcCloseDevice") EndFunction 'Create buffers Local buffer alGenBuffers(1,Varptr(buffer)) ; alError("alGenBuffers") If Not buffer RuntimeError "Failed to create buffer." 'Create buffers Local source alGenSources(1,Varptr(source)) ; alError("alGenSources") If Not source RuntimeError "Failed to create source." Local bbsound:TAudioSample = LoadAudioSample(RequestFile("")) If Not bbsound RuntimeError "Failed to load sound" Local format Local samples = Int(bbsound.samples) Local length = bbsound.length Local freq = bbsound.hertz Select bbsound.format Case SF_MONO8 ; format = AL_FORMAT_MONO8 Case SF_MONO16LE ; format = AL_FORMAT_MONO16 ; length:*2 Case SF_MONO16BE ; format = AL_FORMAT_MONO16 ; length:*2 Case SF_STEREO8 ; format = AL_FORMAT_STEREO8 ; length:*2 Case SF_STEREO16LE ; format = AL_FORMAT_STEREO16 ; length:*4 Case SF_STEREO16BE ; format = AL_FORMAT_STEREO16 ; length:*4 EndSelect alBufferData(buffer,format,samples,length,freq) ; alError("alBufferData") alSourcei(source,AL_BUFFER,buffer) ; alError("alSourcei") alSourcePlay(source) ; alError("alSourcePlay") Local State Repeat alGetSourcei (source,AL_SOURCE_STATE,Varptr(state)) ; alError("alGetSourcei") Until State = AL_STOPPED Function alError$(Extra$="") Repeat Select alGetError() Case AL_INVALID_NAME ; Print Extra+" AL_INVALID_NAME" Case AL_ILLEGAL_ENUM ; Print Extra+" AL_ILLEGAL_ENUM" Case AL_INVALID_ENUM ; Print Extra+" AL_INVALID_ENUM" Case AL_INVALID_VALUE ; Print Extra+" AL_INVALID_VALUE" Case AL_ILLEGAL_COMMAND ; Print Extra+" AL_ILLEGAL_COMMAND" Case AL_INVALID_OPERATION ; Print Extra+" AL_INVALID_OPERATION" Case AL_OUT_OF_MEMORY ; Print Extra+" AL_OUT_OF_MEMORY" Default ; Print Extra+" OKAY!" ; Return EndSelect Forever EndFunction