-
Notifications
You must be signed in to change notification settings - Fork 66
/
index.html
355 lines (346 loc) · 18.2 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>PixiJS Sound | Basics</title>
<meta name='viewport' content='width=device-width, initial-scale=1.0, user-scalable=no'>
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,700" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="css/examples.css?v=2">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.0/codemirror.min.css" integrity="sha512-CCnciBUnVXwa6IQT9q8EmGcarNit9GdKI5nJnj56B1iu0LuD13Qn/GZ+IUkrZROZaBdutN718NK6mIXdUjZGqg==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.0/theme/monokai.min.css" integrity="sha512-R6PH4vSzF2Yxjdvb2p2FA06yWul+U0PDDav4b/od/oXf9Iw37zl10plvwOXelrjV2Ai7Eo3vyHeyFUjhXdBCVQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.0/codemirror.min.js" integrity="sha512-JpMCZgesTWh1iu/8ujURbwkJBgbgFWe3sTNCHdIuEvPwZuuN0nTUr2yawXahpgdEK7FOZUlW254Rp7AyDYJdjg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.0/addon/display/autorefresh.min.js" integrity="sha512-vAsKB7xXQAWMn5kcwda0HkFVKUxSYwrmrGprVhmbGFNAG1Ij+2epT3zzdwjHTJyDsKXsiEdrUdhIxh7loHyX+A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.0/mode/javascript/javascript.min.js" integrity="sha512-DJ/Flq7rxJDDhgkO49H/rmidX44jmxWot/ku3c+XXEF9XFal78KIpu7w6jEaQhK4jli1U3/yOH+Rp3cIIEYFPQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdn.rawgit.com/aidanlister/jquery-stickytabs/1.2.1/jquery.stickytabs.js"></script>
<script src="https://pixijs.download/dev/pixi.min.js"></script>
<script src="../dist/pixi-sound.js"></script>
</head>
<body>
<div class="container-fluid frame">
<h1><img src="resources/logo.svg" class="logo" alt="PixiJS" />Sound <small>Basics</small></h1>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<ul class="nav navbar-nav">
<li class="active"><a href="index.html">Basics</a></li>
<li><a href="sprites.html">Sprites</a></li>
<li><a href="filters.html">Filters</a></li>
<li><a href="demo.html">Demo</a></li>
<li><a href="../docs/index.html">API Docs</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a class="github" href="https://github.com/pixijs/pixi-sound">
<img src="resources/github.svg"> Github
</a></li>
</ul>
</div>
</nav>
<div class="row">
<nav class="col-sm-2">
<ul class="section-tabs nav nav-pills nav-stacked">
<li class="active"><a data-toggle="tab" href="#section-playing">Playing</a></li>
<li><a data-toggle="tab" href="#section-preloading">Preloading</a></li>
<li><a data-toggle="tab" href="#section-filetypes">Filetypes</a></li>
<li><a data-toggle="tab" href="#section-volume">Volume</a></li>
<li><a data-toggle="tab" href="#section-events">Events</a></li>
<li><a data-toggle="tab" href="#section-library">Library</a></li>
<li><a data-toggle="tab" href="#section-utils">Utilities</a></li>
<li><a data-toggle="tab" href="#section-stream">Stream</a></li>
</ul>
</nav>
<main class="col-sm-10">
<div class="tab-content">
<div class="tab-pane fade in active" id="section-playing">
<h3>Playing</h3>
<p>Creation of a sound is simple. Normally <code>play</code> returns the SoundInstance, but in this case it does not because the sound is not preloaded.</p>
<button class="btn btn-danger btn-lg pull-right play" data-code="#play">
<span class="glyphicon glyphicon-play"></span> Run
</button>
<pre><code id="play" class="javascript">const sound = PIXI.sound.Sound.from('resources/boing.mp3');
sound.play();</code></pre>
<p>To start playing the sound immediately after load is finished use the <code>autoPlay</code> flag.</p>
<button class="btn btn-danger btn-lg pull-right play" data-code="#autoplay">
<span class="glyphicon glyphicon-play"></span> Run
</button>
<pre><code id="autoplay" class="javascript">PIXI.sound.Sound.from({
url: 'resources/mechanical.mp3',
autoPlay: true,
complete: function() {
console.log('Sound finished');
}
});</code></pre>
<p>Globally, you can also use the <code>PIXI.sound</code> library methods (<code>add</code>, <code>play</code>) for adding and playing a sound as well.</p>
<button class="btn btn-danger btn-lg pull-right play" data-code="#autoplay-global">
<span class="glyphicon glyphicon-play"></span> Run
</button>
<pre><code id="autoplay-global" class="javascript">PIXI.sound.add('bird', 'resources/bird.mp3');
PIXI.sound.play('bird');</code></pre>
<p>You can set options when playing a sound</p>
<button class="btn btn-danger btn-lg pull-right play" data-code="#play-options">
<span class="glyphicon glyphicon-play"></span> Run
</button>
<pre><code id="play-options" class="javascript">PIXI.sound.Sound.from({
url: 'resources/bird.mp3',
preload: true,
loaded: function(err, sound) {
sound.play({
volume: 0.1,
start: 0,
end: 1,
});
}
});</code></pre>
<p>You can also set filters for different instances, in this demo you will hear two birds sounds, one is the left, one on the right.</p>
<button class="btn btn-danger btn-lg pull-right play" data-code="#play-options-filters">
<span class="glyphicon glyphicon-play"></span> Run
</button>
<pre><code id="play-options-filters" class="javascript">PIXI.sound.Sound.from({
url: 'resources/bird.mp3',
preload: true,
loaded: function(err, sound) {
// sound.filters = [
// new PIXI.sound.filters.TelephoneFilter(),
// ];
sound.volume = 0.5;
sound.play({
filters: [
new PIXI.sound.filters.StereoFilter(-1),
],
});
setTimeout(function () {
sound.play({
filters: [
new PIXI.sound.filters.StereoFilter(1),
],
});
}, 50);
}
});</code></pre>
</div>
<div class="tab-pane fade" id="section-filetypes">
<h3>Filetypes</h3>
<p>Not all web-browsers support different file extensions for audio playback. Setup fallback formats easily by useing the a glob-like expression for the file extension. This will prioritize format from left to right.</p>
<button class="btn btn-danger btn-lg pull-right play" data-code="#filetypes1">
<span class="glyphicon glyphicon-play"></span> Run
</button>
<pre><code id="filetypes1" class="javascript">PIXI.Assets.add({ alias: 'applause', src: 'resources/applause.{ogg,mp3}' });
PIXI.Assets.load('applause').then(sound => sound.play());</code></pre>
<p>Alternatively, if you don't plan to use Assets (typically because you're using a bundler like Parcel or Webpack), but you still would like to resolve multiple files, you can choose from multiple URLs.</p>
<button class="btn btn-danger btn-lg pull-right play" data-code="#filetypes2">
<span class="glyphicon glyphicon-play"></span> Run
</button>
<pre><code id="filetypes2" class="javascript">const sound = PIXI.sound.add('applause', [
'resources/applause.ogg',
'resources/applause.mp3'
]);
sound.play();</code></pre>
</div>
<div class="tab-pane fade" id="section-preloading">
<h3>Preloading</h3>
<p>Preloading a sound can be enabled by passing in the <code>preload</code> flag. You can listen for when the sound is loaded by using the <code>loaded</code> callback.</p>
<button class="btn btn-danger btn-lg pull-right play" data-code="#loaded">
<span class="glyphicon glyphicon-play"></span> Run
</button>
<pre><code id="loaded" class="javascript">PIXI.sound.Sound.from({
url: 'resources/bird.mp3',
preload: true,
loaded: function(err, sound) {
sound.play();
}
});</code></pre>
<p>PIXI's loader can be used to load Sound assets. This returns the Sound object on the Resource's <code>sound</code> property.</p>
<button class="btn btn-danger btn-lg pull-right play" data-code="#loaded2">
<span class="glyphicon glyphicon-play"></span> Run
</button>
<pre><code id="loaded2" class="javascript">PIXI.Assets.add({ alias: 'bird', src: 'resources/bird.mp3' });
PIXI.Assets.load('bird').then(sound => sound.play());</code></pre>
</div>
<div class="tab-pane fade" id="section-volume">
<h3>Volume</h3>
<p>Volume can be set initially by using the object constructor</p>
<button class="btn btn-danger btn-lg pull-right play" data-code="#volume">
<span class="glyphicon glyphicon-play"></span> Run
</button>
<pre><code id="volume" class="javascript">const sound = PIXI.sound.Sound.from({
url: 'resources/boing.mp3',
volume: 0.25
});
sound.play();</code></pre>
<p>Volume can also be set by changing the <code>volume</code> property.</p>
<button class="btn btn-danger btn-lg pull-right play" data-code="#volume2">
<span class="glyphicon glyphicon-play"></span> Run
</button>
<pre><code id="volume2" class="javascript">const sound = PIXI.sound.Sound.from('resources/boing.mp3');
sound.volume = 0.25;
sound.play();</code></pre>
</div>
<div class="tab-pane fade" id="section-events">
<h3>Events</h3>
<p>Preloaded sounds return a SoundInstance object which can be used to listen to playback events such as a the progress and end.</p>
<button class="btn btn-danger btn-lg pull-right play" data-code="#progress">
<span class="glyphicon glyphicon-play"></span> Run
</button>
<pre><code id="progress" class="javascript">PIXI.sound.Sound.from({
url: 'resources/boing.mp3',
preload: true,
loaded: function(err, sound) {
const instance = sound.play();
instance.on('progress', function(progress) {
console.log('Amount played: ', Math.round(progress * 100) + '%');
});
instance.on('end', function() {
console.log('Sound finished playing');
});
}
});</code></pre>
</div>
<div class="tab-pane fade" id="section-library">
<h3>Library</h3>
<p>Library is the simplest way to create a cache of sounds.</p>
<button class="btn btn-danger btn-lg pull-right play" data-code="#library-simple">
<span class="glyphicon glyphicon-play"></span> Run
</button>
<pre><code id="library-simple" class="javascript">PIXI.sound.add('boing', 'resources/boing.mp3');
PIXI.sound.play('boing');</code></pre>
<p>Library utilities are available to help manage the cache of sounds.</p>
<button class="btn btn-danger btn-lg pull-right play" data-code="#library-map">
<span class="glyphicon glyphicon-play"></span> Run
</button>
<pre><code id="library-map" class="javascript">PIXI.sound.add({
boing: 'resources/boing.mp3',
bird: 'resources/bird.mp3',
mechanical: 'resources/mechanical.mp3'
});
PIXI.sound.play('boing');
PIXI.sound.play('bird');
PIXI.sound.play('mechanical');</code></pre>
<p>Couple of convenience methods (<code>find</code>, <code>exist</code>, <code>duration</code>) for manipulating and checking sounds.</p>
<button class="btn btn-danger btn-lg pull-right play" data-code="#library-exists">
<span class="glyphicon glyphicon-play"></span> Run
</button>
<pre><code id="library-exists" class="javascript">PIXI.sound.add('boing', {
url: 'resources/boing.mp3',
preload: true,
loaded: function() {
// duration can only be used once the sound is loaded
console.log('Duration: ', PIXI.sound.duration('boing'), 'seconds');
}
});
console.log('Exists?', PIXI.sound.exists('boing'));
console.log('Sound: ', PIXI.sound.find('boing'));</code></pre>
</div>
<div class="tab-pane fade" id="section-utils">
<h3>Utilities</h3>
<p>First setup an Application to render the waveform.</p>
<code class="hidden" id="render-clear">waveform.stage.removeChildren();</code>
<canvas class="canvas" width="1024" height="128" id="waveform"></canvas>
<pre><code data-autorun class="javascript">window.waveform = new PIXI.Application();
window.waveform.init({
width: 1024,
height: 128,
canvas: document.getElementById('waveform'),
backgroundColor: 0xffffff
});
</code></pre>
<button class="btn btn-danger btn-lg pull-right play" data-code="#render3" data-beforeCode="#render-clear">
<span class="glyphicon glyphicon-play"></span> Run
</button>
<pre><code id="render3" class="javascript">PIXI.Assets.add({ alias: 'applause', src: 'resources/applause.mp3' });
const sound = await PIXI.Assets.load('applause');
const baseTexture = PIXI.sound.utils.render(sound, {
width: waveform.renderer.width,
height: waveform.renderer.height,
fill: '#999'
});
const playhead = new PIXI.Graphics()
.rect(0, 0, 1, waveform.renderer.height)
.fill(0xff0000);
const sprite = new PIXI.Sprite(
new PIXI.Texture(baseTexture)
);
waveform.stage.addChild(sprite, playhead);
sound.play().on('progress', function(value) {
playhead.x = baseTexture.width * value;
});
</code></pre>
<p>Create a simple sine-wave tone.</p>
<button class="btn btn-danger btn-lg pull-right play" data-code="#sine-tone" data-beforeCode="#render-clear">
<span class="glyphicon glyphicon-play"></span> Run
</button>
<pre><code id="sine-tone" class="javascript">const sound = PIXI.sound.utils.sineTone(523.251, 3);
sound.play();</code></pre>
<p>There's a utility method for playing a sound once. It is immediately destroyed and after this.</p>
<button class="btn btn-danger btn-lg pull-right play" data-code="#autoplay2">
<span class="glyphicon glyphicon-play"></span> Run
</button>
<pre><code id="autoplay2" class="javascript">PIXI.sound.utils.playOnce('resources/mechanical.mp3', function() {
console.log('Sound finished');
});</code></pre>
</div>
<div class="tab-pane fade" id="section-stream">
<h3>Stream</h3>
<p>You can get the audio stream by <code>StreamFilter</code></p>
<button class="btn btn-danger btn-lg pull-right play" data-code="#stream">
<span class="glyphicon glyphicon-play"></span> Run
</button>
<pre><code id="stream" class="javascript">const stream = new MediaStream();
const recorder = new MediaRecorder(stream);
const streamFilter = new PIXI.sound.filters.StreamFilter();
// attach audio stream
streamFilter.stream.getAudioTracks().forEach(function (track) {
stream.addTrack(track);
});
// process recorded data
recorder.ondataavailable = function (e) {
if (e.data.size == 0) {
console.error('no data available');
return;
}
const blob = new Blob([e.data], {
type: recorder.mimeType,
});
// You can download the audio file or upload the Blob to server
const url = URL.createObjectURL(blob);
PIXI.sound.filtersAll = []; // remove filters for recorded sound
PIXI.sound.Sound.from({
url: url,
autoPlay: true,
});
};
// apply filters
PIXI.sound.filtersAll = [
new PIXI.sound.filters.TelephoneFilter(),
new PIXI.sound.filters.StereoFilter(),
// You need to place StreamFilter at the end of filters list,
// so that the stream will have the effect of other filters.
streamFilter,
];
// load and play sound
PIXI.sound.Sound.from({
url: 'resources/boing.mp3',
autoPlay: true,
loaded: function () {
recorder.start();
},
complete: function () {
recorder.stop();
}
});</code></pre>
</div>
</div>
</main>
</div>
<script src="./client/index.js"></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-103772589-1', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>