Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions buffers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,32 @@ type sample interface {
byte | int8 | int16 | int32 | int64 | float32 | float64
}

type FullFramesOption[T ~[]S, S sample] func(*fullFrameBuffer[T, S])

func DropTailOnClose[T ~[]S, S sample]() FullFramesOption[T, S] {
return func(b *fullFrameBuffer[T, S]) {
b.dropTail = true
}
}

// FullFrames creates a writer that only writes full frames of a given size to the underlying writer (except the last one).
func FullFrames[T ~[]S, S sample](w WriteCloser[T], frameSize int) WriteCloser[T] {
func FullFrames[T ~[]S, S sample](w WriteCloser[T], frameSize int, opts ...FullFramesOption[T, S]) WriteCloser[T] {
if frameSize <= 0 {
panic("invalid frame size")
}
return &fullFrameBuffer[T, S]{
b := &fullFrameBuffer[T, S]{
w: w,
frameSize: frameSize,
buf: make([]S, 0, frameSize),
}
for _, opt := range opts {
opt(b)
}
return b
}

type fullFrameBuffer[T ~[]S, S sample] struct {
dropTail bool // Avoid flushing partial frames on Close()
frameSize int
mu sync.Mutex
w WriteCloser[T]
Expand Down Expand Up @@ -72,7 +85,7 @@ func (b *fullFrameBuffer[T, S]) flush(force bool) error {
func (b *fullFrameBuffer[T, S]) Close() error {
b.mu.Lock()
defer b.mu.Unlock()
err := b.flush(true)
err := b.flush(!b.dropTail)
err2 := b.w.Close()
return errors.Join(err, err2)
}
Expand Down
27 changes: 27 additions & 0 deletions buffers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,30 @@ func TestFullFrames(t *testing.T) {
{7},
}, got)
}

func TestFullFramesDropTailOnClose(t *testing.T) {
var got []PCM16Sample
w := FullFrames(NewPCM16FrameWriter(&got, 8000), 2, DropTailOnClose[PCM16Sample]())
for _, f := range []PCM16Sample{
{},
{1},
{2, 3},
{4, 5, 6},
{7},
} {
err := w.WriteSample(f)
require.NoError(t, err)
}
require.Equal(t, []PCM16Sample{
{1, 2},
{3, 4},
{5, 6},
}, got)
err := w.Close()
require.NoError(t, err)
require.Equal(t, []PCM16Sample{
{1, 2},
{3, 4},
{5, 6},
}, got)
}
Loading