From 452a2f6f04e283dce18b9fab6e45a9ff1fd15d09 Mon Sep 17 00:00:00 2001 From: vsariola <5684185+vsariola@users.noreply.github.com> Date: Sun, 21 Mar 2021 10:22:47 +0200 Subject: [PATCH] feat(vm): reuse delaytimes if one has already been used before --- vm/bytepatch.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/vm/bytepatch.go b/vm/bytepatch.go index ab638fd..eeddf62 100644 --- a/vm/bytepatch.go +++ b/vm/bytepatch.go @@ -167,11 +167,28 @@ func Encode(patch sointu.Patch, featureSet FeatureSet) (*BytePatch, error) { continue // skip encoding delays without any delay lines } countTrack := count*2 - 1 + unit.Parameters["notetracking"] // 1 means no note tracking and 1 delay, 2 means notetracking with 1 delay, 3 means no note tracking and 2 delays etc. - delStart := len(c.DelayTimes) - for _, v := range unit.VarArgs { - c.DelayTimes = append(c.DelayTimes, uint16(v)) + matchIndex := -1 + for i := 0; i <= len(c.DelayTimes)-len(unit.VarArgs); i++ { + match := true + for j, v := range unit.VarArgs { + if uint16(v) != c.DelayTimes[i+j] { + match = false + break + } + } + if match { + matchIndex = i + break + } + } + if matchIndex > -1 { + values = append(values, byte(matchIndex), byte(countTrack)) + } else { + values = append(values, byte(len(c.DelayTimes)), byte(countTrack)) + for _, v := range unit.VarArgs { + c.DelayTimes = append(c.DelayTimes, uint16(v)) + } } - values = append(values, byte(delStart), byte(countTrack)) } c.Commands = append(c.Commands, byte(opcode+unit.Parameters["stereo"])) c.Values = append(c.Values, values...)