-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio_werklet_example.html
More file actions
61 lines (46 loc) · 1.77 KB
/
audio_werklet_example.html
File metadata and controls
61 lines (46 loc) · 1.77 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width">
<title>AudioWorklet example</title>
</head>
<body>
<h1>AudioWorklet example</h1>
<button>Make Telephone Sound</button>
<pre></pre>
</body>
<script>
// Display this script in page's preformatted secion
const myScript = document.querySelector('script');
const pre = document.querySelector('pre');
pre.innerHTML = myScript.innerHTML;
let Player_node;
const audioContext = new AudioContext({latencyHint: "interactive", sampleRate: 16000});
audioContext.audioWorklet.addModule('audio_werklet_proc.js').then(() => {
Player_node = new AudioWorkletNode(audioContext, 'pcm_player');
Player_node.connect(audioContext.destination)
setTimeout(()=>audioContext.close(), 30000);
});
const button = document.querySelector('button');
button.onclick = function() {
console.log('XXX - Sending test waveform to Worklet');
const sample_hz = 16000;
const td_480 = sample_hz / 480.0 / Math.PI / 2.0;
const td_620 = sample_hz / 620.0 / Math.PI / 2.0;
// Build the test waveform
const samples = 1 * sample_hz;
let wave = new Int16Array(samples);
for (let s = 0; s < samples; s++) {
let sample = (Math.sin(s / td_480) + Math.sin(s / td_620)) / 2.0;
wave[s] = Math.trunc(0x3fff * sample);
}
// Send it in (20ms) chunks to the Worklet processor
for (let n = 0; n < wave.length / 320; n++) {
let array = wave.slice(n * 320, (n + 1) * 320);
Player_node.port.postMessage(array);
}
}
</script>
</html>