-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpiano.js
45 lines (38 loc) · 1.17 KB
/
piano.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
function addPianoNote(v, t, f) {
var fader = ac.createGain();
fader.connect(reverb);
var lnf = Math.log(f);
var peakScale = (0.0529162 * lnf - 0.785209) * lnf + 3.57215
var decayScale = 0.15;
var frequencyCenter = 146.8;
var filterOffset = 0.3 * (f - frequencyCenter);
var safeTime = Math.max(t, ac.currentTime + 0.003);
var attack = 0.005;
var peakTime = safeTime + attack;
var sawPeak = 0.9 * peakScale;
var sawDecay = 2;
var filter = ac.createBiquadFilter();
filter.connect(fader);
filter.frequency.value = 285 + filterOffset;
filter.detune.value = 300;
filter.detune.setTargetAtTime(0, peakTime, 8 * decayScale);
var sawGain = ac.createGain();
sawGain.connect(filter);
sawGain.gain.value = 0;
sawGain.gain.setValueAtTime(0, safeTime);
sawGain.gain.linearRampToValueAtTime(sawPeak, peakTime);
sawGain.gain.setTargetAtTime(0, peakTime, sawDecay * decayScale);
var saw = ac.createOscillator();
saw.type = "sawtooth";
saw.frequency.value = f;
saw.connect(sawGain);
saw.start(safeTime);
var note = {
fader: fader,
oscillators: [saw],
start: t,
expiration: peakTime + sawDecay,
frequency: f
}
return note;
}