Created
February 7, 2014 19:47
-
-
Save TerryMooreII/8870341 to your computer and use it in GitHub Desktop.
Messing around with coding a message and sending/receiving it over audio/mic. Doesnt work....yet....
This file contains hidden or 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 sen = "The quick brown fox"; | |
function codeMessage(message){ | |
var bin = ''; | |
var message = sen.split('') | |
for(var i=0; i < message.length; i++){ | |
var letter = message[i]; | |
var code = letter.charCodeAt().toString(2); | |
bin += new Array( 8-code.length+1 ).join( '0' ) + code; | |
//bin += bin; | |
}; | |
return bin; | |
} | |
function decodeMessage(bin){ | |
var response = ''; | |
for (var i = 0; i < bin.length; i += 8){ | |
response += String.fromCharCode( parseInt( bin.substr( i, 8 ), 2 ) ); | |
} | |
return response; | |
} | |
var codedMsg = codeMessage(sen); | |
var decodeMsg = decodeMessage(codedMsg); | |
console.log(codedMsg); | |
console.log(decodeMsg) | |
var ac = new (window.AudioContext || window.webkitAudioContext); | |
// C4, E4, G4 | |
var freqs = [400]; | |
var oscs = []; | |
for(var i=0; i<codedMsg.length;i++) { | |
var o = ac.createOscillator(); | |
var g = ac.createGainNode(); | |
o.frequency.value = codedMsg[i] === 0 ? 400 : 1000 ; | |
console.log(codedMsg[i]) | |
g.gain.value = 0.25; | |
g.connect(ac.destination); | |
o.connect(g); | |
oscs.push(o); | |
} | |
var i=0; | |
var tone = setInterval(function(){ | |
console.log('here') | |
oscs[i].noteOn(0); | |
oscs[i].noteOff(0.5); | |
if (i === oscs.length) | |
clearInterval(tone); | |
else | |
i++; | |
}, 500) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment