-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.html
223 lines (191 loc) · 9.13 KB
/
test.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
211
212
213
214
215
216
217
218
219
220
221
222
223
<html manifest="test.manifest">
<head>
<title>Test Page!</title>
<meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0" />
<script src="./mt63Wasm.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>
<script src="resampler.js"></script>
<script src="amp.js"></script>
<script type="text/javascript">
window.onload = function() {
window.mod = Module();
window.getMT63SampleRate = mod.cwrap('getSampleRate', 'number');
window.getMT63Buffer = mod.cwrap('getBuffer', 'number');
window.encodeMT63 = mod.cwrap('encodeString', 'number', ['string', 'number', 'number']);
window.crc16 = mod.cwrap('crc16', 'string', ['string']);
var _lzmaEncode = mod.cwrap('lzmaEncode', 'number', ['string', 'number']);
var _lzmaDecode = mod.cwrap('lzmaDecode', 'number', ['number', 'number']);
var _getLzmaOutputStr = mod.cwrap('getLzmaOutput', 'string', []);
var _getLzmaOutputPtr = mod.cwrap('getLzmaOutput', 'number', []);
window.initMT63Rx = mod.cwrap('initRx', 'void', ['number', 'number', 'number', 'number']);
var _processMT63Rx = mod.cwrap('processAudio', 'string', ['number', 'number']);
function sliceArray(arr, len) {
if (arr.subarray) {
return arr.subarray(0, len);
} else if (arr.slice) {
return arr.slice(0, len);
} else {
throw new Error("Invalid array");
}
}
window.lzmaEncode = function(input) {
let outputSize = _lzmaEncode(input, input.length);
if (outputSize === 0) {
// Error!
throw new Error(_getLzmaOutputStr());
}
let outputPtr = _getLzmaOutputPtr();
return mod.HEAPU8.slice(outputPtr, outputPtr + outputSize);
}
window.lzmaDecode = function(input) {
if (typeof input == "string") {
// convert to array
var ptr = mod._malloc(input.length);
mod.HEAPU8.set(input.split('').map(c => c.charCodeAt(0)), ptr);
} else {
var ptr = mod._malloc(input.length);
mod.HEAPU8.set(input, ptr);
}
let outputSize = _lzmaDecode(ptr, input.length);
mod._free(ptr);
if (outputSize === 0) {
// Error!
throw new Error(_getLzmaOutputStr());
}
let outputPtr = _getLzmaOutputPtr();
let outputArr = mod.HEAPU8.subarray(outputPtr, outputPtr + outputSize);
var chunkSize = 5000;
let chunks = Math.ceil(outputArr.length / chunkSize);
let outStr = "";
for (var i = 0; i < chunks; ++i) {
outStr += String.fromCharCode.apply(String, outputArr.subarray(chunkSize*i, chunkSize*(i+1)));
}
return outStr;
}
window.processMT63Rx = function processMT63(floatArr, len, sampleRate) {
var numBytes = len * 4;
var ptr = mod._malloc(numBytes);
mod.HEAPF32.set(sliceArray(floatArr, len), ptr/4);
var outStr = _processMT63Rx(ptr, heapArr.length);
mod._free(ptr);
return outStr;
}
polyfill();
document.getElementById("btnSend").addEventListener('click', function(evt) {
evt.preventDefault();
evt.stopPropagation();
var strToPlay = document.getElementById("sendStr").value;
sendMessage(strToPlay);
}, false);
document.getElementById("btnSendAmp").addEventListener('click', function(evt) {
evt.preventDefault();
evt.stopPropagation();
var strToPlay = document.getElementById("sendStr").value;
var a = new amp("KV9G", "KD7BBC", "quickbrownfox.txt", new Date(2017, 5, 13), strToPlay, 64);
strToPlay = a.getBlocks();
if (strToPlay.length) {
// console.log("Received:", strToPlay);
var span = document.createElement("span");
span.innerHTML = strToPlay;
document.getElementById("output").appendChild(span);
}
sendMessage(strToPlay);
}, false);
document.getElementById("btnListen").addEventListener('click', function(evt) {
evt.preventDefault();
evt.stopPropagation();
startListening();
}, false);
};
function polyfill() {
navigator.getUserMedia = (navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
if (AudioBuffer.prototype.hasOwnProperty("copyToChannel")) {
return;
}
//// ### AudioBuffer.prototype.copyToChannel
//// The `copyToChannel` method copies the samples to the specified channel of the **`AudioBuffer`**, from the `source` array.
////
//// #### Parameters
//// - `source: Float32Array`
//// - The array the channel data will be copied from.
//// - `channelNumber: number`
//// - The index of the channel to copy the data to.
//// - `startInChannel: number = 0`
//// - An optional offset to copy the data to.
////
//// #### Return
//// - `void`
AudioBuffer.prototype.copyToChannel = function(source, channelNumber, startInChannel) {
let clipped = source.subarray(0, Math.min(source.length, this.length - (startInChannel|0)));
this.getChannelData(channelNumber|0).set(clipped, startInChannel|0);
};
}
function sendMessage(str) {
var sampleRate = getMT63SampleRate();
var length = encodeMT63(str, 2000, 1);
var srcPtr = getMT63Buffer();
var srcData = mod.HEAPF32.subarray(srcPtr/4, srcPtr/4 + length);
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var buffer = audioCtx.createBuffer(1, length, sampleRate*3);
buffer.copyToChannel(srcData, 0);
var source = audioCtx.createBufferSource();
source.playbackRate.value = 1/3;
source.buffer = buffer;
source.connect(audioCtx.destination);
source.start();
}
function startListening() {
// Bandwidth 2000, long interleave (1), integration period 16, squelch 8.0
initMT63Rx(2000, 1, 16, 5.0);
var isLocalhost = window.location.hostname == 'localhost' ||
window.location.hostname == '127.0.0.1';
if (window.location.protocol != 'https:' && !isLocalhost) {
alert('HTTPS is required for microphone access, and this site has no SSL cert yet. Sorry!');
}
navigator.mediaDevices.getUserMedia({audio: true}).then(startStream, streamError);
}
function startStream(stream) {
var context = new (window.AudioContext || window.webkitAudioContext)();
let audioInput = context.createMediaStreamSource(stream);
let scriptProc = context.createScriptProcessor(8192, 1, 1);
scriptProc.onaudioprocess = function(aEvt) {
let inpBuffer = aEvt.inputBuffer.getChannelData(0);
let ReSamp = new Resampler(aEvt.inputBuffer.sampleRate, 8000, 1, inpBuffer);
let newLen = ReSamp.resampler(inpBuffer.length);
let newBuff = ReSamp.outputBuffer;
let res = processMT63Rx(newBuff, newLen);
if (res.length) {
// console.log("Received:", res);
var span = document.createElement("span");
span.innerHTML = res;
document.getElementById("output").appendChild(span);
}
// console.log(newBuff);
};
audioInput.connect(scriptProc);
// stream.start();
scriptProc.connect(context.destination);
}
function streamError(err) {
console.warn(err);
alert("Error!", err);
}
</script>
</head>
<body>
Enter the text to send:
<form>
<textarea type="text" rows="8" id="sendStr" style="width: 100%;"></textarea>
<button id="btnSend">Send/Play</button>
<button id="btnSendAmp">Send Amp/Play Amp</button>
</form>
<button id="btnListen">Start Decoding</button>
<div id="output" style="font-family: monospace; white-space: pre;">
Decoded:
<span id="outputSpan"></span>
</div>
</body>
</html>