Skip to content

Commit

Permalink
Further XM packing aggressiveness fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Crawford committed Dec 29, 2020
1 parent 9537f20 commit 82ca633
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
23 changes: 14 additions & 9 deletions music/tracked/xm/instheader.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type SampleHeader struct {
RelativeNoteNumber int8
ReservedP17 uint8
Name [22]uint8
SampleData []int8
SampleData []uint8
}

// GetName returns a string representation of the data stored in the Name field
Expand Down Expand Up @@ -161,7 +161,9 @@ func readInstrumentHeaderPartial(r io.Reader) (*InstrumentHeader, error) {
if err := binary.Read(r, binary.LittleEndian, &ih.SamplesCount); err != nil {
return nil, err
}
sz += 2
if sz += 2; sz >= ih.Size {
return &ih, nil
}

if err := binary.Read(r, binary.LittleEndian, &ih.SampleHeaderSize); err != nil {
return nil, err
Expand Down Expand Up @@ -332,6 +334,10 @@ func readInstrumentHeader(r io.Reader) (*InstrumentHeader, error) {
return nil, err
}

if ih.Size < 29 {
panic("what")
}

for i := uint16(0); i < ih.SamplesCount; i++ {
s := SampleHeader{}

Expand Down Expand Up @@ -375,26 +381,25 @@ func readInstrumentHeader(r io.Reader) (*InstrumentHeader, error) {
return nil, err
}

s.SampleData = make([]int8, int(s.Length))
s.SampleData = make([]uint8, int(s.Length))

ih.Samples = append(ih.Samples, s)
}

for _, s := range ih.Samples {
data := s.SampleData
if err := binary.Read(r, binary.LittleEndian, &data); err != nil {
if err := binary.Read(r, binary.LittleEndian, &s.SampleData); err != nil {
return nil, err
}

// convert the sample in the background
go func(data []int8) {
go func(data []uint8) {
old := int8(0)
for i, s := range data {
new := s + old
data[i] = new
new := int8(s) + old
data[i] = uint8(new)
old = new
}
}(data)
}(s.SampleData)
}
return ih, nil
}
30 changes: 28 additions & 2 deletions music/tracked/xm/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,23 @@ type PatternFileFormat struct {
PackedData []byte
}

func readPatternHeader(r io.Reader, fileVersion uint16) (*PatternHeader, error) {
func readPatternHeaderPartial(r io.Reader, fileVersion uint16) (*PatternHeader, error) {
ph := PatternHeader{}

sz := uint32(0)
if err := binary.Read(r, binary.LittleEndian, &ph.PatternHeaderLength); err != nil {
return nil, err
}
if sz += 4; sz >= ph.PatternHeaderLength {
return &ph, nil
}

if err := binary.Read(r, binary.LittleEndian, &ph.PackingType); err != nil {
return nil, err
}
if sz++; sz >= ph.PatternHeaderLength {
return &ph, nil
}

if fileVersion == 0x0102 {
var rowCount uint8
Expand All @@ -132,15 +139,34 @@ func readPatternHeader(r io.Reader, fileVersion uint16) (*PatternHeader, error)
}

ph.NumRows = uint16(rowCount) + 1
if sz++; sz >= ph.PatternHeaderLength {
return &ph, nil
}

} else {
if err := binary.Read(r, binary.LittleEndian, &ph.NumRows); err != nil {
return nil, err
}
if sz += 2; sz >= ph.PatternHeaderLength {
return &ph, nil
}
}

if err := binary.Read(r, binary.LittleEndian, &ph.PackedPatternDataSize); err != nil {
return nil, err
}
if sz += 2; sz >= ph.PatternHeaderLength {
return &ph, nil
}

return &ph, nil
}

func readPatternHeader(r io.Reader, fileVersion uint16) (*PatternHeader, error) {
ph, err := readPatternHeaderPartial(r, fileVersion)
if err != nil {
return nil, err
}

//if ph.NumRows == 0 {
// ph.NumRows = 64
Expand All @@ -154,5 +180,5 @@ func readPatternHeader(r io.Reader, fileVersion uint16) (*PatternHeader, error)
return nil, errors.New("pattern row count out of range - possibly corrupt file")
}

return &ph, nil
return ph, nil
}

0 comments on commit 82ca633

Please sign in to comment.