forked from Schnark/js13kgames-2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound.js
80 lines (74 loc) · 1.54 KB
/
sound.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//based on https://github.com/foumart/JS.13kGames/blob/master/lib/SoundFX.js
var sound, ac, muted = false;
function playSound (freq, incr, delay, times, vol, type) {
var i = 0, osc, g, interval;
function stop () {
clearInterval(interval);
osc.stop();
}
function internalPlay () {
osc.frequency.value = freq + incr * i;
g.gain.value = (1 - (i / times)) * vol;
i++;
if (i > times) {
setTimeout(stop, delay);
}
}
if (!ac) {
return;
}
osc = ac.createOscillator();
g = ac.createGain();
osc.connect(g);
g.connect(ac.destination);
osc.frequency.value = freq;
osc.type = ['square', 'sawtooth', 'triangle', 'sine'][type || 0];
g.gain.value = 0;
osc.start();
interval = setInterval(internalPlay, delay);
}
sound = {
init: function () {
var AC = window.AudioContext;
if (!AC) {
return;
}
ac = new AC();
},
setMuted: function (onoff) {
muted = onoff;
},
play: function (sound) {
if (muted) {
return;
}
switch (sound) {
case 'move':
playSound(100, -10, 15, 15, 0.7, 2);
break;
case 'turn':
playSound(260, -60, 15, 15, 0.4, 2);
break;
case 'take':
playSound(220, 15, 15, 15, 0.3, 2);
break;
case 'drop':
playSound(440, -15, 15, 15, 0.3, 2);
break;
case 'drop-final':
playSound(510, 0, 15, 20, 0.05);
break;
case 'error':
playSound(440, -15, 15, 15, 0.5);
setTimeout(function () {
playSound(100, -10, 10, 25, 0.5);
}, 300);
break;
case 'win':
playSound(510, 0, 15, 20, 0.1);
setTimeout(function () {
playSound(2600, 1, 10, 50, 0.2);
}, 80);
}
}
};