Hello there !
So I have been given a code and the goal is to create a music player .
I must add features such as play, pause, stop and volume buttons and also a basic bar with the music timer that fills in color, like in VLC.
For now I would like to focus on the music vizualiser part, I have this idea that the music could paint a picture. I need to use FTT (music spectrum) :
-the ftt spectrum must take all of my screen (like in VLC)
-the bands need to leave traces after their passage, with a randomly generated color
If you have any other suggestions I am open, as soon as we stick to my idea of painting something out with a music !
import g4p_controls.*; // G4P library for GUI button, etc
import processing.sound.*; // sound library for audio processing
PFont f; // font for showing text on the canvas
String audioFileName1;
SoundFile audio1;
FFT fft;
int bands = 64;
float[] spectrum = new float[bands];
boolean playstop = false; // keep track of play/pause status
boolean loading = false; // keep track of if we're loading a file
void setup()
{
f = createFont("SansSerif-24.vlw", 24); // load the font
textFont(f);
fft = new FFT(this, bands); // prepare the fft
size(800, 600); // canvas size
frameRate(30); // canvas refresh rate
background(0); // black background/clear scren
createGUI(); // initialise the GUI
}
void draw()
{
background(0); // clear screen
if(audio1 != null) // check if we have a sound file loadd
{
loading = false; // if so, we're not loading anymore
fft.analyze(spectrum); // analyse the file
for(int i = 0; i < bands; i++) // show the analysis
{
// The result of the FFT is normalized.
// draw a rectangle for each frequency band.
noStroke();
fill(spectrum[i]*255, 255-spectrum[i]*255,0 ); // pick a color depending on energy in the band
ellipse(i*width/bands, height - spectrum[i]*height,
width/bands, spectrum[i]*height); // draw a rectangle with height proportional to the spectral energy
}
}
else
{
if(loading == true) // we're loading
{
loadingScreen(); // show a message
}
}
}
void fileSelectedA(File selection)
{
if (selection == null)
{
println("Window was closed or cancelled.");
}
else
{
audioFileName1 = selection.getAbsolutePath();
println(audioFileName1);
loading = true;
audio1 = new SoundFile(this, audioFileName1);
fft.input(audio1);
playstop = true;
audio1.play();
}
}
void loadingScreen()
{
textAlign(CENTER);
fill(#FFFFFF);
text("loading...", width/2, height/2);
}
void keyPressed() {
if (key == ' ') // check if user hits space bar, play/pause.
{
if(playstop == true)
{
playstop = false;
audio1.pause();
}
else
{
playstop = true;
audio1.play();
}
}
}
here is the GUI file with the button so you can upload a song
/* =========================================================
* ==== WARNING ===
* =========================================================
* The code in this tab has been generated from the GUI form
* designer and care should be taken when editing this file.
* Only add/edit code inside the event handlers i.e. only
* use lines between the matching comment tags. e.g.
void myBtnEvents(GButton button) { //_CODE_:button1:12356:
// It is safe to enter your event code here
} //_CODE_:button1:12356:
* Do not rename this tab!
* =========================================================
*/
public void button1_click1(GButton source, GEvent event) { //_CODE_:selectfile1:208344:
//println("selectfile1 - GButton >> GEvent." + event + " @ " + millis());
selectInput("Select primary audio file:", "fileSelectedA");
} //_CODE_:selectfile1:208344:
// Create all the GUI controls.
// autogenerated do not edit
public void createGUI(){
G4P.messagesEnabled(false);
G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
G4P.setMouseOverEnabled(false);
surface.setTitle("Sketch Window");
selectfile1 = new GButton(this, 702, 12, 80, 30);
selectfile1.setText("Select file");
selectfile1.addEventHandler(this, "button1_click1");
}
// Variable declarations
// autogenerated do not edit
GButton selectfile1;