Music Visualizer Upgraded

arduino processing

I had made a simple music visualizer and it had its own set of drawbacks; the most irritating one being that whenever I wanted to play a file I would have to follow the absurd ritual of putting a the new file in the folder containing the Processing sketch, change code, run the code again and then see the file “visualized”. I definitely needed a way to make the Processing sketch automatically detect any sound being played and then send the required values to the Arduino.

I used the method as described by Arduino forum user macegr who did a similar project back in 2011.

To achieve what we want to do i.e. respond to sound events from any program we will redirect the output to a special kind of recording mode available in Windows (I don’t know about other operating systems, use Google) called “Stereo Mix”. It’s main purpose in life is to fetch any sound from the output channels and then use the same sound to act as input. It is something like placing a microphone against your speaker to record whatever sound your speaker produces; just with negligible noise and without any special hardware.

Setting up Stereo Mix

Step 2

The setup should be straight forward. Start by right click on the speaker icon in the taskbar and select ‘Recording Devices’ from the menu that appears.

Step 3

You will see a window similar to the one shown above.

Step 4

Right click on any blank area and select “Show Disabled Devices”. This should give you a list of devices.

Step 5

Right click on “Stereo Mix” and select “Enable”

Step 6

Now, click on “Stereo Mix” to select it and then click on the “Set Default” button.

Step 7

You should see the check mark shift from you default device to “Stereo Mix”

Step 8

To ensure that you have done everything right, open up a sound file and play it. You should see the sound level indicator near the “Stereo Mix” icon change according to whatever you are playing.

Processing Code

Most of the code remains the same. The parts handling the audio channels now take input from the computer’s recording device instead of an mp3 file in the folder.

 *  Music Visualizer ver2
 *  This sketch extracts values from the input audio stream
 *  which happens to give the same values as our output
 *  stream as "Stereo Mix" has been set as the default device.
 *  The values are used to draw waveforms representing the
 *  average, left and right channels and send the values to an
 *  Arduino which changes the brightness of a LED.
 *  This is an upgrade to the previous Music Visualizer Project
 *  Code by:
 *  Abhik Pal; 19th Jan 2014, 14:03

// Import the Minim and Serial Libraries
import ddf.minim.*;
import processing.serial.*;

// creating the required objects
Minim minim;
AudioInput in;
Serial port;

// This value will be sent to the Arduino
int sendVal = 255;

void setup()
  //The P3D mode is trivial, it just speeds up things
  size(400, 400, P3D);

  minim = new Minim(this);

  // Setting 'in' to be the current input stream
  // of audio. This will be equivalent to the output
  // stream as we have selected 'Stereo Mix' as our
  // default recording device
  in = minim.getLineIn();

  // change the COM port to match your Arduino's COM port
  // change the baud Rate if you have used someting
  // different from 9600 bauds.
  port = new Serial(this, "COM5", 9600);

void draw()
  // clearing the screen
  rect(0, 0, width, height);

  for (int i = 0; i < (in.bufferSize()/1000)*width - 1; i++)
    // self explanatory variables ...  :)
    float rightVal = in.right.get(i);
    float nextRightVal = in.right.get(i+1);
    float leftVal = in.left.get(i);
    float nextLeftVal = in.left.get(i+1);
    float avgVal = (leftVal + rightVal)/2;
    float nextAvgVal = (nextLeftVal + nextRightVal)/2;

    // draw some waveforms
    // the average values' waveform
    stroke(20, 220, 20);
    line(i, 100 + avgVal*150, (i+1), 100 + nextAvgVal*150);

    // waveforms for the left and right audio channels
    stroke(220, 220, 20);
    line(i, 225 + leftVal*50, (i+1), 225 + nextLeftVal*50);
    stroke(20, 220, 220);
    line(i, 300 + rightVal*50, (i+1), 300 + nextRightVal*50);

    // sendVal is the average of the left and right channels
    sendVal = (int)(avgVal*255);
    sendVal = (int)map(abs(sendVal), 0, 255, 255, 0);

    // draw a rectangle to show the value to be sent
    int rectLen = (int)map(sendVal, 255, 0, 0, width - 30);
    rect(15, height-15, rectLen, 5);

  // Send the required values to the Arduino

Upload the Dimmer example to your Arduino board and run the Processing sketch. Turn up the volume and try playing some sound files on your computer, they need not be located in the same folder as your Processing sketch. You should see the wave-forms in the Processing sketch change and your LED flash accordingly.

Final Step