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