59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
//go:build !disable_format_mp3 && !cgo
|
|
|
|
package mp3
|
|
|
|
import (
|
|
"bytes"
|
|
"git.gammaspectra.live/S.O.N.G/Kirika/audio"
|
|
"git.gammaspectra.live/S.O.N.G/Kirika/audio/format"
|
|
mp3Lib "git.gammaspectra.live/S.O.N.G/go-mp3"
|
|
"golang.org/x/exp/slices"
|
|
"io"
|
|
)
|
|
|
|
const BlockSize = 1024 * 128
|
|
|
|
type Format struct {
|
|
}
|
|
|
|
func NewFormat() Format {
|
|
return Format{}
|
|
}
|
|
|
|
func (f Format) Name() string {
|
|
return "mp3"
|
|
}
|
|
|
|
func (f Format) DecoderDescription() string {
|
|
return "S.O.N.G/go-mp3"
|
|
}
|
|
|
|
func (f Format) Open(r io.ReadSeekCloser) (audio.Source, error) {
|
|
decoder, err := mp3Lib.NewDecoder(r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
source := audio.NewSource[float32](16, decoder.SampleRate(), 2)
|
|
|
|
go func() {
|
|
defer source.Close()
|
|
samples := make([]float32, BlockSize*2)
|
|
for {
|
|
n, err := decoder.ReadFloat(samples)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
source.IngestFloat32(slices.Clone(samples[:n]))
|
|
}
|
|
}()
|
|
|
|
return source, nil
|
|
}
|
|
|
|
func (f Format) Identify(peek [format.IdentifyPeekBytes]byte, extension string) bool {
|
|
//match ID3 / sync header
|
|
return peek[0] == 0xff && (peek[1]>>5) == 0b111 || (bytes.Compare(peek[:3], []byte{'I', 'D', '3'}) == 0 && extension != "flac") || extension == "mp3"
|
|
}
|