-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
200 lines (172 loc) · 6.07 KB
/
index.js
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
const keyboard = {
"a": { freq: 262, down: false },
"w": { freq: 277, down: false },
"s": { freq: 294, down: false },
"e": { freq: 311, down: false },
"d": { freq: 330, down: false },
"f": { freq: 349, down: false },
"t": { freq: 370, down: false },
"g": { freq: 392, down: false },
"y": { freq: 415, down: false },
"h": { freq: 440, down: false },
"u": { freq: 466, down: false },
"j": { freq: 494, down: false },
"k": { freq: 523, down: false },
"o": { freq: 554, down: false },
"l": { freq: 587, down: false },
"p": { freq: 622, down: false },
";": { freq: 659, down: false },
"'": { freq: 698, down: false },
}
const starterPatch = {
oscillators: [
{
number: 1,
osc_type: "sine",
gain: 0.5,
attack: 0.5,
decay: 0.5,
sustain: 0.5,
release: 0.5
},
{
number: 2,
osc_type: "sine",
gain: 0.5,
attack: 0.5,
decay: 0.5,
sustain: 0.5,
release: 0.5
},
{
number: 3,
osc_type: "sine",
gain: 0.5,
attack: 0.5,
decay: 0.5,
sustain: 0.5,
release: 0.5
}
]
}
const audioContext = new AudioContext()
const oscillators = []
const nodes = []
let patchState = {}
function loadPatch(patch) {
oscillators.length = 0
patch.oscillators.forEach(osc => {
const typeSelect = document.getElementById(`osc_type-select-${osc.number}`)
const gainSlider = document.getElementById(`gain-slider-${osc.number}`)
const attackSlider = document.getElementById(`attack-slider-${osc.number}`)
const releaseSlider = document.getElementById(`release-slider-${osc.number}`)
typeSelect.value = osc.osc_type
gainSlider.value = osc.gain
attackSlider.value = osc.attack
releaseSlider.value = osc.release
typeSelect.addEventListener("input", e => updateValue(e, osc))
gainSlider.addEventListener("input", e => updateValue(e, osc))
attackSlider.addEventListener("input", e => updateValue(e, osc))
releaseSlider.addEventListener("input", e => updateValue(e, osc))
oscillators.push(osc)
})
patchState = patch
}
function startSound(e) {
if (e.repeat) return
const input = e.key
if (Object.keys(keyboard).includes(input) && !keyboard[input].down) {
oscillators.forEach(osc => {
const sampleRate = audioContext.sampleRate
const duration = 1.0
const buffer = audioContext.createBuffer(1, sampleRate * duration, sampleRate)
const channelData = buffer.getChannelData(0)
const attackTime = parseFloat(logifyValue(osc.attack)) * 0.1
const oscNode = new OscillatorNode(audioContext, { type: osc.osc_type, frequency: keyboard[input].freq })
const gainNode = new GainNode(audioContext, { gain: parseFloat(osc.gain) })
const newNode = {
osc_node: oscNode,
gain_node: gainNode,
key_pressed: input,
osc_data: osc
}
for (let i = 0; i < buffer.length; i++) {
channelData[i] = Math.sin((i / sampleRate) * 2 * Math.PI * 440)
}
oscNode.buffer = buffer
oscNode.connect(gainNode)
gainNode.gain.setValueAtTime(0.0000000001, audioContext.currentTime)
gainNode.gain.linearRampToValueAtTime(parseFloat(osc.gain) * 0.1, audioContext.currentTime + attackTime)
gainNode.connect(audioContext.destination)
oscNode.start()
nodes.push(newNode)
})
document.addEventListener("keyup", event => {
if (event.key == input) {
stopSound(event)
}
})
keyboard[input].down = true
}
}
function stopSound(e) {
const input = e.key
nodes.forEach(node => {
const releaseTime = logifyValue(node.osc_data.release)
if (node.key_pressed == input) {
node.gain_node.gain.setValueAtTime(node.gain_node.gain.value, audioContext.currentTime)
node.gain_node.gain.exponentialRampToValueAtTime(0.0000000001, audioContext.currentTime + parseFloat(releaseTime))
setTimeout(() => {
node.gain_node.disconnect()
node.osc_node.disconnect()
}, 5000)
}
})
keyboard[input].down = false
}
function changeOctave(e) {
for (const note in keyboard) {
if (e.key == "z") {
keyboard[note].freq = keyboard[note].freq / 2
}
if (e.key == "x") {
keyboard[note].freq = keyboard[note].freq * 2
}
}
}
function panic(e) {
if (e.key == "Escape") {
nodes.forEach(node => {
node.gain_node.gain.setValueAtTime(node.gain_node.gain.value, audioContext.currentTime)
node.gain_node.gain.exponentialRampToValueAtTime(0.00001, audioContext.currentTime + 0.002)
setTimeout(() => {
node.gain_node.disconnect()
node.osc_node.disconnect()
}, 51)
})
}
}
function updateValue(e, editedOsc) {
const updatedValue = e.target.name === `osc_type-${editedOsc.number}` ?
e.target.value
:
parseFloat(e.target.value)
const slicedName = e.target.name.slice(0, -2)
patchState.oscillators.forEach(osc => {
if (parseFloat(osc.number) === editedOsc.number) {
osc[slicedName] = updatedValue
}
})
}
function logifyValue(position) {
const minInput = 0
const maxInput = 100
const minValue = Math.log(1)
const maxValue = Math.log(100000000000000000000)
const scale = (maxInput - minInput) / (maxValue - minValue)
return Math.exp(minValue + scale * (position - minInput))
}
loadPatch(starterPatch)
document.addEventListener("keydown", e => startSound(e))
document.addEventListener("keydown", e => changeOctave(e))
document.addEventListener("keydown", e => panic(e))