Skip to content

Commit

Permalink
add float32 buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
mattetti committed Dec 30, 2016
1 parent 29e71c1 commit 8503dc9
Showing 1 changed file with 112 additions and 1 deletion.
113 changes: 112 additions & 1 deletion audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ type Buffer interface {
PCMFormat() *Format
// NumFrames returns the number of frames contained in the buffer.
NumFrames() int
// AsFloatBuffer returns a float buffer from this buffer.
// AsFloatBuffer returns a float 64 buffer from this buffer.
AsFloatBuffer() *FloatBuffer
// AsFloat32Buffer returns a float 32 buffer from this buffer.
AsFloat32Buffer() *Float32Buffer

This comment has been minimized.

Copy link
@mattetti

mattetti Dec 30, 2016

Author Member

the addition of a float32 buffer was done to accommodate for the use case discussed here: golang/go#13432 (comment)

// AsIntBuffer returns an int buffer from this buffer.
AsIntBuffer() *IntBuffer
// Clone creates a clean clone that can be modified without
Expand All @@ -51,6 +53,22 @@ func (buf *FloatBuffer) PCMFormat() *Format { return buf.Format }
// AsFloatBuffer implements the Buffer interface and returns itself.
func (buf *FloatBuffer) AsFloatBuffer() *FloatBuffer { return buf }

// AsFloat32Buffer implements the Buffer interface and returns a float 32 version of itself.
func (buf *FloatBuffer) AsFloat32Buffer() *Float32Buffer {
newB := &Float32Buffer{}
newB.Data = make([]float32, len(buf.Data))
for i := 0; i < len(buf.Data); i++ {
newB.Data[i] = float32(buf.Data[i])
}
newB.Format = &Format{
NumChannels: buf.Format.NumChannels,
SampleRate: buf.Format.SampleRate,
BitDepth: buf.Format.BitDepth,
Endianness: buf.Format.Endianness,
}
return newB
}

// AsIntBuffer returns a copy of this buffer but with data truncated to Ints.
func (buf *FloatBuffer) AsIntBuffer() *IntBuffer {
newB := &IntBuffer{}
Expand Down Expand Up @@ -98,6 +116,83 @@ func (buf *FloatBuffer) NumFrames() int {
return len(buf.Data) / numChannels
}

// Float32Buffer is an audio buffer with its PCM data formatted as float32.
type Float32Buffer struct {
// Format is the representation of the underlying data format
Format *Format
// Data is the buffer PCM data as floats
Data []float32
}

// PCMFormat returns the buffer format information.
func (buf *Float32Buffer) PCMFormat() *Format { return buf.Format }

// AsFloatBuffer implements the Buffer interface and returns a float64 version of itself.
func (buf *Float32Buffer) AsFloatBuffer() *FloatBuffer {
newB := &FloatBuffer{}
newB.Data = make([]float64, len(buf.Data))
for i := 0; i < len(buf.Data); i++ {
newB.Data[i] = float64(buf.Data[i])
}
newB.Format = &Format{
NumChannels: buf.Format.NumChannels,
SampleRate: buf.Format.SampleRate,
BitDepth: buf.Format.BitDepth,
Endianness: buf.Format.Endianness,
}
return newB
}

// AsFloat32Buffer implements the Buffer interface and returns itself.
func (buf *Float32Buffer) AsFloat32Buffer() *Float32Buffer { return buf }

// AsIntBuffer returns a copy of this buffer but with data truncated to Ints.
func (buf *Float32Buffer) AsIntBuffer() *IntBuffer {
newB := &IntBuffer{}
newB.Data = make([]int, len(buf.Data))
for i := 0; i < len(buf.Data); i++ {
newB.Data[i] = int(buf.Data[i])
}
newB.Format = &Format{
NumChannels: buf.Format.NumChannels,
SampleRate: buf.Format.SampleRate,
BitDepth: buf.Format.BitDepth,
Endianness: buf.Format.Endianness,
}
return newB
}

// Clone creates a clean clone that can be modified without
// changing the source buffer.
func (buf *Float32Buffer) Clone() Buffer {
if buf == nil {
return nil
}
newB := &Float32Buffer{}
newB.Data = make([]float32, len(buf.Data))
copy(newB.Data, buf.Data)
newB.Format = &Format{
NumChannels: buf.Format.NumChannels,
SampleRate: buf.Format.SampleRate,
BitDepth: buf.Format.BitDepth,
Endianness: buf.Format.Endianness,
}
return newB
}

// NumFrames returns the number of frames contained in the buffer.
func (buf *Float32Buffer) NumFrames() int {
if buf == nil || buf.Format == nil {
return 0
}
numChannels := buf.Format.NumChannels
if numChannels == 0 {
numChannels = 1
}

return len(buf.Data) / numChannels
}

// IntBuffer is an audio buffer with its PCM data formatted as int.
type IntBuffer struct {
// Format is the representation of the underlying data format
Expand Down Expand Up @@ -125,6 +220,22 @@ func (buf *IntBuffer) AsFloatBuffer() *FloatBuffer {
return newB
}

// AsFloat32Buffer returns a copy of this buffer but with data converted to float 32.
func (buf *IntBuffer) AsFloat32Buffer() *Float32Buffer {
newB := &Float32Buffer{}
newB.Data = make([]float32, len(buf.Data))
for i := 0; i < len(buf.Data); i++ {
newB.Data[i] = float32(buf.Data[i])
}
newB.Format = &Format{
NumChannels: buf.Format.NumChannels,
SampleRate: buf.Format.SampleRate,
BitDepth: buf.Format.BitDepth,
Endianness: buf.Format.Endianness,
}
return newB
}

// AsIntBuffer implements the Buffer interface and returns itself.
func (buf *IntBuffer) AsIntBuffer() *IntBuffer { return buf }

Expand Down

0 comments on commit 8503dc9

Please sign in to comment.