diff --git a/channel_area.go b/channel_area.go index 6e7389a..a9997f0 100644 --- a/channel_area.go +++ b/channel_area.go @@ -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 @@ -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 @@ -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)) @@ -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), } } diff --git a/channel_areas.go b/channel_areas.go index 6f27e6a..699a4e2 100644 --- a/channel_areas.go +++ b/channel_areas.go @@ -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{ diff --git a/examples/sio_sine/main.go b/examples/sio_sine/main.go index f3ac2c4..2888f5d 100644 --- a/examples/sio_sine/main.go +++ b/examples/sio_sine/main.go @@ -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" @@ -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()) diff --git a/in_stream.go b/in_stream.go index 25f5d56..60d63c8 100644 --- a/in_stream.go +++ b/in_stream.go @@ -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. diff --git a/out_stream.go b/out_stream.go index 4a84b52..2c381ef 100644 --- a/out_stream.go +++ b/out_stream.go @@ -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.