1
0
forked from um/cli
cli/algo/common/raw.go

51 lines
1.1 KiB
Go
Raw 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"
2021-11-11 15:27:07 +00:00
"strings"
)
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(rd io.ReadSeeker) Decoder {
return &RawDecoder{rd: rd}
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)
}
for ext, sniffer := range snifferRegistry {
if sniffer(header) {
2021-11-11 15:27:07 +00:00
d.audioExt = strings.ToLower(ext)
return nil
}
}
return errors.New("audio doesn't recognized")
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
}