-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsound.js
More file actions
22 lines (17 loc) · 1007 Bytes
/
sound.js
File metadata and controls
22 lines (17 loc) · 1007 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
function playSound(frequency = 440, duration = 2) {
const osc = audioCtx.createOscillator(); // This is a sound wave generator
// osc.type = "sawtooth";
const envelope = audioCtx.createGain(); // Creates a gain node which controls volume
osc.connect(envelope); // This means the raw sound wave from the oscillator goes into the volume controller.
envelope.connect(audioCtx.destination);
// 1. Set initial gain (volume) to 0 (mute) — start of envelope
envelope.gain.setValueAtTime(0, audioCtx.currentTime);
// 2. Fade in smoothly to volume 1 (max) — attack phase
envelope.gain.linearRampToValueAtTime(0.05, audioCtx.currentTime + 0.05);
// 3. Fade out smoothly to 0 over duration — release phase
envelope.gain.linearRampToValueAtTime(0, audioCtx.currentTime + duration);
osc.frequency.setValueAtTime(frequency, audioCtx.currentTime);
osc.start();
osc.stop(audioCtx.currentTime + duration);
}