Skip to content

Commit

Permalink
Merge pull request #9 from crow-misia/fix/6
Browse files Browse the repository at this point in the history
fix the size of Buffer in ChannelArea
  • Loading branch information
crow-misia authored Nov 6, 2024
2 parents 3d542fd + 9644bd3 commit 27e6f35
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 16 deletions.
17 changes: 8 additions & 9 deletions channel_area.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (

// ChannelArea contain sound data.
type ChannelArea struct {
buffer []byte
step int
channelCount int
buffer []byte
step int
bytesPerSample int
}

// fields
Expand All @@ -31,9 +31,8 @@ func (a *ChannelArea) Buffer() []byte {
}

func (a *ChannelArea) bufferWithFrame(frame int) []byte {
step := a.step / a.channelCount
offset := frame * a.step
return a.buffer[offset : offset+step]
return a.buffer[offset : offset+a.bytesPerSample]
}

// Step returns ow many bytes it takes to get from the beginning of one sample to
Expand All @@ -42,7 +41,7 @@ func (a *ChannelArea) Step() int {
return a.step
}

func newChannelArea(ptr uintptr, channel int, frameCount int, channelCount int) *ChannelArea {
func newChannelArea(ptr uintptr, format Format, channel int, frameCount int) *ChannelArea {
size := C.sizeof_struct_SoundIoChannelArea
areaPtr := ptr + uintptr(channel*size)
area := (*C.struct_SoundIoChannelArea)(unsafe.Pointer(areaPtr))
Expand All @@ -57,8 +56,8 @@ func newChannelArea(ptr uintptr, channel int, frameCount int, channelCount int)
buffer := *(*[]byte)(unsafe.Pointer(sh))

return &ChannelArea{
buffer: buffer,
step: areaStep,
channelCount: channelCount,
buffer: buffer,
step: areaStep,
bytesPerSample: BytesPerSample(format),
}
}
6 changes: 3 additions & 3 deletions channel_areas.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ func (a *ChannelAreas) Area(channel int) *ChannelArea {
// Buffer returns ChannelArea buffer.
func (a *ChannelAreas) Buffer(channel int, frame int) []byte {
return a.areas[channel].bufferWithFrame(frame)

}
func newChannelAreas(ptr *C.struct_SoundIoChannelArea, chanelCount int, frameCount int) *ChannelAreas {

func newChannelAreas(ptr *C.struct_SoundIoChannelArea, format Format, chanelCount int, frameCount int) *ChannelAreas {
areasPtr := uintptr(unsafe.Pointer(ptr))
areas := make([]*ChannelArea, chanelCount)

for ch := 0; ch < chanelCount; ch++ {
areas[ch] = newChannelArea(areasPtr, ch, frameCount, chanelCount)
areas[ch] = newChannelArea(areasPtr, format, ch, frameCount)
}

return &ChannelAreas{
Expand Down
4 changes: 2 additions & 2 deletions examples/sio_sine/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"context"
"encoding/binary"
"fmt"
"github.com/crow-misia/go-libsoundio"
soundio "github.com/crow-misia/go-libsoundio"
"log"
"math"
"os"
Expand Down Expand Up @@ -177,7 +177,7 @@ func realMain(ctx context.Context) error {
log.Printf(" Layout Error = %s", outStream.LayoutError())

log.Printf(" Name = %s", outStream.Name())
log.Printf(" BytePerFrame = %d", outStream.BytesPerSample())
log.Printf(" BytePerFrame = %d", outStream.BytesPerFrame())
log.Printf(" BytePerSample = %d", outStream.BytesPerSample())
log.Printf(" SoftwareLatency = %f", outStream.SoftwareLatency())
log.Printf(" SampleRate = %d", outStream.SampleRate())
Expand Down
2 changes: 1 addition & 1 deletion in_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (s *InStream) BeginRead(frameCount *int) (*ChannelAreas, error) {
if ptrs == nil {
return nil, nil
}
return newChannelAreas(ptrs, s.Layout().ChannelCount(), *frameCount), nil
return newChannelAreas(ptrs, s.Format(), s.Layout().ChannelCount(), *frameCount), nil
}

// EndRead will drop all of the frames from when you called.
Expand Down
2 changes: 1 addition & 1 deletion out_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (s *OutStream) BeginWrite(frameCount *int) (*ChannelAreas, error) {
if ptrs == nil {
return nil, nil
}
return newChannelAreas(ptrs, s.Layout().ChannelCount(), *frameCount), nil
return newChannelAreas(ptrs, s.Format(), s.Layout().ChannelCount(), *frameCount), nil
}

// EndWrite commits the write that you began with BeginWrite.
Expand Down

0 comments on commit 27e6f35

Please sign in to comment.