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.
60 lines
1.0 KiB
Go
60 lines
1.0 KiB
Go
4 years ago
|
package tm
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"errors"
|
||
|
"github.com/umlock-music/cli/algo/common"
|
||
|
)
|
||
|
|
||
4 years ago
|
var replaceHeader = []byte{0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70}
|
||
|
var magicHeader = []byte{0x51, 0x51, 0x4D, 0x55} //0x15, 0x1D, 0x1A, 0x21
|
||
4 years ago
|
|
||
|
type Decoder struct {
|
||
4 years ago
|
file []byte
|
||
|
audio []byte
|
||
|
headerMatch bool
|
||
|
audioExt string
|
||
4 years ago
|
}
|
||
|
|
||
|
func (d *Decoder) GetCoverImage() []byte {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (d *Decoder) GetAudioData() []byte {
|
||
4 years ago
|
return d.audio
|
||
4 years ago
|
}
|
||
|
|
||
|
func (d *Decoder) GetAudioExt() string {
|
||
4 years ago
|
return d.audioExt
|
||
4 years ago
|
}
|
||
|
|
||
|
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")
|
||
|
}
|
||
4 years ago
|
if !bytes.Equal(magicHeader, d.file[:4]) {
|
||
4 years ago
|
return errors.New("not a valid tm file")
|
||
|
}
|
||
4 years ago
|
d.headerMatch = true
|
||
4 years ago
|
return nil
|
||
|
}
|
||
|
|
||
|
func (d *Decoder) Decode() error {
|
||
4 years ago
|
d.audio = d.file
|
||
|
if d.headerMatch {
|
||
|
for i := 0; i < 8; i++ {
|
||
|
d.audio[i] = replaceHeader[i]
|
||
|
}
|
||
|
d.audioExt = "m4a"
|
||
|
}
|
||
4 years ago
|
return nil
|
||
|
}
|