-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
pianoroll.go
53 lines (46 loc) · 1.32 KB
/
pianoroll.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package server
import (
"fmt"
"net/http"
"time"
"github.com/emicklei/melrose/core"
"github.com/emicklei/melrose/midi"
"github.com/emicklei/melrose/notify"
"github.com/emicklei/melrose/ui/img"
"github.com/fogleman/gg"
)
// pianorollImageHandler returns PNG content given a variable
func (l *LanguageServer) pianorollImageHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
notify.Console.Warnf("HTTP method not allowed:%s", r.Method)
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
varname := r.URL.Query().Get("var")
if varname == "" {
fmt.Fprintf(w, "var parameter is empty")
w.WriteHeader(http.StatusBadRequest)
return
}
obj, ok := l.context.Variables().Get(varname)
if !ok {
fmt.Fprintf(w, "no object found for var:%s", varname)
w.WriteHeader(http.StatusBadRequest)
return
}
seq, ok := obj.(core.Sequenceable)
if !ok {
fmt.Fprintf(w, "no object is not sequenceable:%s", varname)
w.WriteHeader(http.StatusBadRequest)
return
}
tim := core.NewTimeline()
d := midi.NewOutputDevice(0, nil, 0, tim)
d.Play(core.NoCondition, seq, l.context.Control().BPM(), time.Now())
gc := gg.NewContext(500, 50)
evts := tim.NoteEvents()
nv := img.NotesView{Events: evts, BPM: l.context.Control().BPM()}
nv.DrawOn(gc)
w.Header().Set("content-type", "image/png")
gc.EncodePNG(w)
}