Drawing a waveform
If you've used any kind of audio editor before, you'll most likely know that audio is usually displayed as a waveform. In this recipe, we'll take a look at how we can visualize a song by drawing its waveform to the screen.
How to do it...
The first part of this sketch should look familiar. It's basically the same as the sketch you made in the Playing audio files recipe.
import ddf.minim.*; import ddf.minim.signals.*; import ddf.minim.analysis.*; import ddf.minim.effects.*; Minim minim; AudioPlayer player; void setup() { size( 1024, 480 ); smooth(); minim = new Minim( this ); player = minim.loadFile("song.mp3", 1024); player.play(); strokeWeight( 2 ); }
The code that analyses the audio and renders the waveform goes in the draw()
function. Since our song is a stereo audio file, we'll render the left and right channels in different colors, to the screen. Don't forget to add the stop()
function at the end of your sketch.
void draw() { background(...