Skip to content

Commit

Permalink
Remove changes to reader for mono or stereo
Browse files Browse the repository at this point in the history
This shouldn't be done in the library
  • Loading branch information
braheezy committed Aug 4, 2024
1 parent b80f1b7 commit acbf524
Showing 1 changed file with 7 additions and 25 deletions.
32 changes: 7 additions & 25 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ func NewReader(data []int16, channels int) *Reader {

// Read implements the io.Reader interface
func (r *Reader) Read(p []byte) (n int, err error) {
var samplesToRead int
if r.channels == 1 {
samplesToRead = len(p) / 4
} else {
samplesToRead = len(p) / 2
}
samplesToRead := len(p) / 2

if r.pos >= len(r.data) {
// Return EOF when there is no more data to read
Expand All @@ -42,24 +37,16 @@ func (r *Reader) Read(p []byte) (n int, err error) {
}

for i := 0; i < samplesToRead; i++ {
sample := r.data[r.pos]
if r.channels == 1 {
// Duplicate mono samples to stereo
p[i*4] = byte(sample & 0xFF)
p[i*4+1] = byte(sample >> 8)
p[i*4+2] = byte(sample & 0xFF)
p[i*4+3] = byte(sample >> 8)
} else {
// Assume stereo and copy directly
p[i*2] = byte(sample & 0xFF)
p[i*2+1] = byte(sample >> 8)
if r.pos >= len(r.data) {
return i * (4 / r.channels), io.EOF
}

sample := r.data[r.pos]
p[i*2] = byte(sample & 0xFF)
p[i*2+1] = byte(sample >> 8)
r.pos++
}

if r.channels == 1 {
return samplesToRead * 4, nil
}
return samplesToRead * 2, nil
}

Expand Down Expand Up @@ -95,8 +82,3 @@ func (r *Reader) Seek(offset int64, whence int) (int64, error) {
func (r *Reader) Position() int {
return r.pos
}

// SamplesRead returns the number of samples that have been read
func (r *Reader) SamplesRead() int {
return r.pos / r.channels
}

0 comments on commit acbf524

Please sign in to comment.