func (b *PCMBuffer) AsInt16s() (out []int16) {
	if b == nil {
		return nil
	}
	switch b.DataType {
	case Integer, Float:
		out = make([]int16, len(b.Ints))
		for i := 0; i < len(b.Ints); i++ {
			out[i] = int16(b.Ints[i])
		}
	case Byte:
		if b.Format == nil || b.Format.Endianness == nil || b.Format.BitDepth == 0 {
			return out
		}
		bytesPerSample := int((b.Format.BitDepth-1)/8 + 1)
		buf := bytes.NewBuffer(b.Bytes)
		out := make([]int16, len(b.Bytes)/bytesPerSample)
		binary.Read(buf, b.Format.Endianness, &out)
	}
	return out
}
AsInt16s returns the buffer samples as int16 sample values.
package audio
import (
	"bytes"
	"encoding/binary"
	"errors"
)
var (
	ErrInvalidBuffer = errors.New("invalid buffer")
)
type DataFormat int
const (
	Unknown DataFormat = iota
	Integer
	Float
	Byte
)
type Format struct {
	NumChannels int
	SampleRate int
	BitDepth int
	Endianness binary.ByteOrder
}
type PCMBuffer struct {
	Format *Format
	Ints []int
	Floats []float64
	Bytes []byte
	DataType DataFormat
	framePos int64
}
func NewPCMIntBuffer(data []int, format *Format) *PCMBuffer {
	return &PCMBuffer{
		Format:   format,
		DataType: Integer,
		Ints:     data,
	}
}
func NewPCMFloatBuffer(data []float64, format *Format) *PCMBuffer {
	return &PCMBuffer{
		Format:   format,
		DataType: Float,
		Floats:   data,
	}
}
func NewPCMByteBuffer(data []byte, format *Format) *PCMBuffer {
	return &PCMBuffer{
		Format:   format,
		DataType: Byte,
		Bytes:    data,
	}
}
func (b *PCMBuffer) Len() int {
	if b == nil {
		return 0
	}
	switch b.DataType {
	case Integer:
		return len(b.Ints)
	case Float:
		return len(b.Floats)
	case Byte:
		return len(b.Bytes)
	default:
		return 0
	}
}
func (b *PCMBuffer) Size() (numFrames int) {
	if b == nil || b.Format == nil {
		return 0
	}
	numChannels := b.Format.NumChannels
	if numChannels == 0 {
		numChannels = 1
	}
	switch b.DataType {
	case Integer:
		numFrames = len(b.Ints) / numChannels
	case Float:
		numFrames = len(b.Floats) / numChannels
	case Byte:
		sampleSize := int((b.Format.BitDepth-1)/8 + 1)
		numFrames = (len(b.Bytes) / sampleSize) / numChannels
	}
	return numFrames
}
func (b *PCMBuffer) Clone() *PCMBuffer {
	if b == nil {
		return nil
	}
	newB := &PCMBuffer{DataType: b.DataType}
	switch b.DataType {
	case Integer:
		newB.Ints = make([]int, len(b.Ints))
		copy(newB.Ints, b.Ints)
	case Float:
		newB.Floats = make([]float64, len(b.Floats))
		copy(newB.Floats, b.Floats)
	case Byte:
		newB.Bytes = make([]byte, len(b.Bytes))
		copy(newB.Bytes, b.Bytes)
	}
	newB.Format = &Format{
		NumChannels: b.Format.NumChannels,
		SampleRate:  b.Format.SampleRate,
		BitDepth:    b.Format.BitDepth,
		Endianness:  b.Format.Endianness,
	}
	return newB
}