diff --git a/effects/pan.go b/effects/pan.go new file mode 100644 index 0000000..490ba44 --- /dev/null +++ b/effects/pan.go @@ -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() +}