refactor(compiler): split song encoding logic into smaller reusable functions

This commit is contained in:
vsariola 2021-01-04 13:57:01 +02:00
parent 5dd81430b7
commit d328192834

View File

@ -15,66 +15,107 @@ type EncodedSong struct {
Sequences [][]byte Sequences [][]byte
} }
// flattenPatterns returns the track sequences flattened into linear arrays. // fixPatternLength makes sure that every pattern is the same length. During
// Additionally, after first release (value 0), it replaces every release or // composing. Patterns shorter than the given length are padded with 1 / "hold";
// hold with -1, denoting "don't care if it's either release (0) or hold (1)". // patterns longer than the given length are cropped.
// As we reconstruct the pattern table, we may use any pattern that has either 0 func fixPatternLength(patterns [][]byte, fixedLength int) [][]int {
// or hold in place of don't cares. patternData := make([]int, len(patterns)*fixedLength)
func flattenPatterns(song *sointu.Song) [][]int { ret := make([][]int, len(patterns))
ret := make([][]int, 0, len(song.Tracks)) for i, pat := range patterns {
for _, t := range song.Tracks { for j, note := range pat {
flatSequence := make([]int, 0, song.TotalRows()) patternData[j] = int(note)
dontCare := false
for _, s := range t.Sequence {
for _, note := range t.Patterns[s] {
if !dontCare || note > song.Hold {
if note == song.Hold {
flatSequence = append(flatSequence, 1) // replace holds with 1s, we'll get rid of song.Hold soon and do the hold replacement at the last minute
} else {
flatSequence = append(flatSequence, int(note))
} }
dontCare = note == 0 // after 0 aka release, we don't care if further releases come along for j := len(pat); j < fixedLength; j++ {
} else { patternData[j] = 1 // pad with hold
flatSequence = append(flatSequence, -1)
} }
} ret[i], patternData = patternData[:fixedLength], patternData[fixedLength:]
}
ret = append(ret, flatSequence)
} }
return ret return ret
} }
// constructPatterns finds the smallest global pattern table for a given list of // flattenSequence looks up a sequence of patterns and concatenates them into a
// flattened patterns. If the patterns are not divisible with the patternLength, // single linear array of notes. Note that variable length patterns are
// then: a) if the last note of a track is release (0) or don't care (-1), the // concatenated as such; call fixPatternLength first if you want every pattern
// track is extended with don't cares (-1) until the total length of the song is // to be constant length.
// divisible with the patternLength. b) Otherwise, the track is extended with a func flattenSequence(patterns [][]int, sequence []int) []int {
// single release (0), followed by don't care about release & hold (-1). sumLen := 0
// for _, patIndex := range sequence {
// In otherwords: any playing notes are released when the original song ends. sumLen += len(patterns[patIndex])
func constructPatterns(tracks [][]int, patternLength int) ([][]byte, [][]byte, error) { }
patternTable := make([][]int, 0) notes := make([]int, sumLen)
sequences := make([][]byte, 0, len(tracks)) window := notes
for _, t := range tracks { for _, patIndex := range sequence {
var sequence []byte elementsCopied := copy(window, patterns[patIndex])
for s := 0; s < len(t); s += patternLength { window = window[elementsCopied:]
pat := t[s : s+patternLength] }
if len(pat) < patternLength { return notes
extension := make([]int, patternLength-len(pat)) }
for i := range extension {
if pat[len(pat)-1] > 0 && i == 0 { // markDontCares goes through a linear array of notes and marks every hold (1)
extension[i] = 0 // or release (0) after the first release (0) as -1 or "don't care". This means
// that for -1:s, we don't care if it's a hold or release; it does not affect
// the sound as the note has been already released.
func markDontCares(notes []int) []int {
notesWithDontCares := make([]int, len(notes))
dontCare := false
for i, n := range notes {
if dontCare && n <= 1 {
notesWithDontCares[i] = -1
} else { } else {
extension[i] = -1 notesWithDontCares[i] = n
dontCare = n == 0
} }
} }
pat = append(pat, extension...) return notesWithDontCares
}
// replaceInt replaces all occurrences of needle in the haystack with the value
// "with"
func replaceInts(haystack []int, needle int, with int) {
for i, v := range haystack {
if v == needle {
haystack[i] = with
} }
}
}
// splitSequence splits a linear sequence of notes into patternLength size
// chunks. If the last chunk is shorter than the patternLength, then it is
// padded with dontCares (-1).
func splitSequence(sequence []int, patternLength int) [][]int {
numChunksRoundedUp := (len(sequence) + patternLength - 1) / patternLength
chunks := make([][]int, numChunksRoundedUp)
for i := range chunks {
if len(sequence) >= patternLength {
chunks[i], sequence = sequence[:patternLength], sequence[patternLength:]
} else {
padded := make([]int, patternLength)
j := copy(padded, sequence)
for ; j < patternLength; j++ {
padded[j] = -1
}
chunks[i] = padded
}
}
return chunks
}
// addPatternsToTable adds given patterns to the table, checking if existing
// pattern could be used. DontCares are taken into account so a pattern that has
// don't care where another has a hold or release is ok. It returns a 1D
// sequence of indices of each added pattern in the updated pattern table & the
// updated pattern table.
func addPatternsToTable(patterns [][]int, table [][]int) ([]int, [][]int) {
updatedTable := make([][]int, len(table))
copy(updatedTable, table) // avoid updating the underlying slices for concurrency safety
sequence := make([]int, len(patterns))
for i, pat := range patterns {
// go through the current pattern table to see if there's already a // go through the current pattern table to see if there's already a
// pattern that could be used // pattern that could be used
patternIndex := -1 patternIndex := -1
for j, p := range patternTable { for j, p := range updatedTable {
match := true match := true
identical := true
for k, n := range p { for k, n := range p {
if (n > -1 && pat[k] > -1 && n != pat[k]) || if (n > -1 && pat[k] > -1 && n != pat[k]) ||
(n == -1 && pat[k] > 1) || (n == -1 && pat[k] > 1) ||
@ -82,45 +123,55 @@ func constructPatterns(tracks [][]int, patternLength int) ([][]byte, [][]byte, e
match = false match = false
break break
} }
if n != pat[i] {
identical = false
}
} }
if match { if match {
// if there was any don't cares in the pattern table where if !identical {
// the new pattern has non don't cares, copy them to the // the patterns were not identical; one of them had don't
// patterns that was already in the pattern table // cares where another had hold or release so we make a new
// copy with merged data, that essentially is a max of the
// two patterns
mergedPat := make([]int, len(p))
copy(mergedPat, p) // make a copy instead of updating existing, for concurrency safety
for k, n := range pat { for k, n := range pat {
if n != -1 { if n != -1 {
patternTable[j][k] = n mergedPat[k] = n
} }
} }
updatedTable[j] = mergedPat
}
patternIndex = j patternIndex = j
break break
} }
} }
if patternIndex == -1 { if patternIndex == -1 {
patternIndex = len(patternTable) patternIndex = len(updatedTable)
patternTable = append(patternTable, pat) updatedTable = append(updatedTable, pat)
} }
if patternIndex > 255 { sequence[i] = patternIndex
return nil, nil, errors.New("encoding the song would result more than 256 different unique patterns")
} }
sequence = append(sequence, byte(patternIndex)) return sequence, updatedTable
}
func intsToBytes(array []int) ([]byte, error) {
ret := make([]byte, len(array))
for i, v := range array {
if v < 0 || v > 255 {
return nil, fmt.Errorf("when converting intsToBytes, all values should be 0 .. 255 (was: %v)", v)
} }
sequences = append(sequences, sequence) ret[i] = byte(v)
} }
// finally, if there are still some don't cares in the table, just replace them with zeros return ret, nil
byteTable := make([][]byte, 0, len(patternTable)) }
for _, pat := range patternTable {
bytePat := make([]byte, 0, patternLength) func bytesToInts(array []byte) []int {
for _, n := range pat { ret := make([]int, len(array))
if n >= 0 { for i, v := range array {
bytePat = append(bytePat, byte(n)) ret[i] = int(v)
} else {
bytePat = append(bytePat, 0)
} }
} return ret
byteTable = append(byteTable, bytePat)
}
return byteTable, sequences, nil
} }
func (e *EncodedSong) PatternLength() int { func (e *EncodedSong) PatternLength() int {
@ -136,11 +187,31 @@ func (e *EncodedSong) TotalRows() int {
} }
func EncodeSong(song *sointu.Song) (*EncodedSong, error) { func EncodeSong(song *sointu.Song) (*EncodedSong, error) {
// TODO: we could give the user the possibility to encode the patterns with a different length here also
patLength := song.PatternRows() patLength := song.PatternRows()
patterns, sequences, err := constructPatterns(flattenPatterns(song), patLength) sequences := make([][]byte, len(song.Tracks))
var patterns [][]int
for i, t := range song.Tracks {
fixed := fixPatternLength(t.Patterns, patLength)
flat := flattenSequence(fixed, bytesToInts(t.Sequence))
dontCares := markDontCares(flat)
// TODO: we could give the user the possibility to use another length during encoding that during composing
chunks := splitSequence(dontCares, patLength)
var sequence []int
sequence, patterns = addPatternsToTable(chunks, patterns)
var err error
sequences[i], err = intsToBytes(sequence)
if err != nil { if err != nil {
return nil, fmt.Errorf("error during constructPatterns: %v", err) return nil, errors.New("the constructed pattern table would result in > 256 unique patterns; only 256 unique patterns are supported")
} }
return &EncodedSong{Patterns: patterns, Sequences: sequences}, nil }
bytePatterns := make([][]byte, len(patterns))
for i, pat := range patterns {
var err error
replaceInts(pat, -1, 0) // replace don't cares with releases
bytePatterns[i], err = intsToBytes(pat)
if err != nil {
return nil, fmt.Errorf("invalid note in pattern, notes should be 0 .. 255: %v", err)
}
}
return &EncodedSong{Patterns: bytePatterns, Sequences: sequences}, nil
} }