-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathaudio.js
76 lines (70 loc) · 1.41 KB
/
audio.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
import { Howl, Howler } from 'howler';
import spriteLayout from './sprite_layout';
let clipsLoaded = 0;
let container;
let readyFn = () => {};
function load() {
clipsLoaded += 1;
if (clipsLoaded >= 3) {
readyFn();
}
}
export function onReady(fn) {
if (clipsLoaded >= 3) {
fn();
} else {
readyFn = fn;
}
}
spriteLayout.onload = () => {
// 'silence' is a looping sprite that will run continuously to keep
// other audio actions responsive.
container.play('silence');
load();
};
container = new Howl(spriteLayout);
const idents = {
h_ident: new Howl({
src: 'clips/h_ident_better.mp3',
onload: load,
}),
v_ident: new Howl({
src: 'clips/v_ident_better.mp3',
onload: load,
}),
};
export const sounds = {
play(clip, onEnd) {
let id;
switch (clip) {
case 'h_ident':
case 'v_ident':
id = idents[clip].play();
if (onEnd) {
idents[clip].on('end', onEnd, id);
}
return id;
default:
return container.play(clip);
}
},
duration(clip) {
switch (clip) {
case 'h_ident':
case 'v_ident':
return idents[clip].duration() * 1000;
default:
return spriteLayout.sprite[clip][1];
}
},
seek(clip, amount) {
idents[clip].seek(amount / 1000);
},
stop() {
idents.h_ident.stop();
idents.v_ident.stop();
},
volume(value) {
Howler.volume(value);
},
};