-
Notifications
You must be signed in to change notification settings - Fork 9
/
minimodem.js
326 lines (279 loc) · 10.9 KB
/
minimodem.js
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
function Minimodem(ctx) {
//Audio playing or audio listening flag
this.audioPlaying = false
//Audio context generation
this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
//Empty variables for listening state
this.micInput;
this.decodeNode;
this.decodeFunction;
this.antialias;
this.sampleBuffer = [];
this.symbolBuffer = [];
this.dataBuffer = [];
//Analyser to get FFT data
this.analyser = this.audioContext.createAnalyser();
this.analyser.smoothingTimeConstant = 0;
this.analyser.fftSize = 512;
//Create graphic contexts if object have an graphic context
if(ctx){
this.graphNode = this.audioContext.createScriptProcessor(0, 1, 1);
this.graphContext = ctx
this.startSpectrogram()
this.graphNode.onaudioprocess = this.drawSpectrogram.bind(this)
}
//Web API connections
this.analyser.connect(this.graphNode);
this.graphNode.connect(this.audioContext.destination);
}
//Creating spectrogram
Minimodem.prototype.startSpectrogram = function () {
this.graphContext.font = "10px Arial";
this.graphContext.fillStyle = "#ffffff";
this.graphContext.textAlign = "end";
this.graphContext.textBaseline="middle";
//Axis labels
var axis = 10;
var nyq = this.audioContext.sampleRate/2;
var step = nyq/axis
for(i = 0; i<axis ; i++){
this.graphContext.fillText((nyq*(i)/(1000*axis)).toFixed(2) + " kHz", this.graphContext.canvas.width-1, this.graphContext.canvas.height*(i/axis));
}
}
//Drawing spectrogram
Minimodem.prototype.drawSpectrogram = function () {
var array = new Float32Array(this.analyser.frequencyBinCount);
this.analyser.getFloatFrequencyData(array);
//Color scale
var hot = new chroma.ColorScale({
colors:['#000000','#ffff00','#ff0000'],
positions:[0,0.5,1],
mode:'rgb',
limits:[this.analyser.minDecibels, this.analyser.maxDecibels]
});
requestAnimationFrame(function(){
//Painting in the first column (check the axis label offset)
var axisSpace = 50
for (var i = 0; i < array.length; i++) {
if (this.audioPlaying == true) {
this.graphContext.fillStyle = hot.getColor(array[i]).hex();
}else{
this.graphContext.fillStyle = "#000000";
}
this.graphContext.fillRect(this.graphContext.canvas.width-axisSpace,i, 1, 1);
}
//Move all the graph to the left (contious time effect)
var imageData = this.graphContext.getImageData(1, 0, this.graphContext.canvas.width-axisSpace, this.graphContext.canvas.height);
this.graphContext.putImageData(imageData, 0, 0);
}.bind(this));
}
//Transmit an data packet (it includes one or more channels and )
Minimodem.prototype.transmit = function(data) {
if (this.audioPlaying == false){
var channels = data["config"]["channels"];
var period = data["config"]["period"];
this.audioPlaying = true
setTimeout(function(){
this.audioPlaying = false;
}.bind(this),data["data"].length*period)
for (channel = 0 ; channel < channels ; channel++){
var oscillator = this.audioContext.createOscillator();
var gainNode = this.audioContext.createGain();
oscillator.connect(gainNode);
gainNode.connect(this.analyser);
gainNode.connect(this.audioContext.destination)
oscillator.start(0);
for (time = 0 ; time < data["data"].length ; time++){
oscillator.frequency.setValueAtTime(data["data"][time][channel]["frequency"],this.audioContext.currentTime+(period*time)/1000)
gainNode.gain.setValueAtTime(data["data"][time][channel]["gain"],this.audioContext.currentTime+(period*time)/1000)
//EXPERIMENTAL: Exponential changes between symbols in order to avoid high frequencies in changes
//oscillator.frequency.setTargetAtTime(data["data"][time][channel]["frequency"],this.audioContext.currentTime+(period*time)/1000,0.01)
//gainNode.gain.setTargetAtTime(data["data"][time][channel]["gain"],this.audioContext.currentTime+(period*time)/1000,0.01)
}
oscillator.stop(this.audioContext.currentTime+((data["data"].length)*period)/1000);
}
}
};
//Binary Frequency Shift Keying
Minimodem.prototype.BFSK = function(data,period,sFreq,shift){
var data = this.dataToBin(data)
var config = {"channels":1,"period":period};
var rawData = [];
while(data.length % 1 != 0){
data += "0"
}
console.log("Converted data: " + data)
for (i = 0 ; i < data.length ; i++){
var instant = [];
for (channel = 0 ; channel < config["channels"] ; channel++){
if (data[i] == "0"){
var symbol = {"frequency":sFreq,"gain":1}
}else{
var symbol = {"frequency":sFreq+shift,"gain":1}
}
instant.push(symbol)
}
rawData.push(instant)
}
this.transmit({"config":config,"data":rawData})
}
//Quad Frequency Shift Keying
Minimodem.prototype.QFSK = function(data,period,sFreq,shift){
var data = this.dataToBin(data)
var config = {"channels":1,"period":period};
var rawData = [];
while(data.length % 2 != 0){
data += "0"
}
console.log("Converted data: " + data)
for (i = 0 ; i < data.length ; i+=2){
var instant = [];
for (channel = 0 ; channel < config["channels"] ; channel++){
if (data.substring(i,i+2) == "00"){
var symbol = {"frequency":sFreq,"gain":1}
}else if (data.substring(i,i+2) == "01") {
var symbol = {"frequency":sFreq+shift,"gain":1}
}else if (data.substring(i,i+2) == "10") {
var symbol = {"frequency":sFreq+2*shift,"gain":1}
}else if (data.substring(i,i+2) == "11"){
var symbol = {"frequency":sFreq+3*shift,"gain":1}
}
instant.push(symbol)
}
rawData.push(instant)
}
this.transmit({"config":config,"data":rawData})
}
// Binary Amplitude Shift Keying
Minimodem.prototype.BASK = function(data,period,sFreq){
var data = this.dataToBin(data)
var config = {"channels":1,"period":period};
var rawData = [];
while(data.length % 1 != 0){
data += "0"
}
console.log("Converted data: " + data)
for (i = 0 ; i < data.length ; i++){
var instant = [];
for (channel = 0 ; channel < config["channels"] ; channel++){
if (data[i] == "1"){
var symbol = {"frequency":sFreq,"gain":1}
}else{
var symbol = {"frequency":sFreq,"gain":0.8}
}
instant.push(symbol)
}
rawData.push(instant)
}
this.transmit({"config":config,"data":rawData})
}
//Auxiliar function to convert everything to binary stream
Minimodem.prototype.dataToBin = function(t) {
var output="";
var input= t;
for (i=0; i < input.length; i++) {
output += input[i].charCodeAt(0).toString(2);
}
return output
}
//Receive audio info processing
Minimodem.prototype.receive = function(data) {
var array = new Float32Array(this.analyser.frequencyBinCount);
this.analyser.getFloatFrequencyData(array);
var freq = 11000
//var spread = 1
var thr = this.analyser.minDecibels + 0.8*(this.analyser.maxDecibels - this.analyser.minDecibels)
var nyq = this.audioContext.sampleRate/2
var period = 100
//var s = "";
//for (var i = freq; i < freq+spread; i++) {
var index = Math.round(freq/nyq * array.length);
if(array[index] > thr){
this.sampleBuffer.push({"timestamp":this.audioContext.currentTime*1000,"frequency":freq,"amplitude":array[index]})
//s = (i/1000).toFixed(2) + " kHz: " + array[index].toFixed(4) + " dB"
}
//}
if (this.sampleBuffer.length > 0 && (this.sampleBuffer[this.sampleBuffer.length-1]["timestamp"]-this.sampleBuffer[0]["timestamp"]) >= period){
this.decodeBASK(period)
}
//$("#received").html(s)
}
Minimodem.prototype.decodeBASK = function(period){
var i = 0
var mean = 0
while(this.sampleBuffer[i]["timestamp"]-this.sampleBuffer[0]["timestamp"] <= period){
mean += this.sampleBuffer[i]["amplitude"]
i += 1
}
//console.log("sample count: " + i + " samples mean: " + mean + " dB thr: " + decodeThr)
mean = mean/i
this.symbolBuffer.push(mean)
this.sampleBuffer.splice(0, i-1);
if(this.symbolBuffer.length >= 7){
var min = 999999;
var max = -999999;
//Calculate symbol amplitude threshold
for (var i = 0; i < 7; i++) {
if(this.symbolBuffer[i] > max){
max = this.symbolBuffer[i]
}
if(this.symbolBuffer[i] < min){
min = this.symbolBuffer[i]
}
}
var decodeThr = (min+max)/2
//console.log("max: " + max + " min: " + min + " Decode THR: " + decodeThr)
//Decode symbols
for (var i = 0; i < 7; i++) {
if(this.symbolBuffer[i] > decodeThr){
this.dataBuffer.push("1")
}else{
this.dataBuffer.push("0")
}
}
this.symbolBuffer.splice(0,8);
//console.log(this.dataBuffer)
if(this.dataBuffer.length >= 7){
this.decodeData()
this.dataBuffer.splice(0,8);
}
}
}
Minimodem.prototype.decodeData = function(){
var output = '';
for (var i = 0 ; i < this.dataBuffer.length; i += 7) {
output += String.fromCharCode( parseInt( this.dataBuffer.slice( i, i+7 ), 2 ) );
}
console.log("Received data: " + this.dataBuffer)
//$("#received").html(output)
return output
}
//Asking for the microphone
Minimodem.prototype.listen = function(){
navigator.webkitGetUserMedia(
{audio: {
optional: [{ echoCancellation: false }]}
},
this.onStream.bind(this),
function(fail) {console.log(fail)}
);
};
//Include microphone information in the system
Minimodem.prototype.onStream = function(stream) {
this.audioPlaying = true
this.micInput = this.audioContext.createMediaStreamSource(stream);
//Antialias filter (is it working?)
this.antialias = this.audioContext.createBiquadFilter();
this.antialias.type = "lowpass"
this.antialias.frequency.value = this.audioContext.sampleRate/2;
//console.log("Antialias filter at " + this.audioContext.sampleRate/2000 + " kHz")
this.antialias.Q.value = 0;
//Creating an receiver module
this.decodeNode = this.audioContext.createScriptProcessor(0, 1, 1);
this.decodeNode.onaudioprocess = this.receive.bind(this);
//Web API connections
this.micInput.connect(this.antialias);
this.antialias.connect(this.analyser);
this.analyser.connect(this.decodeNode);
this.decodeNode.connect(this.audioContext.destination);
};