转载Minim库的自带的范例程序
//AnalyzeSound
/**
* This sketch demonstrates how to use an FFT to analyze
* the audio being generated by an AudioPlayer.
* <p>
* FFT stands for Fast Fourier Transform, which is a
* method of analyzing audio that allows you to visualize
* the frequency content of a signal. You've seen
* visualizations like this before in music players
* and car stereos.
* <p>
* For more information about Minim and additional features,
* visit http://code.compartmental.net/minim/
*/
import ddf.minim.analysis.*;
import ddf.minim.*;
Minim minim;
AudioPlayer jingle;
FFT fft;
void setup()
{
size(512, 200, P3D);
minim = new Minim(this);
// specify that we want the audio buffers of the AudioPlayer
// to be 1024 samples long because our FFT needs to have
// a power-of-two buffer size and this is a good size.
jingle = minim.loadFile("jingle.mp3", 1024);
// loop the file indefinitely
jingle.loop();
// create an FFT object that has a time-domain buffer
// the same size as jingle's sample buffer
// note that this needs to be a power of two
// and that it means the size of the spectrum will be half as large.
fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );
}
void draw()
{
background(0);
stroke(255);
// perform a forward FFT on the samples in jingle's mix buffer,
// which contains the mix of both the left and right channels of the file
fft.forward( jingle.mix );
for(int i = 0; i < fft.specSize(); i++)
{
// draw the line for frequency band i, scaling it up a bit so we can see it
line( i, height, i, height - fft.getBand(i)*8 );
}
}
//pause
/**
* This sketch demonstrates how to use the <code>pause</code> method of a <code>Playable</code> class.
* The class used here is <code>AudioPlayer</code>, but you can also pause an <code>AudioSnippet</code>.
* Pausing a <code>Playable</code> causes it to cease playback but not change position, so that when you
* resume playback it will start from where you last paused it. Press 'p' to pause the player.
*
*/
import ddf.minim.*;
Minim minim;
AudioPlayer groove;
void setup()
{
size(512, 200, P3D);
minim = new Minim(this);
groove = minim.loadFile("groove.mp3", 2048);
groove.loop();
}
void draw()
{
background(0);
stroke(255);
for(int i = 0; i < groove.bufferSize() - 1; i++)
{
line(i, 50 + groove.left.get(i)*50, i+1, 50 + groove.left.get(i+1)*50);
line(i, 150 + groove.right.get(i)*50, i+1, 150 + groove.right.get(i+1)*50);
}
}
void keyPressed()
{
if ( groove.isPlaying() )
{
groove.pause();
}
else
{
// simply call loop again to resume playing from where it was paused
groove.loop();
}
}
//rewind
/**
* This sketch demonstrates how to use the <code>rewind</code> method of a <code>Playable</code> class.
* The class used here is <code>AudioPlayer</code>, but you can also rewind an <code>AudioSnippet</code>.
* Rewinding a <code>Playable</code> sets the position to zero, the beginning. Rewinding doesn't change
* the play state of a <code>Playable</code> so if it is playing or looping when you rewind, it will
* continue to play or loop after you rewind it. Press 'r' to rewind the player.
*
*/
import ddf.minim.*;
Minim minim;
AudioPlayer groove;
void setup()
{
size(512, 200, P3D);
minim = new Minim(this);
groove = minim.loadFile("groove.mp3", 2048);
groove.loop();
}
void draw()
{
background(0);
stroke(255);
for(int i = 0; i < groove.bufferSize() - 1; i++)
{
line(i, 50 + groove.left.get(i)*50, i+1, 50 + groove.left.get(i+1)*50);
line(i, 150 + groove.right.get(i)*50, i+1, 150 + groove.right.get(i+1)*50);
}
float posx = map(groove.position(), 0, groove.length(), 0, width);
stroke(0,200,0);
line(posx, 0, posx, height);
stroke(255);
text("Press any key to rewind.", 10, 20);
}
void keyPressed()
{
groove.rewind();
}