-
Notifications
You must be signed in to change notification settings - Fork 7
/
web.go
701 lines (668 loc) · 16.6 KB
/
web.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
package beep
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"html/template"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"runtime/debug"
"strconv"
"strings"
)
// Web params
type Web struct {
music *Music
tmpl *template.Template
}
// StartWebServer starts beep web server
func StartWebServer(music *Music, address string) {
var err error
var ip string
if len(address) == 0 {
ip = "127.0.0.1"
}
parts := strings.Split(address, ":")
if len(parts[0]) > 0 {
ip = parts[0]
}
port := 4444
if len(parts) > 1 && len(parts[1]) > 0 {
port, err = strconv.Atoi(parts[1])
if err != nil {
fmt.Fprintln(os.Stderr, "Invalid port number:", parts[1])
os.Exit(1)
}
}
bind := fmt.Sprintf("%s:%d", ip, port)
if len(ip) == 0 {
fmt.Println("Listening on port", port)
} else {
fmt.Printf("Listening on http://%s/\n", bind)
}
web := NewWeb(music)
music.quietMode = true
err = http.ListenAndServe(bind, web)
if err != nil {
fmt.Fprintln(os.Stderr, "Unable to start web server:", err)
os.Exit(1)
}
}
// NewWeb returns new handler
func NewWeb(music *Music) *Web {
w := &Web{
music: music,
tmpl: template.Must(template.New("tmpl").Parse(webTemplates)),
}
return w
}
func (w *Web) ServeHTTP(res http.ResponseWriter, req *http.Request) {
defer func() {
if obj := recover(); obj != nil {
format := "\" ><pre>\nError: %s\nStack:%s\n</pre>"
fmt.Fprintf(res, format, obj, debug.Stack())
}
}()
path := req.URL.Path
if file, found := webFileMap[path]; found {
if strings.HasSuffix(path, ".css") {
res.Header().Add("Content-Type", "text/css")
}
if strings.HasSuffix(path, ".js") {
res.Header().Add("Content-Type", "text/javascript")
}
fmt.Fprint(res, file)
return
}
defer req.Body.Close()
switch path {
case "/":
w.serveHome(res, req)
case "/play":
w.servePlay(res, req)
case "/stop":
w.serveStop(res, req)
case "/search":
w.serveSearch(res, req)
case "/loadSheet":
w.serveLoadSheet(res, req)
case "/saveSheet":
w.serveSaveSheet(res, req)
case "/voices":
w.serveVoices(res, req)
case "/downloadVoice":
w.serveDownloadVoice(res, req)
case "/exportWave":
w.serveExportWave(res, req)
default:
w.execTemplate("header", nil, res)
w.execTemplate("pageNotFound", req.URL.Path, res)
}
}
// Serves home page
func (w *Web) serveHome(res http.ResponseWriter, req *http.Request) {
type homePage struct {
Demo string
}
data := &homePage{
Demo: DemoMusic,
}
w.execTemplate("header", nil, res)
w.execTemplate("/", data, res)
}
// Playback
func (w *Web) servePlay(res http.ResponseWriter, req *http.Request) {
if w.music.playing {
return
}
type playRequest struct {
Notation string
}
request := &playRequest{}
w.jsonRequest(request, req)
InitSoundDevice()
notation := bytes.NewBuffer([]byte(request.Notation))
reader := bufio.NewReader(notation)
go w.music.Play(reader, 100)
w.music.Wait()
}
// Stops playback
func (w *Web) serveStop(res http.ResponseWriter, req *http.Request) {
if w.music.stopping {
return
}
w.music.stopping = w.music.playing
go StopPlayBack()
if w.music.playing {
<-w.music.stopped
}
w.music.stopping = false
}
// Search sheet names
func (w *Web) serveSearch(res http.ResponseWriter, req *http.Request) {
type searchRequest struct {
Keyword string
}
request := &searchRequest{}
w.jsonRequest(request, req)
names := sheetSearch(request.Keyword)
type loadResponse struct {
Names []string
}
response := loadResponse{
Names: names,
}
w.jsonResponse(response, res)
}
// Loads a sheet
func (w *Web) serveLoadSheet(res http.ResponseWriter, req *http.Request) {
type loadSheetRequest struct {
Name string
}
request := &loadSheetRequest{}
w.jsonRequest(request, req)
type loadSheetResponse struct {
Name string
URL string
Notation string
}
sheet := &Sheet{
Name: filepath.Base(request.Name),
Dir: filepath.Dir(request.Name),
}
err := sheet.Load()
if err != nil {
panic(err)
}
response := loadSheetResponse{
Name: filepath.Join(sheet.Dir, sheet.Name),
URL: sheet.URL,
Notation: sheet.Notation,
}
w.jsonResponse(response, res)
}
// Saves a sheet
func (w *Web) serveSaveSheet(res http.ResponseWriter, req *http.Request) {
type saveSheetResponse struct {
Result string
}
var result = "Sheet has been saved."
defer func() {
response := &saveSheetResponse{
Result: result,
}
w.jsonResponse(response, res)
}()
type saveSheetRequest struct {
Name string
Notation string
}
request := &saveSheetRequest{}
w.jsonRequest(request, req)
name := filepath.Base(request.Name)
id := stringNumber(strings.Split(name, "-")[0])
if id > 0 && id <= 100 {
result = "Can't save builtin music sheets with the same name."
return
}
sheet := &Sheet{
Name: name,
Dir: filepath.Dir(request.Name),
Notation: request.Notation,
}
if len(request.Notation) == 0 && sheet.Exists() {
// delete sheet
err := sheet.Delete()
if err != nil {
result = fmt.Sprintf("Error: %v", err)
} else {
result = "Sheet has been deleted."
}
} else {
// save sheet
err := sheet.Save()
if err != nil {
result = fmt.Sprintf("Error: %v", err)
} else {
result = "Sheet has been saved to: " + sheet.Path()
}
}
}
// Serves voices page
func (w *Web) serveVoices(res http.ResponseWriter, req *http.Request) {
type homePage struct {
}
data := &homePage{}
w.execTemplate("header", nil, res)
w.execTemplate("/voices", data, res)
}
// Downloads a voice file
func (w *Web) serveDownloadVoice(res http.ResponseWriter, req *http.Request) {
type downloadRequest struct {
Name string
}
request := &downloadRequest{}
w.jsonRequest(request, req)
var names []string
if len(request.Name) > 0 {
names = append(names, request.Name)
}
DownloadVoiceFiles(w.music, res, names)
}
// Export to WAV file
func (w *Web) serveExportWave(res http.ResponseWriter, req *http.Request) {
defer func() {
w.music.output = ""
}()
type exportWaveRequest struct {
Output string
Notation string
}
request := &exportWaveRequest{}
w.jsonRequest(request, req)
notation := bytes.NewBuffer([]byte(request.Notation))
reader := bufio.NewReader(notation)
w.music.output = filepath.Join(HomeDir(), "export", request.Output)
os.MkdirAll(filepath.Dir(w.music.output), 0755)
go w.music.Play(reader, 100)
w.music.Wait()
type exportWaveResponse struct {
Result string
}
response := exportWaveResponse{
Result: "WAV file has been save to: " + w.music.output,
}
w.jsonResponse(response, res)
}
// Execute template with name and data
func (w *Web) execTemplate(name string, data interface{}, res http.ResponseWriter) {
if err := w.tmpl.ExecuteTemplate(res, name, data); err != nil {
panic(err)
}
}
// Reads JSON request
func (w *Web) jsonRequest(request interface{}, req *http.Request) {
requestBody, err := ioutil.ReadAll(req.Body)
if err != nil {
panic(err)
}
if err = json.Unmarshal(requestBody, request); err != nil {
panic(err)
}
}
// Writes JSON response
func (w *Web) jsonResponse(response interface{}, res http.ResponseWriter) {
jres, err := json.Marshal(response)
if err != nil {
panic(err)
}
_, err = res.Write(jres)
if err != nil {
panic(err)
}
}
// DownloadVoiceFiles downloads natural voice files
func DownloadVoiceFiles(music *Music, writer io.Writer, names []string) {
dir := filepath.Join(HomeDir(), "voices")
if len(names) == 0 {
names = []string{"piano", "violin"}
}
for _, name := range names {
if !strings.HasSuffix(name, ".zip") {
name += ".zip"
}
url := "http://bmrust.com/dl/beep/voices/" + name
fmt.Fprintf(writer, "Downloading '%s'", url)
// locate file
resp, err := http.Head(url)
if err != nil {
fmt.Fprintln(writer, " Error:", err)
continue
}
if resp.StatusCode != http.StatusOK {
fmt.Fprintln(writer, "Error locating file. Status:", resp.StatusCode)
continue
}
fmt.Fprintf(writer, " %s bytes ...\n", numberComma(resp.ContentLength))
// fetch file
resp, err = http.Get(url)
if err != nil {
fmt.Fprintln(writer, "Error downloading file:", err)
continue
}
if resp.StatusCode != http.StatusOK {
fmt.Fprintln(writer, "Error downloading. Status:", resp.StatusCode)
continue
}
defer resp.Body.Close()
// read file
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Fprintln(writer, "Error reading file:", err)
continue
}
// save file
os.MkdirAll(dir, 0755)
filename := filepath.Join(dir, name)
err = ioutil.WriteFile(filename, body, 0644)
if err != nil {
fmt.Fprintln(writer, "Error saving file:", err)
continue
}
fmt.Fprintf(writer, " Saving %s\n", filename)
}
// reload voices
music.piano = NewPiano()
music.violin = NewViolin()
}
// All HTML templates
var webTemplates = `{{define "header"}}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<script src='js/system.js'></script>
</head>
<body>
<div class="header" ondblclick="this.style.display='none'">beep</div>
<div class="menu">
<a class="menu" href="/">Home</a> |
<a class="menu" href="/voices">Voices</a>
</div>
{{end}}
{{define "/"}}
<script src='js/home.js'></script>
<div style='padding:10px'>
<b>Beep notation:</b> <span id='sheetName'></span>
<a id='sheetUrl' target='_blank' style='color:blue'></a><br>
<textarea id='notation' style='width:99%;height:450px;font-family:monospace;font-size:12px'
spellcheck='false'>{{.Demo}}</textarea>
<div style='padding-top:6px'>
<a id='newSheet' class='button' href='javascript:;'>New Sheet</a>
<a id='play' class='button' href='javascript:;'>Play</a>
<a id='stop' class='button' href='javascript:;'>Stop</a>
<a id='save' class='button' href='javascript:;'>Save</a>
<a id='load' class='button' href='javascript:;'>Load</a>
<a id='exportWave' class='button' href='javascript:;'>Export</a>
<input id='search' title='Search' style='width:100px;margin-left:5px'>
</div>
<div id='result' style='padding-top:10px'>
</div>
</div>
</body>
</html>
{{end}}
{{define "/voices"}}
<script src='js/voices.js'></script>
<div style='padding:10px'>
<div style='padding-bottom:10px'><b>Natural Voices:</b></div>
Piano <a href='javascript:;' class='link' onclick="downloadVoice('piano')">download</a> (13MB)<br>
Violin <a href='javascript:;' class='link' onclick="downloadVoice('violin')">download</a> (6.9MB)<br>
<div id='result' style='padding-top:10px'></div>
</div>
</body>
</html>
{{end}}
{{define "pageNotFound"}}
<div style='padding:10px'>
{{.}} - page not found
</div>
</body>
</html>
{{end}}
`
// Files served by the web server
var webFileMap = map[string]string{
"/css/style.css": `body {margin:0px;font-size:13px;font-family:arial,sans-serif}
div, td, span, input {font-weight:12px;font-family:arial,sans-serif}
div.header {padding:10px;text-shadow:1px 1px #000;font-size:16px;
font-weight:bold;background:#3456ab;color:white}
div.menu {padding:4px;padding-left:10px;text-shadow:1px 1px #333;font-size:13px;
font-weight:bold;background:#456abc;color:white}
a {text-decoration:none;color:black}
a:hover {color:blue}
a.menu {color:white;margin-right:4px}
a.menu:hover {color:yellow}
a.link {color:blue}
a.link:hover {color:red}
a.button {padding:4px 8px 4px 8px;text-shadow:0px 0px #333;font-size:13px;
font-weight:bold;background:#3456ab;color:white;border-radius:3px}
a.button:hover {color:yellow;box-shadow:1px 1px #999}
a.item {font-size:13px;color:white;}
a.item:hover {color:yellow}
div.item {display:inline-block;font-size:13px;padding:2px;border-bottom:1px dotted #333}
`,
"/js/system.js": `
function init() {
window.ids = {}
var all = document.getElementsByTagName('*')
for (i=0; i<all.length; i++) {
var id = all[i].id
if (id) {
ids[id] = all[i]
}
}
}
function Ajax() {
if (window.XMLHttpRequest) {
this.ajax = new XMLHttpRequest()
} else if (window.ActiveXObject) {
this.ajax = new ActiveXObject('Microsoft.XMLHTTP')
}
if (!this.ajax) {
alert('No Ajax support is available.')
return
}
this.send = function(url, data) {
var ajax = this
this.ajax.onreadystatechange = function() {
if (this.readyState != 4)
return
if (this.status != 200) {
alert('Request failed.\n\n'+
'Status Code:'+ this.status +'\n'+
'Status:'+ this.statusText +'\n'+
'Response:'+ this.responseText)
return
}
if (ajax.onready)
ajax.onready(this.responseText)
}
this.ajax.open('POST', url, true)
this.ajax.send(data)
}
this.jsonResp = function() {
var resp = this.ajax.responseText
if (resp.length == '')
return null
if (resp[0] != '{') {
alert(resp)
} else {
try {
eval('var jres = '+ resp)
return jres
} catch (e) {
alert('JSON Error:\n\n'+ e)
}
}
return null
}
}
`,
"/js/home.js": `
window.onload = function() {
init()
ids.newSheet.onclick = newSheet
ids.play.onclick = play
ids.stop.onclick = stop
ids.load.onclick = load
ids.save.onclick = saveSheet
ids.search.onkeypress = searchPress
ids.search.onfocus = searchFocus
ids.search.onblur = searchFocus
ids.exportWave.onclick = exportPath
ids.search.onfocus()
}
function newSheet() {
ids.sheetName.innerText = ''
ids.sheetUrl.href = ''
ids.sheetUrl.innerText = ''
ids.notation.value = ''
ids.result.innerHTML = ''
ids.notation.focus()
}
function play() {
if (ids.play.innerText != 'Play') {
return
}
var ajax = new Ajax
ajax.onready = function(data) {
reset()
}
var data = {
'Notation': ids.notation.value
}
ids.play.innerHTML = 'Play ▶'
ids.stop.innerHTML = 'Stop ◾'
ajax.send('/play', JSON.stringify(data))
}
function load() {
search('')
}
function searchFocus() {
if (this.value) {
this.style.color = ''
if (this.value == this.title)
this.value = ''
} else {
this.style.color = '#aaa'
this.value = this.title
}
}
function searchPress(e) {
e = e || event
if (e.keyCode == 13) {
search(this.value)
}
}
function search(keyword) {
ids.result.innerHTML = 'Searching ...'
var ajax = new Ajax
ajax.onready = function(data) {
var h = []
var jres = this.jsonResp()
if (jres.Names) {
var names = jres.Names
for (i=0; i<names.length; i++) {
h.push("<div class='item'>")
h.push("<a href='javascript:;' class='link' onclick='loadSheet(this.innerText)'>")
h.push(names[i])
h.push("</a></div><br>\n")
}
} else {
h.push('No matches found.')
}
setTimeout(function() {ids.result.innerHTML = h.join('')}, 200)
}
var data = {
'Keyword': keyword
}
ajax.send('/search', JSON.stringify(data))
}
function loadSheet(name) {
var ajax = new Ajax
ajax.onready = function(data) {
var jres = this.jsonResp()
ids.sheetName.innerHTML = jres.Name
ids.sheetUrl.href = jres.URL
ids.sheetUrl.innerText = jres.URL ? 'Music sheet' : ''
ids.notation.value = jres.Notation
document.body.scrollTop = 0
}
var data = {
'Name': name
}
ajax.send('/loadSheet', JSON.stringify(data))
}
function saveNewSheet(elem) {
ids.sheetName.innerText = elem.previousSibling.value
saveSheet()
}
function saveSheet() {
var name = ids.sheetName.innerText
if (!name) {
var h = []
h.push("File Path: <input style='width:350px;margin-right:5px' value='directory/filename.txt'>")
h.push("<a href='javascript:;' onclick='saveNewSheet(this)' class='button'>Save</a>")
ids.result.innerHTML = h.join('')
ids.result.childNodes[1].focus()
return
}
ids.result.innerHTML = 'Saving ...'
var ajax = new Ajax
ajax.onready = function(data) {
var jres = this.jsonResp()
setTimeout(function() {ids.result.innerHTML = jres.Result}, 200)
}
var data = {
'Name': ids.sheetName.innerText,
'Notation': ids.notation.value
}
ajax.send('/saveSheet', JSON.stringify(data))
}
function exportPath() {
var h = []
h.push("File Path: <input style='width:350px;margin-right:5px' value='output.wav'>")
h.push("<a href='javascript:;' onclick='exportWave(this)' class='button'>Export</a>")
ids.result.innerHTML = h.join('')
ids.result.childNodes[1].focus()
}
function exportWave(elem) {
var output = elem.previousSibling.value
if (!output) return
ids.result.innerHTML = 'Exporting ...'
var ajax = new Ajax
ajax.onready = function(data) {
var jres = this.jsonResp()
ids.result.innerHTML = jres.Result
}
var data = {
'Output': output,
'Notation': ids.notation.value
}
ajax.send('/exportWave', JSON.stringify(data))
}
function reset() {
ids.play.innerHTML = 'Play'
ids.stop.innerHTML = 'Stop'
}
function stop() {
var ajax = new Ajax
ajax.onready = function(data) {
if (data == 'stopped')
reset()
}
ajax.send('/stop', null)
}
`,
"/js/voices.js": `
window.onload = function() {
init()
}
function downloadVoice(name) {
var ajax = new Ajax
ajax.onready = function(data) {
ids.result.innerHTML = data.replace(/\n/g, '<br>')
}
var data = {
'Name': name
}
ids.result.innerHTML = 'Downloading ...'
ajax.send('/downloadVoice', JSON.stringify(data))
}
`,
}