Manual: AudioListener
[ javadoc ]
The AudioListener interface is used by the Recordable interface and defines only two methods. These methods are:
void samples(float[] samples) void samples(float[] samplesLeft, float[] samplesRight)
One of these methods will be called repeatedly by the Recordable object that an AudioListener has been added to, depending upon whether the audio being monitored is mono or stereo. There is no guarantee that the array(s) passed to listeners are unique. For efficiency reasons, the same array(s) could be passed to all listeners of a particular Recordable. It is therefore important that an AudioListener not modify the array(s) it receives so that all listeners are guaranteed to receive the same information. Unfortunately, Java does not provide away to enforce this, so it is left to implementers to play nice. Another thing to be aware of is that the samples method of a listener will likely be called from a thread other than the one the object is created in. This means that implementers will probably want to make their implementations synchronized to prevent inconsistency.
Code Sample (online example)
import ddf.minim.*; // the methods of this class are sychronized so that // the left and right arrays are not reassigned while // being accessed in the draw method class WaveformRenderer implements AudioListener { private float[] left; private float[] right; WaveformRenderer() { left = null; right = null; } synchronized void samples(float[] samp) { left = samp; } synchronized void samples(float[] sampL, float[] sampR) { left = sampL; right = sampR; } synchronized void draw() { if ( left != null && right != null ) { noFill(); stroke(255); beginShape(); for ( int i = 0; i < left.length; i++ ) { vertex(i, height/4 + left[i]*50); } endShape(); beginShape(); for ( int i = 0; i < right.length; i++ ) { vertex(i, 3*(height/4) + right[i]*50); } endShape(); } else if ( left != null ) { noFill(); stroke(255); beginShape(); for ( int i = 0; i < left.length; i++ ) { vertex(i, height/2 + left[i]*50); } endShape(); } } } AudioPlayer groove; WaveformRenderer waveform; void setup() { size(512, 200); // always start Minim first Minim.start(this); groove = Minim.loadFile("groove.mp3", 512); groove.loop(); waveform = new WaveformRenderer(); groove.addListener(waveform); } void draw() { background(0); waveform.draw(); } void stop() { // always close Minim audio classes when you are done with them groove.close(); // always stop Minim before exiting Minim.stop(); super.stop(); }