If you have played mp3 files in visualizers of windows media players or any other media player then you must have came across various design patterns , visualization running in background.Processing IDE also provides such capabilities where you can create any graphical effects.The code focuses on fetching the mp3 file performing various file handling operations on it (reading,writing) also performing a spectral analysis or fast fourier transform (FFT) of the audio file.
Minim uses a BeatDetect class.
The BeatDetect class allows us to analyze an audio stream for beats (rhythmic onsets). Beat Detection Algorithms by Frederic Patin describes beats in the following way:
The human listening system determines the rhythm of music by detecting a pseudo periodical succession of beats. The signal which is intercepted by the ear contains a certain energy, this energy is converted into an electrical signal which the brain interprets. The more energy the sound transports, the louder the sound will seem. But a sound will be heard as a beat only if his energy is larger than the prior sound's energy history, that is to say if the brain detects a brutal variation in sound energy. Therefore if the ear intercepts a monotonous sound with sometimes big energy peaks it will detect beats, however, if you play a continuous loud sound you will not perceive any beats. Thus, the beats are big variations of sound energy.
Minim uses a BeatDetect class.
The BeatDetect class allows us to analyze an audio stream for beats (rhythmic onsets). Beat Detection Algorithms by Frederic Patin describes beats in the following way:
The human listening system determines the rhythm of music by detecting a pseudo periodical succession of beats. The signal which is intercepted by the ear contains a certain energy, this energy is converted into an electrical signal which the brain interprets. The more energy the sound transports, the louder the sound will seem. But a sound will be heard as a beat only if his energy is larger than the prior sound's energy history, that is to say if the brain detects a brutal variation in sound energy. Therefore if the ear intercepts a monotonous sound with sometimes big energy peaks it will detect beats, however, if you play a continuous loud sound you will not perceive any beats. Thus, the beats are big variations of sound energy.
Minim
Minim is an audio library that uses the JavaSound API , a bit of Tritonus, and Javazoom's MP3SPI to provide an easy to use audio library for people developing in the Processing environment. The philosophy behind the API is to make integrating audio into your sketches as simple as possible while still providing a reasonable amount of flexibility for more advanced users.
Some of the features of Minim:
For beginners they can play around with QuickStart guide to Minim and load sketches in processing like one i did as an example.So follow these steps :-
Some of the features of Minim:
- AudioPlayer: Mono and Stereo playback of WAV, AIFF, AU, SND, and MP3 files.
- AudioMetaData: An object filled with metadata about a file, such as ID3 tags.
- AudioRecorder: Mono and Stereo audio recording either buffered or direct to disk.
- AudioInput: Mono and Stereo input monitoring.
- AudioOutput: Mono and Stereo sound synthesis.
- FFT: perform a Fourier Transform on audio data to generate a frequency spectrum.
- BeatDetect: a class for doing beat detection.
- A real-time synthesis framework based around unit generators, which we call UGens.
For beginners they can play around with QuickStart guide to Minim and load sketches in processing like one i did as an example.So follow these steps :-
In your processing sketch import the minim library
import ddf.minim.*;
To begin using minim with processing first make an object of minim class which can be used to load audio files or acquire inputs or outputs
like this:-
Minim minim;
AudioPlayer song;
AudioInput input;
It is very easy to play an audio file in processing using minim all you need to make sure is that you have copied your desired audio (mp3 file in data folder of your sketch so that it can easily access the file).If your sketch is giving null pointer exception it is because the audio file is not in the sketch's folder make sure you have copied file properly.
Below is a small code you can try :-
import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;
Minim minim;
AudioPlayer song;
AudioInput input;
void setup()
{
size(512,200,P3D);
minim = new Minim(this);
// this loads Infinity - Wicked Game Carolina Wallace Mashup Cover.mp3 from the data folder
song =minim.loadFile("Infinity_Wicked_Game_Carolina_Wallace_Mashup_Cover.mp3");
song.play();
}
void draw()
{
background(0);
stroke(255);
// draw the waveforms
// the values returned by left.get() and right.get() will be between -1 and 1,
// so we need to scale them up to see the waveform
// note that if the file is MONO, left.get() and right.get() will return the same value
for(int i = 0; i < song.bufferSize() - 1; i++)
{
float x1 = map( i, 0, song.bufferSize(), 0, width ); //map(value, start1, stop1, start2, stop2)
float x2 = map( i+1, 0, song.bufferSize(), 0, width );
line( x1, 50 + song.left.get(i)*50, x2, 50 + song.left.get(i+1)*50 );
line( x1, 150 + song.right.get(i)*50, x2, 150 + song.right.get(i+1)*50 );
}
}
import ddf.minim.*;
To begin using minim with processing first make an object of minim class which can be used to load audio files or acquire inputs or outputs
like this:-
Minim minim;
AudioPlayer song;
AudioInput input;
It is very easy to play an audio file in processing using minim all you need to make sure is that you have copied your desired audio (mp3 file in data folder of your sketch so that it can easily access the file).If your sketch is giving null pointer exception it is because the audio file is not in the sketch's folder make sure you have copied file properly.
Below is a small code you can try :-
import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;
Minim minim;
AudioPlayer song;
AudioInput input;
void setup()
{
size(512,200,P3D);
minim = new Minim(this);
// this loads Infinity - Wicked Game Carolina Wallace Mashup Cover.mp3 from the data folder
song =minim.loadFile("Infinity_Wicked_Game_Carolina_Wallace_Mashup_Cover.mp3");
song.play();
}
void draw()
{
background(0);
stroke(255);
// draw the waveforms
// the values returned by left.get() and right.get() will be between -1 and 1,
// so we need to scale them up to see the waveform
// note that if the file is MONO, left.get() and right.get() will return the same value
for(int i = 0; i < song.bufferSize() - 1; i++)
{
float x1 = map( i, 0, song.bufferSize(), 0, width ); //map(value, start1, stop1, start2, stop2)
float x2 = map( i+1, 0, song.bufferSize(), 0, width );
line( x1, 50 + song.left.get(i)*50, x2, 50 + song.left.get(i+1)*50 );
line( x1, 150 + song.right.get(i)*50, x2, 150 + song.right.get(i+1)*50 );
}
}
In above vizulaiser you can see the waveform generated the values returned by left.get() and right.get() will be between -1 and 1,so we need to scale them up to see the waveform.
Note that if the file is MONO, left.get() and right.get() will return the same value
Note that if the file is MONO, left.get() and right.get() will return the same value
Frequency Spectrum
While your mp3 plays you can see and analyze your mp3 file's frequency spectrum using FFT class.The fft object created will be analysing the buffer size and the sample rate at which the analysis takes place.I would be using this code in next blog to make a beat listening light.
import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;
Minim minim;
AudioPlayer song;
FFT fft;
void setup()
{
size(512,200);
minim = new Minim(this);
song = minim.loadFile("Moves Like Jagger.mp3",512);
song.play();
fft = new FFT(song.bufferSize(),song.sampleRate());
}
void draw()
{
background(0);
fft.forward(song.mix);
stroke(255,0,0,128);
for(int i=0;i<fft.specSize();i++){
line(i,height,i,height-fft.getBand(i)*4);
}
stroke(255);
for(int i = 0; i < song.left.size() - 1; i++)
{
line(i, 50 + song.left.get(i)*50, i+1, 50 + song.left.get(i+1)*50);
line(i, 150 + song.right.get(i)*50, i+1, 150 + song.right.get(i+1)*50);
}
}
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;
Minim minim;
AudioPlayer song;
FFT fft;
void setup()
{
size(512,200);
minim = new Minim(this);
song = minim.loadFile("Moves Like Jagger.mp3",512);
song.play();
fft = new FFT(song.bufferSize(),song.sampleRate());
}
void draw()
{
background(0);
fft.forward(song.mix);
stroke(255,0,0,128);
for(int i=0;i<fft.specSize();i++){
line(i,height,i,height-fft.getBand(i)*4);
}
stroke(255);
for(int i = 0; i < song.left.size() - 1; i++)
{
line(i, 50 + song.left.get(i)*50, i+1, 50 + song.left.get(i+1)*50);
line(i, 150 + song.right.get(i)*50, i+1, 150 + song.right.get(i+1)*50);
}
}
Resource :
http://code.compartmental.net/tools/minim/quickstart/
http://code.compartmental.net/tools/minim/quickstart/