srdusr
aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/keySounds.js
diff options
context:
space:
mode:
Diffstat (limited to 'web/src/keySounds.js')
-rw-r--r--web/src/keySounds.js42
1 files changed, 42 insertions, 0 deletions
diff --git a/web/src/keySounds.js b/web/src/keySounds.js
new file mode 100644
index 0000000..1a20087
--- /dev/null
+++ b/web/src/keySounds.js
@@ -0,0 +1,42 @@
+import { getSettings } from './settings.js';
+
+// Synthesized rather than sampled - a couple of short tones via the Web
+// Audio API instead of shipping/loading actual click-sample audio files,
+// so the feature has zero asset dependencies.
+const THEMES = {
+ click: { freq: 1800, duration: 0.03, type: 'square' },
+ mech: { freq: 180, duration: 0.05, type: 'triangle' },
+};
+
+let audioCtx = null;
+function getContext() {
+ if (!audioCtx) {
+ const AudioContextClass = window.AudioContext || window.webkitAudioContext;
+ audioCtx = new AudioContextClass();
+ }
+ return audioCtx;
+}
+
+// Browsers refuse to start an AudioContext before a user gesture - the
+// first keystroke that reaches here always follows one (focusing/typing
+// into the input), so resuming inline rather than requiring a separate
+// "enable sound" click.
+export function playKeySound() {
+ const { soundTheme } = getSettings();
+ const theme = THEMES[soundTheme];
+ if (!theme) return;
+
+ const ctx = getContext();
+ if (ctx.state === 'suspended') ctx.resume();
+
+ const osc = ctx.createOscillator();
+ const gain = ctx.createGain();
+ osc.type = theme.type;
+ osc.frequency.value = theme.freq;
+ gain.gain.setValueAtTime(0.12, ctx.currentTime);
+ gain.gain.exponentialRampToValueAtTime(0.0001, ctx.currentTime + theme.duration);
+ osc.connect(gain);
+ gain.connect(ctx.destination);
+ osc.start();
+ osc.stop(ctx.currentTime + theme.duration);
+}