Created
July 8, 2012 17:08
-
-
Save spion/3071826 to your computer and use it in GitHub Desktop.
node.js infrared audio to binary
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 spawn = require('child_process').spawn, | |
info = spawn('file', [ process.argv[2] ]); | |
var infos = []; | |
info.stdout.setEncoding('utf8'); | |
info.stdout.on('data', function(buf) { infos.push(buf); }); | |
info.stdout.on('end', function() { | |
var x = infos.join(''), | |
data = /:.*\s*\((.*)\).*\, ([0-9]*) bit.*\, [a-z]* ([0-9]*) Hz/.exec(x), | |
finfo = { | |
endian: data[1], | |
bitRate: data[2], | |
sampleRate: data[3] | |
}, | |
readfn = "readUInt" + finfo.bitRate + (finfo.endian == 'little-endian'?"LE":"BE"), | |
samples = [], | |
sox = spawn('sox', [process.argv[2],'-t','raw','-']); | |
sox.stdout.on('data', function(buf) { | |
for (var k = 0; k < buf.length; k += finfo.bitRate / 8) | |
samples.push(buf[readfn](k)); | |
}); | |
sox.stdout.on('end', function() { | |
ret = processAudio(samples); | |
var minLen = ret.reduce(function(m, x) { return Math.min(m, x.count); }, 96000); | |
oneSamples = ret.filter(function(el) { return Math.round(el.count / minLen) == 1 }); | |
minLen = oneSamples.reduce(function(acc, e) { return acc + e.count }, 0) / oneSamples.length; | |
ret = ret.map(function(el) { return {count: Math.round(el.count / minLen), state: el.state }; }); | |
ret.forEach(function(el) { | |
for (var k = 0; k < Math.round(el.count); ++k) | |
process.stdout.write(""+el.state); | |
}); | |
process.stdout.write("\n"); | |
}); | |
}); | |
function processAudio(s) { | |
var ret = [], last = null; | |
for (var k = 0; k < s.length; ++k) { | |
var smp = s[k] / 65535; | |
if (!last) last = { state: Math.round(smp), count: 0} | |
var state = smp > 0.66 ? 1 | |
: smp < 0.33 ? 0 | |
: last.state; | |
if (last.state != state) { | |
if (last.count > 5) ret.push(last); | |
last = {state: state, count:0}; | |
} | |
last.count += 1; | |
} | |
return ret; | |
}; |
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
spion@lappy:~/Documents/node-ir-wav$ node irwav2bin.js 16c.wav | |
000000000000000000001110100001100010001001101110111100010011111100001110000011101111101000001110000111011011110010111011100011001000101100111000111011101110000011111111111111111111111110111000100010100111101111101000111110111110000011101111111000001111000011101010001100011010100010001110110001110011111000101100101010000111101110111 | |
spion@lappy:~/Documents/node-ir-wav$ node irwav2bin.js 17c.wav | |
00000000000000000000111101110101010000011101110100011111110000000001111000111110010001001100110110110001001000011000010011011101110110010110111111111111111111111000000111111111111111110111101001100001000110000101000001101101110000000001110111000001110111100010011111010010010110010111110100100001110111110101000001100001110111011100000 | |
spion@lappy:~/Documents/node-ir-wav$ node irwav2bin.js 18c.wav | |
000000000000000000011100100000011101110100100011011111010011110000111111111111001101100111010101011101011111011000111100110001011000110001100111111111000010000110000000000000000000000011110010110110110010111000010000001010111111110000111000001110111111101110101110111011101010001011111110001111101110000010101011001000111111101111111 | |
spion@lappy:~/Documents/node-ir-wav$ node irwav2bin.js 19c.wav | |
00000000000000000000110100001001111100001000001100111111010111100001110000011111110101000111110110110101001100000101010111011000110101001100010111011101110111000111111111111111111111110111010101011000101100001111011100100111011101110111100011110000100000000101000100001100010001111111111101011100000101111110010011010111110111111101110 | |
spion@lappy:~/Documents/node-ir-wav$ node irwav2bin.js 20c.wav | |
0000001111111111111111111011110011101001001011110010000010010001000011101110111011101011011010011100110110101111101100111010000011101010100000111111111100001110111000000111111111111111111111110101000110010111110001011110010000000111100001111000011101110111011100010110010101110111100111011100111100010101110100100101110001110111111110001 | |
spion@lappy:~/Documents/node-ir-wav$ node irwav2bin.js 21c.wav | |
000000111111111111111110110000101011101100101111101011101010111011101110000111110110110010111110111100111010001011001010101001001100101110000010000011101110111011111111111111111111111110111001101010101101111111101000101001000011111110111011101110111011111110101000100011101001001011101111101000010011000000111110111111111110111011101110 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment