Last active
September 17, 2015 08:07
-
-
Save PCJohn/700ae4572fdd0d7d354d to your computer and use it in GitHub Desktop.
Processing sketch for a music player
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
/* | |
A Processing sketch for an audio player with frequency visualization. | |
Authors: Prithvijit Chakrabarty (prithvichakra@gmail.com) | |
Kartik Lovekar (kslovekar@gmail.com) | |
*/ | |
import ddf.minim.*; | |
import java.io.*; | |
import java.util.*; | |
Minim minim; | |
ddf.minim.AudioPlayer song; | |
AudioMetaData meta; | |
Play playButton; | |
Rewind rewind; | |
Forward ffwd; | |
long startTime = 0; | |
long endTime = 0; | |
ArrayList<String> filePaths = new ArrayList<String>(); | |
boolean play = false; | |
int num = 0; | |
int numFiles = 0; | |
float position; | |
void setup() | |
{ | |
size(500,500); | |
final File folder = new File("M:\\Music\\Sample Music\\MUSIC\\Queens of the Stone Age"); | |
getFilePaths(folder); | |
playButton = new Play((width-width/25)/2, height-height/5, width/25, height/50); | |
rewind = new Rewind(width/2 - 71, height-height/5, width/25, height/50); | |
ffwd = new Forward(width/2 + 50, height-height/5, width/25, height/50); | |
minim = new Minim(this); | |
startTime = System.currentTimeMillis(); | |
loadSong(); | |
} | |
void draw() | |
{ | |
background(0); | |
playButton.update(); | |
playButton.draw(); | |
rewind.update(); | |
rewind.draw(); | |
ffwd.update(); | |
ffwd.draw(); | |
if(play == true) | |
text("Now Playing: "+meta.author()+" - "+meta.title(),8,15); | |
else | |
text("Paused: "+meta.author()+" - "+meta.title(),8,15); | |
fill(255); | |
stroke(255); | |
line(0, 8*height/9, width, 8*height/9); | |
// sDrawing the waveform | |
for(int i = 0; i < song.bufferSize()-1; i++) | |
{ | |
float x1 = map(i, 0, song.bufferSize(), 0, width); | |
float x2 = map(i+1, 0, song.bufferSize(), 0, width); | |
line(x1, height/5 + song.left.get(i)*height/10, x2, height/5 + song.left.get(i+1)*height/10); | |
line(x1, 2*height/5 + song.right.get(i)*height/10, x2, 2*height/5 + song.right.get(i+1)*height/10); | |
} | |
// Mapping position of the seek circle to song length | |
position = map(song.position(), 0, song.length(), 0, width); | |
ellipse(position, 8*height/9, 15, 15); | |
fill(255); | |
if(play == true) | |
endTime = System.currentTimeMillis(); | |
// check if current track is over to load next track | |
if(endTime - startTime >= song.length() && !song.isPlaying() && play == true) | |
{ | |
num++; | |
loadSong(); | |
song.play(); | |
play = true; | |
} | |
} | |
void keyPressed() | |
{ | |
if(key == 'p') | |
{ | |
if(!play) | |
{ | |
song.play(); | |
play = true; | |
//println(play); | |
} | |
else | |
{ | |
song.pause(); | |
play = false; | |
//println(play); | |
} | |
} | |
if(key == 'n') // to load next track | |
{ | |
song.pause(); // pause current track | |
num++; | |
play = true; | |
loadSong(); | |
song.play(); | |
} | |
if(key == 'b') // to load previous track | |
{ | |
song.pause(); // pause current track | |
num--; | |
play = true; | |
loadSong(); | |
song.play(); | |
} | |
} | |
// Fast forwards or rewinds when mouse is pressed | |
void mousePressed() | |
{ | |
playButton.mousePressed(); | |
rewind.mousePressed(); | |
ffwd.mousePressed(); | |
} | |
void mouseReleased() | |
{ | |
playButton.mouseReleased(); | |
rewind.mouseReleased(); | |
ffwd.mouseReleased(); | |
} | |
// loading file paths from specified directory | |
void getFilePaths(final File folder) | |
{ | |
for(final File fileEntry : folder.listFiles()) | |
{ | |
if(fileEntry.isDirectory()) | |
getFilePaths(fileEntry); | |
else | |
{ | |
String path = fileEntry.getPath(); | |
if(path.substring(path.length()-3,path.length()).equals("mp3")) | |
{ | |
filePaths.add(path); | |
numFiles++; | |
} | |
} | |
} | |
} | |
void loadSong() | |
{ | |
if(num == numFiles) // going back to the first track | |
num = 0; | |
if(num == -1) | |
num = 0; | |
startTime = System.currentTimeMillis(); | |
song = minim.loadFile(filePaths.get(num)); | |
meta = song.getMetaData(); | |
position = map(song.position(), 0, song.length(), 0, width); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment