Skip to content

Instantly share code, notes, and snippets.

@neagle
Last active May 29, 2020 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neagle/c02f23495be22064e83d1c2993fcb661 to your computer and use it in GitHub Desktop.
Save neagle/c02f23495be22064e83d1c2993fcb661 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
var convert = require('color-convert');
var OPC = new require('../fadecandy/examples/node/opc')
var client = new OPC('localhost', 7890);
// This is set in the CAVA config file
const bars = 100;
const frame = [];
const visualizers = [
[0, 250]
];
let hue = 0;
function draw([start, end, rangeStart = 0, rangeEnd = 100]) {
const barPixelRatio = (rangeEnd - rangeStart) / (end - start);
for (var pixel = 0; pixel < end - start; pixel++) {
const frameIndex = Math.floor(pixel * barPixelRatio + rangeStart);
const saturation = 100;
const lightness = frame[0][frameIndex] * (100 / 255);
client.setPixel(pixel + start, ...convert.hsl.rgb(hue, saturation, lightness));
}
client.writePixels();
}
setInterval(() => {
hue += 1;
if (hue > 360) {
hue = 0;
}
}, 1000)
process.stdin.setEncoding('utf8');
let str = '';
process.stdin.on('readable', () => {
let chunk;
// Use a loop to make sure we read all available data.
while ((chunk = process.stdin.read()) !== null) {
str += chunk;
const frameEnd = str.indexOf('@');
if (frameEnd > 0) {
displayFrame(str.substring(0, frameEnd));
str = str.substring(frameEnd + 1);
}
}
});
function displayFrame(f) {
frame.pop();
frame.push(f.split(';'));
visualizers.forEach(draw);
}
process.stdin.on('end', () => {
console.log('end');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment