<commit_before>'use strict';

// require modules
var tessel = require('tessel');
var ambientlib = require('ambient-attx4');
var Neopixels = require('neopixels');

// initializing modules
var ambient = ambientlib.use(tessel.port.B);
var neopixels = new Neopixels();

// setting constants
var leds = 60;
var triggerDelay = 10;
var threshold = 0.01;
var color = {
  off: [0x00, 0x00, 0x00],
  blue: [0x00, 0x00, 0xFF],
  green: [0x00, 0xFF, 0x00],
  red: [0xFF, 0x00, 0x00]
};
var currentColor = color.off;

// set all LEDs off
neopixels.animate( leds, Buffer.concat(setAll(leds, color.off)) );

// when ready
ambient.on('ready', function () {
  // Set a sound level trigger float between 0 and 1
  ambient.setSoundTrigger(threshold);

  ambient.on('sound-trigger', function(data) {
    console.log('Something happened with sound: ', data);
    var newColor = convertColor(data);
    neopixels.animate( leds, Buffer.concat(setAll(leds, newColor)) );
  });

  // Clear it
  ambient.clearSoundTrigger();

  //After delay reset sound trigger
  setTimeout(function () {
    ambient.setSoundTrigger(threshold);
  }, triggerDelay);
});

// error logging
ambient.on('error', function (err) {
  console.log(err);
});

// set leds to color
function setAll (numLEDs, color) {
  var buf = new Buffer(numLEDs * 3);
  for (var i = 0; i < buf.length; i += 3) {
    buf[i] = color[0];
    buf[i+1] = color[1];
    buf[i+2] = color[2];
  }
  currentColor = color;
  return [buf];
}

function convertColor (decimal) {
  var max = 0.1;
  var halfway = max / 2;
  var output = [0x00, 0x00, 0x00]; // RGB off
  decimal = decimal > 1 ? 1 : decimal;
  if ( decimal === halfway ) {
    output[1] = 0xFF; // G
  } else if ( decimal > halfway ) {
    output[0] = (decimal-halfway)/(halfway) * 0xFF; // R
    output[1] = 0xFF - output[0]; // G
  } else {
    output[1] = decimal / halfway * 0xFF; // G
    output[2] = 0xFF - output[1]; // B
  }
  return output;
}<commit_msg>slight refactoring of visualizer.js<commit_after>'use strict';

// require modules
var tessel = require('tessel');
var ambientlib = require('ambient-attx4');
var Neopixels = require('neopixels');

// initializing modules
var ambient = ambientlib.use(tessel.port.B);
var neopixels = new Neopixels();

// setting constants
var leds = 60;
var triggerDelay = 10;
var threshold = 0.01;
var color = {
  off: [0x00, 0x00, 0x00],
  green: [0xFF, 0x00, 0x00],
  red: [0x00, 0xFF, 0x00],
  blue: [0x00, 0x00, 0xFF],
  purple: [0x00, 0xFF, 0xFF],
  yellow: [0xFF, 0xFF, 0x00],
  cyan: [0xFF, 0x00, 0xFF]
};
var currentColor = color.off;

// set all LEDs off
neopixels.animate( leds, Buffer.concat(setAll(leds, color.off)) );

// when ready
ambient.on('ready', function () {
  // ready animation for fun
  neopixels.animate( leds, Buffer.concat(tracer(leds)) );
  // Set a sound level trigger float between 0 and 1
  ambient.setSoundTrigger(threshold);

  ambient.on('sound-trigger', function(data) {
    console.log('Tripped sound threshold at level ', data);

    var newColor = convertColor(data);
    neopixels.animate( leds, Buffer.concat(setAll(leds, newColor)) );

    // Clear it
    ambient.clearSoundTrigger();

    //After delay reset sound trigger
    setTimeout(function () {
      ambient.setSoundTrigger(threshold);
    }, triggerDelay);
  });
});

// error logging
ambient.on('error', function (err) {
  console.log(err);
});

function convertColor (decimal) {
  var max = 0.1;
  var halfway = max / 2;
  var output = [0x00, 0x00, 0x00]; // RGB off
  decimal = decimal > 1 ? 1 : decimal;
  if ( decimal === halfway ) {
    output[1] = 0xFF; // G
  } else if ( decimal > halfway ) {
    output[0] = (decimal-halfway)/(halfway) * 0xFF; // R
    output[1] = 0xFF - output[0]; // G
  } else {
    output[1] = decimal / halfway * 0xFF; // G
    output[2] = 0xFF - output[1]; // B
  }
  return output;
}

// set leds to color
function setAll (numLEDs, color) {
  var buf = new Buffer(numLEDs * 3);
  for (var i = 0; i < buf.length; i += 3) {
    buf[i] = color[0];
    buf[i+1] = color[1];
    buf[i+2] = color[2];
  }
  currentColor = color;
  return [buf];
}

// An example animation
function tracer(numLEDs) {
  var trail = 20;
  var arr = new Array(numLEDs);
  for (var i = 0; i < numLEDs; i++) {
    var buf = new Buffer(numLEDs * 3);
    buf.fill(0);
    for (var col = 0; col < 3; col++){
      for (var k = 0; k < trail; k++) {
        buf[(3*(i+numLEDs*col/3)+col+1 +3*k)] = 0xFF*(trail-k)/trail;
      }
    }
    arr[i] = buf;
  }
  var newArr = [];
  for (var i = 0; i < arr.length; i++) {
    newArr.push(arr[i]);
    newArr.push(arr[i]);
    newArr.push(arr[i]);
    newArr.push(arr[i]);
    newArr.push(arr[i]);
    newArr.push(arr[i]);
  }
  return newArr;
}
{'fraction_non_alphanumeric': 0.16701992369648155, 'fraction_numerical': 0.03751589656634167, 'mean_word_length': 3.150395778364116, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2041628', 'n_tokens_mistral': 1876, 'n_tokens_neox': 1759, 'n_words': 466}