-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmusic.go
More file actions
782 lines (722 loc) · 16.2 KB
/
Copy pathmusic.go
File metadata and controls
782 lines (722 loc) · 16.2 KB
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
package beep
import (
"bufio"
"bytes"
"fmt"
"os"
"strings"
)
// BeepNotation description
const BeepNotation = `
Beep notation:
| | | | | | | | | | | | | | | | | | | | | |
|2|3| |5|6|7| |9|0| |=|a|s| |f|g| |j|k|l| |
| | | | | | | | | | | | | | | | | | | | | |
|q|w|e|r|t|y|u|i|o|p|[|]|z|x|c|v|b|n|m|,|.|
q - middle C (261.63 hertz)
Left and right hand keys are same. Uppercase
letters are control keys. Lowercase letters
are music notes. Space bar is current duration
rest. Spaces after first space are ignored.
Lines start with '#' are ignored.
Control keys:
Rest:
RW - whole rest
RH - half rest
RQ - quarter rest
RE - eighth rest
RS - sixteenth rest
RT - thirty-second rest
RI - sixty-fourth rest
Durations:
DW - whole note
DH - half note
DQ - quarter note
DE - eighth note
DS - sixteenth note
DT - thirty-second note
DI - sixty-fourth note
DD - dotted note (adds half duration)
Octave:
H0 - octave 0 keys
HL - octave 1, 2, 3 (left hand keys)
HR - octave 4, 5, 6 (right hand keys)
H7 - octave 7, 8 keys
Tempo:
T# - where # is 0-9, default is 4 (1 unit speeds up/down by 4%)
Sustain:
SA# - attack level, where # is 0-9, default is 8
SD# - decay level, 0-9, default 4
SS# - sustain level, 0-9, default 4
SR# - release level, 0-9, default 9
Voice:
VD - Computer generated default voice
VP - Piano voice
VV - Violin voice
VN - If a line ends with 'VN', the next line will be
played harmony with the line.
Chord:
C# - Play next # notes as a chord, where # is 2-9.
For example C major chord is "C3qet"
Amplitude:
A# - Changes current amplitude, where # is 1-9, default is 9
Measures:
| - bar, ignored
' ' - space, ignored
Tab - tab, ignored
Comments:
# - a line comment
## - start or end of a block comment
Demo music: Mozart K33b:`
const (
// SampleAmp16bit - 16-bit sample amplitude
SampleAmp16bit = 32767.0
// SampleRate - sample rate
SampleRate = 44100
// SampleRate64 - float64 sample rate
SampleRate64 = float64(SampleRate)
bitsPerSample = 16
sample16bit = bitsPerSample == 16
sampleAmp8bit = 127.0
quarterNote = 1024 * 22
wholeNote = quarterNote * 4
halfNote = wholeNote / 2
)
var (
// DemoMusic notation
DemoMusic = BuiltinMusic[0].Notation
// PrintSheet enables printing beep notation while playing music
PrintSheet bool
// PrintNotes enables printing each notes while playing music
PrintNotes bool
wholeRest = make([]int16, wholeNote)
)
// Music player
type Music struct {
playing bool
stopping bool
quietMode bool
played chan bool // for syncing player
stopped chan bool
linePlayed chan bool // for syncing lines
piano *Piano
violin *Violin
output string // output file name
}
// Note data
type Note struct {
key rune
duration rune
dotted bool
volume int
amplitude int
tempo int
buf []int16
velocity int
samples int
}
// Sustain params
type Sustain struct {
attack int
decay int
sustain int
release int
buf []int16
}
// Chord params
type Chord struct {
number int
count int
buf []int16
}
// Voice interface
// GetNote: Gets a whole note for the key
// SustainNote: Used for sustaining computer generated voice
// Sustain: Indicates whether the instrument sustain note
// NaturalVoice: Indicates whether natural voice file is loaded
// ComputerVoice: Enable or disable computer voice
type Voice interface {
GetNote(note *Note, sustain *Sustain) bool
SustainNote(note *Note, sustain *Sustain)
Sustain() bool
NaturalVoice() bool
NaturalVoiceFound() bool
ComputerVoice(enable bool)
}
// NewMusic returns new music for output file name, or stdout if output is empty
func NewMusic(output string) *Music {
music := &Music{
played: make(chan bool),
stopped: make(chan bool),
linePlayed: make(chan bool),
output: output,
}
return music
}
// Wait until sheet is played
func (m *Music) Wait() {
<-m.played // wait until player is done
}
// WaitLine waits until line is played
func (m *Music) WaitLine() {
<-m.linePlayed
}
// Ratio returns sustain ratio
func (s *Sustain) Ratio() float64 {
return float64(s.sustain) / 10.0
}
// Reset chord
func (c *Chord) Reset() {
c.number = 0
c.count = 0
c.buf = nil
}
// Play music score from reader
func (m *Music) Play(reader *bufio.Reader, volume100 int) {
m.playing = true
defer func() {
if m.stopping {
m.stopped <- true
m.stopping = false
}
m.played <- true
m.playing = false
}()
volume := int(SampleAmp16bit * (float64(volume100) / 100.0))
outputFileName := m.output
if m.piano == nil {
m.piano = NewPiano()
}
var outputFile *os.File
var err error
// output file
if len(outputFileName) > 0 {
if outputFileName == "-" {
outputFile = os.Stdout
m.quietMode = true
} else {
opt := os.O_WRONLY | os.O_TRUNC | os.O_CREATE
outputFile, err = os.OpenFile(outputFileName, opt, 0644)
if err != nil {
fmt.Fprintln(os.Stderr, "Error opening output file:", err)
os.Exit(1)
}
}
defer outputFile.Close()
}
if m.quietMode {
PrintSheet = false
PrintNotes = false
}
// sustain state
sustain := &Sustain{
attack: 8,
decay: 4,
sustain: 4,
release: 9,
buf: make([]int16, quarterNote),
}
// read lines
chord := &Chord{}
bufWaveLimit := 1024 * 1024 * 100
controlKeys := "RDHTSAVC"
measures := "WHQESTI"
hands := "0LR7"
zeroToNine := "0123456789"
tempos := zeroToNine
amplitudes := zeroToNine
chordNumbers := zeroToNine
ignoredKeys := "\t |"
sustainTypes := "ADSR"
sustainLevels := zeroToNine
voiceControls := "DPVN"
var (
bufOutput []int16
duration = 'Q' // default note duration
dotted bool
rest rune
ctrl rune
voice Voice = m.piano // default voice is piano
sustainType rune
hand = 'R' // default is middle C octave
handLevel rune
count int // line counter
tempo = 4 // normal speed
amplitude = 9 // max volume
mixNextLine bool
bufMix []int16
lineMix string
waitNext bool
blockComment bool
)
for {
line, done := nextMusicLine(reader)
if done {
break
}
if strings.HasPrefix(line, "#") {
if strings.HasPrefix(line, "##") {
// ignore block comment
if blockComment {
blockComment = false
} else {
blockComment = true
}
} else {
// ignore comments
if PrintSheet {
fmt.Println(line)
}
}
continue
}
if blockComment {
continue
}
if strings.HasSuffix(line, "VN") {
// include next line to mixer
mixNextLine = true
} else {
mixNextLine = false
}
var bufWave []int16
for _, key := range line {
keystr := string(key)
if strings.ContainsAny(keystr, ignoredKeys) {
continue
}
if ctrl == 0 && strings.ContainsAny(keystr, controlKeys) {
ctrl = key
continue
}
if ctrl > 0 {
switch ctrl {
case 'D': // duration
if strings.ContainsAny(keystr, measures) {
duration = key
}
if key == 'D' {
dotted = true
}
case 'R': // reset
if strings.ContainsAny(keystr, measures) {
rest = key
}
case 'H': // hand
if strings.ContainsAny(keystr, hands) {
hand = key
}
case 'T': // tempo
if strings.ContainsAny(keystr, tempos) {
tempo = strings.Index(tempos, keystr)
}
case 'S': // sustain
if strings.ContainsAny(keystr, sustainTypes) {
sustainType = key
continue
}
if strings.ContainsAny(keystr, sustainLevels) {
level := strings.Index(sustainLevels, keystr)
switch sustainType {
case 'A':
sustain.attack = level
case 'D':
sustain.decay = level
case 'S':
sustain.sustain = level
case 'R':
sustain.release = level
}
}
case 'A': // amplitude
if strings.ContainsAny(keystr, amplitudes) {
amplitude = strings.Index(amplitudes, keystr)
}
case 'V': // voice
if strings.ContainsAny(keystr, voiceControls) {
switch key {
case 'D': // default voice
voice.ComputerVoice(true)
case 'P':
voice = m.piano
if voice.NaturalVoiceFound() {
voice.ComputerVoice(false)
}
case 'V':
if m.violin == nil {
m.violin = NewViolin()
}
voice = m.violin
voice.ComputerVoice(false)
}
}
case 'C': // chord
if strings.ContainsAny(keystr, chordNumbers) {
chord.count = 0
chord.number = strings.Index(chordNumbers, keystr)
}
}
if rest > 0 {
bufRest := restNote(rest, dotted, tempo)
if bufRest != nil {
if voice.NaturalVoice() {
releaseNote(sustain.buf, 0, sustain.Ratio())
mixSoundWave(bufRest, sustain.buf)
clearBuffer(sustain.buf)
}
bufWave = append(bufWave, bufRest...)
}
rest = 0
}
ctrl = 0
continue
}
switch hand {
case '0': // octave 0
handLevel = 1000
case 'L': // octave 1, 2, 3
handLevel = 2000
case 'R': // octave 4, 5, 6
handLevel = 3000
case '7', '8': // octave 7, 8
handLevel = 4000
}
note := &Note{
key: handLevel + key,
volume: volume,
amplitude: amplitude,
duration: duration,
dotted: dotted,
tempo: tempo,
samples: 0,
}
note.measure()
if voice.GetNote(note, sustain) {
dotted = false
if chord.number > 0 {
// playing a chord
chord.count++
if chord.buf == nil {
chord.buf = make([]int16, len(note.buf))
copy(chord.buf, note.buf)
if voice.NaturalVoice() {
//copyBuffer(sustain.buf, chord.buf)
}
} else {
mixSoundWave(chord.buf, note.buf)
if voice.NaturalVoice() {
//mixSoundWave(sustain.buf, note.buf)
}
}
if chord.count == chord.number {
if voice.NaturalVoice() {
release := len(note.buf) / 10 * sustain.sustain
ratio := sustain.Ratio()
releaseNote(sustain.buf, release, ratio)
}
note.buf = chord.buf
chord.Reset()
} else {
if PrintNotes {
fmt.Printf("%v-", m.piano.keyNoteMap[note.key])
}
continue
}
}
voice.SustainNote(note, sustain)
bufWave = append(bufWave, note.buf...)
if len(bufWave) > bufWaveLimit {
fmt.Fprintln(os.Stderr, "Line wave buffer exceeds 100MB limit.")
os.Exit(1)
}
if PrintNotes {
fmt.Printf("%v ", m.piano.keyNoteMap[note.key])
}
if m.stopping {
break
}
} else {
voiceName := strings.Split(fmt.Sprintf("%T", voice), ".")[1]
noteName := m.piano.keyNoteMap[note.key]
fmt.Printf("%s: Invalid note: %s (%s)\n", voiceName, keystr, noteName)
}
}
if mixNextLine {
if bufMix == nil {
bufMix = make([]int16, len(bufWave))
copy(bufMix, bufWave)
lineMix = line
} else {
lineMix += "\n" + line
mixSoundWave(bufMix, bufWave)
}
count++
clearBuffer(sustain.buf)
continue
}
if bufMix != nil {
mixSoundWave(bufMix, bufWave)
bufWave = bufMix
bufMix = nil
line = lineMix + "\n" + line
}
if PrintNotes {
fmt.Println()
}
if outputFile == nil {
if len(bufWave) > 0 {
if waitNext {
m.WaitLine() // wait until previous line is done playing
}
if m.stopping {
break
}
// prepare next line while playing
go m.Playback(bufWave, bufWave)
if PrintSheet {
fmt.Println(line)
}
waitNext = true
} else if PrintSheet {
fmt.Println(line)
}
} else {
// saving to file
buf := make([]int16, 2*len(bufWave))
for i, bar := range bufWave {
buf[i*2] = bar
buf[i*2+1] = bar
}
bufOutput = append(bufOutput, buf...)
if PrintSheet {
fmt.Println(line)
}
}
clearBuffer(sustain.buf)
count++
if m.stopping {
break
}
}
if waitNext && !m.stopping {
m.WaitLine()
}
if outputFile != nil {
// save wave to file
buflen := len(bufOutput)
header := NewWaveHeader(2, SampleRate, 16, buflen*2)
_, err = header.WriteHeader(outputFile)
if err != nil {
fmt.Fprintln(os.Stderr, "Error writing to output file:", err)
os.Exit(1)
}
buf := int16ToByteBuf(bufOutput)
_, err := outputFile.Write(buf)
if err != nil {
fmt.Fprintln(os.Stderr, "Error writing to output file:", err)
os.Exit(1)
}
if outputFileName != "-" {
fmt.Printf("wrote %s bytes to '%s'\n", numberComma(int64(len(buf))), outputFileName)
}
}
}
// measure sets the number of samples for the node
func (n *Note) measure() {
var samples int
length := wholeNote + (wholeNote / 100 * 4 * (4 - n.tempo)) // 4% per tempo unit
switch n.duration {
case 'W':
samples = length
case 'H':
samples = length / 2
case 'Q':
samples = length / 4
case 'E':
samples = length / 8
case 'S':
samples = length / 16
case 'T':
samples = length / 32
case 'I':
samples = length / 64
}
if samples > 0 {
// Apply dot measure
if n.dotted {
samples += samples / 2
}
}
n.samples = samples
}
// Reads next line from music sheet
func nextMusicLine(reader *bufio.Reader) (string, bool) {
var buf bytes.Buffer
limit := 1024 * 100
for {
part, isPrefix, err := reader.ReadLine()
if err != nil {
return "", true
}
buf.Write(part)
if buf.Len() > limit {
fmt.Println("Line exceeds 100KB limit.")
os.Exit(1)
}
if !isPrefix {
break
}
}
line := buf.String()
line = strings.Trim(line, " \t")
return line, false
}
// Changes note amplitude
func applyNoteVolume(buf []int16, volume, amplitude int) {
volume64 := float64(volume)
amplitude64 := float64(amplitude)
for i, bar := range buf {
bar64 := float64(bar)
bar64 *= (volume64 / SampleAmp16bit)
if amplitude64 > 0 {
bar64 *= (amplitude64 / 9.0)
}
buf[i] = int16(bar64)
}
}
// Mixes two waveform
func mixSoundWave(buf1, buf2 []int16) {
buflen2 := len(buf2)
gap := SampleAmp16bit - 500.0
for i := range buf1 {
if i == buflen2 {
break
}
bar1 := float64(buf1[i])
bar2 := float64(buf2[i])
bar64 := (bar1 - bar2) / 2 * 1.6
if bar64 > gap {
bar64 = gap
} else if bar64 <= -gap {
bar64 = -gap
}
buf1[i] = int16(bar64)
}
}
func copyBuffer(target, src []int16) {
bufsize := len(src)
for i := range target {
if i < bufsize {
target[i] = src[i]
} else {
target[i] = 0
}
}
}
func clearBuffer(buf []int16) {
for i := range buf {
buf[i] = 0
}
}
// Removes sharp edge at the end of waveform
func trimWave(buf []int16) {
if len(buf) == 0 {
return
}
bufsize := len(buf)
cut := bufsize - 1
var last int16
for i := range buf {
if i == 0 {
last = buf[cut]
}
if buf[cut] < last {
// falling
if buf[cut] < 0 && buf[cut] < 32 {
break
}
}
last = buf[cut]
if i > 1024 {
// too long
cut = len(buf) - 1
break
}
cut--
if cut == 0 {
// volume must be low
cut = len(buf) - 1
break
}
}
for i := cut; i < bufsize; i++ {
buf[i] = 0
}
}
func releaseNote(buf []int16, duration int, ratio float64) {
// | release
// |----|
// | \ buflen
// |----|--|----|
// | duration > 0
// ratio
buflen := len(buf)
if duration > 0 && duration < buflen {
buflen = duration
}
release := int(float64(buflen) * ratio)
decay := float64(buflen - release)
tick := SampleAmp16bit / decay
volume := SampleAmp16bit
for i, bar := range buf {
bar64 := float64(bar)
if i >= release && volume > 0 {
bar64 = bar64 * (volume / SampleAmp16bit)
buf[i] = int16(bar64)
volume -= tick
}
if volume <= 0 {
buf[i] = 0
}
}
}
func raiseNote(buf []int16, ratio float64) {
buflen := len(buf)
raise := float64(buflen) * ratio
tick := SampleAmp16bit / raise
volume := 0.0
for i, bar := range buf {
bar64 := float64(bar)
bar64 = bar64 * (volume / SampleAmp16bit)
buf[i] = int16(bar64)
volume += tick
if SampleAmp16bit <= volume {
break
}
}
}
// Returns rest note buffer with tempo
func restNote(rest rune, dotted bool, tempo int) []int16 {
var samples int
length := wholeNote + (wholeNote / 100 * 4 * (4 - tempo)) // 4% per tempo unit
switch rest {
case 'W':
samples = length
case 'H':
samples = length / 2
case 'Q':
samples = length / 4
case 'E':
samples = length / 8
case 'S':
samples = length / 16
case 'T':
samples = length / 32
case 'I':
samples = length / 64
}
if dotted {
samples += samples / 2
}
return make([]int16, samples)
}