From d5031e1935f81e2a83c5c850d28909372dbe3d2c Mon Sep 17 00:00:00 2001 From: Emmm Monster <58943012+emmmx@users.noreply.github.com> Date: Sat, 26 Dec 2020 15:47:10 +0800 Subject: [PATCH] Add Decoder Registry Add Raw Decoder --- algo/common/dispatch.go | 12 ++++++++++++ algo/common/raw.go | 41 +++++++++++++++++++++++++++++++++++++++++ algo/kgm/kgm.go | 9 ++++++++- algo/kwm/kwm.go | 7 ++++++- algo/ncm/ncm.go | 7 ++++++- algo/qmc/qmc.go | 28 +++++++++++++++++++++++++--- algo/tm/tm.go | 18 ++++++++++++++---- algo/xm/xm.go | 12 +++++++++++- 8 files changed, 123 insertions(+), 11 deletions(-) create mode 100644 algo/common/dispatch.go create mode 100644 algo/common/raw.go diff --git a/algo/common/dispatch.go b/algo/common/dispatch.go new file mode 100644 index 0000000..2392152 --- /dev/null +++ b/algo/common/dispatch.go @@ -0,0 +1,12 @@ +package common + +type NewDecoderFunc func([]byte) Decoder + +var decoderRegistry = make(map[string][]NewDecoderFunc) + +func RegisterDecoder(ext string, dispatchFunc NewDecoderFunc) { + decoderRegistry[ext] = append(decoderRegistry[ext], dispatchFunc) +} +func GetDecoder(ext string) []NewDecoderFunc { + return decoderRegistry[ext] +} diff --git a/algo/common/raw.go b/algo/common/raw.go new file mode 100644 index 0000000..13af2a5 --- /dev/null +++ b/algo/common/raw.go @@ -0,0 +1,41 @@ +package common + +type RawDecoder struct { + file []byte +} + +func NewRawDecoder(file []byte) Decoder { + return &RawDecoder{file: file} +} + +func (d RawDecoder) Validate() error { + return nil +} + +func (d RawDecoder) Decode() error { + return nil +} + +func (d RawDecoder) GetCoverImage() []byte { + return nil +} + +func (d RawDecoder) GetAudioData() []byte { + return d.file +} + +func (d RawDecoder) GetAudioExt() string { + return "" +} + +func (d RawDecoder) GetMeta() Meta { + return nil +} + +func init() { + RegisterDecoder("mp3", NewRawDecoder) + RegisterDecoder("flac", NewRawDecoder) + RegisterDecoder("wav", NewRawDecoder) + RegisterDecoder("ogg", NewRawDecoder) + RegisterDecoder("m4a", NewRawDecoder) +} diff --git a/algo/kgm/kgm.go b/algo/kgm/kgm.go index 7de2f21..308751d 100644 --- a/algo/kgm/kgm.go +++ b/algo/kgm/kgm.go @@ -25,7 +25,7 @@ type Decoder struct { audio []byte } -func NewDecoder(file []byte) *Decoder { +func NewDecoder(file []byte) common.Decoder { return &Decoder{ file: file, } @@ -89,3 +89,10 @@ func (d *Decoder) Decode() error { } return nil } +func init() { + // Kugou + common.RegisterDecoder("kgm", NewDecoder) + common.RegisterDecoder("kgma", NewDecoder) + // Viper + common.RegisterDecoder("vpr", NewDecoder) +} diff --git a/algo/kwm/kwm.go b/algo/kwm/kwm.go index b057cf8..4e94a38 100644 --- a/algo/kwm/kwm.go +++ b/algo/kwm/kwm.go @@ -48,7 +48,7 @@ func (d *Decoder) GetMeta() common.Meta { return nil } -func NewDecoder(data []byte) *Decoder { +func NewDecoder(data []byte) common.Decoder { //todo: Notice the input data will be changed for now return &Decoder{file: data} } @@ -123,3 +123,8 @@ func padOrTruncate(raw string, length int) string { } return out } + +func init() { + // Kuwo Mp3/Flac + common.RegisterDecoder("kwm", NewDecoder) +} diff --git a/algo/ncm/ncm.go b/algo/ncm/ncm.go index 07592e0..0dfb393 100644 --- a/algo/ncm/ncm.go +++ b/algo/ncm/ncm.go @@ -26,7 +26,7 @@ var ( 0x5C, 0x5D, 0x26, 0x30, 0x55, 0x3C, 0x27, 0x28} ) -func NewDecoder(data []byte) *Decoder { +func NewDecoder(data []byte) common.Decoder { return &Decoder{ file: data, fileLen: uint32(len(data)), @@ -255,3 +255,8 @@ func (d Decoder) GetCoverImage() []byte { func (d Decoder) GetMeta() common.Meta { return d.meta } + +func init() { + // Netease Mp3/Flac + common.RegisterDecoder("ncm", NewDecoder) +} diff --git a/algo/qmc/qmc.go b/algo/qmc/qmc.go index a5d88f1..bde250f 100644 --- a/algo/qmc/qmc.go +++ b/algo/qmc/qmc.go @@ -22,15 +22,15 @@ type Decoder struct { audio []byte } -func NewDefaultDecoder(data []byte) *Decoder { +func NewDefaultDecoder(data []byte) common.Decoder { return &Decoder{file: data, mask: getDefaultMask()} } -func NewMflac256Decoder(data []byte) *Decoder { +func NewMflac256Decoder(data []byte) common.Decoder { return &Decoder{file: data, maskDetector: detectMflac256Mask, audioExt: "flac"} } -func NewMgg256Decoder(data []byte) *Decoder { +func NewMgg256Decoder(data []byte) common.Decoder { return &Decoder{file: data, maskDetector: detectMgg256Mask, audioExt: "ogg"} } @@ -94,3 +94,25 @@ func (d Decoder) GetAudioExt() string { func (d Decoder) GetMeta() common.Meta { return nil } + +func init() { + common.RegisterDecoder("qmc3", NewDefaultDecoder) //QQ Music Mp3 + common.RegisterDecoder("qmc2", NewDefaultDecoder) //QQ Music Ogg + common.RegisterDecoder("qmc0", NewDefaultDecoder) //QQ Music Mp3 + common.RegisterDecoder("qmcflac", NewDefaultDecoder) //QQ Music Flac + common.RegisterDecoder("qmcogg", NewDefaultDecoder) //QQ Music Ogg + common.RegisterDecoder("tkm", NewDefaultDecoder) //QQ Music Accompaniment M4a + + common.RegisterDecoder("bkcmp3", NewDefaultDecoder) //Moo Music Mp3 + common.RegisterDecoder("bkcflac", NewDefaultDecoder) //Moo Music Flac + + common.RegisterDecoder("666c6163", NewDefaultDecoder) //QQ Music Weiyun Flac + common.RegisterDecoder("6d7033", NewDefaultDecoder) //QQ Music Weiyun Mp3 + common.RegisterDecoder("6f6767", NewDefaultDecoder) //QQ Music Weiyun Ogg + common.RegisterDecoder("6d3461", NewDefaultDecoder) //QQ Music Weiyun M4a + common.RegisterDecoder("776176", NewDefaultDecoder) //QQ Music Weiyun Wav + + common.RegisterDecoder("mgg", NewMgg256Decoder) //QQ Music Weiyun Wav + common.RegisterDecoder("mflac", NewMflac256Decoder) //QQ Music Weiyun Wav + +} diff --git a/algo/tm/tm.go b/algo/tm/tm.go index 063882b..91ba0c6 100644 --- a/algo/tm/tm.go +++ b/algo/tm/tm.go @@ -32,10 +32,6 @@ func (d *Decoder) GetMeta() common.Meta { return nil } -func NewDecoder(data []byte) *Decoder { - return &Decoder{file: data} -} - func (d *Decoder) Validate() error { if len(d.file) < 8 { return errors.New("invalid file size") @@ -57,3 +53,17 @@ func (d *Decoder) Decode() error { } return nil } + +func NewDecoder(data []byte) common.Decoder { + return &Decoder{file: data} +} + +func init() { + // QQ Music IOS M4a + common.RegisterDecoder("tm2", NewDecoder) + common.RegisterDecoder("tm6", NewDecoder) + // QQ Music IOS Mp3 + common.RegisterDecoder("tm0", common.NewRawDecoder) + common.RegisterDecoder("tm3", common.NewRawDecoder) + +} diff --git a/algo/xm/xm.go b/algo/xm/xm.go index de92ae1..816a594 100644 --- a/algo/xm/xm.go +++ b/algo/xm/xm.go @@ -45,7 +45,7 @@ func (d *Decoder) GetMeta() common.Meta { return nil } -func NewDecoder(data []byte) *Decoder { +func NewDecoder(data []byte) common.Decoder { return &Decoder{file: data} } @@ -84,3 +84,13 @@ func (d *Decoder) Decode() error { } return nil } +func init() { + // Xiami Wav/M4a/Mp3/Flac + common.RegisterDecoder("xm", NewDecoder) + // Xiami Typed Format + // todo: Init With Type + common.RegisterDecoder("wav", NewDecoder) + common.RegisterDecoder("mp3", NewDecoder) + common.RegisterDecoder("flac", NewDecoder) + common.RegisterDecoder("m4a", NewDecoder) +}