Kirika/audio/format/vorbis/vorbis.go
DataHoarder 09f3cf3b56
All checks were successful
continuous-integration/drone/push Build is passing
Use generics to implement TypedSource[float32|int16|int32]
2022-07-22 12:07:01 +02:00

59 lines
1,011 B
Go

//go:build !disable_format_vorbis
package vorbis
import (
"bytes"
"git.gammaspectra.live/S.O.N.G/Kirika/audio"
libvorbis "github.com/jfreymuth/oggvorbis"
"io"
)
type Format struct {
}
func NewFormat() Format {
return Format{}
}
func (f Format) Name() string {
return "vorbis"
}
func (f Format) DecoderDescription() string {
return "jfreymuth/oggvorvis"
}
func (f Format) Open(r io.ReadSeekCloser) (audio.Source, error) {
reader, err := libvorbis.NewReader(r)
if err != nil {
return nil, err
}
source := audio.NewSource[float32](16, reader.Channels(), reader.SampleRate())
go func() {
defer source.Close()
for {
buffer := make([]float32, 8192)
n, err := reader.Read(buffer)
if err != nil {
return
}
if n > 0 {
source.IngestFloat32(buffer[:n])
}
}
}()
return source, nil
}
func (f Format) Identify(peek []byte, extension string) bool {
return bytes.Compare(peek[:4], []byte{'O', 'g', 'g', 'S'}) == 0 || extension == "vorbis" || extension == "ogg"
}