2020-12-25 20:54:59 +00:00
|
|
|
package tm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2022-11-18 23:25:43 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-11-18 23:25:40 +00:00
|
|
|
|
2021-03-02 10:16:19 +00:00
|
|
|
"github.com/unlock-music/cli/algo/common"
|
2020-12-25 20:54:59 +00:00
|
|
|
)
|
|
|
|
|
2020-12-26 07:03:34 +00:00
|
|
|
var replaceHeader = []byte{0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70}
|
|
|
|
var magicHeader = []byte{0x51, 0x51, 0x4D, 0x55} //0x15, 0x1D, 0x1A, 0x21
|
2020-12-25 20:54:59 +00:00
|
|
|
|
|
|
|
type Decoder struct {
|
2022-11-18 23:25:43 +00:00
|
|
|
raw io.ReadSeeker
|
|
|
|
offset int
|
|
|
|
audio io.Reader
|
2020-12-25 20:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Decoder) Validate() error {
|
2022-11-18 23:25:43 +00:00
|
|
|
header := make([]byte, 8)
|
|
|
|
if _, err := io.ReadFull(d.raw, header); err != nil {
|
|
|
|
return fmt.Errorf("tm read header: %w", err)
|
2020-12-25 20:54:59 +00:00
|
|
|
}
|
2022-11-18 23:25:43 +00:00
|
|
|
if !bytes.Equal(magicHeader, header[:len(magicHeader)]) {
|
|
|
|
return errors.New("tm: valid magic header")
|
2020-12-25 20:54:59 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 23:25:43 +00:00
|
|
|
d.audio = io.MultiReader(bytes.NewReader(replaceHeader), d.raw)
|
2020-12-25 20:54:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-12-26 07:47:10 +00:00
|
|
|
|
2022-11-18 23:25:43 +00:00
|
|
|
func (d *Decoder) Read(buf []byte) (int, error) {
|
|
|
|
return d.audio.Read(buf)
|
2020-12-26 07:47:10 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 23:25:43 +00:00
|
|
|
func NewTmDecoder(rd io.ReadSeeker) common.Decoder {
|
|
|
|
return &Decoder{raw: rd}
|
|
|
|
|
2020-12-26 08:07:48 +00:00
|
|
|
}
|
|
|
|
|
2020-12-26 07:47:10 +00:00
|
|
|
func init() {
|
|
|
|
// QQ Music IOS M4a
|
2022-11-18 23:25:43 +00:00
|
|
|
common.RegisterDecoder("tm2", false, NewTmDecoder)
|
|
|
|
common.RegisterDecoder("tm6", false, NewTmDecoder)
|
2020-12-26 07:47:10 +00:00
|
|
|
// QQ Music IOS Mp3
|
2021-11-11 15:43:20 +00:00
|
|
|
common.RegisterDecoder("tm0", false, common.NewRawDecoder)
|
|
|
|
common.RegisterDecoder("tm3", false, common.NewRawDecoder)
|
2020-12-26 07:47:10 +00:00
|
|
|
}
|