This is code for my project one tab is called Music Project and the other is called star. I also have another file which contains code for the button that can be pushed to stop and play music. The button code on its own works fine, however when I try to add it to my project code I get errors and the code will not run. I know for sure the button code can be added and it will work but I’m not adding it correctly. I have tried moving the code around make sure that its put in the draw statement, put setup code in setup, have minim etc. I’m still not able to properly add my code in so everything can run. Please let me know suggestions where and how I should add the button code so my project runs, thanks. Here is my code:
MUSIC PROJECT TAB/CODE
//minim is the audio library allowing for us to play and pull the music in
import ddf.minim.*;
Minim minim;
//all of the songs that we have
AudioPlayer song1;
AudioPlayer song2;
AudioPlayer song3;
AudioInput in;
//number of stars & speed is declaring the varible for the speed of the stars
Star[] stars = new Star[100];
float speed;
//creating a filler variable for the color of the lines, then sets the height and
//width variable for the button
color c;
float buttonH = 50;
float buttonW = 200;
// number of lines in x & y direction, float magitude is the wave,boolean is declaring the variable for the if statement involving line, line 21 are the parameteres for the line movement
int linesX = 40;
int linesY = 26;
float magnitude;
boolean voice = false;
float stepsX, stepsY, radius, intensity, movement, last_sum, scale, factor, wave, sum;
//only runs once, full screen makes the screen full screen, noSmooth is for making the lines of the wave jagged
void setup() {
fullScreen();
noSmooth();
//once the stars position is equal to >0 it is reset randomly back to the top of the screen, the for loop takes into account the full length of
//the star rather than resetting it as soon as it hits the bottom
for (int i = 0; i < stars.length; i++) {
stars[i] = new Star();
}
//tells minim to locate the song file and play it, then song1,2,3 play each of the mp3 files, .play plays the whole song, .rewind rewinds current song and plays the new song
//keep the wavelength within the screen
minim = new Minim(this);
song1 = minim.loadFile("Shawn James- Aint No Sunshine - Bill Withers Cover.mp3");
song1.play();
song1.rewind();
factor = float(width)/song1.bufferSize();
minim = new Minim(this);
song2 = minim.loadFile("Jack Garratt - The Love Youre Given.mp3");
song2.play();
song2.rewind();
factor = float(width)/song2.bufferSize();
minim = new Minim(this);
song3 = minim.loadFile("Tobu - Higher.mp3");
song3.play();
song3.rewind();
factor = float(width)/song3.bufferSize();
}
//continuously executes the line of the code contained in its block unit the program is stopped, noCursor just makes the cursor diappeared
void draw(){
noCursor();
//link the value of the speed variable to the mouse position
{
speed = map(mouseX, 0, width, 0, 20);
background(0);
//moving the center of the screen from the top left corner to the center of the canvas
translate(width/2, height/2);
//draw each star, running the update method to its position and show method to show it on the canvas
for (int i = 0; i < stars.length; i++) {
stars[i].update();
stars[i].show();
}
}
{
//moving the center of the screen from the top left corner to the center of the canvas
translate(-width /2, -height /2);
c = color(170 + wave/2, wave*5, 255,255);
fill(random(0,255), random(0,255), random(0,255));
stroke(random(0,255), random(0,255), random(0,255));
strokeWeight(2);
sum = 0;
// creates sound waves that change in frequency and amplitude, initially makes it so the array "i" is less than that of the songs buffer size or its bass. The "if" and "else" statements
//lock the line on the left and right side of the screen at exactly the middle y value of the entire screen. It brings in factor just putting all of the parameters of the line in one place
for (int i = 0; i < song1.bufferSize() - 1; i++)
{
if (voice) {
line(i*factor, height/2 + in.left.get(i)*last_sum + 1, i*factor+1, height/2 + in.left.get(i+1)*last_sum + 2);
sum += abs(in.left.get(i));
} else {
line(i*factor, height/2 + song1.left.get(i)*last_sum + 1, i*factor+1, height/2 + song1.left.get(i+1)*last_sum + .05);
sum += abs(song1.left.get(i));
}
}
}
//makes the two sections wavelength equal
last_sum = sum;
{
//randomizes the color for the moon
fill(random(0,255), random(0,255), random(0,255));
//draws the moon shape, and noStroke gives it no outline
ellipse(width /2, height /2, 500, 500);
noStroke();
}
{
//puts the moon the in the center, and fills it the color white, and noStroke makes it have no outline, the next ellipse statement is for the black circle on top of the white
// that makes the cresent shape
ellipseMode(CENTER);
fill (0);
ellipse(width /2 -50,height /2 -50, 460, 450);
noStroke();
}
{
//makes the thickness of all lines to 2
strokeWeight(2);
}
}
STAR TAB/CODE
//create a "Star" class
class Star {
// variables specify the x and y of each star,z variable wil be used in a formula to modify the stars position
float x;
float y;
float z;
// avariable to store the previous value of the z variable
// (the value of the z variable at the previous frame).
float pz;
Star() {
//x&y places values in the variables, note: height and width are the same: the canvas is a square,note: the z value can't exceed the width/2 (and height/2) value,
//because use "z" as divisor of the "x" and "y", whose values are also between "0" and "width/2"
//pz=z : set the previous position of "z" in the same position of "z", stars are not moving during the first frame.
x = random(-width/2, width/2);
y = random(-height/2, height/2);
z = random(width/2);
pz = z;
}
void update() {
// here you take the variable z and divide it to get the new x,y coordinates of the star, the speed value is bigger= z decreses
// when the "z" value equals to 1, the star have passed the
// you can place it on more time in the canvas, with new x, y and z values.
// this way we also avoid a potential division by 0.
z = z - speed;
if (z < 1) {
z = width/2;
x = random(-width/2, width/2);
y = random(-height/2, height/2);
pz = z;
}
}
void show() {
float temp= random(0, 100);
//if the speed of the stars is less than 50 then we display random colors for the stars with no outline which is the noStroke
if (temp > 50 )
fill(random(0,255), random(0,255), random(0,255));
else
fill(random(0,255), random(0,255), random(0,255));
noStroke();
// map will give new star positions
// the division x / z get a number between 0 and a very high number,
// map this number (proportionally to a range of 0 - 1), inside a range of 0 - width/2.
//this makes sure the new coordinates "sx" and "sy" move faster at each frame
// and which they finish their travel outside of the canvas (they finish when "z" is less than a).
float sx = map(x / z, 0, 1, 0, width/2);
float sy = map(y / z, 0, 1, 0, height/2);
//the z value to increase the star size between a range from 0 to 16.
float r = map(z, 0, width/2, 16, 0);
ellipse(sx, sy, r, r);
//use the "pz" valute to get the previous position of the stars,
//to draw a line from the previous position to the new (current) one.
float px = map(x / pz, 0, 1, 0, width/2);
float py = map(y / pz, 0, 1, 0, height/2);
pz = z;
stroke(255);
line(px, py, sx, sy);
}
}
BUTTON CODE
import ddf.minim.*;
Minim minim;
AudioPlayer player;
boolean play;
int button_x = 40, button_y = 40, button_sz = 20;
void setup()
{
minim=new Minim(this);
player=minim.loadFile("Tobu - Higher.mp3");
}
void draw(){
background(20, 0, 0);
stroke(255, 0, 255);
fill(50, 0, 120);
rect(button_x, button_y, button_sz, button_sz);
if (play)
player.play();
else
player.pause();
}
void mousePressed(){
if( mouseX > button_x && mouseX < button_x + button_sz &&
mouseY > button_y && mouseY < button_y + button_sz){
play = !play;//will switch false/true each click
}
}