-
Notifications
You must be signed in to change notification settings - Fork 3
/
handler.go
91 lines (79 loc) · 2.26 KB
/
handler.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"encoding/json"
"image"
"math"
"net/http"
"github.com/wwgberlin/go-funk/audio"
"github.com/wwgberlin/go-funk/renderer"
"github.com/wwgberlin/go-funk/sampler"
)
func handler(data audio.WavData, defaults *RequestData, rdr renderer.RenderFunc) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
reqData, err := parseRequest(r, defaults)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
compressed, err := sampler.Compress(data.Samples, reqData.Conf.Count, reqData.Sampler)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
rdr(w, renderer.Project(compressed, reqData.Conf.Height), data.Duration, reqData.Conf)
}
}
func samplesHandler(data audio.WavData) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
enc := json.NewEncoder(w)
enc.Encode(data.Samples)
}
}
func gifHandler(data audio.WavData) func(w http.ResponseWriter, req *http.Request) {
defaults := RequestData{
Sampler: sampler.AbsAvg,
Conf: renderer.Config{
Width: 50,
Height: 100,
Count: 3000,
Colorer: renderer.Colors["black"],
},
}
return handler(data, &defaults, renderer.MakeGif)
}
func gif2Handler(data audio.WavData) func(w http.ResponseWriter, req *http.Request) {
defaults := RequestData{
Sampler: sampler.AbsAvg,
Conf: renderer.Config{
Width: 50,
Height: 100,
Count: 3000,
Colorer: renderer.Colors["black"],
},
}
return handler(data, &defaults, renderer.MakeGif2)
}
func gopherHandler(data audio.WavData, img image.Image) func(w http.ResponseWriter, req *http.Request) {
defaults := RequestData{
Sampler: sampler.AbsAvg,
Conf: renderer.Config{
Width: 100,
Height: 100,
Count: 3000,
Colorer: renderer.Colorer{ColorGopherFunc(img), ColorGopherPalette},
},
}
return handler(data, &defaults, renderer.Gopher)
}
func waveformHandler(data audio.WavData) func(w http.ResponseWriter, r *http.Request) {
defaults := RequestData{
Sampler: sampler.AbsAvg,
Conf: renderer.Config{
Width: 1200,
Count: 1200,
Height: math.MaxUint8,
Colorer: renderer.Colors["black"],
},
}
return handler(data, &defaults, renderer.DrawWaveform)
}