From 744419254439e86075d96fdffdc8edfbc35df8f0 Mon Sep 17 00:00:00 2001 From: faiface Date: Tue, 1 Aug 2017 18:53:04 +0200 Subject: [PATCH] effects: add Pan --- effects/pan.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 effects/pan.go 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() +}