2020-12-26 07:47:10 +00:00
|
|
|
package common
|
|
|
|
|
2021-11-11 15:27:07 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
2022-11-18 23:25:43 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-11-21 22:16:40 +00:00
|
|
|
|
|
|
|
"unlock-music.dev/cli/internal/sniff"
|
2021-11-11 15:27:07 +00:00
|
|
|
)
|
2021-05-23 11:59:29 +00:00
|
|
|
|
2020-12-26 07:47:10 +00:00
|
|
|
type RawDecoder struct {
|
2022-11-18 23:25:43 +00:00
|
|
|
rd io.ReadSeeker
|
|
|
|
|
2020-12-26 08:07:48 +00:00
|
|
|
audioExt string
|
2020-12-26 07:47:10 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 23:25:43 +00:00
|
|
|
func NewRawDecoder(rd io.ReadSeeker) Decoder {
|
|
|
|
return &RawDecoder{rd: rd}
|
2020-12-26 07:47:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-23 11:59:29 +00:00
|
|
|
func (d *RawDecoder) Validate() error {
|
2022-11-18 23:25:43 +00:00
|
|
|
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
|
2022-11-21 22:16:40 +00:00
|
|
|
d.audioExt, ok = sniff.AudioExtension(header)
|
2022-11-18 23:25:45 +00:00
|
|
|
if !ok {
|
|
|
|
return errors.New("raw: sniff audio type failed")
|
2021-05-23 11:59:29 +00:00
|
|
|
}
|
2022-11-18 23:25:45 +00:00
|
|
|
return nil
|
2020-12-26 07:47:10 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 23:25:43 +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
|
|
|
}
|
2021-05-23 11:59:29 +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
|
|
|
}
|