-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathindex.html
238 lines (203 loc) · 5.6 KB
/
index.html
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Sound.js demonstration</title>
<script src="sound.js"></script>
</head>
<body>
<p>Press keyboard keys to control the sounds</p>
<p>Sounds from loaded sound files:
<ul>
<li>a - Shoot sound</li>
<li>b - Play music</li>
<li>c - Pause music</li>
<li>d - Restart music</li>
<li>e - Play music from the 10 second point</li>
<li>f - Play the bounce sound with an echo effect</li>
<li>g - Fade the music out over 3 seconds</li>
<li>h - Fade the music in over 3 seconds</li>
</ul>
</p>
<p>Generated sound effects
<ul>
<li>i - Shoot</li>
<li>j - Jump</li>
<li>k - Explosion </li>
<li>l - Bonus</li>
</ul>
</p>
<p>
<a href="javascript:sounds['sounds/bounce.mp3'].play();">hyperlink trigger (bounce)<a/>
<button id="btn_some_trigger">Button Trigger (bounce + bonus)</button>
</p>
<script>
/*
Part 1 - Working with sound files
=================================
*/
//Load the sounds
sounds.load([
"sounds/shoot.wav",
"sounds/music.wav",
"sounds/bounce.mp3"
]);
//Assign the callback function that should run
//when the sounds have loaded
sounds.whenLoaded = setup;
function setup() {
console.log("sounds loaded");
//Create the sounds
var shoot = sounds["sounds/shoot.wav"],
music = sounds["sounds/music.wav"],
bounce = sounds["sounds/bounce.mp3"];
//Pan the shoot sound to the right
shoot.pan = 0.8;
//Make the music loop
music.loop = true;
//Set the pan to the left
music.pan = -0.8;
//Set the music volume
music.volume = 0.7;
//Set a reverb effect on the bounce sound
//arguments: duration, decay, reverse?
//music.setReverb(2, 2, false);
//Set the sound's `reverb` property to `false` to turn it off
//music.reverb = false;
//Add an echo effect to the bounce sound
//arguments: delay time, feedback time, optional frequency filtering
bounce.setEcho(0.2, 0.3, 1000);
//Set `echo` to false to turn it off
//bounce.echo = false;
//Optionally set the music playback rate to half speed
//music.playbackRate = 0.5;
//Capture the keyboard events
var a = keyboard(65),
b = keyboard(66),
c = keyboard(67),
d = keyboard(68),
e = keyboard(69),
f = keyboard(70);
g = keyboard(71);
h = keyboard(72);
//Control the sounds based on which keys are pressed
//Play the loaded shoot sound
a.press = function() { shoot.play() };
//Play the loaded music sound
b.press = function() {
if (!music.playing) {
music.play();
}
console.log("music playing");
};
//Pause the music
c.press = function() {
music.pause();
console.log("music paused");
};
//Restart the music
d.press = function() {
music.restart();
console.log("music restarted");
};
//Play the music from the 10 second mark
e.press = function() {
music.playFrom(10);
console.log("music start point changed");
};
//Play the bounce sound
f.press = function() { bounce.play() };
//Fade the music out over 3 seconds
g.press = function() {
music.fadeOut(3);
};
//Fade the music in over 3 seconds
h.press = function() {
music.fadeIn(3);
};
document.getElementById("btn_some_trigger").addEventListener('click', function(){
//You can even conditionally apply more effects here, prior to playing sound(s)
sounds["sounds/bounce.mp3"].play();
//Note the different "style" of invocation for pure effects generated sounds:
bonusSound();
});
}
/*
Part 2 - Working with sound effects
===================================
*/
var i = keyboard(73),
j = keyboard(74),
k = keyboard(75),
l = keyboard(76);
i.press = function(){ shootSound() };
j.press = function(){ jumpSound() };
k.press = function(){ explosionSound() };
l.press = function(){ bonusSound() };
//The sound effect functions
//The shoot sound
function shootSound() {
soundEffect(
1046.5, //frequency
0, //attack
0.3, //decay
"sawtooth", //waveform
1, //Volume
-0.8, //pan
0, //wait before playing
1200, //pitch bend amount
false, //reverse bend
0, //random pitch range
25, //dissonance
[0.2, 0.2, 2000], //echo: [delay, feedback, filter]
undefined //reverb: [duration, decay, reverse?]
);
}
//The jump sound
function jumpSound() {
soundEffect(
523.25, //frequency
0.05, //attack
0.2, //decay
"sine", //waveform
3, //volume
0.8, //pan
0, //wait before playing
600, //pitch bend amount
true, //reverse
100, //random pitch range
0, //dissonance
undefined, //echo: [delay, feedback, filter]
undefined //reverb: [duration, decay, reverse?]
);
}
//The explosion sound
function explosionSound() {
soundEffect(
16, //frequency
0, //attack
1, //decay
"sawtooth", //waveform
1, //volume
0, //pan
0, //wait before playing
0, //pitch bend amount
false, //reverse
0, //random pitch range
50, //dissonance
undefined, //echo: [delay, feedback, filter]
undefined //reverb: [duration, decay, reverse?]
);
}
//The bonus points sound
function bonusSound() {
//D
soundEffect(587.33, 0, 0.2, "square", 1, 0, 0);
//A
soundEffect(880, 0, 0.2, "square", 1, 0, 0.1);
//High D
soundEffect(1174.66, 0, 0.3, "square", 1, 0, 0.2);
}
</script>
</body>
</html>