Created
May 27, 2018 13:07
-
-
Save Johann150/0c0d5b4b2fad86706b319ff4a976aa5f to your computer and use it in GitHub Desktop.
Small demo of how Modems transmit data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var AudioContext = window.AudioContext || window.webkitAudioContext; | |
var ctx = new AudioContext(); | |
requestAnimationFrame(s); | |
var number = "0696675356"; | |
var text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; | |
var analy = ctx.createAnalyser(); | |
analy.connect(ctx.destination); | |
var gain = ctx.createGain(); | |
gain.connect(analy); | |
var p=document.querySelector('#time'); | |
var baud=document.querySelector('#baud'); | |
dialtone(ctx.currentTime,gain,2.2); | |
var finishAt=data(dtfm(ctx.currentTime+2,gain,number)+2,gain,text); | |
document.querySelector('#finish').innerHTML=padZeroesLeft(Math.floor(finishAt/60),2)+":" | |
+padZeroesLeft(Math.floor(finishAt)%60,2)+":" | |
+padZeroesLeft(Math.floor((finishAt-Math.floor(finishAt))*1000),3); | |
setTimeout(()=>{ | |
gain.disconnect(); | |
analy.disconnect(); | |
document.querySelector('#finish').style.color="green"; | |
},finishAt*1000); | |
var canv=document.querySelector('canvas'); | |
var canvCtx=canv.getContext("2d"); | |
canvCtx.imageSmoothingEnabled=false; | |
console.log(analy); | |
const pxlSize = (canv.width/analy.frequencyBinCount)*2.5; | |
function s(){ | |
p.innerHTML=padZeroesLeft(Math.floor(ctx.currentTime/60),2)+":" | |
+padZeroesLeft(Math.floor(ctx.currentTime)%60,2)+":" | |
+padZeroesLeft(Math.floor((ctx.currentTime-Math.floor(ctx.currentTime))*1000),3); | |
var buffer = new Float32Array(analy.frequencyBinCount); | |
analy.getFloatFrequencyData(buffer); | |
canvCtx.drawImage(canv,0,pxlSize);// draw canvas to itself, only a bit lower | |
for(var i=0;i<buffer.length;i++){ | |
const value = buffer[i]+140*2; | |
let height=buffer[i]+100*2; | |
if(height>150){ | |
canvCtx.fillStyle='rgb('+Math.floor(height)+',0,0)'; | |
}else{ | |
canvCtx.fillStyle="black"; | |
} | |
canvCtx.fillRect(i*pxlSize,0,pxlSize,pxlSize); | |
} | |
requestAnimationFrame(s); | |
} | |
function padZeroesLeft(string,n){ | |
if(typeof string == "number"){ | |
string=string.toString(); | |
} | |
if(string.length>n){ | |
return string; | |
}else{ | |
var z=""; | |
for(var i=0;i<n;i++){ | |
z+="0"; | |
} | |
return (z+string).slice(-n); | |
} | |
} | |
function dialtone(start,destination,duration){ | |
var dialtone0=ctx.createOscillator(); | |
var dialtone1=ctx.createOscillator(); | |
dialtone0.connect(destination); | |
dialtone1.connect(destination); | |
dialtone0.frequency.setValueAtTime(350,0); | |
dialtone1.frequency.setValueAtTime(440,0); | |
dialtone0.start(start); | |
dialtone1.start(start); | |
dialtone0.stop(start+duration); | |
dialtone1.stop(start+duration); | |
setTimeout(()=>{ | |
dialtone0.disconnect(); | |
dialtone1.disconnect(); | |
},((start+duration)-ctx.currentTime)*1000); | |
} | |
// returns time when signal ends | |
function dtfm(start,destination,number){ | |
var osc0 = ctx.createOscillator(); | |
var osc1 = ctx.createOscillator(); | |
osc0.connect(destination); | |
osc1.connect(destination); | |
const interv = .2; | |
for(var i=0;i<number.length;i++){ | |
var cycleTime=start+(i*interv); | |
switch(number[i]){ | |
case '1': | |
case '2': | |
case '3': | |
case 'A': | |
osc0.frequency.setValueAtTime(697,cycleTime); | |
break; | |
case '4': | |
case '5': | |
case '6': | |
case 'B': | |
osc0.frequency.setValueAtTime(770,cycleTime); | |
break; | |
case '7': | |
case '8': | |
case '9': | |
case 'C': | |
osc0.frequency.setValueAtTime(852,cycleTime); | |
break; | |
case '*': | |
case '0': | |
case '#': | |
case 'D': | |
osc0.frequency.setValueAtTime(941,cycleTime); | |
break; | |
default: | |
osc0.frequency.setValueAtTime(0,cycleTime); | |
break; | |
} | |
osc0.frequency.setValueAtTime(0,cycleTime+interv*.75); | |
switch(number[i]){ | |
case '1': | |
case '4': | |
case '7': | |
case '*': | |
osc1.frequency.setValueAtTime(1209,cycleTime); | |
break; | |
case '2': | |
case '5': | |
case '8': | |
case '0': | |
osc1.frequency.setValueAtTime(1336,cycleTime); | |
break; | |
case '3': | |
case '6': | |
case '9': | |
case '#': | |
osc1.frequency.setValueAtTime(1477,cycleTime); | |
break; | |
case 'A': | |
case 'B': | |
case 'C': | |
case 'D': | |
osc1.frequency.setValueAtTime(1633,cycleTime); | |
break; | |
default: | |
osc1.frequency.setValueAtTime(0,cycleTime); | |
break; | |
} | |
osc1.frequency.setValueAtTime(0,cycleTime+interv*.75); | |
} | |
osc0.start(start); | |
osc1.start(start); | |
var end=start+(number.length*interv); | |
osc0.stop(end); | |
osc1.stop(end); | |
setTimeout(()=>{ | |
osc0.disconnect(); | |
osc1.disconnect(); | |
},(end-ctx.currentTime)*1000); | |
return end; | |
} | |
// returns time when signal ends | |
function data(start,destination,text){ | |
const interv=0.008; | |
baud.innerHTML=1/interv+" Bd"; | |
var osc=ctx.createOscillator(); | |
osc.connect(destination); | |
for(var i=0;i<text.length;i++){ | |
var binary=padZeroesLeft(text.charCodeAt(i).toString(2),8); | |
for(var j=0;j<binary.length;j++){ | |
osc.frequency.setValueAtTime(binary[j]=='0'?1000:4000,start+(i*8+j)*interv); | |
} | |
} | |
osc.start(start); | |
var end=start+text.length*8*interv; | |
osc.stop(end); | |
setTimeout(()=>{ | |
osc.disconnect(); | |
},(end-ctx.currentTime)*1000); | |
return end; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en" dir="ltr"> | |
<head> | |
<meta charset="utf-8"> | |
<title></title> | |
<style> | |
*{ | |
margin: 0; | |
padding: 0; | |
} | |
p{ | |
z-index: 1; | |
font-family: monospace; | |
font-size: 13px; | |
color: white; | |
position: absolute; | |
left:10px; | |
} | |
#time{ | |
top: 10px; | |
} | |
#time::before{ | |
content: 'timestamp:::'; | |
} | |
#finish{ | |
top: 25px; | |
} | |
#finish::before{ | |
content: 'finish at:::'; | |
} | |
#baud{ | |
top: 40px; | |
} | |
#baud::before{ | |
content: 'symbol rate:'; | |
} | |
canvas{ | |
z-index: 0; | |
position: absolute; | |
bottm:0; | |
left:0; | |
width:100%; | |
height:100%; | |
box-sizing: border-box; | |
background-color: black; | |
} | |
</style> | |
</head> | |
<body> | |
<p id="time"></p> | |
<p id="finish"></p> | |
<p id="baud"></p> | |
<canvas id="canvas" width="300" height="300"></canvas> | |
<script type="text/javascript" src="audio.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment