Created
December 5, 2012 12:42
-
-
Save SanderSmeekes/4215212 to your computer and use it in GitHub Desktop.
Play sound with Minim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This sketch demonstrates how to play a file with Minim. <br /> | |
* It's also a good example of how to draw waveforms using the sample buffers of an AudioSource. | |
*/ | |
// Example by Tom I | |
import ddf.minim.*; | |
Minim minim; | |
AudioPlayer player; | |
void setup() | |
{ | |
size(512, 200, P3D); | |
minim = new Minim(this); | |
// load a file, give the AudioPlayer buffers that are 2048 samples long | |
player = minim.loadFile("marcus_kellis_theme.mp3", 2048); | |
// play the file | |
player.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 < player.bufferSize() - 1; i++) | |
{ | |
float x1 = map(i, 0, player.bufferSize(), 0, width); | |
float x2 = map(i+1, 0, player.bufferSize(), 0, width); | |
line(x1, 50 + player.left.get(i)*50, x2, 50 + player.left.get(i+1)*50); | |
line(x1, 150 + player.right.get(i)*50, x2, 150 + player.right.get(i+1)*50); | |
} | |
} | |
void stop() | |
{ | |
// always close Minim audio classes when you are done with them | |
player.close(); | |
// always stop Minim before exiting | |
minim.stop(); | |
super.stop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment