Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
65 lines (59 loc) · 2.32 KB
/
Copy pathindex.html
File metadata and controls
65 lines (59 loc) · 2.32 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script type="text/javascript">
async function test(mp3link) {
audio = new Audio(mp3link);
audio.setAttribute("controls", "true");
audio.crossOrigin = "anonymous";
document.getElementById("divid").appendChild(audio);
const audioContext = new AudioContext();
const audioMediaElement = audioContext.createMediaElementSource(audio);
await audioContext.audioWorklet.addModule("testprocessor.js");
const recorder = new AudioWorkletNode(audioContext, "testprocessor");
setInterval(() => recorder.port.postMessage("still alive ?"), 300);
const convertFloatToAudioBuffer = (data) => {
const sampleRate = 8000 | audioContext.sampleRate;
const channels = 1;
const sampleLength = 128 | data.length; // 1sec = sampleRate * 1
const audioBuffer = audioContext.createBuffer(
channels,
sampleLength,
sampleRate
); // Empty Audio
audioBuffer.copyToChannel(new Float32Array(data), 0); // depending on your processing this could be already a float32array
return audioBuffer;
};
let startAt = 0;
const streamDestination = audioContext.createMediaStreamDestination();
const play = (data) => {
const audioBufferSourceNode = audioContext.createBufferSource();
audioBufferSourceNode.buffer = convertFloatToAudioBuffer(data);
audioBufferSourceNode.connect(streamDestination);
audioBufferSourceNode.start();
};
recorder.port.onmessage = (ev) => play(ev.data);
audioMediaElement.connect(recorder); // connect the processor with the source
audioMediaElement.connect(audioContext.destination); // so speakers still work
//getAudioFocus();
console.log("v3");
audio.play();
}
</script>
</head>
<body>
<div id="divid">
<div>
<button onclick="test('smalltalk.mp3')">
local hosted mp3 -- testprocessor:process() will not fire
</button>
</div>
<div>
<button onclick="test('https\:\/\/am820.wnyc.org/wnycam-web')">
streaming mp3 -- testprocessor:process() will not fire
</button>
</div>
</div>
</body>
</html>