-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspatialize.go
46 lines (34 loc) · 1.27 KB
/
spatialize.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package sound
import (
"github.com/splace/signals"
"math"
"time"
)
// offset in polar attenuation space from omnidirecional hearing, just guesses.
const humanOmniOffset = 0.6
const humanOmniAngle = math.Pi * .1
const humanEarOffset = 0.25
const rateOfSound = float64(time.Second)/340
type vector struct{
x,y float64
}
func distance(point vector) float64 {
return math.Sqrt(point.x*point.x + point.y*point.y)
}
func angle(point vector) float64 {
return math.Atan2(point.x,point.y)
}
// Spatialized returns a Sound adjusted for location.
func Spatialized(source Sound, location vector ) Sound {
return signals.Modulated{Delayed(source,time.Duration(rateOfSound*distance(location))), signals.NewConstant(signals.DB(attenuation(location)))}
}
func attenuation(location vector) float64{
return angleAttenuation(angle(location))/distance(location)
}
func angleAttenuation(angle float64) float64{
return math.Sqrt(1+humanOmniOffset*(humanOmniOffset+math.Cos(angle-humanOmniAngle)))
}
// Stereo returns two Sound's spatialized for human ear locations, from a head centred location.
func Stereo(source Sound, location vector)(left,right Sound){
return Spatialized(source,vector{location.y,-location.x-humanEarOffset}),Spatialized(source,vector{location.y,location.x-humanEarOffset})
}