-
Notifications
You must be signed in to change notification settings - Fork 0
/
bnplay-audio-api.html
210 lines (177 loc) Β· 5.49 KB
/
bnplay-audio-api.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
<!doctype html>
<html>
<head>
<title>BNPlay - Audio API version</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" href="favicon.png" />
<style type="text/css">
body,
td,
th {
font-family: sans-serif;
}
body {
background-color: #222;
color: #ccc;
}
.text-center {
text-align: center;
}
.btn-ctrl {
background-color: #000;
color: #ccc;
border: 1px solid #888;
padding: 20px 30px;
font-weight: bold;
}
.btn-ctrl-loading {
color: #c60;
border: 1px solid #f80;
}
.btn-ctrl-error {
color: #c00;
border: 1px solid #f00;
}
</style>
<script type="text/javascript">
// Brown Noise Player
class BNP {
static audioCtx = new AudioContext();
static audioData;
static source = null;
static async getSampleFromWeb() {
const response = await fetch(
// This URL has CORS enabled
"https://dmotte.github.io/bnplay/bn-sample.flac",
);
if (!response.ok) throw new Error("HTTP error " + response.status);
return await response.arrayBuffer();
}
static async getSample() {
const dbName = "bnplay-cache",
storeName = "files",
itemName = "bn-sample.flac";
const db = await new Promise((resolve, reject) => {
const req = indexedDB.open(dbName, 1);
req.onerror = (event) => {
reject(new Error(`Error opening IndexedDB database ${dbName}`));
};
req.onupgradeneeded = (event) => {
const db = event.target.result;
if (!db.objectStoreNames.contains(storeName))
db.createObjectStore(storeName);
};
req.onsuccess = (event) => {
resolve(event.target.result);
};
});
const transaction = db.transaction(storeName, "readonly");
const store = transaction.objectStore(storeName);
let buffer = await new Promise((resolve, reject) => {
const req = store.get(itemName);
req.onerror = (event) => {
reject(
new Error(
`Error getting item ${itemName} from store ${storeName} from IndexedDB database ${dbName}`,
),
);
};
req.onsuccess = (event) => {
resolve(event.target.result);
};
});
if (!buffer) {
buffer = await this.getSampleFromWeb();
// We cache the audio file indefinitely because we assume that it
// will never change
const transaction = db.transaction(storeName, "readwrite");
const store = transaction.objectStore(storeName);
store.put(buffer, itemName);
}
return buffer;
}
static async initialize() {
this.audioData = await this.audioCtx.decodeAudioData(
await this.getSample(),
);
}
static isPlaying() {
return this.source !== null;
}
static play() {
if (this.isPlaying()) return;
this.source = new AudioBufferSourceNode(this.audioCtx);
this.source.buffer = this.audioData;
this.source.connect(this.audioCtx.destination);
this.source.loop = true;
this.source.start(0);
}
static stop() {
if (!this.isPlaying()) return;
this.source.stop();
this.source = null;
}
static toggle() {
if (this.isPlaying()) this.stop();
else this.play();
}
}
class UI {
static load() {
const btnToggle = document.getElementById("btnToggle"),
divStatus = document.getElementById("divStatus");
return BNP.initialize()
.then(() => {
btnToggle.classList.remove("btn-ctrl-loading");
btnToggle.removeAttribute("disabled");
this.update();
})
.catch((error) => {
console.log(error);
btnToggle.classList.remove("btn-ctrl-loading");
btnToggle.classList.add("btn-ctrl-error");
divStatus.textContent = error;
btnToggle.value = "ERROR";
});
}
static update() {
const btnToggle = document.getElementById("btnToggle"),
divStatus = document.getElementById("divStatus");
if (BNP.isPlaying()) {
divStatus.textContent = "Playing...";
btnToggle.value = "STOP";
} else {
divStatus.textContent = "Stopped.";
btnToggle.value = "PLAY";
}
}
static toggle() {
BNP.toggle();
this.update();
}
}
</script>
</head>
<body
onload="UI.load()"
onkeypress="if (event.key == ' ') btnToggle.click();"
>
<div class="text-center">
<h1>Brown noise</h1>
<p><i>Audio API version</i></p>
<br />
<div id="divStatus">Please wait...</div>
<br />
<input
type="button"
id="btnToggle"
value="Loading..."
class="btn-ctrl btn-ctrl-loading"
onfocus="this.blur()"
onclick="UI.toggle()"
disabled="disabled"
/>
</div>
</body>
</html>