-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (57 loc) · 2.13 KB
/
index.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
import Transmitter from './tx.js'
import Receiver from './rx.js'
// Chrome won't allow audio stuff without user action sooooo...
document.getElementById('create').addEventListener('click', () => {
function stringToBinary(str) {
var buf = new ArrayBuffer(str.length*2)
var binArr = new Uint16Array(buf)
for (var i=0; i < str.length; i++) {
binArr[i] = str.charCodeAt(i)
}
return buf
}
let data = new Uint8Array(stringToBinary('testing'))
const AudioContext = window.AudioContext || window.webkitAudioContext
const audioCtx = new AudioContext()
let tx
let rx
let params = {
duration: .05,
rampTime: .002,
fftSize: 256,
freqs: [3550, 4050, 4550, 5050],
minPeak: 20
}
let onDataLoaded = function(data) {
let typedData = new Uint16Array(data)
let str = String.fromCharCode.apply(null, typedData);
document.getElementById('message').innerHTML = 'Message is: ' + str
}
document.getElementById('mic').addEventListener('click', () => {
if(navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia ({audio: true, video: false})
.then((stream) => {
let sourceNode = audioCtx.createMediaStreamSource(stream)
rx = new Receiver(audioCtx, onDataLoaded, params)
sourceNode.connect(rx.scriptNode)
}).catch(err => {
console.log('ERROR getting stream', err)
})
} else {
console.log('getUserMedia not available in this enviornment')
}
})
document.getElementById('simulate').addEventListener('click', () => {
let transfer = new GainNode(audioCtx)
transfer.gain.value = 1
params.output = transfer
tx = new Transmitter(audioCtx, params)
rx = new Receiver(audioCtx, onDataLoaded, params)
transfer.connect(rx.scriptNode)
tx.send(data)
})
document.getElementById('broadcast').addEventListener('click', () => {
tx = new Transmitter(audioCtx, params)
tx.send(data)
})
})