comparison game/js/audio.js @ 4:2d46729a022e

sound processing and in-game sounds
author ark
date 2026-10-18 20:58 +0200
parents
children 910b26feca76
comparison
equal deleted inserted replaced
3:cc0c32e71090 4:2d46729a022e
1 // sound: recorded sounds from data/sounds.js, placeholder beeps for events
2 // without a recording
3
4 window.MM = window.MM || {};
5
6 MM.audio = (function () {
7 "use strict";
8
9 var cfg = MM.config.sound;
10 var ctx = null, master = null;
11 var buffers = {}; // event -> decoded recordings
12 var last = {}; // event -> audio clock time of its last play
13 var ends = {}; // event -> audio clock time when its sounds have ended
14
15 // create the audio context and decode the recordings. the browser keeps
16 // the context suspended until unlock() runs in a user interaction.
17 function init() {
18 var AC = window.AudioContext || window.webkitAudioContext;
19 if (!AC || !cfg.enabled) return;
20 ctx = new AC();
21 master = ctx.createGain();
22 master.gain.value = cfg.volume;
23 master.connect(ctx.destination);
24 var sounds = MM.sounds || {};
25 Object.keys(sounds).forEach(function (name) {
26 buffers[name] = [];
27 sounds[name].forEach(function (b64) { decode(name, b64); });
28 });
29 }
30
31 function decode(name, b64) {
32 var bin = atob(b64), bytes = new Uint8Array(bin.length);
33 for (var i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
34 // the callback form, as older safari has no promise form
35 ctx.decodeAudioData(bytes.buffer,
36 function (buf) { buffers[name].push(buf); },
37 function () { console.error("sound not decoded:", name); });
38 }
39
40 // call from a pointer or touch handler: browsers start sound only there
41 function unlock() {
42 if (!ctx || ctx.state === "running") return;
43 ctx.resume();
44 // older safari also needs a sound started in the handler
45 var src = ctx.createBufferSource();
46 src.buffer = ctx.createBuffer(1, 1, 22050);
47 src.connect(ctx.destination);
48 src.start(0);
49 }
50
51 // notes of [frequency, seconds] from when on, into out. returns the
52 // total length.
53 function beep(notes, out, when) {
54 var t = when;
55 notes.forEach(function (n) {
56 var osc = ctx.createOscillator(), env = ctx.createGain();
57 osc.type = cfg.beep_wave;
58 osc.frequency.value = n[0];
59 env.gain.setValueAtTime(1, t);
60 env.gain.exponentialRampToValueAtTime(0.001, t + n[1]);
61 osc.connect(env);
62 env.connect(out);
63 osc.start(t);
64 osc.stop(t + n[1]);
65 t += n[1];
66 });
67 return t - when;
68 }
69
70 // play a random recording of the event, or its beep if it has none.
71 // after lists events whose sounds still playing are let finish first.
72 // variants are picked with Math.random, so sound does not change the
73 // game's seeded random numbers.
74 function play(name, after) {
75 if (!ctx || ctx.state !== "running") return;
76 var ev = cfg.event[name];
77 if (!ev) return;
78 var now = ctx.currentTime;
79 if (ev.min_interval && now - last[name] < ev.min_interval) return;
80 last[name] = now;
81 var when = now;
82 (after || []).forEach(function (a) {
83 if (ends[a] > when) when = ends[a];
84 });
85 var gain = ctx.createGain();
86 gain.gain.value = ev.volume;
87 gain.connect(master);
88 var list = buffers[name], length;
89 if (list && list.length) {
90 var src = ctx.createBufferSource();
91 src.buffer = list[Math.floor(Math.random() * list.length)];
92 src.connect(gain);
93 src.start(when);
94 length = src.buffer.duration;
95 } else if (ev.beep) {
96 length = beep(ev.beep, gain, when);
97 } else {
98 return;
99 }
100 ends[name] = Math.max(ends[name] || 0, when + length);
101 }
102
103 return { init: init, unlock: unlock, play: play };
104 })();