Add Decoder Registry

Add Raw Decoder
This commit is contained in:
Emmm Monster 2020-12-26 15:47:10 +08:00
parent 1aa5731225
commit d5031e1935
No known key found for this signature in database
GPG Key ID: C98279C83FB50DB9
8 changed files with 123 additions and 11 deletions

12
algo/common/dispatch.go Normal file
View File

@ -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]
}

41
algo/common/raw.go Normal file
View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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)
}