You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
unlock-music-cli/algo/xm/xm.go

110 lines
2.3 KiB
Go

4 years ago
package xm
import (
"bytes"
"errors"
"github.com/unlock-music/cli/algo/common"
"github.com/unlock-music/cli/internal/logging"
4 years ago
"go.uber.org/zap"
)
var (
magicHeader = []byte{'i', 'f', 'm', 't'}
magicHeader2 = []byte{0xfe, 0xfe, 0xfe, 0xfe}
typeMapping = map[string]string{
4 years ago
" WAV": "wav",
"FLAC": "flac",
" MP3": "mp3",
" A4M": "m4a",
}
ErrFileSize = errors.New("xm invalid file size")
ErrMagicHeader = errors.New("xm magic header not matched")
)
4 years ago
type Decoder struct {
file []byte
4 years ago
headerLen uint32
outputExt string
mask byte
audio []byte
}
func (d *Decoder) GetCoverImage() []byte {
return nil
}
func (d *Decoder) GetAudioData() []byte {
return d.audio
}
func (d *Decoder) GetAudioExt() string {
if d.outputExt != "" {
return "." + d.outputExt
}
return ""
4 years ago
}
func (d *Decoder) GetMeta() common.Meta {
return nil
}
func NewDecoder(data []byte) common.Decoder {
return &Decoder{file: data}
4 years ago
}
func (d *Decoder) Validate() error {
lenData := len(d.file)
4 years ago
if lenData < 16 {
return ErrFileSize
4 years ago
}
if !bytes.Equal(magicHeader, d.file[:4]) ||
!bytes.Equal(magicHeader2, d.file[8:12]) {
return ErrMagicHeader
4 years ago
}
var ok bool
d.outputExt, ok = typeMapping[string(d.file[4:8])]
4 years ago
if !ok {
return errors.New("detect unknown xm file type: " + string(d.file[4:8]))
4 years ago
}
if d.file[14] != 0 {
logging.Log().Warn("not a simple xm file", zap.Uint8("b[14]", d.file[14]))
4 years ago
}
d.headerLen = uint32(d.file[12]) | uint32(d.file[13])<<8 | uint32(d.file[14])<<16 // LittleEndian Unit24
4 years ago
if d.headerLen+16 > uint32(lenData) {
return ErrFileSize
4 years ago
}
4 years ago
return nil
}
func (d *Decoder) Decode() error {
d.mask = d.file[15]
d.audio = d.file[16:]
4 years ago
dataLen := uint32(len(d.audio))
for i := d.headerLen; i < dataLen; i++ {
d.audio[i] = ^(d.audio[i] - d.mask)
}
return nil
}
func DecoderFuncWithExt(ext string) common.NewDecoderFunc {
return func(file []byte) common.Decoder {
return &Decoder{file: file, outputExt: ext}
}
}
func init() {
// Xiami Wav/M4a/Mp3/Flac
common.RegisterDecoder("xm", false, NewDecoder)
// Xiami Typed Format
common.RegisterDecoder("wav", false, DecoderFuncWithExt("wav"))
common.RegisterDecoder("mp3", false, DecoderFuncWithExt("mp3"))
common.RegisterDecoder("flac", false, DecoderFuncWithExt("flac"))
common.RegisterDecoder("m4a", false, DecoderFuncWithExt("m4a"))
}