October 29, 2020, 02:40:41 AM

### Author Topic: [bmx] PlaySound (better) by UnderwoodNullium [ 1+ years ago ]  (Read 1227 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bmx] PlaySound (better) by UnderwoodNullium [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : PlaySound (better)
Author : UnderwoodNullium
Posted : 1+ years ago

Description : By using a sound's x and y coordinates, this function pans the sound, and decreases the volume the further away it is from the middle of the screen.  There is also a 'spread' variable that controls the volume 'dropoff'...

Also, it changes the pitch of the sound to make each sound a little different from the past one.  The distance formula is needed, so it's added as well.

*I am also Frostbyte for everyone at blitzcodebase.co.uk, so I'm not stealing!

Code :
Code: BlitzMax
2.
3.         Local channel:TChannel
4.         Local volume:Float
5.         Local panvalue:Float
6.
7.         channel:TChannel = AllocChannel()                                       ' allocate a channel
8.
9.         volume#   = (1 - ((GetDistance(originx,originy,soundx,soundy) * (1 - volspread#)) / 100))
10.         panvalue# = (2 * ((soundx / GraphicsWidth())) - 1)
11.
12.                 If panvalue# < -1 panvalue# = -1
13.                 If panvalue# > 1 panvalue# = 1
14.
15.         SetChannelRate(channel:TChannel,Rnd(.8,1.2))                            ' make each sound original
16.         SetChannelVolume(channel:TChannel,volume#)
17.         SetChannelPan(channel:TChannel,panvalue#)
18.
19.                 If volume# > 0 PlaySound(sound:TSound,channel:TChannel)        ' play sound
20.
21. End Function
22.
23.
24. Function GetDistance:Float(x1#,y1#,x2#,y2#)
25.
26.         Return(Sqr(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1))))
27.
28. End Function