Kirika/audio/format/vorbis/vorbis_nocgo.go

60 lines
1.1 KiB
Go

//go:build !disable_format_vorbis && (disable_codec_libvorbis || !cgo)
package vorbis
import (
"bytes"
"git.gammaspectra.live/S.O.N.G/Kirika/audio"
"git.gammaspectra.live/S.O.N.G/Kirika/audio/format"
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.SampleRate(), reader.Channels())
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 [format.IdentifyPeekBytes]byte, extension string) bool {
return bytes.Compare(peek[:4], []byte{'O', 'g', 'g', 'S'}) == 0 || extension == "vorbis" || extension == "ogg"
}