cli/algo/common/raw.go

51 lines
1.1 KiB
Go
Raw Permalink Normal View History

2020-12-26 07:47:10 +00:00
package common
2021-11-11 15:27:07 +00:00
import (
"errors"
"fmt"
"io"
"unlock-music.dev/cli/internal/sniff"
2021-11-11 15:27:07 +00:00
)
2020-12-26 07:47:10 +00:00
type RawDecoder struct {
rd io.ReadSeeker
audioExt string
2020-12-26 07:47:10 +00:00
}
func NewRawDecoder(p *DecoderParams) Decoder {
return &RawDecoder{rd: p.Reader}
2020-12-26 07:47:10 +00:00
}
func (d *RawDecoder) Validate() error {
header := make([]byte, 16)
if _, err := io.ReadFull(d.rd, header); err != nil {
return fmt.Errorf("read file header failed: %v", err)
}
if _, err := d.rd.Seek(0, io.SeekStart); err != nil {
return fmt.Errorf("seek file failed: %v", err)
}
2022-11-18 23:25:45 +00:00
var ok bool
d.audioExt, ok = sniff.AudioExtension(header)
2022-11-18 23:25:45 +00:00
if !ok {
return errors.New("raw: sniff audio type failed")
}
2022-11-18 23:25:45 +00:00
return nil
2020-12-26 07:47:10 +00:00
}
func (d *RawDecoder) Read(p []byte) (n int, err error) {
return d.rd.Read(p)
2020-12-26 07:47:10 +00:00
}
2020-12-26 07:47:10 +00:00
func init() {
2021-11-11 15:43:20 +00:00
RegisterDecoder("mp3", true, NewRawDecoder)
RegisterDecoder("flac", true, NewRawDecoder)
RegisterDecoder("ogg", true, NewRawDecoder)
RegisterDecoder("m4a", true, NewRawDecoder)
RegisterDecoder("wav", true, NewRawDecoder)
RegisterDecoder("wma", true, NewRawDecoder)
RegisterDecoder("aac", true, NewRawDecoder)
2020-12-26 07:47:10 +00:00
}