forked from martme/webaudio-modem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.html
142 lines (123 loc) · 4.52 KB
/
decoder.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>martinmelhus.com - Web Audio Encoder</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="overlay">
<button>Start</button>
</div>
<h1>decoder</h1>
<code>0b00000000</code>
<textarea readonly></textarea>
<p>
<button>clear</button>
<a class="button" href="index.html">back</a>
</p>
<h2>config</h2>
<label for="bin-value-threshold">Significant bin byte value threshold: <span data-val></span>
<input id="bin-value-threshold" type="range" value="124" min="1", max="255", step="1">
<label for="duplicate-state-threshold">Output after consecutive duplicate states: <span data-val></span>
<input id="duplicate-state-threshold" type="range" value="10" min="1", max="50", step="1">
<script>
document.querySelector(".overlay > button").addEventListener("click", (e) => {
let overlay = e.currentTarget.parentNode;
overlay.parentNode.removeChild(overlay);
startDecoder();
})
function startDecoder() {
const frequencies = [392, 784, 1046.5, 1318.5, 1568, 1864.7, 2093, 2637];
let audioContext;
if('webkitAudioContext' in window) {
audioContext = new webkitAudioContext();
} else {
audioContext = new AudioContext();
}
// create audio nodes
let analyser = audioContext.createAnalyser();
analyser.fftSize = 512;
analyser.smoothingTimeConstant = 0.0;
analyser.minDecibels = -58;
// connect nodes
navigator.mediaDevices.getUserMedia({audio: true})
.then(stream => {
var microphone = audioContext.createMediaStreamSource(stream);
microphone.connect(analyser);
})
.catch(err => { alert("Microphone is required."); });
// buffer for analyser output
let buffer = new Uint8Array(analyser.frequencyBinCount);
// helper functions
const frequencyBinValue = (f) => {
const hzPerBin = (audioContext.sampleRate) / (2*analyser.frequencyBinCount);
const index = parseInt((f + hzPerBin/2) / hzPerBin);
return buffer[index];
};
const isActive = (value) => {
let threshold = +document.querySelector('#bin-value-threshold').value;
return value > threshold;
};
const getState = () => {
return frequencies
.map(frequencyBinValue)
.reduce((acc, val, idx) => {
if (isActive(val)) {
acc += (1 << idx);
}
return acc;
}, 0);
}
const output = (state) => {
let textarea = document.querySelector('textarea[readonly]');
textarea.innerHTML += String.fromCharCode(state % 256);
}
const trace = (state) => {
let str = state.toString(2);
let pad = "0b00000000"
let text = pad.substring(0, pad.length - str.length) + str
document.querySelector('code').innerHTML = text;
}
const decode = () => {
let prevState = 0;
let duplicates = 0;
const iteration = () => {
analyser.getByteFrequencyData(buffer);
let state = getState();
let duplicateThreshold = +document.querySelector('#duplicate-state-threshold').value;
if (state === prevState) {
duplicates++;
}
else {
trace(state);
prevState = state;
duplicates = 0;
}
if (duplicates === duplicateThreshold) {
output(state);
}
window.setTimeout(iteration, 1);
};
iteration();
};
decode();
document.querySelector('button').addEventListener('click', (e) => {
document.querySelector('textarea[readonly]').innerHTML = '';
e.target.blur();
});
// config behaviour
for (let input of document.querySelectorAll('input')) {
const selector = `label[for="${input.getAttribute('id')}"] [data-val]`;
let label = document.querySelector(selector);
label.innerHTML = input.value;
input.addEventListener('change', (e) => {
label.innerHTML = e.target.value;
});
};
}
</script>
</body>
</html>