-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstereo_pan.go
36 lines (31 loc) · 915 Bytes
/
stereo_pan.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
package transforms
import (
"errors"
"github.com/go-audio/audio"
)
// StereoPan changes the balance of the input buffer by "panning"
// more to the left or the right. The pan value must be between
// 0 and 1 where 0 is full first channel, 0.5 center and 1 full second channel.
// Note that the signal from 1 channel is not sent to the other, this transform
// only reduces the balance of one of the 2 channels.
func StereoPan(buf *audio.FloatBuffer, pan float64) error {
if buf == nil || buf.Format == nil || buf.Format.NumChannels != 2 {
return audio.ErrInvalidBuffer
}
if pan < 0 || pan > 1 {
return errors.New("invalid pan value, should be betwen 0 and 1")
}
if pan == 0.5 {
return nil
}
if pan < 0.5 {
for i := 0; i+2 <= len(buf.Data); i += 2 {
buf.Data[i+1] *= (pan * 2)
}
} else {
for i := 0; i+2 <= len(buf.Data); i += 2 {
buf.Data[i] *= ((1 - pan) * 2)
}
}
return nil
}