Kirika/audio/format/vorbis/vorbis.go
2022-04-23 20:41:14 +02:00

65 lines
1.1 KiB
Go

//go:build !disable_format_vorbis
// +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) Description() string {
return "jfreymuth/oggvorvis"
}
func (f Format) Open(r io.ReadSeekCloser) (audio.Source, error) {
reader, err := libvorbis.NewReader(r)
if err != nil {
return audio.Source{}, err
}
newChannel := make(chan []float32)
go func() {
defer close(newChannel)
for {
buffer := make([]float32, 8192)
n, err := reader.Read(buffer)
if err != nil {
return
}
if n > 0 {
newChannel <- buffer[:n]
}
}
}()
//We always get two channels via stereo api
return audio.Source{
Channels: reader.Channels(),
SampleRate: reader.SampleRate(),
Blocks: newChannel,
}, 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"
}