Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving a slice of frames in a single operation, and removed fsync #19

Closed
wants to merge 3 commits into from
Closed
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
56 changes: 50 additions & 6 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"errors"
"fmt"
"io"
"os"
"reflect"

"github.com/go-audio/audio"
"github.com/go-audio/riff"
Expand Down Expand Up @@ -195,8 +195,12 @@ func (e *Encoder) Write(buf *audio.IntBuffer) error {
return e.addBuffer(buf)
}

// WriteFrame writes a single frame of data to the underlying writer.
// WriteFrame writes a single frame (sample) of data to the underlying writer.
func (e *Encoder) WriteFrame(value interface{}) error {
if reflect.TypeOf(value).Kind() == reflect.Slice {
return fmt.Errorf("WriteFrame() only writes a single frame. Use WriteFrames() for slices")
}

if !e.wroteHeader {
e.writeHeader()
}
Expand All @@ -218,6 +222,50 @@ func (e *Encoder) WriteFrame(value interface{}) error {
return e.AddLE(value)
}

// WriteFrames writes a slice of frames (samples) of data to the underlying writer.
func (e *Encoder) WriteFrames(value interface{}) error {
if !e.wroteHeader {
e.writeHeader()
}
if !e.pcmChunkStarted {
// sound header
if err := e.AddLE(riff.DataFormatID); err != nil {
return fmt.Errorf("error encoding sound header %v", err)
}
e.pcmChunkStarted = true

// write a temporary chunksize
e.pcmChunkSizePos = e.WrittenBytes
if err := e.AddLE(uint32(42)); err != nil {
return fmt.Errorf("%v when writing wav data chunk size header", err)
}
}

nFrames := 0
switch v := value.(type) {
// Note that v is of different type in each case clause
case []byte:
nFrames += len(v)
case []int16:
nFrames += len(v)
case []uint16:
nFrames += len(v)
case []int32:
nFrames += len(v)
case []uint32:
nFrames += len(v)
case []float32:
nFrames += len(v)
default:
nFrames++
}
// A frame holds samples for each channel
nFrames /= e.NumChans

e.frames += nFrames
return e.AddLE(value)
}

func (e *Encoder) writeMetadata() error {
chunkData := encodeInfoChunk(e)
if err := e.AddBE(CIDList); err != nil {
Expand Down Expand Up @@ -267,9 +315,5 @@ func (e *Encoder) Close() error {
if _, err := e.w.Seek(0, 2); err != nil {
return err
}
switch e.w.(type) {
case *os.File:
return e.w.(*os.File).Sync()
}
return nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/go-audio/wav
module github.com/ivoras/wav
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should import go-audio/wav


go 1.12

Expand Down