diff game/js/audio.js @ 32:85d23328a215 0.10.0

added intro music, logic for browser audio triggering on splash screen
author ark
date 2026-10-20 18:39 +0200
parents fe680946164b
children
line wrap: on
line diff
--- a/game/js/audio.js	Tue Oct 20 17:58:10 2026 +0200
+++ b/game/js/audio.js	Tue Oct 20 18:39:22 2026 +0200
@@ -1,5 +1,5 @@
 // sound: recorded sounds from data/sounds.js, placeholder beeps for events
-// without a recording. the player's sound on or off choice is stored with
+// without a recording, and looping music for the splash screen. the player's sound on or off choice is stored with
 // MM.storage; the config's enabled setting applies until the first choice.
 
 window.MM = window.MM || {};
@@ -13,6 +13,9 @@
     var last = {};      // event -> audio clock time of its last play
     var ends = {};      // event -> audio clock time when its sounds have ended
     var started = false;
+    // the looping music: {name, src, gain}, src null while its recording is
+    // still being decoded
+    var music = null;
     var on = MM.storage.get("sound", cfg.enabled);
 
     // create the audio context and decode the recordings. the browser keeps
@@ -36,7 +39,10 @@
         for (var i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
         // the callback form, as older safari has no promise form
         ctx.decodeAudioData(bytes.buffer,
-            function (buf) { buffers[name].push(buf); },
+            function (buf) {
+                buffers[name].push(buf);
+                if (music && music.name === name && !music.src) startMusic();
+            },
             function () { console.error("sound not decoded:", name); });
     }
 
@@ -110,6 +116,38 @@
         ends[name] = Math.max(ends[name] || 0, when + length);
     }
 
+    // play the event's first recording in a loop until stopMusic(). does
+    // nothing while sound is off. on a context not yet unlocked, the music
+    // starts with the unlock; before its recording is decoded, once it is.
+    function playMusic(name) {
+        if (!on || !ctx || music || !cfg.event[name]) return;
+        music = { name: name, src: null, gain: null };
+        if (buffers[name] && buffers[name].length) startMusic();
+    }
+
+    function startMusic() {
+        music.gain = ctx.createGain();
+        music.gain.gain.value = cfg.event[music.name].volume;
+        music.gain.connect(master);
+        music.src = ctx.createBufferSource();
+        music.src.buffer = buffers[music.name][0];
+        music.src.loop = true;
+        music.src.connect(music.gain);
+        music.src.start(ctx.currentTime);
+    }
+
+    // fade the music out over the music_fade setting and stop it
+    function stopMusic() {
+        if (!music) return;
+        if (music.src) {
+            var now = ctx.currentTime;
+            music.gain.gain.setValueAtTime(music.gain.gain.value, now);
+            music.gain.gain.linearRampToValueAtTime(0, now + cfg.music_fade);
+            music.src.stop(now + cfg.music_fade);
+        }
+        music = null;
+    }
+
     function enabled() {
         return on;
     }
@@ -122,5 +160,6 @@
         MM.storage.set("sound", on);
     }
 
-    return { init: init, start: start, unlock: unlock, play: play, enabled: enabled, toggle: toggle };
+    return { init: init, start: start, unlock: unlock, play: play, playMusic: playMusic,
+             stopMusic: stopMusic, enabled: enabled, toggle: toggle };
 })();