effects: add Pan

This commit is contained in:
faiface
2017-08-01 18:53:04 +02:00
parent 07e03454fa
commit 7444192544

34
effects/pan.go Normal file
View File

@@ -0,0 +1,34 @@
package effects
import "github.com/faiface/beep"
// Pan balances the wrapped Streamer between the left and the right channel. The Pan field value of
// -1 means that both original channels go through the left channel. The value of +1 means the same
// for the right channel. The value of 0 changes nothing.
type Pan struct {
Streamer beep.Streamer
Pan float64
}
// Stream streams the wrapped Streamer balanced by Pan.
func (p *Pan) Stream(samples [][2]float64) (n int, ok bool) {
n, ok = p.Streamer.Stream(samples)
for i := range samples[:n] {
l := samples[i][0]
r := samples[i][1]
switch {
case p.Pan < 0:
samples[i][0] += -p.Pan * r
samples[i][1] -= -p.Pan * r
case p.Pan > 0:
samples[i][0] -= p.Pan * l
samples[i][1] += p.Pan * l
}
}
return n, ok
}
// Err propagates the wrapped Streamer's errors.
func (p *Pan) Err() error {
return p.Streamer.Err()
}