AGK simple 3d Sound Library
provides you a simple sound interface for 3d sounds in AGK.
I have even added the doppler effect, so have fun with it!
This will give life into your 3d worlds!
Features:
3d Sound System
Easy to use
Doppler effect
Commandset:
Create3dSound(snd,X#,Y#,Z#,initvol#,nearmul#,playing,rate#)
Set3dSoundPosition(pos,X#,Y#,Z#)
Set3dSoundVolume(pos,initvol#,nearmul#)
Set3dSoundRate(pos,rate#)
Update3dSound()
Play3dSound(pos,stat)
Stop3dSound(pos)
See attachments for project + media.
// Project: AGK_3d_Sound
// Created: 10.04.2017
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "AGK_3d_Sound" )
SetWindowSize( 1024, 768, 0 )
// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
/*
Simple 3d Sound Library
commandset:
Create3dSound(snd,X#,Y#,Z#,initvol#,nearmul#,playing,rate#)
Set3dSoundPosition(pos,X#,Y#,Z#)
Set3dSoundVolume(pos,initvol#,nearmul#)
Set3dSoundRate(pos,rate#)
Update3dSound()
Play3dSound(pos,stat)
Stop3dSound(pos)
*/
#constant SOUND3D_SCALE 1.0
#constant SOUND3D_DOPPLER 1
#constant SOUND3D_DOPPLER_INTENSITY 0.1
type Sound3d_def
rootsnd as integer
insnum as integer
X# as float
Y# as float
Z# as float
lastdis# as float
play as integer
initvol# as float
nearmul# as float
rate# as float
doppler as integer
endtype
// regular sounds
snd = LoadSound("industrial1.wav")
snd2 = LoadSound("wind1.wav")
snd3 = LoadSound("deadsignal2.wav")
snd4 = LoadSound("veryniceBirdys_loop.wav")
snd5 = LoadSound("police.wav")
// create 3d sounds from regular sounds
// snd,X#,Y#,Z#,initvol#,nearmul,playing,rate,doppler
// initial volume, distance multiplicator, playing state (0 play -1 loop)
snd3d = Create3dSound(snd,0.0,2.0,0.0,10.0,20,1,1.0,0)
snd3d_2 = Create3dSound(snd2,80.0,2.0,0.0,10.0,20,1,1.0,0)
snd3d_3 = Create3dSound(snd3,80.0,2.0,80.0,5.0,20,1,1.0,0)
snd3d_4 = Create3dSound(snd4,-80.0,2.0,-80.0,30.0,20,1,1.0,0)
snd3d_5 = Create3dSound(snd5,-80.0,2.0,-80.0,100.0,20,1,1.0,1)
box = CreateObjectBox(1,1,1)
SetObjectPosition(box,-80,2,-80)
// create simple scene
// ===================
ground = CreateObjectBox(200,1,200)
SetObjectImage(ground,CreateImageColor(0,100,0,255),0)
car = CreateObjectBox(4,4,10)
SetObjectImage(car,CreateImageColor(100,0,0,255),0)
SetObjectPosition(car,-80,3,300)
do
MoveObjectLocalZ(car,-1)
if getobjectz(car)<-300 then SetObjectPosition(car,-80,3,300)
Set3dSoundPosition(snd3d_5,getobjectx(car),getobjecty(car),getobjectz(car))
// SetCameraPosition(1,getobjectx(car),getobjecty(car),getobjectz(car))
// control camera
currentPosX#=0
currentPosY#=0
currentPosZ#=0
if (GetRawKeyState(87)) then currentPosZ#=1.8
if (GetRawKeyState(83)) then currentPosZ#=-1.8
if (GetRawKeyState(65)) then currentPosX#=-1.8
if (GetRawKeyState(68)) then currentPosX#=1.8
if (GetRawKeyState(81)) then currentPosY#=1.8
if (GetRawKeyState(69)) then currentPosY#=-1.8
newPosX#=CurveValue(currentPosX#,newPosX#,8)
newPosY#=CurveValue(currentPosY#,newPosY#,8)
newPosZ#=CurveValue(currentPosZ#,newPosZ#,8)
MoveCameraLocalZ(1,newPosZ#)
MoveCameraLocalX(1,newPosX#)
MoveCameraLocalY(1,newPosY#)
if (GetPointerPressed()=1)
startx#=GetPointerX()
starty#=GetPointerY()
angx#=GetCameraAngleX(1)
angy#=GetCameraAngleY(1)
pressed=1
endif
if (GetPointerState()=1)
fDiffX#=(GetPointerX()-startx#)*0.15
fDiffY#=(GetPointerY()-starty#)*0.15
currentX#=angx#+fDiffY#
if (tempnewX#>89) then tempnewX#=89
if (tempnewX#<-89) then tempnewX#=-89
currentY#=angy#+fDiffX#
endif
newX#=CurveValue(currentX#,newX#,4)
newY#=CurveValue(currentY#,newY#,4)
SetCameraRotation(1,newX#,newY#,0.0)
// update 3d sound
Update3dSound()
print(Sound3d.length)
Print( ScreenFPS() )
Sync()
loop
// 3d Sound Library
// ================
function Create3dSound(snd,X#,Y#,Z#,initvol#,nearmul#,playing,rate#,doppler)
if Sound3d.length=-1
dim Sound3d[] as Sound3d_def
endif
Sound3d.length=Sound3d.length+1
pos = Sound3d.length
ins = PlaySound(snd,initvol#,playing)
SetSoundInstanceRate(ins,rate#)
Sound3d[pos].rootsnd=snd
Sound3d[pos].insnum = ins
Sound3d[pos].X# = X#
Sound3d[pos].Y# = Y#
Sound3d[pos].Z# = Z#
Sound3d[pos].play = playing
Sound3d[pos].initvol# = initvol#
Sound3d[pos].nearmul# = nearmul#
Sound3d[pos].rate# = rate#
Sound3d[pos].doppler = doppler
endfunction pos
function Set3dSoundPosition(pos,X#,Y#,Z#)
Sound3d[pos].X# = X#
Sound3d[pos].Y# = Y#
Sound3d[pos].Z# = Z#
endfunction
function Set3dSoundVolume(pos,initvol#,nearmul#)
Sound3d[pos].initvol# = initvol#
Sound3d[pos].nearmul# = nearmul#
endfunction
function Set3dSoundRate(pos,rate#)
Sound3d[pos].rate# = rate#
endfunction
function Play3dSound(pos,playing)
Sound3d[pos].play = playing
snd = Sound3d[pos].rootsnd
rate# = Sound3d[pos].rate#
ins = PlaySound(snd,10.0,playing)
SetSoundInstanceRate( ins, rate# )
Sound3d[pos].insnum = ins
endfunction
function Stop3dSound(pos)
StopSoundInstance(Sound3d[pos].insnum)
Sound3d.remove(pos)
endfunction
function Update3dSound()
CamX# = GetCameraX(1)
CamY# = GetCameraY(1)
CamZ# = GetCameraZ(1)
for pos =0 to Sound3d.length
ins = Sound3d[pos].insnum
if GetSoundInstancePlaying(ins)=1
X# = Sound3d[pos].X#
Y# = Sound3d[pos].Y#
Z# = Sound3d[pos].Z#
dis# = Get3dDistance(CamX#,CamY#,CamZ#,X#,Y#,Z#)
initvol# = Sound3d[pos].initvol#
nearmul# = Sound3d[pos].nearmul#
vol# = initvol#-(((dis#/10.0)/SOUND3D_SCALE)^2)*(1/nearmul#)
if vol#>0.0
bal# = sin(atan2(CamX#-X#,CamZ#-Z#)-GetCameraAngleY(1)+180.0)
SetSoundInstanceVolume(ins,vol#)
SetSoundInstanceBalance(ins,bal#)
if SOUND3D_DOPPLER = 1 and Sound3d[pos].doppler = 1
absrate# = ((dis#-Sound3d[pos].lastdis#)*SOUND3D_DOPPLER_INTENSITY)
if absrate#<-SOUND3D_DOPPLER_INTENSITY then absrate#=-SOUND3D_DOPPLER_INTENSITY
if absrate#>SOUND3D_DOPPLER_INTENSITY then absrate#=SOUND3D_DOPPLER_INTENSITY
rate# = Sound3d[pos].rate#-absrate#
SetSoundInstanceRate(ins,(rate#+GetSoundInstanceRate(ins))/2)
else
rate# = Sound3d[pos].rate#
SetSoundInstanceRate(ins,rate#)
endif
else
SetSoundInstanceVolume(ins,0.0)
endif
Sound3d[pos].lastdis# = dis#
else
Sound3d.remove(pos)
endif
next pos
endfunction
// math library
function CurveValue(Destination#,Current#,Smooth#)
Current#=Current#+((Destination#-Current#)/Smooth#)
endfunction Current#
function Get3dDistance(SX#,SY#,SZ#,EX#,EY#,EZ#)
dis# = sqrt((SX#-EX#)^2+(SY#-EY#)^2+(SZ#-EZ#)^2)
endfunction dis#