October 28, 2020, 05:54:54 AM

Author Topic: [bb] Area Sound by BMA [ 1+ years ago ]  (Read 1218 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Area Sound by BMA [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Area Sound
Author : BMA
Posted : 1+ years ago

Description : This extension lets you control the volume of a specific sound according to the position of two entities aka. listener and emitter.
For looped sounds place 'AreaSound_update' into your main loop...


Code :
Code: BlitzBasic
  1. ; ************************************************************************************************************
  2. ; AreaSound - BMA 2006
  3. ; ************************************************************************************************************
  4.  
  5. Type tAreaSound
  6.   Field emitter%
  7.   Field listener%
  8.   Field sound%
  9.   Field emitVolume#
  10.   Field range#
  11.   Field channel%
  12.   Field volume#
  13. End Type
  14.  
  15.  
  16. Function AreaSound_create(emitter%, listener%, sound%, range#=10.0, emitVolume#=1.0, loop%=False, play%=True)
  17.   Local s.tAreaSound  = New tAreaSound
  18.   semitter     = emitter
  19.   slistener      = listener
  20.   s
  21. ange       = range
  22.   semitVolume    = emitVolume
  23.   ssound       = sound%
  24.   If loop Then LoopSound(ssound)
  25.   If play Then schannel = PlaySound(ssound)
  26.   AreaSound_calc(s)
  27.   If Not loop Then Delete s
  28.   Return Handle(s)
  29. End Function
  30.  
  31. ;call this method in your game loop, when using looped sounds
  32. Function AreaSound_update()
  33.   For s.tAreaSound = Each tAreaSound
  34.     AreaSound_calc(s)
  35.   Next
  36. End Function
  37.  
  38. Function AreaSound_getCurrentVolume#(soundID%)
  39.   s.tAreaSound = Object.tAreaSound(soundID)
  40.   Return svolume
  41. End Function
  42.  
  43. Function AreaSound_setEmitVolume(soundID%, volume#)
  44.   s.tAreaSound = Object.tAreaSound(soundID)
  45.   semitVolume = volume
  46.   AreaSound_calc(s)
  47. End Function
  48.  
  49. Function AreaSound_setEmitterRange(soundID%, range#)
  50.   s.tAreaSound = Object.tAreaSound(soundID)
  51.   s
  52. ange = range
  53.   AreaSound_calc(s)
  54. End Function
  55.  
  56. Function AreaSound_play(soundID%, loop%=False)
  57.   s.tAreaSound = Object.tAreaSound(soundID)
  58.   If loop Then LoopSound(ssound)
  59.   schannel = PlaySound(ssound)
  60.   AreaSound_calc(s)
  61. End Function
  62.  
  63. Function AreaSound_stop(soundID%)
  64.   s.tAreaSound = Object.tAreaSound(soundID)
  65.   StopChannel(ssound)
  66. End Function
  67.  
  68. Function AreaSound_cleanUp()
  69.   Delete Each tAreaSound
  70. End Function
  71.  
  72. ; ************************************************************************************************************
  73. ; Private Method
  74. ; ************************************************************************************************************
  75.  
  76. Function AreaSound_calc(s.tAreaSound)
  77.   d# = EntityDistance(slistener, semitter)
  78.   If d < s
  79. ange Then
  80.     svolume = semitVolume - d * semitVolume / (s
  81. ange)
  82.     ChannelVolume schannel, svolume
  83.   Else
  84.     svolume = 0.0
  85.     ChannelVolume schannel, 0.0
  86.   EndIf
  87. End Function
  88.  
  89.  
  90.  
  91.  
  92. ; ************************************************************************************************************
  93. ; ************************************************************************************************************
  94. ; Test Stuff
  95. ; ************************************************************************************************************
  96. ; ************************************************************************************************************
  97.  
  98. Graphics3D 640,480,32,2
  99. SetBuffer BackBuffer()
  100. SeedRnd MilliSecs()
  101.  
  102. camera=CreateCamera()
  103. PositionEntity camera,0,20,-10
  104.  
  105. light=CreateLight()
  106. RotateEntity light,90,0,0
  107.  
  108. listener  = CreateCube()
  109. EntityColor listener,255,0,0
  110.  
  111. emitter   = CreateCube()
  112. range   = CreateSphere(32,emitter)
  113.  
  114. EntityColor emitter,255,255,0
  115. EntityColor range,0,255,255
  116. EntityAlpha range,0.2
  117.  
  118. PointEntity camera,listener
  119.  
  120. PositionEntity listener,0,1,0
  121. PositionEntity emitter,Rand(-10,10),1,Rand(-10,10)
  122.  
  123. ;define emitter range
  124. r# = 8.0
  125.  
  126. soundFile% = LoadSound("your_sound_here.mp3")
  127.  
  128. aSound% = AreaSound_create(emitter, listener, soundFile, r, 1.0, True)
  129.  
  130. While Not KeyHit(1)
  131.    If KeyDown(205) Then TurnEntity listener,0,-1,0
  132.    If KeyDown(203) Then TurnEntity listener,0,1,0
  133.    If KeyDown(208) Then MoveEntity listener,0,0,-0.1
  134.    If KeyDown(200) Then MoveEntity listener,0,0,0.1
  135.    
  136.    If KeyDown(49) Then
  137.       r = r -0.05
  138.       AreaSound_setEmitterRange(aSound, r)
  139.    EndIf
  140.    If KeyDown(50) Then
  141.       r# = r# +0.05
  142.       AreaSound_setEmitterRange(aSound, r)
  143.    EndIf
  144.    
  145.    Xscale#  = ((100/1)  * r) / 100.0
  146.    Yscale#  = ((100/1)  * r) / 100.0
  147.    Zscale#  = ((100/1)  * r) / 100.0
  148.  
  149.    ScaleEntity range,Xscale#,Yscale#,Zscale#
  150.    
  151.    ;update looped sounds
  152.    AreaSound_update()
  153.    
  154.    RenderWorld
  155.    Text 0,0,"Move Listener with Cursor-Keys"
  156.    Text 0,20,"Set Emitter Range with 'N' and 'M' Keys - Range: "+r
  157.    Text 0,40,"Emitter Sound Volume: "+AreaSound_getCurrentVolume(aSound)
  158.    Flip
  159. Wend
  160. End


Comments :


_PJ_(Posted 1+ years ago)

 I hear nothing.... :S


Mikorians(Posted 1+ years ago)

 Here's an update that makes it a TRUE REPLACEMENT:
Code: [Select]
; UPDATED FOR 3D RELATIVE PANNING by Mikorians 2014
; ID: 1759
; Author: BMA
; Date: 2006-07-22 12:29:29
; Title: Area Sound
; Description: Alternative to BB's 3DSound Functions

; ************************************************************************************************************
; AreaSound - BMA 2006
; ************************************************************************************************************

Type tAreaSound
  Field emitter%
  Field listener%
  Field sound%
  Field emitVolume#
  Field range#
  Field channel%
  Field volume#
Field pan#
End Type


Function AreaSound_create(emitter%, listener%, sound%, range#=10.0, emitVolume#=1.0, loop%=False, play%=True)
  Local s.tAreaSound  = New tAreaSound
  semitter     = emitter
  slistener    = listener
  sange       = range
  semitVolume  = emitVolume
  ssound       = sound%
span = 0.0
  If loop Then LoopSound(ssound)
  If play Then schannel = PlaySound(ssound)
  AreaSound_calc(s)
  If Not loop Then Delete s
  Return Handle(s)
End Function

;call this method in your game loop, when using looped sounds
Function AreaSound_update()
  For s.tAreaSound = Each tAreaSound
    AreaSound_calc(s)
  Next
End Function

Function AreaSound_getCurrentVolume#(soundID%)
  s.tAreaSound = Object.tAreaSound(soundID)
  Return svolume
End Function

Function AreaSound_setEmitVolume(soundID%, volume#)
  s.tAreaSound = Object.tAreaSound(soundID)
  semitVolume = volume
  AreaSound_calc(s)
End Function

Function AreaSound_setEmitterRange(soundID%, range#)
  s.tAreaSound = Object.tAreaSound(soundID)
  sange = range
  AreaSound_calc(s)
End Function

Function AreaSound_play(soundID%, loop%=False)
  s.tAreaSound = Object.tAreaSound(soundID)
  If loop Then LoopSound(ssound)
  schannel = PlaySound(ssound)
  AreaSound_calc(s)
End Function

Function AreaSound_stop(soundID%)
  s.tAreaSound = Object.tAreaSound(soundID)
  StopChannel(ssound)
End Function

Function AreaSound_cleanUp()
  Delete Each tAreaSound
End Function

; ************************************************************************************************************
; Private Method
; ************************************************************************************************************

Function AreaSound_calc(s.tAreaSound)
  d# = EntityDistance(slistener, semitter)
  If d < sange Then
    svolume = semitVolume - d * semitVolume / (sange)
    ChannelVolume schannel, svolume
q#=rec2pol#(EntityX#(slistener,True),EntityZ#(slistener,True),EntityX#(semitter,True),EntityZ#(semitter,True)) ;The angle betwixt the two
q#=q#+angtrm#(EntityYaw#(slistener,True)) ;Add the listener's rotation (ear direction)
span=-Sgn((q#-180) Mod 180)*(1-(Abs(90-(q# Mod 180))/90)) ;This works.
ChannelPan schannel,span
  Else
span=0
    svolume = 0.0
    ChannelVolume schannel, 0.0
ChannelPan schannel,span
  EndIf
End Function




; ************************************************************************************************************
; ************************************************************************************************************
; Test Stuff
; ************************************************************************************************************
; ************************************************************************************************************

Graphics3D 640,480,32,2
SetBuffer BackBuffer()
SeedRnd MilliSecs()

camera=CreateCamera()
PositionEntity camera,0,20,-10

light=CreateLight()
RotateEntity light,90,0,0

listener  = CreateCube()
EntityColor listener,255,0,0

emitter   = CreateCube()
range   = CreateSphere(32,emitter)

EntityColor emitter,255,255,0
EntityColor range,0,255,255
EntityAlpha range,0.2

PointEntity camera,listener

PositionEntity listener,0,1,0
PositionEntity emitter,Rand(-10,10),1,Rand(-10,10)

;define emitter range
r# = 8.0

soundFile% = LoadSound("C:WINDOWSMediaSnore.wav")

aSound% = AreaSound_create(emitter, listener, soundFile, r, 1.0, True)

While Not KeyHit(1)
   If KeyDown(205) Then TurnEntity listener,0,-1,0
   If KeyDown(203) Then TurnEntity listener,0,1,0
   If KeyDown(208) Then MoveEntity listener,0,0,-0.1
   If KeyDown(200) Then MoveEntity listener,0,0,0.1
   
   If KeyDown(49) Then
      r = r -0.05
      AreaSound_setEmitterRange(aSound, r)
   EndIf
   If KeyDown(50) Then
      r# = r# +0.05
      AreaSound_setEmitterRange(aSound, r)
   EndIf
   
   Xscale#  = ((100/1)  * r) / 100.0
   Yscale#  = ((100/1)  * r) / 100.0
   Zscale#  = ((100/1)  * r) / 100.0

   ScaleEntity range,Xscale#,Yscale#,Zscale#
   
   ;update looped sounds
   AreaSound_update()
   
   RenderWorld
   Text 0,0,"Move Listener with Cursor-Keys"
   Text 0,20,"Set Emitter Range with 'N' and 'M' Keys - Range: "+r
   Text 0,40,"Emitter Sound Volume: "+AreaSound_getCurrentVolume(aSound)
   Flip
Wend
End

Function Rec2Pol#(X1#,Y1#,X2#,Y2#)
C# = 3.141592654 / 180
OX# = X2# - X1#: OY# = Y2# - Y1#:R# = Sqr((OX# ^ 2) + (OY# ^ 2))
If Sgn(OX#)=0 And Sgn(OY#)=0 Then Ret#=0:Goto XitR
If OX<0 And OY<0 Then QT=180
If OX<0 And OY>0 Then QT=180
If OX=0 And OY>0 Then Ret#=0:Goto XitR
If OX>0 And OY=0 Then Ret#=90:Goto XitR
If OX=0 And OY<0 Then Ret#=180:Goto XitR
If OX<0 And OY=0 Then Ret#=270:Goto XitR
Ret# = 90-ATan((OY / R#) / (OX# / R#))+QT
.XitR
Return Ret#
End Function

Function AngTrm#(Ang#)
Return (360.0+(Ang# Mod 360)) Mod 360.0
End Function


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal