vosk-recognize-dev/pkg/audio/float_buffer.go
2025-10-23 13:06:22 +07:00

133 lines
3.2 KiB
Go

package audio
var _ Buffer = (*FloatBuffer)(nil)
var _ Buffer = (*Float32Buffer)(nil)
type FloatBuffer struct {
Format *Format
Data []float64
}
func (buf *FloatBuffer) PCMFormat() *Format {
return buf.Format
}
func (buf *FloatBuffer) AsFloatBuffer() *FloatBuffer {
return buf
}
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,
}
return newB
}
func (buf *FloatBuffer) 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,
}
return newB
}
func (buf *FloatBuffer) Clone() Buffer {
if buf == nil {
return nil
}
newB := &FloatBuffer{}
newB.Data = make([]float64, len(buf.Data))
copy(newB.Data, buf.Data)
newB.Format = &Format{
NumChannels: buf.Format.NumChannels,
SampleRate: buf.Format.SampleRate,
}
return newB
}
func (buf *FloatBuffer) NumFrames() int {
if buf == nil || buf.Format == nil {
return 0
}
numChannels := buf.Format.NumChannels
if numChannels == 0 {
numChannels = 1
}
return len(buf.Data) / numChannels
}
type Float32Buffer struct {
Format *Format
Data []float32
SourceBitDepth int
}
func (buf *Float32Buffer) PCMFormat() *Format { return buf.Format }
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,
}
return newB
}
func (buf *Float32Buffer) AsFloat32Buffer() *Float32Buffer { return buf }
func (buf *Float32Buffer) AsIntBuffer() *IntBuffer {
newB := &IntBuffer{SourceBitDepth: buf.SourceBitDepth}
if newB.SourceBitDepth == 0 {
newB.SourceBitDepth = 16
}
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,
}
return newB
}
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,
}
return newB
}
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
}