-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio.js
More file actions
280 lines (221 loc) · 8.06 KB
/
Copy pathaudio.js
File metadata and controls
280 lines (221 loc) · 8.06 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
let audioContext = null;
let audioReadyPromise = null;
function getAudioContext() {
if (audioContext) return audioContext;
const AudioContextClass = window.AudioContext || window.webkitAudioContext;
if (!AudioContextClass) {
return null;
}
audioContext = new AudioContextClass();
return audioContext;
}
export async function ensureAudioReady() {
const context = getAudioContext();
if (!context) return false;
if (context.state === 'running') {
return true;
}
if (!audioReadyPromise) {
audioReadyPromise = context.resume().finally(() => {
audioReadyPromise = null;
});
}
await audioReadyPromise;
return context.state === 'running';
}
let _reverb = null;
function getReverb(context) {
if (_reverb) return _reverb;
const length = context.sampleRate * 1.5;
const impulse = context.createBuffer(2, length, context.sampleRate);
for (let ch = 0; ch < 2; ch++) {
const data = impulse.getChannelData(ch);
for (let i = 0; i < length; i++) {
data[i] = (Math.random() * 2 - 1) * Math.pow(1 - i / length, 2.5);
}
}
const convolver = context.createConvolver();
convolver.buffer = impulse;
const wet = context.createGain();
const dry = context.createGain();
wet.gain.value = 0.2;
dry.gain.value = 0.8;
_reverb = {
connect(source, destination) {
source.connect(dry);
source.connect(convolver);
convolver.connect(wet);
dry.connect(destination);
wet.connect(destination);
},
};
return _reverb;
}
function playSinePad({
start = 0,
frequency = 440,
volume = 0.38,
attack = 0.01,
decay = 0.25,
release = 0.2,
filterFrequency = 4000,
} = {}) {
const context = getAudioContext();
if (!context || context.state !== 'running') return;
const startTime = context.currentTime + start;
// Local mix bus for this pad instance so its layers mix cleanly before the reverb split
const padBus = context.createGain();
padBus.gain.setValueAtTime(1.0, startTime);
getReverb(context).connect(padBus, context.destination);
function playLayer(freq, vol, att, dec, rel) {
const osc = context.createOscillator();
const gain = context.createGain();
const filter = context.createBiquadFilter();
osc.type = 'sine';
osc.frequency.value = freq;
filter.type = 'lowpass';
filter.frequency.value = filterFrequency;
filter.Q.value = 0.7;
gain.gain.cancelScheduledValues(startTime);
gain.gain.setValueAtTime(0.0001, startTime);
gain.gain.linearRampToValueAtTime(vol, startTime + att);
gain.gain.exponentialRampToValueAtTime(0.0001, startTime + att + dec + rel);
osc.connect(filter);
filter.connect(gain);
// Connect layers to the single instance-controlled pad bus
gain.connect(padBus);
osc.start(startTime);
osc.stop(startTime + att + dec + rel + 0.05);
}
// Root note
playLayer(frequency, volume, attack, decay, release);
// Quiet octave shimmer
playLayer(frequency * 2, volume * 0.18, attack * 1.5, decay * 0.6, release * 0.6);
}
async function withReady(callback) {
const ready = await ensureAudioReady();
if (!ready) return false;
callback();
return true;
}
export async function playRoundSinePad() {
return withReady(() => {
[523, 659, 784, 1047].forEach((freq, i) => {
playSinePad({ start: i * 0.1, frequency: freq, volume: 0.36, decay: 0.25, release: 0.2 });
});
});
}
function getSinePadCountdownStep(step) {
const freq = step === 1 ? 659 : 440;
const volume = step === 1 ? 0.42 : 0.34;
const attack = 0.01;
const decay = 0.25;
const release = 0.2;
return { frequency: freq, volume, attack, decay, release, filterFrequency: 4000 };
}
export async function playCountdownSinePadBeep(step) {
return withReady(() => {
playSinePad(getSinePadCountdownStep(step));
});
}
function runCountdownSequence(beepPlayer) {
beepPlayer(3);
window.setTimeout(() => beepPlayer(2), 1000);
window.setTimeout(() => beepPlayer(1), 2000);
}
export async function playCountdownSinePadSequence() {
const ready = await ensureAudioReady();
if (!ready) return false;
runCountdownSequence(playCountdownSinePadBeep);
return true;
}
export async function playDirectionChangeSinePad() {
return withReady(() => {
[523, 659, 1047].forEach((freq, i) => {
playSinePad({ start: i * 0.15, frequency: freq });
});
});
}
let urgencyIntervalId = null;
let urgencyStartTime = 0;
export async function startUrgencyAudio() {
if (urgencyIntervalId) return true; // Already running
return withReady(() => {
const context = getAudioContext();
if (!context) return;
urgencyStartTime = context.currentTime;
let tickCounter = 0;
urgencyIntervalId = window.setInterval(() => {
const timeActive = context.currentTime - urgencyStartTime;
const baseNote = 659;
const alertNote = 698;
const currentPitch = (tickCounter % 2 === 0) ? baseNote : alertNote;
// Gradually swells from a quiet 0.04 to 0.35 ceiling
const volumeSwell = Math.min(0.04 + (timeActive * 0.018), 0.35);
const pendulumConfig = {
frequency: currentPitch,
volume: volumeSwell,
attack: 0.05,
decay: 0.20,
release: 0.20,
filterFrequency: 3000
};
playSinePad({ ...pendulumConfig, start: 0 });
tickCounter++;
}, 800); // Every 800ms for a steady, heartbeat-like rhythm
});
}
export function stopUrgencyAudio() {
if (urgencyIntervalId) {
window.clearInterval(urgencyIntervalId);
urgencyIntervalId = null;
}
urgencyStartTime = 0;
}
export function playTapGlassBead(playerProgressIndex, totalPlayers) {
const context = getAudioContext();
if (!context || context.state !== 'running') return;
const now = context.currentTime;
const floorMidi = 72; // C5 (523.25 Hz)
const ceilingMidi = 84; // C6 (1046.50 Hz)
const midiRange = ceilingMidi - floorMidi;
const steps = Math.max(1, totalPlayers - 1);
const exactMidiNote = floorMidi + (midiRange * (playerProgressIndex / steps));
let targetMidi = Math.round(exactMidiNote);
const noteInOctave = targetMidi % 12;
if ([1, 3, 6, 8, 10].includes(noteInOctave)) {
targetMidi += 1;
}
if (targetMidi > ceilingMidi) {
targetMidi = ceilingMidi;
}
const fundamental = 440 * Math.pow(2, (targetMidi - 69) / 12);
// 1. Create a local mix bus gain node for this tap interaction instance
const glassTapBus = context.createGain();
glassTapBus.gain.setValueAtTime(1.0, now);
// 2. Set up the high-pass filter
const filter = context.createBiquadFilter();
filter.type = 'highpass';
filter.frequency.setValueAtTime(150, now);
// 3. Connect the signal path: Oscillators -> Filter -> Local Bus -> SINGLE REVERB -> Speakers
filter.connect(glassTapBus);
getReverb(context).connect(glassTapBus, context.destination);
const components = [
{ freq: fundamental, type: 'triangle', vol: 0.12, decay: 0.08 },
{ freq: fundamental * 2.00, type: 'sine', vol: 0.06, decay: 0.04 },
{ freq: fundamental * 4.00, type: 'sine', vol: 0.03, decay: 0.02 }
];
components.forEach(comp => {
const osc = context.createOscillator();
const gainNode = context.createGain();
osc.type = comp.type;
osc.frequency.setValueAtTime(comp.freq, now);
gainNode.gain.setValueAtTime(0, now);
gainNode.gain.linearRampToValueAtTime(comp.vol, now + 0.001);
gainNode.gain.exponentialRampToValueAtTime(0.0001, now + comp.decay);
osc.connect(gainNode);
gainNode.connect(filter); // Route into filter
osc.start(now);
osc.stop(now + comp.decay + 0.02);
});
}