DataHoarder
439ad19af8
All checks were successful
continuous-integration/drone/push Build is passing
61 lines
1.1 KiB
Go
61 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"
|
|
"golang.org/x/exp/slices"
|
|
"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()
|
|
|
|
buffer := make([]float32, 8192)
|
|
|
|
for {
|
|
|
|
n, err := reader.Read(buffer)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
if n > 0 {
|
|
source.IngestFloat32(slices.Clone(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"
|
|
}
|