From 15e340eac40a1ea7b0595280ccbaf8ce262dd17b Mon Sep 17 00:00:00 2001 From: Emmm Monster <58943012+emmmx@users.noreply.github.com> Date: Sun, 23 May 2021 19:59:29 +0800 Subject: [PATCH] Update RawDecoder & Support `.kwm` as `.aac` --- algo/common/raw.go | 33 +++++++++++++++++++-------------- algo/common/sniff.go | 10 +++++++--- algo/kwm/kwm.go | 1 + algo/tm/tm.go | 4 ++-- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/algo/common/raw.go b/algo/common/raw.go index 5182757..f12e783 100644 --- a/algo/common/raw.go +++ b/algo/common/raw.go @@ -1,17 +1,24 @@ package common +import "errors" + type RawDecoder struct { file []byte audioExt string } -//goland:noinspection GoUnusedExportedFunction func NewRawDecoder(file []byte) Decoder { return &RawDecoder{file: file} } -func (d RawDecoder) Validate() error { - return nil +func (d *RawDecoder) Validate() error { + for ext, sniffer := range snifferRegistry { + if sniffer(d.file) { + d.audioExt = ext + return nil + } + } + return errors.New("audio doesn't recognized") } func (d RawDecoder) Decode() error { @@ -27,21 +34,19 @@ func (d RawDecoder) GetAudioData() []byte { } func (d RawDecoder) GetAudioExt() string { - return "." + d.audioExt + return d.audioExt } func (d RawDecoder) GetMeta() Meta { return nil } -func DecoderFuncWithExt(ext string) NewDecoderFunc { - return func(file []byte) Decoder { - return &RawDecoder{file: file, audioExt: ext} - } -} + func init() { - /*RegisterDecoder("mp3", DecoderFuncWithExt("mp3")) - RegisterDecoder("flac", DecoderFuncWithExt("flac")) - RegisterDecoder("wav", DecoderFuncWithExt("wav")) - RegisterDecoder("ogg", DecoderFuncWithExt("ogg")) - RegisterDecoder("m4a", DecoderFuncWithExt("m4a"))*/ + RegisterDecoder("mp3", NewRawDecoder) + RegisterDecoder("flac", NewRawDecoder) + RegisterDecoder("ogg", NewRawDecoder) + RegisterDecoder("m4a", NewRawDecoder) + RegisterDecoder("wav", NewRawDecoder) + RegisterDecoder("wma", NewRawDecoder) + RegisterDecoder("aac", NewRawDecoder) } diff --git a/algo/common/sniff.go b/algo/common/sniff.go index 650e3ae..899c639 100644 --- a/algo/common/sniff.go +++ b/algo/common/sniff.go @@ -5,12 +5,13 @@ import "bytes" type Sniffer func(header []byte) bool var snifferRegistry = map[string]Sniffer{ - ".m4a": SnifferM4A, - ".ogg": SnifferOGG, + ".mp3": SnifferMP3, ".flac": SnifferFLAC, + ".ogg": SnifferOGG, + ".m4a": SnifferM4A, ".wav": SnifferWAV, ".wma": SnifferWMA, - ".mp3": SnifferMP3, + ".aac": SnifferAAC, } func SniffAll(header []byte) (string, bool) { @@ -42,3 +43,6 @@ func SnifferWAV(header []byte) bool { func SnifferWMA(header []byte) bool { return bytes.HasPrefix(header, []byte("\x30\x26\xb2\x75\x8e\x66\xcf\x11\xa6\xd9\x00\xaa\x00\x62\xce\x6c")) } +func SnifferAAC(header []byte) bool { + return bytes.HasPrefix(header, []byte{0xFF, 0xF1}) +} diff --git a/algo/kwm/kwm.go b/algo/kwm/kwm.go index e0097e8..e846ac7 100644 --- a/algo/kwm/kwm.go +++ b/algo/kwm/kwm.go @@ -125,4 +125,5 @@ func padOrTruncate(raw string, length int) string { func init() { // Kuwo Mp3/Flac common.RegisterDecoder("kwm", NewDecoder) + common.RegisterDecoder("kwm", common.NewRawDecoder) } diff --git a/algo/tm/tm.go b/algo/tm/tm.go index a505ba4..40c6ab4 100644 --- a/algo/tm/tm.go +++ b/algo/tm/tm.go @@ -73,7 +73,7 @@ func init() { common.RegisterDecoder("tm2", DecoderFuncWithExt("m4a")) common.RegisterDecoder("tm6", DecoderFuncWithExt("m4a")) // QQ Music IOS Mp3 - common.RegisterDecoder("tm0", common.DecoderFuncWithExt("mp3")) - common.RegisterDecoder("tm3", common.DecoderFuncWithExt("mp3")) + common.RegisterDecoder("tm0", common.NewRawDecoder) + common.RegisterDecoder("tm3", common.NewRawDecoder) }