-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
107 lines (100 loc) · 3.74 KB
/
index.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
<!--
* @Author: xiuquanxu
* @Company: kaochong
* @Date: 2020-07-23 18:23:21
* @LastEditors: xiuquanxu
* @LastEditTime: 2020-07-24 11:52:49
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h2>Webassembly sonic demo</h2>
<input type="file" name="fileDemo" id="fileDemo"/>
<button onclick="getBuffer()">获取buffer</button>
<button onclick="start()">start</button>
</body>
</html>
<script src="./webassembly/sonic.js"></script>
<script src="./player.js"></script>
<script type="text/javascript">
var ModuleSonic = {};
var c_write_ptr = null;
var c_reader_ptr = null;
var handle = null;
var buffer = null;
var player = null;
function getBuffer() {
var resultFile = document.getElementById("fileDemo").files[0];
if (resultFile) {
var reader = new FileReader();
reader.readAsArrayBuffer(resultFile);
reader.onload = function (e) {
console.log(e.target.result);
buffer = e.target.result;
};
}
}
function download(pcm) {
const data = pcm;
const fd = new Blob([data]);
const oa = document.createElement('a');
oa.href = window.URL.createObjectURL(fd);
oa.download = 'sonic-web-pcm.pcm';
// oa.click();
}
// Module["onRuntimeInitialized"] = onRuntimeInitialized;
function start() {
// ModuleSonic.setSpeed(handle, 0.8);
// player = new PcmPlayer();
// player.play()
var player = new PCMPlayer({
encoding: '16bitInt',
channels: 1,
sampleRate: 44800,
flushingTime: 1000
});
// Now feed PCM data into player getting from websocket or ajax whatever the transport you are using.
const needWriteBuffer = new Uint8Array(buffer.slice(0, 48000 * 10));
console.log(needWriteBuffer);
ModuleSonic.wasmDsp.HEAPU8.set(needWriteBuffer, c_write_ptr);
var writeRes = ModuleSonic.handleRate(handle, c_write_ptr, 48000 * 10, c_reader_ptr, 48000 * 10);
console.log("writeRes:", writeRes, c_reader_ptr);
const pcm = ModuleSonic.wasmDsp.HEAP16.slice(c_reader_ptr / 2, (c_reader_ptr + writeRes) / 2);
// const player_pcm = new Uint8Array(pcm);
// console.log("player pcm:", player_pcm);
// download(pcm);
// console.log(pcm);
// player.write(pcm)
player.feed(pcm);
// const res = c_reader_ptr.slice();
}
function onRuntimeInitialized() {
c_write_ptr = ModuleSonic.wasmDsp._malloc(1024 * 1024);
c_reader_ptr = ModuleSonic.wasmDsp._malloc(1024 * 1024);
handle = ModuleSonic.init(48000, 1, 10);
}
function loadWasm() {
var req = new XMLHttpRequest();
req.responseType = 'arraybuffer';
req.addEventListener('load', () => {
var wasmBuffer = req.response;
WebSonic['wasmBinary'] = wasmBuffer;
var wasmDsp = WebSonic({ wasmBinary: WebSonic.wasmBinary });
var sonicInit = wasmDsp.cwrap('sonicInit', ['number'], ['number', 'number', 'number']);
var pcmHandleRate = wasmDsp.cwrap('pcmHandleRateBySonic', ['number'], ['number', 'number', 'number', 'number', 'number']);
var setSpeed = wasmDsp.cwrap('setSpeed', null, ['number', 'number']);
ModuleSonic.init = sonicInit;
ModuleSonic.handleRate = pcmHandleRate;
ModuleSonic.setSpeed = setSpeed;
ModuleSonic.wasmDsp = wasmDsp;
});
req.open('GET', './webassembly/sonic.wasm');
req.send();
}
loadWasm();
</script>