-
Notifications
You must be signed in to change notification settings - Fork 17
/
volume.go
41 lines (34 loc) · 828 Bytes
/
volume.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
package godub
import (
"fmt"
"math"
)
// Volume is the quantity of three-dimensional space enclosed by a closed surface.
// Volume unit is dBFS.
type Volume float64
func NewVolumeFromRatio(ratio float64, denominator float64, useAmplitude bool) Volume {
if denominator != 0 {
ratio = ratio / denominator
}
if ratio == 0 {
return Volume(math.Inf(1))
}
if useAmplitude {
return Volume(20 * math.Log10(ratio))
} else {
return Volume(10 * math.Log10(ratio))
}
}
func (volume Volume) String() string {
return fmt.Sprintf("%.3fdBFS", float64(volume))
}
// ToFloat64 converts db to a float, which represents
// the equivalent ratio in power
func (volume Volume) ToRatio(useAmplitude bool) float64 {
v := float64(volume)
if useAmplitude {
return math.Pow(10, v/20)
} else {
return math.Pow(10, v/10)
}
}