-
Notifications
You must be signed in to change notification settings - Fork 0
/
OggmentedContent.js
307 lines (288 loc) · 9.92 KB
/
OggmentedContent.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
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
import React, { useContext, useEffect, useState, useRef } from "react";
import AudioContextContext from "./AudioContextContext";
import ClipLoader from "react-spinners/ClipLoader";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faPlay, faStop } from "@fortawesome/free-solid-svg-icons";
import AudioFileLooper from "./AudioFileLooper";
// ReaderDemo states
const [select, loading, ready, playing] = Array.from({ length: 4 }, Symbol);
const OggmentedContent = () => {
const { oggmentedContext } = useContext(AudioContextContext);
const [browser, setBrowser] = useState();
useEffect(() => {
oggmentedContext.nativeVorbisLevel().then((level) => setBrowser(level));
}, [oggmentedContext]);
const differentBrowsersNote = `The content of this page is different depending on which engine is present. Check out the Blink version (Brave/Opera/Chrome) for a demo of its buggy vorbis decoding implementation.`;
const content = {
webkit: (
<p>
<b>
We seem to be running inside a browser that doesn’t natively support
ogg vorbis. This is expected for Safari, and all the iOS browsers.{" "}
</b>
{differentBrowsersNote}
</p>
),
blink: (
<>
<p>
<b>
Oggmented has detected that it’s running in a browser with buggy ogg
vorbis support. This is expected for Browsers using the Blink
engine: Brave, Opera, Chrome, etc.{" "}
</b>{" "}
Blink uses{" "}
<a href="https://bugs.chromium.org/p/chromium/issues/detail?id=456252">
ffmpeg to decode vorbis
</a>
, which has{" "}
<a href="https://trac.ffmpeg.org/ticket/6367">
a bug specific to vorbis
</a>{" "}
where it adds a few extra samples at the end, resulting in a popping
sound if you were to loop some audio that’s meant to sound smooth when
looped. For example with the file produced by{" "}
<code>
sox -nr 44100 triangle.ogg synth .75 triangle 440 gain -20
</code>
:
</p>
<LoopDemo />
</>
),
gecko: (
<p>
<b>
Oggmented has detected that it’s running in a browser that has full
ogg vorbis support. This is expected for Firefox.
</b>{" "}
{differentBrowsersNote}
</p>
),
};
return (
<>
<h2 style={{ textAlign: "center" }}>Ogg Vorbis and your browser</h2>
<p>
Audio capabilities in a browser come from their <i>Engines</i>:{" "}
<a href="https://en.wikipedia.org/wiki/Blink_(browser_engine)#Engine">
Blink
</a>{" "}
is the engine for Brave, Opera, and Chrome.{" "}
<a href="https://en.wikipedia.org/wiki/WebKit">WebKit</a> is the engine
for Safari and all iOS browsers (e.g. Chrome on an iPhone uses Webkit,
not Blink).{" "}
<a href="https://en.wikipedia.org/wiki/Gecko_(software)">Gecko</a> is
the engine for Firefox.
</p>
<br />
<div className="content">{content[browser]}</div>
<ReaderDemo />
<br />
<a href="https://github.com/jfrancos/react-resume/blob/master/src/OggmentedContent.js">
Source for this page
</a>
</>
);
};
const useDecodeAudioData = (filename, audioContext) => {
const [audioBuffer, setAudioBuffer] = useState();
useEffect(() => {
fetch(filename)
.then((response) => response.arrayBuffer())
.then((arrayBuffer) =>
audioContext.decodeAudioData(arrayBuffer, setAudioBuffer)
);
}, [filename, audioContext]);
return audioBuffer;
};
const useReader = (setReaderState) => {
const [readerBuffer, setReaderBuffer] = useState();
const { oggmentedContext } = useContext(AudioContextContext);
const fileSelector = document.createElement("input");
fileSelector.setAttribute("type", "file");
fileSelector.setAttribute("accept", ".ogg");
const handleFiles = ({
target: {
files: [file],
},
}) => {
setReaderState(loading);
fileReader.readAsArrayBuffer(file);
};
fileSelector.addEventListener("change", handleFiles, false);
const fileReader = new FileReader();
fileReader.onload = async ({ target: { result } }) => {
oggmentedContext.decodeAudioData(result, (buffer) => {
setReaderBuffer(buffer);
setReaderState(ready);
});
};
return { fileSelector, readerBuffer };
};
const LoopDemo = () => {
const { oggmentedContext, nativeContext } = useContext(AudioContextContext);
const [runningBuffer, setRunningBuffer] = useState(null);
const nativeTriangleBuffer = useDecodeAudioData(
"/triangle.ogg",
nativeContext
);
const oggmentedTriangleBuffer = useDecodeAudioData(
"/triangle.ogg",
oggmentedContext
);
const togglePlayer = (buffer) => () => {
nativeContext.resume();
oggmentedContext.resume();
setRunningBuffer(runningBuffer === buffer ? null : buffer);
};
useEffect(() => {
if (nativeTriangleBuffer) {
console.log(
"native AudioContext:",
nativeTriangleBuffer.getChannelData(0)
);
}
}, [nativeTriangleBuffer]);
useEffect(() => {
if (oggmentedTriangleBuffer) {
console.log(
"oggmented AudioContext",
oggmentedTriangleBuffer.getChannelData(0)
);
}
}, [oggmentedTriangleBuffer]);
return (
<>
<div
style={{
display: "flex",
width: "100%",
justifyContent: "space-evenly",
flexWrap: "wrap",
}}
>
<div
style={{
display: "flex",
flexDirection: "column",
alignItems: "center",
}}
>
<AudioFileLooper
audioCtx={nativeContext}
running={runningBuffer === nativeTriangleBuffer}
onClick={togglePlayer(nativeTriangleBuffer)}
buffer={nativeTriangleBuffer}
/>
<p>Looped using native AudioContext</p>
</div>
<div
style={{
display: "flex",
flexDirection: "column",
alignItems: "center",
}}
>
<AudioFileLooper
audioCtx={oggmentedContext}
running={runningBuffer === oggmentedTriangleBuffer}
onClick={togglePlayer(oggmentedTriangleBuffer)}
buffer={oggmentedTriangleBuffer}
/>
<p>Looped using OggmentedAudioContext</p>
</div>
</div>
<br />
<p>
If you look in your browser’s JavaScript console, you’ll see the
Float32Arrays for both buffers. The Oggmented AudioContext’s buffer has
33075 samples, which is a reasonable number of samples to see for 0.75s
of 44100 Hz audio. The native AudioContext however, has{" "}
{nativeTriangleBuffer && nativeTriangleBuffer.length} samples.
</p>
<br/>
<p>
Note: If you’re seeing the above console demo and you’re using Firefox, consider it a bug (in my page). It won’t demonstrate anything interesting in Firefox, and is just showing up because your computer’s audio output is currently set to run at something other than 44,100 Hz, and Firefox is resampling the audio to that rate (the code just looks at your native AudioContext capabilities, not your user-agent). I’ll need to come up with a slight redesign to account for this, but in the short term I wanted to provide this info.
</p>
</>
);
};
const ReaderDemo = () => {
const { oggmentedContext } = useContext(AudioContextContext);
const [readerState, setReaderState] = useState(select);
const source = useRef();
const { fileSelector, readerBuffer } = useReader(setReaderState);
const startBuffer = () => {
const bufferSource = oggmentedContext.createBufferSource();
bufferSource.connect(oggmentedContext.destination);
bufferSource.buffer = readerBuffer;
bufferSource.onended = () => {
setReaderState(select);
};
bufferSource.start();
source.current = bufferSource;
};
const handleReaderClick = () => {
if (readerState === select) {
fileSelector.click();
return;
}
if (readerState === ready) {
startBuffer();
setReaderState(playing);
return;
}
if (readerState === playing) {
source.current.stop();
setReaderState(select);
return;
}
};
return (
<>
<br />
<hr />
<br />
<p>
<a href="https://github.com/jfrancos/oggmented">
Oggmented AudioContext
</a>{" "}
is a library that decodes vorbis using its own JavaScript and Wasm,
transpiled from Xiph’s libvorbis C libraries, rather than using the
browser’s built in audio decoding support. I initially wrote it to
compensate for Safari’s lack of support, but along the way I realized
it’s also useful in Blink browsers, where vorbis decoding is buggy.
</p>
<button
className="button"
style={{ height: "4rem", margin: "1rem auto" }}
onClick={handleReaderClick}
>
{readerState === select &&
"Use OggmentedAudioContext to play an Ogg Vorbis file"}
{readerState === loading && <ClipLoader color="white" />}
{readerState === ready && (
<FontAwesomeIcon
icon={faPlay}
style={{ padding: "0 1rem", fontSize: "1.5rem" }}
/>
)}
{readerState === playing && (
<FontAwesomeIcon
icon={faStop}
style={{ padding: "0 1rem", fontSize: "1.5rem" }}
/>
)}
</button>
<p>
If you don’t happen to have any vorbis files lying around but you’d like
to try this demonstration, here’s one you can download:{" "}
<a href="ValsaDoCorpo.ogg">Só Sol – Valsa Do Corpo</a>. (More Só Sol can
be found at their <a href="https://sosol.bandcamp.com">Bandcamp page</a>
)
</p>
</>
);
};
export default OggmentedContent;