-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnoises.js
More file actions
82 lines (68 loc) · 2.73 KB
/
noises.js
File metadata and controls
82 lines (68 loc) · 2.73 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
// Nose is a simple triangle
export function playTri(x, y, context, playLength = 0.5, startTime = 0) {
const osc = new OscillatorNode(context, {
type: 'triangle',
detune: 0,
frequency: x
});
const gain = new GainNode(context);
osc.connect(gain).connect(context.destination);
gain.gain.exponentialRampToValueAtTime(0.01, context.currentTime + startTime + playLength);
osc.start(context.currentTime + startTime);
osc.stop(context.currentTime + startTime + playLength);
}
// Frequency modulation -> NOT USING THIS ATM
export function playPulse(x, y, context, playLength = 0.5, startTime = 0) {
const osc = context.createOscillator();
osc.type = 'sine';
osc.frequency.value = y;
const amp = context.createGain();
amp.gain.value = 0.5;
const lfo = context.createOscillator();
lfo.type = 'square';
lfo.frequency.value = x / 10;
lfo.connect(amp.gain);
osc.connect(amp).connect(context.destination);
lfo.start();
osc.start();
osc.stop(context.currentTime + playLength);
}
// Noise buffer for hihat
export function playNoise(x, y, context, playLength = 0.1, startTime = 0) {
const bufferSize = context.sampleRate * playLength;
const buffer = context.createBuffer(1, bufferSize, context.sampleRate); // create an empty buffer
let data = buffer.getChannelData(0); // get data
// fill the buffer with noise
for (let i = 0; i < bufferSize; i++) {
data[i] = Math.random() * 2 - 1;
}
// create a buffer source for our created data
let noise = context.createBufferSource();
noise.buffer = buffer;
const bandpass = context.createBiquadFilter();
bandpass.type = 'bandpass';
bandpass.frequency.value = x;
const gain = context.createGain();
gain.gain.exponentialRampToValueAtTime(0.01, context.currentTime + startTime + playLength)
// connect our graph
noise.connect(bandpass).connect(gain).connect(context.destination);
noise.start(context.currentTime + startTime);
noise.stop(context.currentTime + startTime + playLength);
}
// Noise buffer for kick
export function playKick(x, y, context, playLength = 0.5, startTime = 0.5) {
const osc = context.createOscillator();
const gain = context.createGain();
osc.frequency.value = y;
gain.gain.value = 1;
osc.frequency.exponentialRampToValueAtTime(0.01, context.currentTime + startTime + playLength);
gain.gain.exponentialRampToValueAtTime(0.01, context.currentTime + startTime + playLength);
// connect our graph
osc.connect(gain).connect(context.destination);
osc.start(context.currentTime + startTime);
osc.stop(context.currentTime + startTime + playLength);
}
//
export function playWrist() { }
//
export function playElbow() { }