package audio import ( "encoding/binary" "io" "math" "github.com/sirupsen/logrus" ) // Sample is a type alias for float64. type Sample float64 // Format is the format of a Buffer or another audio source. type Format interface { // BitsPerSample is the number of bits per sample. BitsPerSample() int // Order is the byte order. Order() binary.ByteOrder // DecodeFromBytes reads one sample from b. No bounds checking is performed on b! DecodeFromBytes(b []byte) Sample // EncodeToBytes writes on sample to b. EncodeToBytes(b []byte, s Sample) } type pcmFormat uint8 const ( Int8 pcmFormat = iota Int16 Int16BE Int32 Int32BE Float32 Float32BE Float64 Float64BE ) func makeFormat(bits int) pcmFormat { switch bits { case 8: return Int8 case 16: return Int16 case 32: return Int32 default: return Int16 } } var pcmfFormatBitsPerSample = map[pcmFormat]int{ Int8: 8, Int16: 16, Int16BE: 16, Int32: 32, Int32BE: 32, Float32: 32, Float32BE: 32, Float64: 64, Float64BE: 64, } func (f pcmFormat) BitsPerSample() int { if v, ok := pcmfFormatBitsPerSample[f]; ok { return v } return 0 } func (f pcmFormat) Order() binary.ByteOrder { if f%2 == 0 { return binary.BigEndian } return binary.LittleEndian } func (f pcmFormat) DecodeFromBytes(b []byte) Sample { order := f.Order() switch f { case Int8: return Sample(int8(b[0])) / math.MaxInt8 case Int16, Int16BE: return Sample(int16(order.Uint16(b))) / math.MaxInt16 case Int32, Int32BE: return Sample(int32(order.Uint32(b))) / math.MaxInt32 case Float32, Float32BE: return Sample(math.Float32frombits(order.Uint32(b))) case Float64, Float64BE: return Sample(math.Float64frombits(order.Uint64(b))) default: return 0 } } func (f pcmFormat) EncodeToBytes(b []byte, s Sample) { order := f.Order() switch f { case Int8: b[0] = byte(int8(s * math.MaxInt8)) case Int16, Int16BE: order.PutUint16(b, uint16(int16(s*math.MaxInt16))) case Int32, Int32BE: order.PutUint32(b, uint32(int32(s*math.MaxInt32))) case Float32, Float32BE: order.PutUint32(b, math.Float32bits(float32(s))) case Float64, Float64BE: order.PutUint64(b, math.Float64bits(float64(s))) } } var pcmFormatNames = map[pcmFormat]string{ Int8: "8-bit signed PCM", Int16: "16-bit signed PCM", Int16BE: "16-bit signed PCM (BE)", Int32: "32-bit signed PCM", Int32BE: "32-bit signed PCM (BE)", Float32: "32-bit floating piont PCM", Float32BE: "32-bit floating point PCM (BE)", Float64: "64-bit floating point PCM", Float64BE: "64-bit floating point PCM (BE)", } func (f pcmFormat) String() string { if s, ok := pcmFormatNames[f]; ok { return s } return "unknown" } // Buffer of audio samples, only supports mono and stereo samples. type Buffer struct { Format Format Rate Rate Samples [][]Sample buf []byte } func NewBuffer(format Format, rate Rate, n, channels int) *Buffer { if channels < 1 { channels = 1 } if channels > 2 { channels = 2 } logrus.WithFields(logrus.Fields{ "channels": channels, "rate": rate, "format": format, "samples": n, }).Debug("new audio buffer") samples := make([][]Sample, channels) for channel := 0; channel < channels; channel++ { samples[channel] = make([]Sample, n) } return &Buffer{ Format: format, Rate: rate, Samples: samples, } } // DecodeFrom decodes samples read from r. func (b *Buffer) DecodeFrom(r io.Reader) (n int, err error) { if s, ok := r.(SamplesReader); ok { // Use the SamplesReader interface, if available. return s.ReadSamples(b.Samples) } var ( channels = len(b.Samples) samples = len(b.Samples[0]) bitsPerSample = b.Format.BitsPerSample() bytesPerSample = bitsPerSample / 8 bytesRequired = channels * samples * bytesPerSample ) // Resize buffer, if required. if len(b.buf) < bytesRequired { b.buf = make([]byte, bytesRequired) } else { b.buf = b.buf[:bytesRequired] } // Read raw bytes. if _, err = io.ReadFull(r, b.buf); err != nil { return } // Convert bytes to samples, deinterleave multi channel audio. var o, c int for i, l := 0, bytesRequired/bytesPerSample; i < l; i++ { b.Samples[0][c] = b.Format.DecodeFromBytes(b.buf[o : o+bytesPerSample]) c = (c + 1) % channels o = o + bytesPerSample } return bytesRequired / bytesPerSample, nil } // ToMono converts the buffer to mono. If the buffer is already mono, it returns the buffer. func (b *Buffer) ToMono() *Buffer { if len(b.Samples) == 1 { return b } // Convert stereo buffer to mono. o := NewBuffer(b.Format, b.Rate, len(b.Samples[0]), 2) for i := range o.Samples[0] { o.Samples[0][i] = (b.Samples[0][i] + b.Samples[1][i]) / 2 } return o } // ToStereo converts the buffer to stereo. If the buffer is already stereo, it returns the buffer. func (b *Buffer) ToStereo() *Buffer { if len(b.Samples) == 2 { return b } // Convert mono buffer to stereo. o := NewBuffer(b.Format, b.Rate, len(b.Samples[0]), 1) copy(o.Samples[0], b.Samples[0]) copy(o.Samples[1], b.Samples[0]) return o } type BufferProcessor interface { ProcessBuffer(buffer *Buffer) } type Processor interface { ProcessSamples(samples [][]Sample) }