Project-10: Sonic Story

My Project

//Chuong Truong;
//cbtruong;
//cbtruong@andrew.cmu.edu;
//Section B;

//The Overall Story of this program is that of fall;
//Leaves are falling off of trees, the sun is getting whiter;
//The air is getting frostier;
//And the birds are seen more actively flying;
//I included no custom sound and instead wanted to portray a scene;
//That was described with a song;


//initial empty array that will hold leaf objects;
var leaves = [];
//variables for the sun's green and blue values which change;
var sunG = 238;
var sunB = 82;
//initial empty array for birds;
var birds = [];

//function of the leaf object that allows it to fall;
function leafFall (){
    //stops each leaf from falling off the canvas;
    //if it has not reached that point, it will continue to fall until it does;
    if (this.f == true){
        if (this.y > height-17){
            this.y = height-5;
        }
        else {
            this.y += random(30, 60);
        }
    }
    //if met, this condition causes the leaf to fall;
    //this randomization occurs to allow for the tree and leaves to be more lifelike;
    //as not all leaves fall off immediately;
    else if (floor(random(200)) == 25){
            this.y += random(30);
            this.x += random(-15, 15);
            this.f = true;

        }
    return this.y;
}

//function that draws the leaves;
function drawLeaf(){
    //this switch and case allows for color variance for the leaves;
    //the colors are those of fall;
    switch(this.col){
      case 0: fill(237, 164, 23); break;
      case 1: fill(167, 159, 15); break;
      case 2: fill(233, 134, 4); break;
      case 3: fill(223, 57, 8); break;
      case 4: fill(201, 30, 10); break;
    }
    rectMode(CENTER);
    push();
    translate(this.x, this.y);
    rotate(radians(45));
    rect(0, 0, 15, 15);
    pop();
}

//function that makes leaves;
function leaf(lx, ly, cPreset){
    //the leaf object constructor;
    l = {x: lx, y: ly, orgY: ly, col: cPreset, f: false, 
        drawFunction: drawLeaf, fallFunction: leafFall};
    return l;
}

function makeLeavesOnTrees (x, y){
    //for loops create the leaves on the trees themselves;
    //the leaves' x and y are dependent on the tree's x and y;
    //the first row is 17 and then decreases by 1 as the row gets closer to the tree's x;
    //as the row gets closer, the leaves' initial x and y increase;

    //this variable allows the leaves to center as each row of them becomes less;
    //it is initalized again to allow for the top row to be centered correctly;
    //like it was before the nested for loops below change it;
    var centeringX = 80;

    //the starting number of leaves for each row
    var leavesRowNum = 17;

    //these nested for loops creates the leaves and gives them their inital x and y;
    //the inner loop creates the leaves for each row, giving them the x, y, and colorPreset;
    //the outer loop controls the leaves' y, as it decreases, a new row will not only be drawn;
    //but that row's total number of leaves decreases, and the centering of them is centered;
    //the loops repeat until only 5 leaves are left and they are closest to the tree's x and y;
    for (var j = 120; j > -10; j-= 10){
        for (var i = 0; i < leavesRowNum; i++){
        var l = leaf(x-centeringX + i*10, y-j, floor(random(5)));
        leaves.push(l);
    }
    //the leaves row number decreases to make each row have less leaves;
    leavesRowNum--;
    //the centering is decreased to make sure the row, as it becomes less in number;
    //is centered correctly to the tree's x;
    centeringX -= 5;
    }
}

//this function creates bird objects;
function bird (bx, by){
    //bird object constructor;
    b = {x: bx, y: by, orgY: by, flyFunction: birdFly, drawFunction: drawBird};
    return b;
}

//function that allows the bird to fly;
function birdFly (){
    this.x -= 10;
    if (this.x < 0){
        this.x = width - 20;
    }
    if (this.y > this.orgY + 20 ){
        this.y -= 5;
    }
    else {
        this.y += 5;
    }
}

//this draws the bird;
function drawBird(){
    fill(36, 143, 190);
    circle(this.x, this.y, 20);
    fill(30);
    circle(this.x, this.y, 5);
    fill(131, 67, 51);
    triangle(this.x-15, this.y, this.x - 10, this.y - 2, this.x - 10, this.y+2);
    stroke(0);
    line(this.x - 15, this.y, this.x - 10, this.y);
    fill(16, 123, 170);
    push();
    rectMode(CORNER);
    translate(this.x, this.y);
    rotate(270);
    rect(0+5, 0 - 12, 15, 10);
    rect(0+5, 0 + 4, 15, 10);
    pop();
}


function setup() {
    createCanvas(700, 500);
    frameRate(10);
    useSound();
    frameRate(3);

    //calls the makeLeavesOnTree function to create the leaves pattern;
    //on each tree dependent on the trees' location;
    makeLeavesOnTrees(125, 390);
    makeLeavesOnTrees(350, 390);
    makeLeavesOnTrees(575, 390);

    //calls the bird function and creates the bird objects;
    for (var i = 0; i < 7; i++){
         var bD = bird(random(100, 600), random(70, 120));
         birds.push(bD);
    }
}
//I kept the same sound setup from the assignment because I liked the sounds;
function soundSetup() { 
    osc = new p5.Oscillator();
    osc.amp(0.25);
    osc.setType('sine');
    osc.start();
}


function draw() {
    //this creates the background of a blue sky and  green grass;
    background(154, 211, 222);
    noStroke();
    rectMode(CORNER);
    fill(24, 136, 39);
    rect(0, 475, 700, 5);
    fill(14, 146, 49);
    rect(0, 480, 700, 20);

    //calls the drawTree function to draw trees;
    drawTree(125, 390);
    drawTree(350, 390);
    drawTree(575, 390);
    
    //this for loop updates and draws each leaf in the leaves array;
    for (var i = 0; i < leaves.length; i++){
        var l = leaves[i];
        l.fallFunction();
        l.drawFunction();
    }
    
    //calls the clouds function to make clouds;
    drawClouds(125, 80);
    drawClouds(325, 130);
    drawClouds(525, 105);

    //draws the sun;
    //this if statement allows for the sun to slowly change colors;
    if (!(sunG == 250 & sunB == 193)){
        sunG += 1.2/5;
        sunB += 11.1/5;
    }
    noStroke();
    fill(255, sunG, sunB);
    circle(600, 75, 100);
    print(leaves.length);

    //draws the birds;
    for (var o = 0; o < birds.length; o++){
        var b = birds[o];
        b.flyFunction();
        b.drawFunction();
    }

    //I turned the code from the assignment into a function;
    musicPlayer(pitches, durations);
}

//function that draws the trees without the leaves;
function drawTree (x, y){
    fill(191, 75, 25);
    rectMode(CENTER);
    rect(x, y, 20, 205);
    push();
    translate(x - 40, y - 40);
    rotate(radians(-30));
    rect(0, 0, 10, 150);
    pop();
    push();
    translate(x + 40, y - 40);
    rotate(radians(30));
    rect(0, 0, 10, 150);
    pop();
    push();
    translate(x - 30, y - 70);
    rotate(radians(-30));
    rect(0, 0, 8, 100);
    pop();
    push();
    translate(x + 30, y - 70);
    rotate(radians(30));
    rect(0, 0, 8, 100);
    pop();
    push();
    translate(x - 20, y - 90);
    rotate(radians(-30));
    rect(0, 0, 5, 80);
    pop();
    push();
    translate(x + 20, y - 90);
    rotate(radians(30));
    rect(0, 0, 5, 80);
    pop();
    push();
    translate(x - 5, y - 100);
    rotate(radians(-30));
    rect(0, 0, 3, 60);
    pop();
    push();
    translate(x + 5, y - 100);
    rotate(radians(30));
    rect(0, 0, 3, 60);
    pop();
    rect(x, y - 105, 2, 40);
}

//this function creates the clouds and draws them;
function drawClouds (x, y){
    fill(243, 242, 231);
    ellipse(x, y, 80, 50);
    ellipse(x + 20, y + 10, 80, 40);
    ellipse(x - 40, y + 10, 80, 30);
    ellipse(x - 10, y + 20, 70, 50);
}

//I used the same code from the assignments to make the music;

//arrays for the pitches and durations;
//also count variables for the index and frameCount;
//finally, the boolean rest is added to account for rests in the music;

//I changed the pitches and went with a simple 1 beat per note;
var pitches = [ 64, 64, 62, 64, 64, 62, 0, 62, 64, 66, 65, 67, 65, 0,
  60, 60, 62, 66, 65, 64, 62, 68, 68, 65, 64, 62, 62 ];
var durations = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
var indexTracker = 0;
var frameCount = 0;
var rest = false;

//I turned the code that creates the music with different notes and beat durations into a function;
//the function takes a note array and a corresponding note duration array;
function musicPlayer (noteArray, noteDurationArray){

    //code for the piano to play;
    //this sets the frequencies of the oscillator to the MIDI pitches;
    osc.freq(midiToFreq(noteArray[indexTracker]));
    
    //checks for when the note's duration is over and acts accordingly;
    if (frameCount > noteDurationArray[indexTracker]){
        //this checks for whether the tune has been completely played;
        //if it has been, then the program and oscillator ends;
        if (indexTracker + 1 == noteArray.length) {
            //print(frameCount);
            //print(indexTracker);
            osc.stop();
            noLoop();
        }
        //if it has not been completely played, then the next note will play;
        //with the frameCount reset to 0;
        else {
        print(frameCount);
        indexTracker++;
        //after the indexTracker is increased;
        //it checks to see if the new note is a rest;
        //if it is, then it deals according;
        //it also turns on the oscillator after a rest;
        if (noteArray[indexTracker] != 0){
            if (rest == true) {
            osc.start();
            rest = false;
                }
            }
        else if (noteArray[indexTracker] == 0){
            rest = true;
            osc.stop();
            }
        }
        frameCount = 0;
    }
    else {
        frameCount++;
    }
    print(noteArray[indexTracker]);
}

Instead of making 4 sounds, I used the same code from the assignments to make a song that I felt fit what I wanted to show, a scene of Fall. That scene has birds flying, leaves falling, and a sun that becomes whiter and whiter to fit the upcoming Winter.

Sonic Story : frog in the rain

file

//Story:
//There is a frog sitting on the ground who waves at you.
//You can here rain and frogs croaking in the background.
//a rain cloud appears and soon the frog stops waving
//as the rain cloud moves across the screen, thunder strikes and makes the frog blush

var m = 13; // for my measurement ration out of 13
var f = 220; //frog size
var thunder;
var raining;
var frog;

function preload() {
   frog = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/frogs.wav");
   raining = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/rain.wav");
   thunder = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/thunderstorm.wav");
}

function soundSetup() { // setup for audio generation
    frog.setVolume(1.3);
    raining.setVolume(0.7);
    thunder.setVolume(5);
}

function setup() {
    createCanvas(400, 400);
    frameRate(1);
    useSound();
}

function froggie(){
    translate(90,170);
    strokeWeight(5);
    fill(168, 239, 154);
    beginShape(); //measurements are by #/13 because my reference was 13 x 13 roughly
    curveVertex((11.4 / m) * f, (12.4 / m) * f);//1
    curveVertex((11.4 / m) * f, (12.4 / m) * f);//1
    curveVertex((12.5 / m) * f, (9.2 / m) * f);//2
    curveVertex((9.4 / m) * f, (2.5 / m) * f);//4 (skips 3 did not use point)
    curveVertex((8.9 / m) * f, (.5 / m) * f);//5
    curveVertex((8.1 / m) * f, (.1 / m) * f);//6
    curveVertex((7.4 / m) * f, (.5 / m) * f);//7
    curveVertex((6 / m) * f, (.4 / m) * f);//8
    curveVertex((4.6 / m) * f, (.5 / m) * f);//9
    curveVertex((3 / m) * f, (.2 / m) * f);//10
    curveVertex((1.5 / m) * f, (3 / m) * f);//11
    curveVertex((2.3 / m) * f, (5.3 / m) * f);//12
    curveVertex((2 / m) * f, (6.5 / m) * f);//13
    curveVertex((1.9 / m) * f, (7.5 / m) * f);//13.5 (new point)
    curveVertex((2 / m) * f, (9 / m) * f);//14
    curveVertex((2.2 / m) * f, (9.7 / m) * f);//15
    curveVertex((1.4 / m) * f, (10.4 / m) * f);//15.5 (new point)
    curveVertex((.8 / m) * f, (10.8 / m) * f);//16
    curveVertex((.2 / m) * f, (10.5 / m) * f);//17
    curveVertex((.8/ m) * f, (12.4 / m) * f);//18
    curveVertex((6.5/ m) * f, f);//19
    endShape(CLOSE);
    
    noFill(); 
    beginShape();
    curveVertex((3.8 / m) * f,(1.9 / m) * f);
    curveVertex((3.8 / m) * f,(1.9 / m) * f);
     curveVertex((4.2 / m) * f,(1.95 / m) * f);
    curveVertex((5 / m) * f,(1.7 / m) * f);
     curveVertex((5.8 / m)*f,(1.95 / m) * f);
    curveVertex((6.2 / m)*f,(1.9 / m) * f);
    curveVertex((6.2 / m)*f,(1.9 / m) * f);
    endShape();

    beginShape(); // arm on left
    curveVertex((2.3 / m) * f, (9.6 / m) * f);
    curveVertex((2.3 / m) * f, (9.6 / m) * f);
    curveVertex((2.5 / m) * f, (10.7 / m) * f);
    curveVertex((3.3 / m) * f, (11.8 / m) * f);
    curveVertex((3.5 / m) * f, (9.2 / m) * f);
    curveVertex((3.5 / m) * f, (9.2 / m) * f);
    endShape();

    beginShape(); // foot on right
    curveVertex((8 / m) * f, (12.9 / m) * f);
    curveVertex((8 / m) * f, (12.9 / m) * f);
    curveVertex((7.6 / m) * f, (11 / m) * f);
    curveVertex((8.4 / m) * f, (11.4 / m) * f);
    curveVertex((8.8 / m) * f, (10.6 / m) * f);
    curveVertex((9.7 / m) * f, (10.5 / m) * f);
    curveVertex((9.7 / m) * f, (10.5 / m) * f);
    endShape();
}

function arm1() { //wave arm
    strokeWeight(5);
    noFill(); 
    beginShape(); // arm on left
    curveVertex((5.5 / m) * f, (9.2 / m) * f);
    curveVertex((5.5 / m) * f, (9.2 / m) * f);
    curveVertex((5.7 / m) * f, (10.7 / m) * f);
    curveVertex((6.5 / m) * f, (11.8 / m) * f);
    curveVertex((7.2 / m) * f, (9.2 / m) * f);
    curveVertex((7.2 / m) * f, (9.2 / m) * f);
    endShape();
} 

function arm2() { //wave arm
    beginShape(); // arm on left
    curveVertex((5.5 / m) * f, (9.2 / m) * f);
    curveVertex((5.5 / m) * f, (9.2 / m) * f);
    curveVertex((5.7 / m) * f, (9.7 / m) * f);
    curveVertex((6.5 / m) * f, (10.5 / m) * f);
    curveVertex((7.2 / m) * f, (9.2 / m) * f);
    curveVertex((7.2 / m) * f, (9.2 / m) * f);
    endShape();
} 

function arm3() { //wave arm
    beginShape(); // arm on left
    curveVertex((5.5 / m) * f, (9.2 / m) * f);
    curveVertex((5.5 / m) * f, (9.2 / m) * f);
    curveVertex((5.7 / m) * f, (9 / m) * f);
    curveVertex((6.5 / m) * f, (8.8 / m) * f);
    curveVertex((7.2 / m) * f, (9.2 / m) * f);
    curveVertex((7.2 / m) * f, (9.2 / m) * f);
    endShape();
} 

function arm4() { //wave arm
    beginShape(); // arm on left
    curveVertex((5.5 / m) * f, (9.2 / m) * f);
    curveVertex((5.5 / m) * f, (9.2 / m) * f);
    curveVertex((6 / m) * f, (7 / m) * f);
    curveVertex((6.5 / m) * f, (7 / m) * f);
    curveVertex((7.2 / m) * f, (9.2 / m) * f);
    curveVertex((7.2 / m) * f, (9.2 / m) * f);
    endShape();
} 

function arm5() { //wave arm
    beginShape(); // arm on left
    curveVertex((5.5 / m) * f, (9.2 / m) * f);
    curveVertex((5.5 / m) * f, (9.2 / m) * f);
    curveVertex((5 / m) * f, (7 / m) * f);
    curveVertex((5.7 / m) * f, (7 / m) * f);
    curveVertex((7.2 / m) * f, (9.2 / m) * f);
    curveVertex((7.2 / m) * f, (9.2 / m) * f);
    endShape();
} 

function arm6() { //wave arm
    beginShape(); // arm on left
    curveVertex((5.5 / m) * f, (9.2 / m) * f);
    curveVertex((5.5 / m) * f, (9.2 / m) * f);
    curveVertex((7.3 / m) * f, (7 / m) * f);
    curveVertex((8 / m) * f, (7 / m) * f);
    curveVertex((7.2 / m) * f, (9.2 / m) * f);
    curveVertex((7.2 / m) * f, (9.2 / m) * f);
    endShape();
} 

function eyes1() {
    fill(0);
    ellipse((3 / m) * f, (1.2 / m) * f, 7, 10);
    ellipse((7.7 / m) * f, (1.2 / m) * f, 7, 10);
}

function eyes2() {
    arc((3 / m) * f, (1.2 / m) * f, 7, 10, 0, PI, CHORD);
    arc((7.7 / m) * f, (1.2 / m) * f, 7, 10, 0, PI, CHORD);
}

function eyes3() {
    rect((2.9 / m) * f, (1.1 / m) * f, 4, .05);
    rect((7.6 / m) * f, (1.1 / m) * f, 4, .05);
}

function blush(alpha) {
    push();
    translate(90,180);
    noStroke();
    fill(239, 101, 58, alpha);
    circle((2.5 / m) * f, (2.3 / m) * f, 30);
    circle((8 / m) * f, (2.3 / m) * f, 30);
    pop();
}

function cloud(b) { //b is x movement of cloud
    translate(-90, -180);
    fill(240);
    strokeWeight(0);
    beginShape();
    vertex(-50 + b, 100);
    vertex(-50 + b, 100);
    vertex(-150 + b, 100);
    curveVertex(-170 + b, 90);
    curveVertex(-165 + b, 85);
    curveVertex(-170 + b, 50);
    curveVertex(-75 + b, 80);
    curveVertex(-55 + b, 85);
    curveVertex(-45 + b, 90);
    endShape(CLOSE);
}

function ground(){
    fill(124, 66, 55);
    ellipse(200, 430, 550, 300);
}

function drawEyes(){
    if(frameCount % 4 === 0) { //for eyes //frameCount starts @ 0 
        eyes1();
      }
    if(frameCount % 4 === 1 || frameCount % 4 === 3) {
        eyes2();
      }
    if(frameCount % 4 === 2){
        eyes3();
      }
}

function drawarm() {
    if(frameCount % 1 === 0 & frameCount <= 1 || frameCount >= 16){
        arm1();
      }
    if(frameCount < 3 & frameCount >= 2 || frameCount == 15){
        arm2();
      }
    if(frameCount < 4 & frameCount >= 3 || frameCount == 14){
        arm3();
      }
    if(frameCount == 5 || frameCount == 7 || frameCount == 9 || 
        frameCount == 11 || frameCount == 13) {
        arm4();
      }
    if(frameCount == 4 || frameCount == 12 || frameCount == 8) {  
        arm5();
      }
    if(frameCount == 6 || frameCount == 10) {
        arm6();
      }
}

function rain(b, c, d) { //c is x movement of rain, d is y movement 
    fill(58, 101, 239); 
    constrain(c, -50 + b, -150 + b);
    constrain(d, 100, 400);
    circle(-87.5 + c + b, -145 + d, 5);//need to use noise
}

function draw() {
    background(173, 220, 216);
    ground();
    fill(0);
    if(frameCount > 0){
    text("Froggie in rain", 10, 15);
    froggie();
    drawarm(); 
    drawEyes();
    frog.play();
    raining.play();
    }
    if(frameCount >= 7){
        cloud(-170 + (frameCount * 35));
        for(var i = 0; i <= frameCount; i++){
        rain(-170 + (frameCount * 35), random(-85,40), random(250,600)); 
        }
      }
    if(frameCount >= 18){
        blush(45 * frameCount);//take 5 frames to get to full opac
      }
    if(frameCount >= 17){
      thunder.play();
      }
    if(frameCount > 22){
      frameCount === 1;
      }
}


I ended up using a ton of curves to draw my frog, so I have a ton of sketches showing my progress. I first worked by making my animations and characters and then moving on to sound so I knew my code better and new if things were not working last second it would be the sound files.

the numbers and amount of curves
figuring out my statistics

Project: Sonic Story

luca sonic story

//Luca Cao
//lucacao@andrew.cmu.edu
//Section D

//Story: 
//pan put on stove. Sound 1
//Gas starts. Sound 2
//Steak cooks on pan and sizzle. Sound 3
//eats steak. Sound 4

var pan = {x:200,y:400,d:100,r:73,g:80,b:87,
            hx:200,hy:480,hw:15,hh:70,dy:20};//object pan
var stv = {x:200,y:200,d:150,r:33, g:37, b:41};//object stove
var stk = {x:200,y:200,w:40,h:60,r:220,g:24,b:54,dy:30};//object steak
var metal;
var gas;
var steak;
var eat;

function preload(){
    metal = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/metal.wav");
    gas = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/gas1.wav");
    steak = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/steak.wav");
    eat = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/eat.wav");

}

function soundSetup(){
    metal.setVolume(5);
    gas.setVolume(0.5);
    steak.setVolume(1);
    eat.setVolume(2);
}

function setup() {
    createCanvas(400, 400);
    noStroke();
    frameRate(2);
    useSound();
}

function draw() {
    background(255, 243, 176);

    //platform
    push();
    fill(173, 181, 189);
    rect(0,0,400,400);
    fill(108, 117, 125);
    rect(0,320,400,90);
    pop();

    //controls
    push();
    if (pan.y <= 350){
        fill(0,255,0);
        ellipse(300,350,10,10);
        ellipse(350,350,10,10);
    }
    pop();

    //stove
    push();
    fill(stv.r,stv.g,stv.b);
    ellipse(stv.x,stv.y,stv.d,stv.d);

    if (pan.y <= 300){
        stv.r = 200;
        stv.g = 0;
        stv.b = 0;
        gas.play();
    }
    pop();

    //pan
    push();
    rectMode(CENTER);
    fill(pan.r,pan.g,pan.b);
    ellipse(pan.x,pan.y,pan.d,pan.d);
    rect(pan.hx,pan.hy,pan.hw,pan.hh);
    pan.y = pan.y - pan.dy
    pan.hy = pan.hy - pan.dy

    if (pan.y <= 200){
        gas.stop();
        pan.dy = 0; 
    }
        //pan.hy = 280
    
    pop();


    //steak
    if (frameCount >= 15){
        fill(stk.r,stk.g,stk.b);
        ellipse(stk.x,stk.y,stk.w,stk.h);
        steak.play();
        

        if (frameCount >= 20){
            stk.r = 157;
            stk.g = 2;
            stk.b = 8;

            if (frameCount >= 25){
                stk.r = 106;
                stk.g = 4;
                stk.b = 15;

                if (frameCount >= 35){
                    stk.r = 80;
                    stk.g = 6;
                    stk.b = 23;
                    stk.y = stk.y - stk.dy;
                    steak.stop();

                    if (frameCount >= 45){
                        eat.play();
                    }

                    if (frameCount >= 50){
                        eat.stop();
                        background(0);
                        noLoop();
                    }
                }
            }
        }
    }
}

I created the story of cooking a steak. I started by animating all my visual assets and making sure that they appear at the right time. After that, I added sound into my code based on the position of the assets, as well as the frame count. I stored all the values on my characters in object form, which helped me to better manipulate them. During the process, I had difficulties preloading sounds into my code and I later realized that I used the wrong template for my code. I enjoyed making this project because, besides visual interaction, I also get to experiment with sonic interaction and make the outcome more engaging.

Project 10: Sonic Story

My project is a simple animation of a bee flying through some daisies and a sunflower to collect nectar. For my process, I created several objects that I animated first. Afterward, I inserted sounds that at different places within the story where the action was happening.

Canvas is 900 pixels wide so you can see the right side on here 🙁

sketch
//Anthony Pan
//Section C

var index = 0;
var duration = 0;
var sunflower;
var daisy;
var daisy2;
var bee;
var cloud;

//sounds
var beeSound;
var windSound;
var popSound;
var yumSound;



//bee positions
var beex = [900, 880, 860, 840, 820, 800, 780, 760, 740, 720, 700, 
680, 660, 640, 620, 600, 580, 560, 540, 540, 540, 540, 540, 540, 540, 540, 500, 450, 400, 200, -100];

//noise for beey heights
var noiseParam = 0;
var noiseStep = 0.05;
var beey = [];

//cloud positions
var cloudX = [];

function preload() {
    beeSound = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/beeBuzz.wav");
    windSound = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/wind.wav");
    popSound = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/pop.wav");
    yumSound = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/yum.wav");

}


function soundSetup() {
    windSound.amp(0.4);
    beeSound.amp(0.8);
    popSound.amp(1.0);
    yumSound.amp(0.8);
}



function setup() {
    createCanvas(900, 400);
    frameRate(1);
    useSound();

    //random y positions of beey
    for(var i=0; i < 30; i++) {
        var n = noise(noiseParam);
        var value = map(n, 0, 1, 0, height);
        beey.push(value);
        noiseParam += noiseStep;
    }

    //positions for cloudX
    for(var j = 0; j < 30; j++) {
        var cloudvalue = 900 - (j * 20);
        cloudX.push(cloudvalue);

    }


    //make objects
    daisy = makeDaisy(width/2, 2* height/3, 100);
    sunflower = makeSunflower(width/4, height/2, 80);
    daisy2 = makeDaisy(2 * width/3, height/3, 100);
    bee = makeBee(beex[index], beey[index]);
    cloud = makeCloud();

    
}

function draw() {
    //draw sky
    background(149, 217, 255);
    cloud.draw(cloudX[index], 100);

    //play windSound at end
    if(frameCount >= 28 & frameCount <= 31) {
        windSound.play();

    } 
    
    sunflower.draw();
    daisy.draw();
    daisy2.draw();

    //daisy petal fall 
    if(index >= 20) {
        fill(255);
        ellipse(600, index * 15, 60, 160);
    }

    //daisy petal pop
    if(frameCount >= 23 & frameCount <= 24) {
        popSound.play();
    }

    //play yum sound when bee is above flower
    if(index >= 20 & index < 21) {
        yumSound.play();
    }

    //sunflower petal pop
    if(frameCount >= 24 & frameCount <= 25) {
        popSound.play();
    }

    //sunflower petal fall
    if(index >= 24) {
        fill("yellow");
        ellipse(width/4, 50+index*15, 20, 180);
        ellipse(1.25 * width/4, 50+index*15, 20, 180);

    }

    bee.draw(beex[index], beey[index]);

    //play bee sound at beginning
    if(frameCount >= 0 & frameCount <= 3) {
        beeSound.play();

    }

    
    index += 1;

    //stop sounds at 30 seconds
    if(index >= 32) {
        popSound.stop();
        windSound.stop();
        beeSound.stop();
        yumSound.stop();
        background(0);
        noLoop();
    }





}

//cloud constructor
function makeCloud(cx, cy) {
    var cloud = {x: cx, y: cy, draw: drawCloud}
    return cloud;

}

//draw cloud
function drawCloud() {
    fill(230);
    ellipse(cloudX[index], 100, 300, 100);
    ellipse(cloudX[index]-90, 110, 100, 80);
    ellipse(cloudX[index]-30, 120, 100, 80);
    ellipse(cloudX[index]+30, 120, 100, 80);
    ellipse(cloudX[index]+90, 110, 100, 80);

    ellipse(cloudX[index]-90, 90, 100, 80);
    ellipse(cloudX[index]-30, 80, 100, 80);
    ellipse(cloudX[index]+30, 80, 100, 80);
    ellipse(cloudX[index]+90, 90, 100, 80);

}

//constructor for daisy
function makeDaisy(fx, fy, fh) {
    var daisy = {x: fx, y: fy, height: fh, draw: drawDaisy}
    return daisy; //return daisy as object

}

//draw daisy
function drawDaisy() {
    fill(10, 200, 20);
    rect(this.x, this.y, 40, height);
    //petals
    for(var i = 0; i < 10; i++) {
        push();
        translate(this.x, this.y);
        noStroke();
        var rotationAngle = radians(36);
        rotate(i * rotationAngle);
        fill(255);
        ellipse(0, -60, 60, 160);
        pop();

    }

    fill("yellow");
    noStroke();
    circle(this.x, this.y, 80);

}

//constructor for sunflower
function makeSunflower(sx, sy, sh) {
    var sunflower = {x: sx, y: sy, height: sh, draw: drawSunflower}
    return sunflower;

}

function drawSunflower() {
    fill(10, 200, 20);
    rect(this.x, this.y, 40, height/2);
    for(var i = 0; i < 40; i++) {
        push();
        translate(this.x, this.y);
        var rotationAngle2 = radians(9);
        rotate(i * rotationAngle2);
        fill("yellow");
        ellipse(0, -80, 20, 180);
        pop();
    }
    fill(33, 12, 11);
    circle(this.x, this.y, 200);
}


//bee constructor
function makeBee(bx, by) {
    var bee = {x: bx, y: by, draw: drawBee}
    return bee;

}

function drawBee() {
    //wings
    stroke(0);
    fill(255);
    ellipse(beex[index]-5, beey[index]-30, 20, 40);
    ellipse(beex[index]+10, beey[index]-20, 20, 40);

    //body
    fill(0);
    ellipse(beex[index], beey[index], 60, 40);

    //stinger
    triangle(beex[index]+25, beey[index]+5, beex[index]+40, beey[index], beex[index]+25, beey[index]-5);

    //stripes
    noStroke();
    fill("yellow");
    rect(beex[index], beey[index]-20,5, 40);
    rect(beex[index]-10, beey[index]-20, 5, 40);
    rect(beex[index]+10, beey[index]-20, 5, 40);


}