Kirika/audio/format/opus/opus.go
2022-02-22 10:35:08 +01:00

53 lines
967 B
Go

package opus
import (
"bytes"
"git.gammaspectra.live/S.O.N.G/Kirika/audio"
libopus "git.gammaspectra.live/S.O.N.G/go-pus"
"io"
)
const OPUS_SAMPLE_RATE int = 48000
type Format struct {
}
func NewFormat() Format {
return Format{}
}
func (f Format) Open(r io.ReadSeekCloser, blockSize int) (*audio.Stream, error) {
stream, err := libopus.NewStream(r)
if err != nil {
return nil, err
}
newChannel := make(chan []float32)
go func() {
defer stream.Close()
defer close(newChannel)
for {
buf := make([]float32, blockSize*2)
n, err := stream.ReadStereoFloat32(buf)
if err != nil {
return
}
if n > 0 {
newChannel <- buf[:n*2]
}
}
}()
//We always get two channels via stereo api
return audio.NewStream(newChannel, 2, float64(OPUS_SAMPLE_RATE), blockSize), nil
}
func (f Format) Identify(peek []byte, extension string) bool {
return bytes.Compare(peek[:4], []byte{'O', 'g', 'g', 'S'}) == 0 || extension == "opus"
}