raw.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package common
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "unlock-music.dev/cli/internal/sniff"
  7. )
  8. type RawDecoder struct {
  9. rd io.ReadSeeker
  10. audioExt string
  11. }
  12. func NewRawDecoder(p *DecoderParams) Decoder {
  13. return &RawDecoder{rd: p.Reader}
  14. }
  15. func (d *RawDecoder) Validate() error {
  16. header := make([]byte, 16)
  17. if _, err := io.ReadFull(d.rd, header); err != nil {
  18. return fmt.Errorf("read file header failed: %v", err)
  19. }
  20. if _, err := d.rd.Seek(0, io.SeekStart); err != nil {
  21. return fmt.Errorf("seek file failed: %v", err)
  22. }
  23. var ok bool
  24. d.audioExt, ok = sniff.AudioExtension(header)
  25. if !ok {
  26. return errors.New("raw: sniff audio type failed")
  27. }
  28. return nil
  29. }
  30. func (d *RawDecoder) Read(p []byte) (n int, err error) {
  31. return d.rd.Read(p)
  32. }
  33. func init() {
  34. RegisterDecoder("mp3", true, NewRawDecoder)
  35. RegisterDecoder("flac", true, NewRawDecoder)
  36. RegisterDecoder("ogg", true, NewRawDecoder)
  37. RegisterDecoder("m4a", true, NewRawDecoder)
  38. RegisterDecoder("wav", true, NewRawDecoder)
  39. RegisterDecoder("wma", true, NewRawDecoder)
  40. RegisterDecoder("aac", true, NewRawDecoder)
  41. }