IMA Recitation 7: Neopixel Music Visualization

In task1, I tested the Neopixel using the FastLED library, which makes the first LED blink. The circuit looks like this.

In task2, I need to use SerialRecord to send messages from Processing to Arduino. The function of the given code is to change the color of the Neopixel according to the coordinates of mouse.

The main part of the recitation was task3, where I first downloaded and tested a given code. It displayed a circle that changes according to the volume of the music. To add my own music, I had to transform mp3, the type of my downloaded music, into aiff, the acquired music type. 

void setup() {
  size(640, 480);
  W = width/NUM;
  // load and play a sound file in a loop
  sample = new SoundFile(this, "Alan-Walker-Faded.aiff");
  sample.loop();

You can actually see I add the soundfile of Faded. The modification was successful. Since the original code already display a visualization on computer, I just needed to focus on how to make the Neopixel produce the light. The code in task2 gave me inspirations. If it can make Neopixel shine when mouse is clicked, then I only need to change the variables to the volume of the music, so that not only the circle in Processing will change, but also the LED on Neopixel. There are 60 LED on Neopixel, so I need to reset them after it reaches the capacity. 

After the Neopixel was done, I could consider modifying the visualization of Processing. I want to change the pattern displayed after a certain amount of time, but  I won’t change the code before that point. Writing the code during refcitation would take much of the time, so I utilized the code from my IMA post. Adding the code to Processing, the final result looks like this.

 

import processing.sound.*;
import processing.serial.*;
import osteele.processing.SerialRecord.*;
Serial serialPort;
SerialRecord serialRecord;

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
int i;
int m=1;

SoundFile sample;
Amplitude analysis;

void setup() {
  size(640, 480);
  W = width/NUM;
  // load and play a sound file in a loop
  sample = new SoundFile(this, "Alan-Walker-Faded.aiff");
  sample.loop();

  // create the Amplitude analysis object
  analysis = new Amplitude(this);
  // analyze the playing sound file
  analysis.input(sample);
  
  String serialPortName = SerialUtils.findArduinoPort();
  serialPort = new Serial(this, serialPortName, 9600);
  serialRecord = new SerialRecord(this, serialPort, 4);
  serialRecord.logToCanvas(false);
  rectMode(CENTER);
}

void draw() {
  //println(analysis.analyze());
  long t = millis();
  background(125, 255, 125);
  noStroke();
  fill(255, 0, 150);

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

  // volume is a number between 0.0 and 1.0
  // map the volume value to a useful scale
  float diameter = map(volume, 0, 1, 0, width);
  // draw a circle based on the microphone amplitude (volume)
  circle(width/2, height/2, diameter);
  println(t);
  if (t>13000){
    background(255);
  //for(int m=0; m<width;m=m+
    noFill();
    for (int j=-10; j< 10; j = j+1) {
      stroke(50,82,234);
      ellipse(width/2,height/2,m + j *30 ,m+j*30);
    }
    m=m+5;
    if(m>=width){
      m=-m-150;
    }
  }
  if (i>60){
    i=1;
    for (int j=1;j<60;j=j+1){
      serialRecord.values[0] = j;     // which pixel we change (0-59)
      serialRecord.values[1] = r[0];  // how much red (0-255)
      serialRecord.values[2] = g[0];  // how much green (0-255)
      serialRecord.values[3] = b[0];  // how much blue (0-255)
      serialRecord.send();
    }
  }
  light(i);
  i=i+1;
}
void light(int i){
    int n = floor(random(60));

    r[n] = floor(random(255));
    g[n] = floor(random(255));
    b[n] = floor(random(255));
    if (analysis.analyze()<0.3){
      serialRecord.values[0]=i;
      serialRecord.values[1]=r[n];
      serialRecord.values[2]=g[n];
      serialRecord.values[3]=b[n];
    } else if(analysis.analyze()>0.3 && analysis.analyze()<0.6){
      serialRecord.values[0]=i;
      serialRecord.values[1]=r[n];
      serialRecord.values[2]=g[n];
      serialRecord.values[3]=b[n];
    } else{
      serialRecord.values[0]=i;
      serialRecord.values[1]=r[n];
      serialRecord.values[2]=g[n];
      serialRecord.values[3]=b[n];
    }
    serialRecord.send();
    delay(50);
}

 

Leave a Reply

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