-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhihat.js
76 lines (63 loc) · 2.26 KB
/
hihat.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
function createClosedHiHat(audioContext) {
var context = audioContext;
function makeDistortionCurve(amount) {
var n_samples = 44100;
var curve = new Float32Array(n_samples);
var deg = Math.PI / 180;
var x;
for (var i = 0; i < n_samples; ++i) {
x = i * 2 / n_samples - 1;
curve[i] = (3 + amount) * x * 20 * deg / (Math.PI + amount * Math.abs(x));
}
return curve;
}
// Set up the distortion node with a subtle curve
var distortion = context.createWaveShaper();
distortion.curve = makeDistortionCurve(0);
distortion.oversample = "4x";
// Set up the filter node as a bandpass filter
var filter = context.createBiquadFilter();
filter.type = "bandpass";
filter.frequency.value = 11175;
filter.Q.value = 2;
return {
trigger: function() {
var noise = context.createBufferSource();
var noiseGain = context.createGain();
// Set up the noise buffer
var bufferSize = 2 * context.sampleRate;
var noiseBuffer = context.createBuffer(1, bufferSize, context.sampleRate);
var output = noiseBuffer.getChannelData(0);
for (var i = 0; i < bufferSize; i++) {
output[i] = Math.random() * 2 - 1;
}
noise.buffer = noiseBuffer;
noise.loop = true;
// Set up the noise gain node with ADSR controls
var attackTime = 0.001;
var decayTime = 0.1;
var sustainLevel = 0.01;
var releaseTime = 0.1;
var now = context.currentTime;
noiseGain.gain.setValueAtTime(0, now);
noiseGain.gain.linearRampToValueAtTime(1, now + attackTime);
noiseGain.gain.exponentialRampToValueAtTime(sustainLevel, now + attackTime + decayTime);
noiseGain.gain.setValueAtTime(sustainLevel, now + attackTime + decayTime);
noiseGain.gain.exponentialRampToValueAtTime(0.001, now + attackTime + decayTime + releaseTime);
// Connect the nodes together
noise.connect(noiseGain);
noiseGain.connect(filter);
filter.connect(distortion);
distortion.connect(context.destination);
// Start the noise
noise.start();
setTimeout(() => {
noise.stop();
}, releaseTime * 1000);
},
};
}
// Export the createClosedHihat function
export function createVoice(audioContext) {
return createClosedHiHat(audioContext);
}