-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio-sequencer.js
More file actions
188 lines (170 loc) · 6.37 KB
/
Copy pathaudio-sequencer.js
File metadata and controls
188 lines (170 loc) · 6.37 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
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
let audioCtx, masterGain, padFilter;
let schedulerTimer = null;
let step = 0;
const tempo = 72;
const subdivision = 4;
const stepMs = (60 / tempo) * 1000 / subdivision;
const rootMidi = 57;
const minorScale = [0,2,3,5,7,8,10];
function midiToFreq(m) {
return 440 * Math.pow(2, (m - 69) / 12);
}
function randChoice(arr) {
return arr[Math.floor(Math.random()*arr.length)];
}
function mkReverbBuffer(ctx, seconds = 3, decay = 2.5) {
const rate = ctx.sampleRate;
const length = rate * seconds;
const impulse = ctx.createBuffer(2, length, rate);
for (let channel = 0; channel < impulse.numberOfChannels; channel++) {
const ch = impulse.getChannelData(channel);
for (let i = 0; i < length; i++) {
ch[i] = (Math.random() * 2 - 1) * Math.pow(1 - i / length, decay);
}
}
return impulse;
}
function mkNoiseBuffer(ctx, seconds = 1) {
const rate = ctx.sampleRate;
const buffer = ctx.createBuffer(1, rate * seconds, rate);
const data = buffer.getChannelData(0);
for (let i = 0; i < data.length; i++) data[i] = Math.random() * 2 - 1;
return buffer;
}
function createAudio() {
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
masterGain = audioCtx.createGain();
masterGain.gain.value = 0.35;
masterGain.connect(audioCtx.destination);
padFilter = audioCtx.createBiquadFilter();
padFilter.type = 'lowpass';
padFilter.frequency.value = 1200;
padFilter.Q.value = 0.7;
padFilter.connect(masterGain);
const convolver = audioCtx.createConvolver();
convolver.buffer = mkReverbBuffer(audioCtx, 4.0, 2.5);
const reverbSend = audioCtx.createGain();
reverbSend.gain.value = 0.35;
reverbSend.connect(convolver);
convolver.connect(masterGain);
const padOsc1 = audioCtx.createOscillator();
padOsc1.type = 'sawtooth';
padOsc1.frequency.value = midiToFreq(rootMidi - 12);
const padOsc2 = audioCtx.createOscillator();
padOsc2.type = 'sawtooth';
padOsc2.frequency.value = midiToFreq(rootMidi - 12);
padOsc2.detune.value = 7;
const padGain = audioCtx.createGain();
padGain.gain.value = 0.18;
const padLFO = audioCtx.createOscillator();
padLFO.type = 'sine';
padLFO.frequency.value = 0.07;
const padLFODepth = audioCtx.createGain();
padLFODepth.gain.value = 600;
padOsc1.connect(padGain);
padOsc2.connect(padGain);
padGain.connect(padFilter);
padLFO.connect(padLFODepth);
padLFODepth.connect(padFilter.frequency);
padOsc1.start();
padOsc2.start();
padLFO.start();
}
function triggerSynthNote(freq, time, duration = 0.4, type = 'sawtooth', gain = 0.16) {
const osc = audioCtx.createOscillator();
osc.type = type;
osc.frequency.setValueAtTime(freq, time);
const g = audioCtx.createGain();
g.gain.setValueAtTime(0.0001, time);
g.gain.exponentialRampToValueAtTime(gain, time + 0.02);
g.gain.exponentialRampToValueAtTime(0.0001, time + duration);
const filter = audioCtx.createBiquadFilter();
filter.type = 'lowpass';
filter.frequency.setValueAtTime(1200 + Math.random() * 900, time);
filter.Q.value = 0.6;
osc.connect(filter);
filter.connect(padFilter);
osc.start(time);
osc.stop(time + duration + 0.05);
}
function triggerBass(freq, time, duration = 0.36) {
const osc = audioCtx.createOscillator();
osc.type = 'sine';
osc.frequency.setValueAtTime(freq, time);
const g = audioCtx.createGain();
g.gain.setValueAtTime(0.0001, time);
g.gain.linearRampToValueAtTime(0.6, time + 0.01);
g.gain.linearRampToValueAtTime(0.0001, time + duration);
const bassFilter = audioCtx.createBiquadFilter();
bassFilter.type = 'lowpass';
bassFilter.frequency.setValueAtTime(800, time);
osc.connect(bassFilter);
bassFilter.connect(masterGain);
osc.start(time);
osc.stop(time + duration + 0.02);
}
const noiseBufferCache = { buf: null };
function triggerHat(time, duration = 0.06) {
if (!noiseBufferCache.buf) noiseBufferCache.buf = mkNoiseBuffer(audioCtx, 1);
const src = audioCtx.createBufferSource();
src.buffer = noiseBufferCache.buf;
const hf = audioCtx.createBiquadFilter();
hf.type = 'highpass';
hf.frequency.setValueAtTime(6000, time);
const g = audioCtx.createGain();
g.gain.setValueAtTime(0.0001, time);
g.gain.exponentialRampToValueAtTime(0.35, time + 0.005);
g.gain.exponentialRampToValueAtTime(0.0001, time + duration);
src.connect(hf);
hf.connect(g);
g.connect(masterGain);
src.start(time);
src.stop(time + duration + 0.02);
}
function triggerKick(time) {
const osc = audioCtx.createOscillator();
osc.type = 'sine';
const startFreq = 120;
osc.frequency.setValueAtTime(startFreq, time);
osc.frequency.exponentialRampToValueAtTime(50, time + 0.12);
const g = audioCtx.createGain();
g.gain.setValueAtTime(0.0001, time);
g.gain.exponentialRampToValueAtTime(0.9, time + 0.01);
g.gain.exponentialRampToValueAtTime(0.0001, time + 0.35);
osc.connect(g);
g.connect(masterGain);
osc.start(time);
osc.stop(time + 0.36);
}
function startSequencer() {
if (!audioCtx) return;
step = 0;
const stepsPerBar = 16;
schedulerTimer = setInterval(() => {
const now = audioCtx.currentTime + 0.05;
if (step % 4 === 0) {
const chordRoot = rootMidi + (Math.random() > 0.6 ? 0 : -12);
const note = chordRoot + randChoice(minorScale) + (Math.random() > 0.5 ? 0 : 12);
triggerSynthNote(midiToFreq(note), now, 0.75, 'triangle', 0.12);
}
if (Math.random() < 0.45) {
const arpNote = rootMidi + randChoice(minorScale) + (Math.random() > 0.6 ? 12 : 0);
triggerSynthNote(midiToFreq(arpNote), now, 0.12, 'sawtooth', 0.08);
}
if (step % 16 === 0) {
const bassNote = rootMidi - 24 + randChoice([0, -2, 0]);
triggerBass(midiToFreq(bassNote), now, 0.36);
}
if (Math.random() < 0.42) triggerHat(now, 0.05);
if (Math.random() < 0.18 && (step % 4 === 0)) triggerKick(now);
if (padFilter) {
const base = 900 + Math.sin(audioCtx.currentTime * 0.15) * 350;
padFilter.frequency.setTargetAtTime(base + Math.random() * 400, audioCtx.currentTime, 0.6);
}
step = (step + 1) % stepsPerBar;
}, stepMs);
}
window.addEventListener('load', async () => {
createAudio();
startSequencer();
});