-
Notifications
You must be signed in to change notification settings - Fork 7
/
violin.go
331 lines (302 loc) · 8.21 KB
/
violin.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
package beep
import (
"archive/zip"
"fmt"
"io/ioutil"
"math"
"os"
"path/filepath"
"strings"
)
// Violin voice
type Violin struct {
naturalVoice bool
naturalVoiceFound bool
keyDefMap map[rune][]int16 // default voice
keyNatMap map[rune][]int16 // natural voice
keyFreqMap map[rune]float64
keyNoteMap map[rune]string
noteKeyMap map[string]rune
}
// NewViolin return new violin voice
func NewViolin() *Violin {
v := &Violin{
keyDefMap: make(map[rune][]int16),
keyNatMap: make(map[rune][]int16),
keyFreqMap: make(map[rune]float64),
keyNoteMap: make(map[rune]string),
noteKeyMap: make(map[string]rune),
}
keys := "q2w3er5t6y7ui9o0p[=]azsxcfvgbnjmk,l."
octaveFreq3 := []float64{
// C3, Db3, D3, Eb3, E3
196.0, 207.6, 220.0, 233.0, 246.9, // 3
}
octaveFreq456 := []float64{
// C4, Db4, D4, Eb4, E4, F4, Gb4, G4, Ab4, A4, Bb4, B4
261.6, 277.1, 293.6, 311.1, 329.6, 349.2, 369.9, 392.0, 415.3, 440.0, 466.1, 493.8, // 4
523.2, 554.3, 587.3, 622.2, 659.2, 698.4, 739.9, 783.9, 830.6, 880.0, 932.3, 987.7, // 5
1046.5, 1108.7, 1174.6, 1244.5, 1318.5, 1396.9, 1479.9, 1567, 1661, 1760, 1864, 1975, // 6
}
octaveFreq7 := []float64{
// C7, Db7, D7, Eb7, E7
2093, 2217.5, 2349.3, 2489, 2637, 2793, 2960,
}
noteNames := []string{
"G3", "Ab3", "A3", "Bb3", "B3",
"C4", "Db4", "D4", "Eb4", "E4", "F4", "Gb4", "G4", "Ab4", "A4", "Bb4", "B4",
"C5", "Db5", "D5", "Eb5", "E5", "F5", "Gb5", "G5", "Ab5", "A5", "Bb5", "B5",
"C6", "Db6", "D6", "Eb6", "E6", "F6", "Gb6", "G6", "Ab6", "A6", "Bb6", "B6",
"C7", "Db7", "D7", "Eb7", "E7",
}
// initialize maps
ni := 0
for i, key := range keys[31:] { // actave 3
keyID := 2000 + key
note := noteNames[ni]
v.keyFreqMap[keyID] = octaveFreq3[i]
v.keyNoteMap[keyID] = note
v.noteKeyMap[note] = keyID
ni++
}
for i, key := range keys { // actave 4, 5, 6
keyID := 3000 + key
note := noteNames[ni]
v.keyFreqMap[keyID] = octaveFreq456[i]
v.keyNoteMap[keyID] = note
v.noteKeyMap[note] = keyID
ni++
}
for i, key := range keys[:5] { // actave 7
keyID := 4000 + key
note := noteNames[ni]
v.keyFreqMap[keyID] = octaveFreq7[i]
v.keyNoteMap[keyID] = note
v.noteKeyMap[note] = keyID
ni++
}
for key := range v.keyFreqMap {
// generate default violin voice
v.keyDefMap[key] = v.generateNote(key, wholeNote)
}
// load natural voice file, if exists
filename := filepath.Join(HomeDir(), "voices", "violin.zip")
voiceFile, err := zip.OpenReader(filename)
if err == nil {
// voice file exists
defer voiceFile.Close()
v.naturalVoice = true
for _, zfile := range voiceFile.File {
file, err := zfile.Open()
if err != nil {
fmt.Fprintln(os.Stderr, "Unable to open file from zip:", zfile.Name)
continue
}
defer file.Close()
if !strings.HasSuffix(zfile.Name, ".wav") {
continue
}
noteName := strings.Split(filepath.Base(zfile.Name), ".")[0]
if key, found := v.noteKeyMap[noteName]; found {
var header WaveHeader
header.ReadHeader(file)
if header.SampleRate != 44100 || header.BitsPerSample != 16 {
fmt.Fprintln(os.Stderr, "Unsupported sample file:", zfile.Name)
continue
}
buf, err := ioutil.ReadAll(file)
if err != nil {
fmt.Fprintln(os.Stderr, "Unable to read file from zip:", zfile.Name)
continue
}
rest := wholeNote - len(buf)/2
if rest > 0 {
// too short, sample should be a whole note
bufRest := make([]byte, rest*2)
buf = append(buf, bufRest...)
}
buf16 := byteToInt16Buf(buf)
if len(buf16) < wholeNote {
fmt.Fprintln(os.Stderr, "Sample note duration must be 90112 samples long.")
}
trimWave(buf16)
v.keyNatMap[key] = buf16
} else {
fmt.Fprintln(os.Stderr, "Unknown note name in voice file:", noteName)
}
}
}
return v
}
func (v *Violin) generateNote(key rune, duration int) []int16 {
// default voice
freq, found := v.keyFreqMap[key]
if !found {
fmt.Fprintln(os.Stderr, "frequency not found: key", key)
return []int16{}
}
buf := make([]int16, duration)
timer0 := 0.0
timer1 := 0.0
timer2 := 0.0
timer3 := 0.0
tick0 := 2 * math.Pi / SampleRate64 * freq
tick1 := tick0 * 2
tick2 := tick1 * 3
tick3 := tick2 * 4
amp := SampleAmp16bit * 0.5
for i := range buf {
sin0 := math.Sin(timer0)
sin1 := sin0 * math.Sin(timer1)
sin2 := sin1 * math.Sin(timer2)
sin3 := sin2 * math.Sin(timer3)
bar0 := amp * sin0
bar1 := bar0 * sin1 / 2 * sin0
bar2 := bar0 * sin2 / 3 * sin0
bar3 := bar0 * sin3 / 4 * sin0
buf[i] = int16(bar0 + bar1 + bar2 + bar3)
timer0 += tick0
timer1 += tick1
timer2 += tick2
timer3 += tick3
}
trimWave(buf)
return buf
}
// GetNote prepares note wave form
func (v *Violin) GetNote(note *Note, sustain *Sustain) (found bool) {
var bufNote []int16
if v.naturalVoice {
bufNote, found = v.keyNatMap[note.key]
}
if !found {
bufNote, found = v.keyDefMap[note.key]
}
if !found {
return
}
buf := make([]int16, len(bufNote))
copy(buf, bufNote) // get a copy of the note
applyNoteVolume(buf, note.volume, note.amplitude)
// Sustain note
if note.duration == 'W' {
// Whole note
if v.NaturalVoice() {
// sustain current note
copyBuffer(sustain.buf, buf[len(buf)/3:])
}
} else {
if v.NaturalVoice() {
// sustain current note
copyBuffer(sustain.buf, buf[note.samples:])
}
}
// measure note
if n := note.samples - len(buf); n > 0 {
// expand buffer
buf = append(buf, make([]int16, n)...)
}
buf = buf[:note.samples]
// clean note
trimWave(buf)
// release note
releaseNote(buf, 0, 0.99)
note.buf = buf
return
}
// Sustain flag
func (v *Violin) Sustain() bool {
return false
}
// NaturalVoice flag
func (v *Violin) NaturalVoice() bool {
return v.naturalVoice
}
// NaturalVoiceFound flag
func (v *Violin) NaturalVoiceFound() bool {
return v.naturalVoiceFound
}
// ComputerVoice flag
func (v *Violin) ComputerVoice(enable bool) {
v.naturalVoice = !enable
}
func (v *Violin) raiseNote(note *Note, ratio float64) {
buflen := len(note.buf)
raise := float64(buflen) * ratio
tick := SampleAmp16bit / raise
volume := 0.0
for i, bar := range note.buf {
bar64 := float64(bar)
bar64 = bar64 * (volume / SampleAmp16bit)
note.buf[i] = int16(bar64)
volume += tick
if SampleAmp16bit <= volume {
break
}
}
}
// SustainNote applies sustain settings to a note
func (v *Violin) SustainNote(note *Note, sustain *Sustain) {
// | ___ release
// | / \
// | / ----| <----------- sustain
// |/ \ buflen
// |---|---|-------|--|----|
// attack| | duration > 0
// | ratio
// decay
//
// attack: allows overriting the beginning by the previous note
//
buf := note.buf
buflen := len(buf)
volume64 := float64(note.volume)
if v.naturalVoice {
attack := float64(9-sustain.attack) / 10
v.raiseNote(note, attack)
release := float64(1+sustain.release) / 10
releaseNote(buf, 0, release)
return
}
// Sustain default voice amplitude for ADSR phases
// | /|\ A - attack
// | / | \ _____ D - decay
// |/ | | | \ S - sustain
// |-------------- R - release
// A D S R
if note.volume == 0 {
return
}
attack := int(float64(buflen/200) * float64(sustain.attack))
decay := (buflen-attack)/10 + ((buflen - attack) / 20 * sustain.decay)
S := int16(volume64 / 10.0 * float64(sustain.sustain+1))
sustainCount := (buflen - attack - decay) / 2
R := buflen - attack - decay - sustainCount
attack64 := float64(attack)
decay64 := float64(decay)
sustain64 := float64(S)
release64 := float64(R)
countD := 0.0
countR := 0.0
for i, bar := range buf {
i64 := float64(i)
bar64 := float64(bar)
if i >= attack+decay+sustainCount {
// Release phase, decay volume to zero
bar64 = bar64 * ((sustain64 * (release64 - countR) / release64) / volume64)
countR++
} else if i >= attack+decay {
// Sustain phase, hold volume on sustain level
bar64 = bar64 * (sustain64 / volume64)
} else if i >= attack && decay > 0 {
// Decay phase, decay volume to sustain level
gap := (volume64 - sustain64) * ((decay64 - countD) / decay64)
bar64 = bar64 * ((sustain64 + gap) / volume64)
countD++
} else if i <= attack && attack > 0 {
// Attack phase, raise volume to max
bar64 = bar64 * (i64 / attack64)
}
buf[i] = int16(bar64)
}
}