From 5b96439fed6d365cc4df62f6983168bcdd86ba8d Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 20 Apr 2021 00:19:50 +0900 Subject: [PATCH] Revert "examples/audiopanning: Bug fix: Out of range at reading bytes" This reverts commit b1545b115cccac3946621ad163cebb058f5bdb69. Reason: Calling UnreadByte twice is illegal Updates #1598 --- examples/audiopanning/main.go | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/examples/audiopanning/main.go b/examples/audiopanning/main.go index 136c18ba0..511f9f093 100644 --- a/examples/audiopanning/main.go +++ b/examples/audiopanning/main.go @@ -17,7 +17,6 @@ package main import ( - "bufio" "bytes" "fmt" "image" @@ -144,28 +143,16 @@ func main() { // StereoPanStream is an audio buffer that changes the stereo channel's signal // based on the Panning. type StereoPanStream struct { - src *bufio.Reader + io.ReadSeeker pan float64 // -1: left; 0: center; 1: right } func (s *StereoPanStream) Read(p []byte) (n int, err error) { - n, err = s.src.Read(p) - if err != nil && err != io.EOF { + n, err = s.ReadSeeker.Read(p) + if err != nil { return } - // Align the read size to multiples of 4. - extra := n - n/4*4 - for i := 0; i < extra; i++ { - if err := s.src.UnreadByte(); err != nil { - return 0, err - } - if err == io.EOF { - err = nil - } - } - n -= extra - // This implementation uses a linear scale, ranging from -1 to 1, for stereo or mono sounds. // If pan = 0.0, the balance for the sound in each speaker is at 100% left and 100% right. // When pan is -1.0, only the left channel of the stereo sound is audible, when pan is 1.0, @@ -173,7 +160,7 @@ func (s *StereoPanStream) Read(p []byte) (n int, err error) { // https://docs.unity3d.com/ScriptReference/AudioSource-panStereo.html ls := math.Min(s.pan*-1+1, 1) rs := math.Min(s.pan+1, 1) - for i := 0; i < n; i += 4 { + for i := 0; i < len(p); i += 4 { lc := int16(float64(int16(p[i])|int16(p[i+1])<<8) * ls) rc := int16(float64(int16(p[i+2])|int16(p[i+3])<<8) * rs) @@ -198,8 +185,8 @@ func (s *StereoPanStream) Pan() float64 { // The src's format must be linear PCM (16bits little endian, 2 channel stereo) // without a header (e.g. RIFF header). The sample rate must be same as that // of the audio context. -func NewStereoPanStreamFromReader(src io.Reader) *StereoPanStream { +func NewStereoPanStreamFromReader(src io.ReadSeeker) *StereoPanStream { return &StereoPanStream{ - src: bufio.NewReader(src), + ReadSeeker: src, } }