<script>

import java.awt.event.*;

Maxim maxim;
AudioPlayer player1;
AudioPlayer player2;
float speedAdjust = 1.0; //music&animation speeed
boolean volumeBox = false;
float vol1 = 1.0;
float vol2 = 1.0;
float time = 0; //variable used to determine where the music stopped

PImage[] TV;
int currTV = 0;
int TVleftMargin = 48;
int TVupMargin = 165;
PImage[] buttons; //vector with buttons images
PImage[] slider;
int[] sliderYpos = new int[2];
int sliderXpos;

PImage[] movie;
float currFrame = 0;
int movH, movW;
int zoom = 0;
boolean video = true; //true – movie, false – audioVisualisation Algorithm

String[] audioFiles; //vector of string with names of audio files
int maxNameLength = 0; //the maximul length of a string in vector

boolean play = false; //is the player1, player2 playing
boolean stop = false; //was stop button pressed?
boolean loop = true;

boolean selectedPlayer; //true – player1, false – player2
boolean selectBox = false; //box with audio files
int xSB, ySB; //x,y coordinates of the select box

int imgsize = 60; //square size (buttons)
void setup()
{
size(480, 700); //my htc hd2 resolution
background(128);

audioFiles = new String[8]; //vector of string with names of audio files
audioFiles[0] = “atmos1.wav”;
audioFiles[1] = “bass_snare.wav”;
audioFiles[2] = “beat1.wav”;
audioFiles[3] = “beat2.wav”;
audioFiles[4] = “bells.wav”;
audioFiles[5] = “mybeat.wav”;
audioFiles[6] = “ping.wav”;
audioFiles[7] = “sine.wav”;
for (int i =0; i<audioFiles.length; i++) //calculate the maximul length
{
if ( maxNameLength < audioFiles[i].length()) {
maxNameLength = audioFiles[i].length();
}
}

imageMode(CENTER);
TV = new PImage[2];
TV[0] = loadImage(“tv_frame_1.png”);
TV[1] = loadImage(“tv_frame_2.png”);
slider = loadImages(“Slider/slider”, “.png”, 10);
buttons = loadImages(“Buttons/button”, “.png”, 11); //load buttons images
movie = loadImages(“Animation_data/movie”, “.jpg”, 135);
movW = movie[0].width;
if (movW > width) movW = width – TVleftMargin;
movH = movie[0].height;
if (movH > height – (imgsize+10)) movH = height – (imgsize+10) – TVupMargin;

frameRate(60); //frameRate of 60 (60 frames per second)
/* if in a second there are 60 frames, the using this we can
determine the aproximate millisecond the audio stopped on
if we have 354 frames, then we do 354 * 1000/60 and we get the
aproximate millisecond when the audio stopped */
time = 0;

maxim = new Maxim(this);
player1 = maxim.loadFile(“Beats/beat2.wav”);
player1.setLooping(true);
player2 = maxim.loadFile(“Beats/beat1.wav”);
player2.setLooping(true);

sliderYpos[0] = (int)(height – (imgsize + 20) – 100 * 1.0);
sliderYpos[1] = (int)(height – (imgsize + 20) – 100 * 1.0);
sliderXpos = (int)(50 + 75 * 1.0);
}

void draw()
{
stroke(1);
fill(200, 200, 200);
rectMode(CORNER);
rect(0, height – (imgsize + 10), width, 120);
fill(20);
rect(0, 0, width, height – (imgsize + 10));
imageMode(CENTER);
if (video) //show movie
{
image(movie[(int)currFrame], width/2, (height – (imgsize + 10))/2, movH + zoom, movH + zoom);
image(TV[currTV], width/2, (height – (imgsize + 10))/2, width, height – (imgsize + 10));
image(buttons[8], width/2, height – (imgsize + 37), 40, 40);
}
else //show AudioVisual Algorithm
{
image(TV[currTV], width/2, (height – (imgsize + 10))/2, width, height – (imgsize + 10));
image(buttons[9], width/2, height – (imgsize + 37), 40, 40);
}

image(slider[6], 50, height – (imgsize + 37), 9, 9);
image(slider[7], 200, height – (imgsize + 37), 9, 9);
imageMode(CORNER);
image(slider[8], 53, height – (imgsize + 37) – 5, sliderXpos – 53, 9);
image(slider[9], sliderXpos, height – (imgsize + 37) – 5, 197 – sliderXpos, 9);
imageMode(CENTER);
image(slider[5], sliderXpos, height – (imgsize + 37), 21, 21);

if (play) // is the program playing
{
if (player1.isPlaying() == false || player2.isPlaying() == false)
{
play = false;
time = 0;
}
else
{
time++;
currFrame = currFrame+1*speedAdjust;
if (currFrame >= movie.length) {
currFrame = 0;
}
image(buttons[5], width/2 – 1.5 * imgsize – 15, height-(imgsize/2 + 5), imgsize, imgsize);
}
}
else {
image(buttons[0], width/2 – 1.5 * imgsize – 15, height-(imgsize/2 + 5), imgsize, imgsize);
}
image(buttons[10], width/2 – 2.5 * imgsize – 25, height – (imgsize/2 + 5), imgsize – 10, imgsize – 10);
image(buttons[1], width/2 – 1.5 * imgsize – 15 + (imgsize + 10), height – (imgsize/2 + 5), imgsize, imgsize);
for (int i = 2; i < 4; i++) //create the buttons
{
image(buttons[i], width/2 – 1.5 * imgsize – 15 + (i * (imgsize + 10)), height – (imgsize/2 + 5), imgsize – 10, imgsize – 10);
}

/* Volume Image */
if ((vol1 + vol2) > 1) {
image(buttons[4], width/2 – 1.5 * imgsize – 15 + (4 * (imgsize + 10)), height – (imgsize/2 + 5), imgsize – 10, imgsize – 10);
}
else if ((vol1 + vol2) > 0) {
image(buttons[6], width/2 – 1.5 * imgsize – 15 + (4 * (imgsize + 10)), height – (imgsize/2 + 5), imgsize – 10, imgsize – 10);
}
else {
image(buttons[7], width/2 – 1.5 * imgsize – 15 + (4 * (imgsize + 10)), height – (imgsize/2 + 5), imgsize – 10, imgsize – 10);
}

if (selectBox)
{
textSize(20);
stroke(220);
strokeWeight(3);
fill(150);
rect(xSB + 5, ySB – 21 * (audioFiles.length + 1) + 3, 12 * maxNameLength, 21 * audioFiles.length + 3);
for (int i = 0; i < audioFiles.length; i++)
{
fill(255);
text(audioFiles[i], xSB + 8, ySB + 3 – 21 * (audioFiles.length – i));
if (mouseX > xSB + 5 && mouseY > ySB + 3 – 21 * (audioFiles.length + 1 – i) + i * 1
&& mouseX < xSB + 5 + 12 * maxNameLength&& mouseY < ySB + 3 – 21 * (audioFiles.length + 1 – i) + i * 1 + 22)
{
strokeWeight(1);
stroke(200);
fill(0, 0, 180, 80);
rect(xSB + 5, ySB + 3 – 21 * (audioFiles.length + 1 – i) + i * 1, 12 * maxNameLength, 22);
}
}
}
if (volumeBox)
{
rectMode(CENTER);
stroke(220);
strokeWeight(3);
fill(150);
rect(width/2 – 1.5 * imgsize – 15 + (4 * (imgsize + 10)), height – (imgsize + 70), 70, 130);

pushMatrix();
translate(width/2 – 1.5 * imgsize – 15 + (4 * (imgsize + 10)) – 17, height – (imgsize + 20));
rotate(-PI/2);
imageMode(CENTER);
image(slider[1], 0, 0);
image(slider[2], 100, 0);
imageMode(CORNER);
image(slider[3], 4, -slider[3].height/2, 100*vol1, slider[3].height);
image(slider[4], 100*vol1, -slider[4].height/2, 100 – 100*vol1, slider[4].height);
popMatrix();

pushMatrix();
translate(width/2 – 1.5 * imgsize – 15 + (4 * (imgsize + 10)) + 17, height – (imgsize + 20));
rotate(-PI/2);
imageMode(CENTER);
image(slider[1], 0, 0);
image(slider[2], 100, 0);
imageMode(CORNER);
image(slider[3], 4, -slider[3].height/2, 100*vol2, slider[3].height);
image(slider[4], 100*vol2, -slider[4].height/2, 100 – 100*vol2, slider[4].height);
popMatrix();

imageMode(CENTER);
image(slider[0], width/2 – 1.5 * imgsize – 15 + (4 * (imgsize + 10)) – 17, sliderYpos[0]);
image(slider[0], width/2 – 1.5 * imgsize – 15 + (4 * (imgsize + 10)) + 17, sliderYpos[1]);
}
}

void mousePressed()
{
if (mouseButton == RIGHT)
{
currTV++;
if (currTV >= TV.length) currTV = 0;
}
else
{
/* – Play/Pause button */
if (dist(mouseX, mouseY, width/2 – 1.5 * imgsize – 15, height – (imgsize/2 + 5)) < imgsize/2)
{
if (!play)
{
if (stop == false)
{
float sp2 = (player2.getLengthMs()/player1.getLengthMs())*speedAdjust;
player1.cue((int)(time * 1000/60));
player2.cue((int)(time * 1000/60 * sp2));
}
else
{
player1.cue(0);
player2.cue(0);
stop = false;
}
player1.play();
player2.play();
}
else
{
player1.stop();
player2.stop();
}
play = !play;
selectBox = false;
volumeBox = false;
}
/* – Stop button – */
else if (dist(mouseX, mouseY, width/2 – 0.5 * imgsize – 5, height – (imgsize/2 + 5)) < imgsize/2)
{
player1.stop();
player2.stop();
stop = true;
if (play) play = !play;
time = 0;
currFrame = 0;
selectBox = false;
volumeBox = false;
}
/* – Player1 songs list button – */
else if (dist(mouseX, mouseY, width/2 – 1.5 * imgsize – 15 + (2 * (imgsize + 10)), height – (imgsize/2 + 5)) < (imgsize-10)/2)
{
selectedPlayer = true;
selectBox = true;
xSB = mouseX;
ySB = mouseY;
volumeBox = false;
}
/* – Player2 songs list button – */
else if (dist(mouseX, mouseY, width/2 – 1.5 * imgsize – 15 + (3 * (imgsize + 10)), height – (imgsize/2 + 5)) < (imgsize – 10)/2)
{
selectedPlayer = false;
selectBox = true;
xSB = mouseX;
ySB = mouseY;
volumeBox = false;
}
/* – Volume Button – */
else if (dist(mouseX, mouseY, width/2 – 1.5 * imgsize – 15 + (4 * (imgsize + 10)), height – (imgsize/2 + 5)) < (imgsize – 10)/2)
{
selectBox = false;
volumeBox = true;
}
/* – Video change button – */
else if (dist(mouseX, mouseY, width/2, height – (imgsize + 37)) < 20)
{
video = !video; //change between movie and AudioVisual Algorithm
selectBox = false;
volumeBox = false;
}
else if (dist(mouseX, mouseY, width/2 – 2.5 * imgsize – 25, height – (imgsize/2 + 5)) < (imgsize – 10)/2)
{
loop = !loop;
player1.setLooping(loop);
player2.setLooping(loop);
selectBox = false;
volumeBox = false;
}
else if (volumeBox) //volumeBox active
{
//if mouseX, mouseY outside box then close volumeBox
if (mouseX < width/2 – 1.5 * imgsize – 15 + (4 * (imgsize + 10)) – 35 || mouseY < height – (imgsize + 70) – 65 ||
mouseX > width/2 – 1.5 * imgsize – 15 + (4 * (imgsize + 10)) + 35 || mouseY > height – (imgsize + 70) + 65)
{
volumeBox = false;
}
}
else if (selectBox)
{
selectBox = false;
for (int i = 0; i < audioFiles.length; i++)
{
if (mouseX > xSB + 5 && mouseY > ySB + 3 – 21 * (audioFiles.length + 1 – i) + i * 1
&& mouseX < xSB + 5 + 12 * maxNameLength&& mouseY < ySB + 3 – 21 * (audioFiles.length + 1 – i) + i * 1 + 22)
{
fill(0, 0, 180, 120);
rect(xSB + 5, ySB + 3 – 21 * (audioFiles.length + 1 – i) + i * 1, 12 * maxNameLength, 22);

player1.stop();
player2.stop();
stop = true;

if (selectedPlayer)
{
player1 = maxim.loadFile(“Beats/” + audioFiles[i]);
}
else
{
player2 = maxim.loadFile(“Beats/” + audioFiles[i]);
}

if (play) play = !play;
time = 0;

player1.speed(speedAdjust);
player2.speed((player2.getLengthMs()/player1.getLengthMs())*speedAdjust);
}
}
}
else {
selectBox = false;
volumeBox = false;
}
}
}

/* — Doesn’t work on Android version — */
void keyPressed()
{
if (key == CODED)
{
if (keyCode == UP)
{
zoom += 5;
if (movie[0].width + zoom > width – TVleftMargin)
zoom = width – TVleftMargin – movie[0].width;
else if (movie[0].height + zoom > height – (imgsize+10) – TVupMargin)
zoom = height – (imgsize+10) – TVupMargin – movie[0].height;
}
else if (keyCode == DOWN)
{
zoom -= 5;
if (movie[0].width + zoom < movie[0].width)
zoom = 0;
else if (movie[0].height + zoom < movie[0].height)
zoom = 0;
}
}
}

void mouseDragged()
{
if (volumeBox)
{
int x = (int)(width/2 – 1.5 * imgsize – 15 + (4 * (imgsize + 10)));
for (int i = 0; i < 2; i++)
{
if (mouseX >= x-17 + i*34-11 && mouseX <= x-17+i*34+11 &&
mouseY > sliderYpos[i] – 11 && mouseY < sliderYpos[i] + 11)
{
int my = constrain(mouseY, height – (imgsize+20) – 100, height – (imgsize+20));
sliderYpos[i] = my;
}
}
vol1 = (height – (imgsize+20) – sliderYpos[0])*0.01;
player1.volume(vol1);
vol2 = (height – (imgsize+20) – sliderYpos[1])*0.01;
player2.volume(vol2);
}

int y = (int)(height – (imgsize + 37) – 5);
//image(slider[5], 50 + 75 * speedAdjust, height – (imgsize + 37), 21, 21);
if (mouseX >= sliderXpos – 11 && mouseX <= sliderXpos + 11 &&
mouseY >= y – 11 && mouseY <= y + 11)
{
//fill(255);
//rect(100, 100, 40, 40);
int mx = constrain(mouseX, 50, 200);
sliderXpos = mx;
//sliderXpos = 100;
}

speedAdjust = (float)(sliderXpos – 50) / 75;
player1.speed(speedAdjust);
player2.speed((player2.getLengthMs()/player1.getLengthMs())*speedAdjust);

 

/*
if (mouseY>height/2)
{
speedAdjust=map(mouseX, 0, width, 0, 2);
}*/
}

//The MIT License (MIT)

//Copyright (c) 2013 Mick Grierson, Matthew Yee-King, Marco Gillies

//Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
//The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

 

PImage [] loadImages(String stub, String extension, int numImages)
{
PImage [] images = new PImage[0];
for(int i =0; i < numImages; i++)
{
PImage img = loadImage(stub+i+extension);
if(img != null)
{
images = (PImage [])append(images,img);
}
else
{
break;
}
}
return images;
}

</script>