wobblyhead hg > minor_monsters
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 31:d1ae50439686 | 32:85d23328a215 |
|---|---|
| 1 // sound: recorded sounds from data/sounds.js, placeholder beeps for events | 1 // sound: recorded sounds from data/sounds.js, placeholder beeps for events |
| 2 // without a recording. the player's sound on or off choice is stored with | 2 // without a recording, and looping music for the splash screen. the player's sound on or off choice is stored with |
| 3 // MM.storage; the config's enabled setting applies until the first choice. | 3 // MM.storage; the config's enabled setting applies until the first choice. |
| 4 | 4 |
| 5 window.MM = window.MM || {}; | 5 window.MM = window.MM || {}; |
| 6 | 6 |
| 7 MM.audio = (function () { | 7 MM.audio = (function () { |
| 11 var ctx = null, master = null; | 11 var ctx = null, master = null; |
| 12 var buffers = {}; // event -> decoded recordings | 12 var buffers = {}; // event -> decoded recordings |
| 13 var last = {}; // event -> audio clock time of its last play | 13 var last = {}; // event -> audio clock time of its last play |
| 14 var ends = {}; // event -> audio clock time when its sounds have ended | 14 var ends = {}; // event -> audio clock time when its sounds have ended |
| 15 var started = false; | 15 var started = false; |
| 16 // the looping music: {name, src, gain}, src null while its recording is | |
| 17 // still being decoded | |
| 18 var music = null; | |
| 16 var on = MM.storage.get("sound", cfg.enabled); | 19 var on = MM.storage.get("sound", cfg.enabled); |
| 17 | 20 |
| 18 // create the audio context and decode the recordings. the browser keeps | 21 // create the audio context and decode the recordings. the browser keeps |
| 19 // the context suspended until unlock() runs in a user interaction. | 22 // the context suspended until unlock() runs in a user interaction. |
| 20 function init() { | 23 function init() { |
| 34 function decode(name, b64) { | 37 function decode(name, b64) { |
| 35 var bin = atob(b64), bytes = new Uint8Array(bin.length); | 38 var bin = atob(b64), bytes = new Uint8Array(bin.length); |
| 36 for (var i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i); | 39 for (var i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i); |
| 37 // the callback form, as older safari has no promise form | 40 // the callback form, as older safari has no promise form |
| 38 ctx.decodeAudioData(bytes.buffer, | 41 ctx.decodeAudioData(bytes.buffer, |
| 39 function (buf) { buffers[name].push(buf); }, | 42 function (buf) { |
| 43 buffers[name].push(buf); | |
| 44 if (music && music.name === name && !music.src) startMusic(); | |
| 45 }, | |
| 40 function () { console.error("sound not decoded:", name); }); | 46 function () { console.error("sound not decoded:", name); }); |
| 41 } | 47 } |
| 42 | 48 |
| 43 // call from the play button's handler. sound stays off until then. | 49 // call from the play button's handler. sound stays off until then. |
| 44 function start() { | 50 function start() { |
| 108 return; | 114 return; |
| 109 } | 115 } |
| 110 ends[name] = Math.max(ends[name] || 0, when + length); | 116 ends[name] = Math.max(ends[name] || 0, when + length); |
| 111 } | 117 } |
| 112 | 118 |
| 119 // play the event's first recording in a loop until stopMusic(). does | |
| 120 // nothing while sound is off. on a context not yet unlocked, the music | |
| 121 // starts with the unlock; before its recording is decoded, once it is. | |
| 122 function playMusic(name) { | |
| 123 if (!on || !ctx || music || !cfg.event[name]) return; | |
| 124 music = { name: name, src: null, gain: null }; | |
| 125 if (buffers[name] && buffers[name].length) startMusic(); | |
| 126 } | |
| 127 | |
| 128 function startMusic() { | |
| 129 music.gain = ctx.createGain(); | |
| 130 music.gain.gain.value = cfg.event[music.name].volume; | |
| 131 music.gain.connect(master); | |
| 132 music.src = ctx.createBufferSource(); | |
| 133 music.src.buffer = buffers[music.name][0]; | |
| 134 music.src.loop = true; | |
| 135 music.src.connect(music.gain); | |
| 136 music.src.start(ctx.currentTime); | |
| 137 } | |
| 138 | |
| 139 // fade the music out over the music_fade setting and stop it | |
| 140 function stopMusic() { | |
| 141 if (!music) return; | |
| 142 if (music.src) { | |
| 143 var now = ctx.currentTime; | |
| 144 music.gain.gain.setValueAtTime(music.gain.gain.value, now); | |
| 145 music.gain.gain.linearRampToValueAtTime(0, now + cfg.music_fade); | |
| 146 music.src.stop(now + cfg.music_fade); | |
| 147 } | |
| 148 music = null; | |
| 149 } | |
| 150 | |
| 113 function enabled() { | 151 function enabled() { |
| 114 return on; | 152 return on; |
| 115 } | 153 } |
| 116 | 154 |
| 117 // switch sound on or off and store the choice. sounds already playing | 155 // switch sound on or off and store the choice. sounds already playing |
| 120 on = !on; | 158 on = !on; |
| 121 if (master) master.gain.value = on ? cfg.volume : 0; | 159 if (master) master.gain.value = on ? cfg.volume : 0; |
| 122 MM.storage.set("sound", on); | 160 MM.storage.set("sound", on); |
| 123 } | 161 } |
| 124 | 162 |
| 125 return { init: init, start: start, unlock: unlock, play: play, enabled: enabled, toggle: toggle }; | 163 return { init: init, start: start, unlock: unlock, play: play, playMusic: playMusic, |
| 164 stopMusic: stopMusic, enabled: enabled, toggle: toggle }; | |
| 126 })(); | 165 })(); |
