-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcm_scale.go
38 lines (32 loc) · 1 KB
/
pcm_scale.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
package transforms
import (
"math"
"github.com/go-audio/audio"
)
// PCMScale converts a buffer with audio content from -1 to 1 into
// the PCM scale based on the passed bitdepth.
// Note that while the PCM data is scaled, the PCM format is not changed.
func PCMScale(buf *audio.FloatBuffer, bitDepth int) error {
if buf == nil || buf.Format == nil {
return audio.ErrInvalidBuffer
}
factor := math.Pow(2, 8*float64(bitDepth/8)-1)
for i := 0; i < len(buf.Data); i++ {
buf.Data[i] *= factor
}
return nil
}
// PCMScaleF32 converts a buffer with audio content from -1 to 1 (float32) into
// the PCM scale based on the passed bitdepth.
// Note that while the PCM data is scaled, the PCM format is not changed.
func PCMScaleF32(buf *audio.Float32Buffer, bitDepth int) error {
if buf == nil || buf.Format == nil {
return audio.ErrInvalidBuffer
}
buf.SourceBitDepth = bitDepth
factor := float32(math.Pow(2, float64(bitDepth)-1)) - 1.0
for i := 0; i < len(buf.Data); i++ {
buf.Data[i] *= factor
}
return nil
}