xxxxxxxxxx
186
// inspired by a project i worked on last summer
//last summer's code - https://editor.p5js.org/Alexis_Sanders/sketches/mXiWdTijg
//also used help from the class example - https://editor.p5js.org/Alexis_Sanders/sketches/Uv2NRfQRs
//used the ml5 pose net reference for more help - https://learn.ml5js.org/#/reference/posenet
//used this example for the sound: https://editor.p5js.org/monniqian/sketches/TwckaaeGl
//used this reference to help with the sound code: https://p5js.org/reference/#/p5.Oscillator
//smoothing tutorial from Beth: https://javascript.plainenglish.io/simple-smoothing-for-posenet-keypoints-cd1bc57f5872
console.log("ml5 version:", ml5.version);
let ball;
let speed = 5;
let myColor = [
"crimson",
"dodgerblue",
"yellow",
"limegreen",
"purple",
"orange",
"blueviolet",
"aquamarine",
];
let size = 50;
let counter = 0;
let cam, poseNet;
let poses = [];
let person;
//create variable to hold past x positions
let recentX = [];
let recentY = [];
let numX = 5;
let numY = 5;
// i have my notes here in an array with a vairable for my oscilaor under it
let notes = [60, 62, 64, 65, 67, 69, 71, 72]; //notes C D E F G A B C on a midi piano
let osc;
function setup() {
createCanvas(640, 480);
cam = createCapture(VIDEO);
cam.hide();
poseNet = ml5.poseNet(cam, modelLoaded);
poseNet.on("pose", gotResults);
poseNet.flipHorizontal = true;
// //here is where i have started my oscilator and turned the ampliture down to 0
// //sqr = square oscilator, different type of oscilator
osc = new p5.TriOsc();
osc.start();
osc.amp(0);
noStroke();
ball = new Ball(width / 2, 0, size, speed);
}
function draw() {
//
background(220);
push();
//to mirror the ball to match the camera
//flips the x start and end points, instead of it being 0, width, its now width,0
translate(cam.width, 0);
//actually flips the image
scale(-1, 1);
image(cam, 0, 0);
pop();
if (person != undefined) {
ball.display();
// console.log(person.pose.keypoints); //want to use the nose then use person.pose.nose.x/y
rectMode(CENTER);
let xPerson = person.pose.nose.x;
let avgX = averageXPos(xPerson);
let yPerson = person.pose.nose.y;
let avgY = averageYPos(yPerson);
rect(xPerson, yPerson, 100, 75, 15);
if (ball.y - ball.size > windowHeight - ball.size) {
let randomIndex = floor(random(notes.length));
ball.col = myColor[randomIndex];
ball.note = notes[randomIndex];
ball.y = 0;
ball.x = int(random(ball.size * 2, windowWidth - ball.size * 2));
// console.log(ball.x, ball.y, ball.color);
}
// console.log(ball.x, ball.y);
if (
yPerson - 25 < ball.y + ball.size / 2 &&
yPerson > ball.y - ball.size / 2 &&
xPerson - 50 < ball.x + ball.size / 2 &&
xPerson + 50 > ball.x - ball.size / 2
) {
// console.log('hit');
let randomIndex = floor(random(notes.length));
ball.col = myColor[randomIndex];
ball.note = notes[randomIndex];
// console.log(ball.note);
ball.y = 0;
ball.x = int(random(ball.size * 2, windowWidth - ball.size * 2));
counter += 1;
//turns amplitude up and plays the note selscted
osc.amp(0.3);
playNote(ball.note);
if (counter > 10) {
ball.speed += 0.5;
}
}
}
}
function modelLoaded() {
console.log("Model loaded");
}
function gotResults(results) {
person = results[0];
}
function playNote(note) {
//changes the midi note to a frequency the computer can understand
osc.freq(midiToFreq(note));
}
function averageXPos(x) {
// adds the current x pos the the array and only runs once
if (recentX.length < 1) {
for (let i = 0; i < numX; i++) {
recentX.push(x);
}
} else {
//updates the array with the most recent x number
recentX.shift(); //removes the first item from the array
recentX.push(x); //pushes the new x values into the array
}
let sum = 0;
for (let i = 0; i < recentX.length; i++) {
sum += recentX[i];
}
return sum / recentX.length;
}
function averageYPos(y) {
if (recentY.length < 1) {
for (let i = 0; i < numY; i++) {
recentY.push(y);
}
} else {
recentY.shift();
recentY.push(y);
}
let sum = 0;
for (let i = 0; i < recentY.length; i++) {
sum += recentY[i];
}
return sum / recentY.length;
}
class Ball {
constructor(x, y, size, speed) {
this.x = x;
this.y = y;
this.size = size;
this.col = myColor[0];
this.note = notes[0];
this.speed = speed;
}
display() {
fill(this.col);
this.y += this.speed;
circle(this.x, this.y, this.size);
}
}