TGC Codebase Backup



AGK Basic 3d Sound Library by Jack

10th Apr 2017 21:04
Summary

This library provides you 3d sound based on regular loaded sound files.



Description

This library provides you 3d sound based on regular loaded sound files.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    
// 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

type Sound3d_def
	rootsnd as integer
	insnum as integer
	X# as float
	Y# as float
	Z# as float
	play as integer
	initvol# as float
	nearmul# as float
	rate# as float
endtype



// regular sounds
snd = LoadSound("industrial1.wav")
snd2 = LoadSound("wind1.wav")
snd3 = LoadSound("deadsignal2.wav")


// create 3d sounds from regular sounds
// snd,X#,Y#,Z#,initvol#,nearmul,playing
// initial volume, distance multiplicator, playing state (0 -1 play -2 loop)
snd3d = Create3dSound(snd,0.0,2.0,0.0,10.0,1,2,1.0)	
snd3d_2 = Create3dSound(snd2,80.0,2.0,0.0,10.0,1,2,1.0)
snd3d_3 = Create3dSound(snd3,80.0,2.0,80.0,5.0,2,2,1.0)





// create simple scene
// ===================
ground = CreateObjectBox(200,1,200)
SetObjectImage(ground,CreateImageColor(0,100,0,255),0)





do
	
	// control camera
	currentPosX#=0
	currentPosY#=0
	currentPosZ#=0

	if (GetRawKeyState(87)) then currentPosZ#=10.8
	if (GetRawKeyState(83)) then currentPosZ#=-10.8
	
	if (GetRawKeyState(65)) then currentPosX#=-10.8
	if (GetRawKeyState(68)) then currentPosX#=10.8

	if (GetRawKeyState(81)) then currentPosY#=10.8
	if (GetRawKeyState(69)) then currentPosY#=-10.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( ScreenFPS() )
    Sync()
loop



// 3d Sound Library
// ================
function Create3dSound(snd,X#,Y#,Z#,initvol#,nearmul#,playing,rate#)

	if Sound3d.length=-1
		dim Sound3d[] as Sound3d_def
	endif

	Sound3d.length=Sound3d.length+1
	pos = Sound3d.length

	if playing=1 then ins = PlaySound(snd)
	if playing=2 then ins = PlaySound(snd,initvol#,1)
	
	if playing>0
		SetSoundInstanceRate(ins,rate#)
	endif
	
	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#
	dbgbox = CreateObjectBox(0.5,0.5,0.5)
	SetObjectImage(dbgbox,CreateImageColor(0,255,0,255),0)
	SetObjectPosition(dbgbox,X#,Y#,Z#)

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,stat)
	Sound3d[pos].play = stat	
	snd = Sound3d[pos].rootsnd
	rate# = Sound3d[pos].rate#
	if stat=2
		ins = PlaySound(snd,10.0,1)
	else
		ins = PlaySound(snd,10.0,0)
	endif
	SetSoundInstanceRate( ins, rate# ) 
	Sound3d[pos].insnum = ins
	
endfunction

function Stop3dSound(pos)
	StopSoundInstance(Sound3d[pos].insnum)
endfunction

function Update3dSound()

	CamX# = GetCameraX(1)
	CamY# = GetCameraY(1)
	CamZ# = GetCameraZ(1)


	for pos =0 to Sound3d.length

		X# = Sound3d[pos].X#
		Y# = Sound3d[pos].Y#
		Z# = Sound3d[pos].Z#
		play = Sound3d[pos].play
		ins = Sound3d[pos].insnum
		
	
		if GetSoundInstancePlaying(ins)=1
			dis# = Get3dDistance(CamX#,CamY#,CamZ#,X#,Y#,Z#)
			initvol# = Sound3d[pos].initvol#
			nearmul# = Sound3d[pos].nearmul#
			rate# = Sound3d[pos].rate#
			vol# = initvol#-(((dis#/10.0)/SOUND3D_SCALE)^2)*(1/nearmul#)
			if vol#=>0.0

				ang# = atan2(CamX#-X#,CamZ#-Z#)-GetCameraAngleY(1)+180.0
				bal# = sin(ang#)

				SetSoundInstanceVolume(ins,vol#)
				SetSoundInstanceBalance(ins,bal#)
				SetSoundInstanceRate(ins,rate#)
			else
				SetSoundInstanceVolume(ins,0.0)
			endif
		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#