From ea3236e14bc9a4dcf8b5920802dbec5c78044280 Mon Sep 17 00:00:00 2001 From: Unlock Music Dev Date: Sun, 4 Dec 2022 23:05:38 +0800 Subject: [PATCH] refactor: change decoder init parameter --- algo/common/dispatch.go | 8 +++++++- algo/common/raw.go | 4 ++-- algo/kgm/kgm.go | 4 ++-- algo/kwm/kwm.go | 4 ++-- algo/ncm/ncm.go | 6 ++---- algo/qmc/qmc.go | 6 ++++-- algo/tm/tm.go | 4 ++-- algo/xiami/xm.go | 4 ++-- algo/ximalaya/ximalaya.go | 4 ++-- cmd/um/main.go | 8 +++++++- 10 files changed, 32 insertions(+), 20 deletions(-) diff --git a/algo/common/dispatch.go b/algo/common/dispatch.go index 07b110c..263ad64 100644 --- a/algo/common/dispatch.go +++ b/algo/common/dispatch.go @@ -6,7 +6,13 @@ import ( "strings" ) -type NewDecoderFunc func(rd io.ReadSeeker) Decoder +type DecoderParams struct { + Reader io.ReadSeeker // required + Extension string // required, source extension, eg. ".mp3" + + FilePath string // optional, source file path +} +type NewDecoderFunc func(p *DecoderParams) Decoder type decoderItem struct { noop bool diff --git a/algo/common/raw.go b/algo/common/raw.go index c145e25..dd74886 100644 --- a/algo/common/raw.go +++ b/algo/common/raw.go @@ -14,8 +14,8 @@ type RawDecoder struct { audioExt string } -func NewRawDecoder(rd io.ReadSeeker) Decoder { - return &RawDecoder{rd: rd} +func NewRawDecoder(p *DecoderParams) Decoder { + return &RawDecoder{rd: p.Reader} } func (d *RawDecoder) Validate() error { diff --git a/algo/kgm/kgm.go b/algo/kgm/kgm.go index 7b5330e..a02508d 100644 --- a/algo/kgm/kgm.go +++ b/algo/kgm/kgm.go @@ -16,8 +16,8 @@ type Decoder struct { header header } -func NewDecoder(rd io.ReadSeeker) common.Decoder { - return &Decoder{rd: rd} +func NewDecoder(p *common.DecoderParams) common.Decoder { + return &Decoder{rd: p.Reader} } // Validate checks if the file is a valid Kugou (.kgm, .vpr, .kgma) file. diff --git a/algo/kwm/kwm.go b/algo/kwm/kwm.go index 166e732..1bbc604 100644 --- a/algo/kwm/kwm.go +++ b/algo/kwm/kwm.go @@ -30,8 +30,8 @@ func (d *Decoder) GetAudioExt() string { return "." + d.outputExt } -func NewDecoder(rd io.ReadSeeker) common.Decoder { - return &Decoder{rd: rd} +func NewDecoder(p *common.DecoderParams) common.Decoder { + return &Decoder{rd: p.Reader} } // Validate checks if the file is a valid Kuwo .kw file. diff --git a/algo/ncm/ncm.go b/algo/ncm/ncm.go index 5e20093..f54f1ba 100644 --- a/algo/ncm/ncm.go +++ b/algo/ncm/ncm.go @@ -29,10 +29,8 @@ var ( } ) -func NewDecoder(rd io.ReadSeeker) common.Decoder { - return &Decoder{ - rd: rd, - } +func NewDecoder(p *common.DecoderParams) common.Decoder { + return &Decoder{rd: p.Reader} } type Decoder struct { diff --git a/algo/qmc/qmc.go b/algo/qmc/qmc.go index 24f94e8..96e56c7 100644 --- a/algo/qmc/qmc.go +++ b/algo/qmc/qmc.go @@ -38,8 +38,8 @@ func (d *Decoder) Read(p []byte) (int, error) { return n, err } -func NewDecoder(r io.ReadSeeker) common.Decoder { - return &Decoder{raw: r} +func NewDecoder(p *common.DecoderParams) common.Decoder { + return &Decoder{raw: p.Reader} } func (d *Decoder) Validate() error { @@ -214,6 +214,8 @@ func init() { "mgg", "mgg1", "mggl", //QQ Music New Ogg "mflac", "mflac0", //QQ Music New Flac + + "mflach", // QQ Music Flac (storing key in dedicate MMKV) } for _, ext := range supportedExts { common.RegisterDecoder(ext, false, NewDecoder) diff --git a/algo/tm/tm.go b/algo/tm/tm.go index d29326a..d11ecbc 100644 --- a/algo/tm/tm.go +++ b/algo/tm/tm.go @@ -43,8 +43,8 @@ func (d *Decoder) Read(buf []byte) (int, error) { return d.audio.Read(buf) } -func NewTmDecoder(rd io.ReadSeeker) common.Decoder { - return &Decoder{raw: rd} +func NewTmDecoder(p *common.DecoderParams) common.Decoder { + return &Decoder{raw: p.Reader} } func init() { diff --git a/algo/xiami/xm.go b/algo/xiami/xm.go index f878175..83d94e4 100644 --- a/algo/xiami/xm.go +++ b/algo/xiami/xm.go @@ -37,8 +37,8 @@ func (d *Decoder) GetAudioExt() string { return "" } -func NewDecoder(rd io.ReadSeeker) common.Decoder { - return &Decoder{rd: rd} +func NewDecoder(p *common.DecoderParams) common.Decoder { + return &Decoder{rd: p.Reader} } // Validate checks if the file is a valid xiami .xm file. diff --git a/algo/ximalaya/ximalaya.go b/algo/ximalaya/ximalaya.go index fc92528..885693b 100644 --- a/algo/ximalaya/ximalaya.go +++ b/algo/ximalaya/ximalaya.go @@ -16,8 +16,8 @@ type Decoder struct { audio io.Reader } -func NewDecoder(rd io.ReadSeeker) common.Decoder { - return &Decoder{rd: rd} +func NewDecoder(p *common.DecoderParams) common.Decoder { + return &Decoder{rd: p.Reader} } func (d *Decoder) Validate() error { diff --git a/cmd/um/main.go b/cmd/um/main.go index dceea7b..01cf963 100644 --- a/cmd/um/main.go +++ b/cmd/um/main.go @@ -163,9 +163,15 @@ func tryDecFile(inputFile string, outputDir string, allDec []common.NewDecoderFu } defer file.Close() + decParams := &common.DecoderParams{ + Reader: file, + Extension: filepath.Ext(inputFile), + FilePath: inputFile, + } + var dec common.Decoder for _, decFunc := range allDec { - dec = decFunc(file) + dec = decFunc(decParams) if err := dec.Validate(); err == nil { break } else {