Skip to content

Commit

Permalink
cache lilypond results for faster loading
Browse files Browse the repository at this point in the history
  • Loading branch information
schollz committed Sep 29, 2020
1 parent f2a1bd4 commit 2bf955a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/gin-gonic/gin v1.6.3
github.com/kbinani/midi v0.0.0-20170127113123-ce7c305abe3b
github.com/kr/pretty v0.2.1
github.com/schollz/jsonstore v1.1.0
github.com/schollz/logger v1.2.0
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
github.com/stretchr/testify v1.6.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/schollz/jsonstore v1.1.0 h1:WZBDjgezFS34CHI+myb4s8GGpir3UMpy7vWoCeO0n6E=
github.com/schollz/jsonstore v1.1.0/go.mod h1:15c6+9guw8vDRyozGjN3FoILt0wpruJk9Pi66vjaZfg=
github.com/schollz/logger v1.2.0 h1:5WXfINRs3lEUTCZ7YXhj0uN+qukjizvITLm3Ca2m0Ho=
github.com/schollz/logger v1.2.0/go.mod h1:P6F4/dGMGcx8wh+kG1zrNEd4vnNpEBY/mwEMd/vn6AM=
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA=
Expand Down
25 changes: 25 additions & 0 deletions src/music/music.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"math"
"os"
"os/exec"
"path"
"runtime"
"strconv"
"strings"

"github.com/kbinani/midi"
"github.com/schollz/jsonstore"
log "github.com/schollz/logger"
)

Expand Down Expand Up @@ -201,6 +203,26 @@ func MidiToNote(midi int) Note {

// ChordToNotes converts chords to notes using lilypond
func ChordToNotes(c string) (notes []Note, err error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return
}
chordDir := path.Join(homeDir, ".cache", "miti")
chordFile := path.Join(chordDir, "chords.json")
log.Tracef("chordFile: %s", chordFile)
ks, err := jsonstore.Open(chordFile)
if err != nil {
err = os.MkdirAll(chordDir, os.ModePerm)
if err != nil {
return
}
ks = new(jsonstore.JSONStore)
}
err = ks.Get(c, &notes)
if err == nil {
return
}

if _, ok := chordToNotesCache[c]; ok {
notes = chordToNotesCache[c]
return
Expand Down Expand Up @@ -272,6 +294,9 @@ func ChordToNotes(c string) (notes []Note, err error) {
}
if len(notes) == 0 {
err = fmt.Errorf("no notes")
} else {
ks.Set(c, &notes)
err = jsonstore.Save(ks, chordFile)
}
return
}
Expand Down

0 comments on commit 2bf955a

Please sign in to comment.