xxxxxxxxxx
474
let incrementCircle = true;
let greenB=true;
let blueB=true;
let yellowB=true;
let playBg1 = true;
let playStart = true;
let start = true;
let numOfCircles = 0;
let circleX = [];
let circleY = [];
let circleWidth = []
let s = 0;
let alt = true;
let mode = 'pause';
let drum, flute, sitar, veena;
let volumeSlider;
let vol;
function setup() {
startSound = loadSound('audio/bubbles.mp3');
starting = loadSound('audio/start.mp3');
volumeSlider = createSlider(0,100);
volumeSlider.position(650, 20);
drum = loadSound('audio/tabla.mp3');
flute = loadSound('audio/bansuri.mp3');
sitar = loadSound('audio/strike.mp3');
veena = loadSound('audio/spiral.mp3');
bgMusic = loadSound('audio/calmBg.mp3');
//drumA=createAudio('audio/drum.wav');
startImg = loadImage('images/pak music.png');
displayImg = loadImage('images/display.png');
startMusic = loadSound('audio/clay.mp3');
createCanvas(800, 800);
angleMode(DEGREES);
rectMode(CENTER);
}
function draw() {
vol = volumeSlider.value();
drum.setVolume(vol);
flute.setVolume(vol);
veena.setVolume(vol);
sitar.setVolume(vol);
if (mode == 'pause')
{
startPage();
}
if (mode == 'play')
{
background(0);
for (i = 0; i < 10; i++)
{
fill('#F44336A5');
//CAN DRAW PERMANENT CIRCLES
noStroke();
ellipse(width/2, height/2, s+15*i, s+15*i);
fill('rgb(126,9,9)');
ellipse(width/2, height/2, s-i*10, s-i*10);
}
s = s+10;
if (playStart == true){
startSound.play();
playStart = false;
}
}
if (mode == 'drums')
{ //if button pressed, random circles
if (incrementCircle == false)
{
playDrums();
}
else
{
drawCircles();
}
}
if (mode == 'sitar')
{
if (blueB == false )
{
background(0);
}
else if (blueB == true)
{
playSitar();
}
}
if (mode == 'flute')
{
console.log(mode);
console.log(greenB);
if (greenB == false)
{
background(0);
playFlute();
}
else if (greenB == true)
{
console.log('calling playFlute')
playFlute();
}
}
if (mode == 'veena')
{
if (yellowB == false)
{
background(0);
}
else if (yellowB==true)
{
playVeena();
}
}
textSize(12);
if (!serialActive)
{
text("Press Space Bar to select Serial Port", 20, 30);
} else
{
text("Connected", 20, 30);
let volumeLabel = "Volume: " + vol;
text(volumeLabel, 600, 20 );
//everything else goes here
}
}
function readSerial(data) {
////////////////////////////////////
//READ FROM ARDUINO HERE
////////////////////////////////////
if (data != null)
{
//console.log("data:");
console.log(data);
// make sure there is actually a message
let fromArduino = split(trim(data), ",");
// if the right length, then proceed
if (fromArduino.length == 5)
{
let redButton = int(fromArduino[0]);
if (redButton == 1)
{
mode = 'drums';
if (incrementCircle == true)
{
numOfCircles = numOfCircles + 1;
circleX[numOfCircles] = random(width);
circleY[numOfCircles] = random(height);
circleWidth[numOfCircles] = random(100);
incrementCircle = false;
drum.play();
}
}
else if (redButton == 0)
{
incrementCircle = true;
}
let greenButton = int(fromArduino[1]);
if (greenButton == 1)
{
mode = 'flute';
if(greenB==true)
{
console.log("greenb set to false");
flute.play();
greenB = false;
}
}
else if (greenButton == 0)
{
greenB = true;
console.log("greenb set to true");
}
let yellowButton = int(fromArduino[2]);
if (yellowButton == 1)
{
mode = 'sitar';
if (yellowB == true)
{
sitar.play();
yellowB=false;
}
}
else if (yellowButton == 0)
{
yellowB = true;
}
let blueButton = int(fromArduino[3]);
if (blueButton == 1)
{
mode = 'veena';
if (blueB == true)
{
veena.play();
blueB=false;
}
}
else if (blueButton == 0)
{
blueB = true;
}
let songButton = int(fromArduino[4]);
if (songButton ==1)
{
console.log('here');
if (playBg1 == true && mode == 'play')
{
console.log('here1');
bgMusic.play();
playBg1 = false;
}
}
else if (bgMusic.isPlaying() == true && mode == 'play' && songButton == 0)
{
console.log('here2');
bgMusic.stop();
playBg1 = true;
}
if (greenButton == 0 && blueButton ==0 && yellowButton == 0 && redButton == 0 && mode != 'pause')
{
mode = "play";
}
// if (playBg1==true && mode == 'play')
// {
// startMusic.play();
// playBg1 = false;
// }
// else {
// //calmBg.stop();
// }
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
}
//////////////////////////////////
//SEND TO ARDUINO HERE (handshake)
//////////////////////////////////
let light;
if (mode != 'pause')
{
light = 1;
}
else if (mode =='pause')
{
light =0;
}
let sendToArduino = light + "\n";
writeSerial(sendToArduino);
}
}
function startPage()
{
//background('white');
image(startImg, 0, 0, width, height);
//fill(255);
// textSize(64);
// text("موسیقی",60,120);
// textSize(20);
// fill(0);
// rect(width/3-10,height-50,200,60);
fill(255);
let x = mouseX;
let y = mouseY;
let t = x+','+y;
text(t, mouseX, mouseY);
//text("Press 's' to play your own music", width/3-230, height-10);
textSize(14);
textStyle(NORMAL);
text("Play, mix, and visualize Pakistani music by pressing the buttons", width/4, 3*height/4+50);
if (mouseX >= 242 && mouseX <=558 && mouseY >=679 && mouseY <= 764)
{
noStroke();
fill('#503418')
ellipse(390,720,120,75);
fill(255);
textSize(38);
//fontWeight(200);
textStyle(BOLD);
text("PLAY",335,734);
if (mouseIsPressed)
{
mode='play';
}
}
}
function keyPressed()
{
if (key == " ")
{
// important to have in order to start the serial connection!!
setUpSerial();
if (playStart==true && mode == "play")
{
startMusic.play();
playStart = false;
}
startMusic.play();
}
if (key == 's')
{
mode = "play";
}
// if (key =='d')
// {
// mode = 'drums'
// incrementCircle = false;
// drum.play();
// }
// if (key == 'f')
// {
// mode = 'flute';
// flute.play();
// }
// if (key == 'g')
// {
// mode = 'sitar';
// sitar.play();
// }
// if (key == 'v')
// {
// mode = 'veena';
// veena.play();
// }
}
function drawCircles()
{
noStroke();
//fill(random(255));
fill(240);
for (i =0; i < numOfCircles; i++)
{
circle(circleX[i], circleY[i], circleWidth[i]);
}
}
function playDrums()
{
if (alt== true)
{alt = false}
else
{alt =true;}
background('black');
for (i = 23; i > 0; i--)
{
noStroke();
if (alt == true)
{
if(i%2 == 0)
{
fill('white');
}
else
{
fill('black');
}
}
else
{
if(i%2 == 0)
{
fill('black');
}
else
{
fill('white');
}
}
ellipse(width/2, height/2, random(50*i));
}
fill(0);
//ellipse(width/2,height/2,40)
}
function playFlute()
{
console.log("here ");
background(0);
noFill();
push();
translate(width/2, height/2);
for (let i =0; i < 200; i++)
{
push();
rotate(sin(frameCount+i)*100);
let r = map(sin(frameCount), -1,1,50,255);
let g = map(cos(frameCount/2),-1,1,50,255);
let b = map(sin(frameCount/4),-1,1,50,255);
noFill();
stroke(r,g,b);
rect(0,0,800-i*2,300-i);
pop();
}
pop();
}
function playSitar()
{
noStroke();
for (i =30; i>0; i--)
{
if (i%2==0)
{
fill(0);
}
else
{
fill(255,150,0);
}
square(width/2, height/2, random(50*i));
}
}
let direction = 1;
function playVeena()
{
background(0);
noStroke();
let x=random(width);
let y=0;
direction = random(-1,1);
//random(0,width);
//y=random(0,height);
for (i =0; i < 3*width;i++)
{
x=x+direction;
y=y+1;
fill(random(x,x+30),random(y-20,y+10),random(y-30,y+20));
circle(x,y,30);
}
}
function displayScreen()
{
image(displayImg, 0,0, width, height);
}