Categories
interaction lab recitations

Neopixel Music Visualization

I had already installed all the libraries before the lesson, so I started right with the work. 

From the beginning, I had struggles with sounds (with playing the sounds). 

Spent wayyy to much time on that. However when I figured that out, the initial sketches (code provided in the recitation webpage) worked:

then, for some reason it stopped working again, so I decided to continue my work during the weekend.

on Saturday, I continued and manipulated some code to link the colors of the LED to the volume:

and then also added some code so that the number of LED that lights up also depend on the volume level:

then, I started working on the visualization, however the LED stopped lighting up. I tried uploading solely Arduino code (just trying to light up LEDs all red), however they still did not work. The code I have now is displayed below:

P.S. I will try to run it again on some other day or will replace the LED strip on Monday. 

import processing.serial.*;
import processing.sound.*;
import osteele.processing.SerialRecord.*;


Serial serialPort;
SerialRecord serialRecord;
SoundFile sample;
Amplitude analysis;

int W;    //width of the tiles
int NUM = 60;  //amount of pixels
int[] r = new int[NUM]; //red of each tile
int[] g = new int[NUM]; //red of each tile
int[] b = new int[NUM]; //red of each tile



void setup() {
  String serialPortName = SerialUtils.findArduinoPort();
  serialPort = new Serial(this, serialPortName, 9600);
  serialRecord = new SerialRecord(this, serialPort, 4);
  serialRecord.logToCanvas(false);
  size(640, 480);

  // load and play a sound file in a loop
  sample = new SoundFile(this, "finalTamacun.wav");
  sample.loop();

  // create the Amplitude analysis object
  analysis = new Amplitude(this);
  // analyze the playing sound file
  analysis.input(sample);
}

void draw() {
  //long t = millis();
  println(analysis.analyze());
  noStroke();
  background(255);

  // analyze the audio for its volume level
  float volume = analysis.analyze();


  //if (floor(t/1000) % 2 == 0) {
  //  background(255, 0, 0);
  //  fill(0);
  //  println(t);
  //} else {
  //  fill(255, 0, 0);
  //}
  //float diameter = map(volume, 0, 1, 0, width);

  //ellipse(width/2, height/2, diameter, diameter);
  float numberOfLedFromVolume = int(floor(map(volume, 0, 1, 0, 60)));

  for (int i=0; i< NUM; i++) {
    float volumeToColor1 = map(volume, 0, 1, 150, 255);
    float volumeToColor2 = map(volume, 0, 1, 50, 150);
    float volumeToColor3 = map(volume, 0, 1, 0, 50);
    if (i < numberOfLedFromVolume) {
      r[i] = floor(volumeToColor1);
      g[i] = floor(volumeToColor2);
      b[i] = floor(volumeToColor3);
      serialRecord.values[0] = i;
      serialRecord.values[1] = r[i];
      serialRecord.values[2] = g[i];
      serialRecord.values[3] = b[i];
    } else {
      serialRecord.values[0] = i;
      serialRecord.values[1] = 0;
      serialRecord.values[2] = 0;
      serialRecord.values[3] = 0;
      serialRecord.send();
    }
    serialRecord.send();
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *