-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsounds.js
More file actions
102 lines (80 loc) · 2.6 KB
/
sounds.js
File metadata and controls
102 lines (80 loc) · 2.6 KB
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
/*
Web Audio API
Based on code samples from the page "Getting Started with Web Audio API"
by Boris Smus at http://www.html5rocks.com/en/tutorials/webaudio/intro/ .
Code samples licensed under the Apache 2.0 License.
*/
/*jslint browser, devel */
/*property
AudioContext, buffer, connect, createBuffer, createBufferSource,
decodeAudioData, destination, onload, open, response, responseType, send,
start, webkitAudioContext
*/
let lockBuffer = null;
let mistakeBuffer = null;
let pagefumbleBuffer = null;
let sparksBuffer = null;
let tingBuffer = null;
let lockReady = false;
let mistakeReady = false;
let pagefumbleReady = false;
let sparksReady = false;
let tingReady = false;
//window.AudioContext = window.AudioContext || window.webkitAudioContext;
//let context = new AudioContext();
let AudioCtx = window.AudioContext || window.webkitAudioContext;
let context = new AudioCtx();
function onError(e) {
alert("sounds:onError: " + e);
}
function loadBuffer(url, callback) {
let request = new XMLHttpRequest();
request.open("GET", url, true);
request.responseType = "arraybuffer";
request.onload = function () {
context.decodeAudioData(request.response, function (buffer) {
callback(buffer);
}, onError);
};
request.send();
}
function initSound() {
const lockURL = "Resources/Sounds/lock.mp3";
const mistakeURL = "Resources/Sounds/mistake.mp3";
const pagefumbleURL = "Resources/Sounds/pagefumble.mp3";
const sparksURL = "Resources/Sounds/sparks.mp3";
const tingURL = "Resources/Sounds/ting.mp3";
loadBuffer(lockURL, function (buffer) {
lockBuffer = buffer;
lockReady = true;
});
loadBuffer(mistakeURL, function (buffer) {
mistakeBuffer = buffer;
mistakeReady = true;
});
loadBuffer(pagefumbleURL, function (buffer) {
pagefumbleBuffer = buffer;
pagefumbleReady = true;
});
loadBuffer(sparksURL, function (buffer) {
sparksBuffer = buffer;
sparksReady = true;
});
loadBuffer(tingURL, function (buffer) {
tingBuffer = buffer;
tingReady = true;
});
}
function playSound(buffer) {
let source = context.createBufferSource();
source.buffer = buffer;
source.connect(context.destination);
source.start(0);
}
function unlockSound() {
let buffer = context.createBuffer(1, 1, 22050);
playSound(buffer);
}
export {initSound, playSound, unlockSound};
export {lockBuffer, mistakeBuffer, pagefumbleBuffer, sparksBuffer, tingBuffer};
export {lockReady, mistakeReady, pagefumbleReady, sparksReady, tingReady};