-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffscreen.js
More file actions
72 lines (55 loc) · 2.56 KB
/
Copy pathoffscreen.js
File metadata and controls
72 lines (55 loc) · 2.56 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
// offscreen.js — speelt geluiden af via Web Audio API
chrome.runtime.onMessage.addListener((msg) => {
if (msg.type === 'playSound') {
playSound(msg.sound, msg.volume ?? 0.5);
}
});
function playSound(type, volume) {
const ctx = new AudioContext();
switch (type) {
case 'ping': playPing(ctx, volume); break;
case 'radar': playRadar(ctx, volume); break;
case 'alert': playAlert(ctx, volume); break;
case 'chime': playChime(ctx, volume); break;
default: playPing(ctx, volume); break;
}
}
// ── Hulpfunctie: speel een toon ──────────────────────────────────────────────
function tone(ctx, freq, startTime, duration, volume, type = 'sine', fadeOut = true) {
const osc = ctx.createOscillator();
const gain = ctx.createGain();
osc.connect(gain);
gain.connect(ctx.destination);
osc.type = type;
osc.frequency.setValueAtTime(freq, startTime);
gain.gain.setValueAtTime(volume, startTime);
if (fadeOut) {
gain.gain.exponentialRampToValueAtTime(0.001, startTime + duration);
}
osc.start(startTime);
osc.stop(startTime + duration + 0.05);
}
// ── 🔔 Ping — heldere korte toon ─────────────────────────────────────────────
function playPing(ctx, volume) {
tone(ctx, 880, ctx.currentTime, 0.4, volume);
}
// ── 📡 Radar — dubbele sonar beep ────────────────────────────────────────────
function playRadar(ctx, volume) {
const t = ctx.currentTime;
tone(ctx, 440, t, 0.25, volume);
tone(ctx, 660, t + 0.3, 0.25, volume);
}
// ── 🚨 Alert — urgente drietoon ──────────────────────────────────────────────
function playAlert(ctx, volume) {
const t = ctx.currentTime;
tone(ctx, 523, t, 0.15, volume, 'square');
tone(ctx, 659, t + 0.18, 0.15, volume, 'square');
tone(ctx, 784, t + 0.36, 0.25, volume, 'square');
}
// ── 🎵 Chime — zachte melodietoon ────────────────────────────────────────────
function playChime(ctx, volume) {
const t = ctx.currentTime;
tone(ctx, 523, t, 0.6, volume * 0.8, 'sine');
tone(ctx, 659, t + 0.15, 0.6, volume * 0.6, 'sine');
tone(ctx, 784, t + 0.30, 0.8, volume * 0.5, 'sine');
}