wobblyhead hg > minor_monsters
annotate game/js/audio.js @ 14:7fda634d15ad
adjustments to wave logic, tower pivot points
| author | ark |
|---|---|
| date | 2026-10-19 22:50 +0200 |
| parents | 2c4fed8baed9 |
| children |
| rev | line source |
|---|---|
| 4 | 1 // sound: recorded sounds from data/sounds.js, placeholder beeps for events |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
2 // without a recording. the player's sound on or off choice is stored with |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
3 // MM.storage; the config's enabled setting applies until the first choice. |
| 4 | 4 |
| 5 window.MM = window.MM || {}; | |
| 6 | |
| 7 MM.audio = (function () { | |
| 8 "use strict"; | |
| 9 | |
| 10 var cfg = MM.config.sound; | |
| 11 var ctx = null, master = null; | |
| 12 var buffers = {}; // event -> decoded recordings | |
| 13 var last = {}; // event -> audio clock time of its last play | |
| 14 var ends = {}; // event -> audio clock time when its sounds have ended | |
| 6 | 15 var started = false; |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
16 var on = MM.storage.get("sound", cfg.enabled); |
| 4 | 17 |
| 18 // create the audio context and decode the recordings. the browser keeps | |
| 19 // the context suspended until unlock() runs in a user interaction. | |
| 20 function init() { | |
| 21 var AC = window.AudioContext || window.webkitAudioContext; | |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
22 if (!AC) return; |
| 4 | 23 ctx = new AC(); |
| 24 master = ctx.createGain(); | |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
25 master.gain.value = on ? cfg.volume : 0; |
| 4 | 26 master.connect(ctx.destination); |
| 27 var sounds = MM.sounds || {}; | |
| 28 Object.keys(sounds).forEach(function (name) { | |
| 29 buffers[name] = []; | |
| 30 sounds[name].forEach(function (b64) { decode(name, b64); }); | |
| 31 }); | |
| 32 } | |
| 33 | |
| 34 function decode(name, b64) { | |
| 35 var bin = atob(b64), bytes = new Uint8Array(bin.length); | |
| 36 for (var i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i); | |
| 37 // the callback form, as older safari has no promise form | |
| 38 ctx.decodeAudioData(bytes.buffer, | |
| 39 function (buf) { buffers[name].push(buf); }, | |
| 40 function () { console.error("sound not decoded:", name); }); | |
| 41 } | |
| 42 | |
| 6 | 43 // call from the play button's handler. sound stays off until then. |
| 44 function start() { | |
| 45 started = true; | |
| 46 unlock(); | |
| 47 } | |
| 48 | |
| 49 // call from a pointer or touch handler: browsers start sound only there. | |
| 50 // does nothing before start(). | |
| 4 | 51 function unlock() { |
| 6 | 52 if (!ctx || !started || ctx.state === "running") return; |
| 4 | 53 ctx.resume(); |
| 54 // older safari also needs a sound started in the handler | |
| 55 var src = ctx.createBufferSource(); | |
| 56 src.buffer = ctx.createBuffer(1, 1, 22050); | |
| 57 src.connect(ctx.destination); | |
| 58 src.start(0); | |
| 59 } | |
| 60 | |
| 61 // notes of [frequency, seconds] from when on, into out. returns the | |
| 62 // total length. | |
| 63 function beep(notes, out, when) { | |
| 64 var t = when; | |
| 65 notes.forEach(function (n) { | |
| 66 var osc = ctx.createOscillator(), env = ctx.createGain(); | |
| 67 osc.type = cfg.beep_wave; | |
| 68 osc.frequency.value = n[0]; | |
| 69 env.gain.setValueAtTime(1, t); | |
| 70 env.gain.exponentialRampToValueAtTime(0.001, t + n[1]); | |
| 71 osc.connect(env); | |
| 72 env.connect(out); | |
| 73 osc.start(t); | |
| 74 osc.stop(t + n[1]); | |
| 75 t += n[1]; | |
| 76 }); | |
| 77 return t - when; | |
| 78 } | |
| 79 | |
| 80 // play a random recording of the event, or its beep if it has none. | |
| 81 // after lists events whose sounds still playing are let finish first. | |
| 82 // variants are picked with Math.random, so sound does not change the | |
| 83 // game's seeded random numbers. | |
| 84 function play(name, after) { | |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
85 if (!on || !ctx || ctx.state !== "running") return; |
| 4 | 86 var ev = cfg.event[name]; |
| 87 if (!ev) return; | |
| 88 var now = ctx.currentTime; | |
| 89 if (ev.min_interval && now - last[name] < ev.min_interval) return; | |
| 90 last[name] = now; | |
| 91 var when = now; | |
| 92 (after || []).forEach(function (a) { | |
| 93 if (ends[a] > when) when = ends[a]; | |
| 94 }); | |
| 95 var gain = ctx.createGain(); | |
| 96 gain.gain.value = ev.volume; | |
| 97 gain.connect(master); | |
| 98 var list = buffers[name], length; | |
| 99 if (list && list.length) { | |
| 100 var src = ctx.createBufferSource(); | |
| 101 src.buffer = list[Math.floor(Math.random() * list.length)]; | |
| 102 src.connect(gain); | |
| 103 src.start(when); | |
| 104 length = src.buffer.duration; | |
| 105 } else if (ev.beep) { | |
| 106 length = beep(ev.beep, gain, when); | |
| 107 } else { | |
| 108 return; | |
| 109 } | |
| 110 ends[name] = Math.max(ends[name] || 0, when + length); | |
| 111 } | |
| 112 | |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
113 function enabled() { |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
114 return on; |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
115 } |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
116 |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
117 // switch sound on or off and store the choice. sounds already playing |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
118 // are muted at once. |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
119 function toggle() { |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
120 on = !on; |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
121 if (master) master.gain.value = on ? cfg.volume : 0; |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
122 MM.storage.set("sound", on); |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
123 } |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
124 |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
125 return { init: init, start: start, unlock: unlock, play: play, enabled: enabled, toggle: toggle }; |
| 4 | 126 })(); |
