xxxxxxxxxx
196
let angle = 0;
let visualizers = []; // Array to hold ShapeVisualizer instances
let oscillators = {}; // Object to hold active oscillators
let playingNotes = {}; // Track which notes are currently playing
let reverb; // Declare reverb globally
let c=0;
let gameState = "start";
let img;
let notes = {
'A': 261.63, // C4
'S': 293.66, // D4
'D': 329.63, // E4
'F': 349.23, // F4
'G': 392.00, // G4
'H': 440.00, // A4
'J': 493.88, // B4
'K': 523.25, // C5
};
function preload(){
img = loadImage("music.png");
}
function setup() {
createCanvas(windowWidth, windowHeight);
colorMode(HSB, 360, 100, 100, 100);
noFill();
let positions = [
[width * 0.5, height * 0.5],
[width * 0.25, height * 0.25],
[width * 0.75, height * 0.25],
[width * 0.25, height * 0.75],
[width * 0.75, height * 0.75]
];
for (let i = 0; i < positions.length; i++) {
visualizers.push(new ShapeVisualizer(positions[i][0], positions[i][1]));
}
reverb = new p5.Reverb(); // Initialize reverb
}
function draw() {
if (gameState == "game") {
background(0, 0.1);
let newPositions = [
[width * 0.5, height * 0.5],
[width * 0.25, height * 0.25],
[width * 0.75, height * 0.25],
[width * 0.25, height * 0.75],
[width * 0.75, height * 0.75]
];
for (let i = 0; i < visualizers.length; i++) {
visualizers[i].updatePosition(newPositions[i][0], newPositions[i][1]);
}
let activeNotes = Object.keys(playingNotes).length;
let minSize = 1;
let maxSize = map(activeNotes, 0, Object.keys(notes).length, windowHeight / 10, windowHeight / 2);
let size = minSize + maxSize;
let alpha = map(activeNotes, 0, 7, 50, 255);
let weight = map(activeNotes, 0, 7, 0.5, 8);
angle += 0.05;
for (let i = 0; i < visualizers.length; i++) {
visualizers[i].draw(size, alpha, weight);
}
// Adjust the amplitude of each oscillator based on the mouse Y position
let amp = map(mouseY, 0, height, 1, 0);
Object.keys(oscillators).forEach(key => {
oscillators[key].amp(amp, 0.05);
});
// Adjust reverb level based on the mouse X position
let dryWet = map(mouseX, 0, width, 0, 1);
reverb.drywet(dryWet);
} else {
background(0);
fill(255);
textSize(32);
textAlign(CENTER, CENTER);
text("Press ENTER to start experience, R to restart", width / 2, height / 2-50);
text("Use the mouseX for reverb and mouseY for amp", width / 2, height / 2+50);
text("Use A,S,D,F,G,H,J,K for key notes", width / 2, height / 2);
image(img,50,50,50,50);
image(img,width-100,height-100,50,50);
}
}
class ShapeVisualizer {
constructor(x, y) {
this.x = x;
this.y = y;
}
draw(size, alpha, weight) {
strokeWeight(weight);
push();
translate(this.x, this.y);
beginShape();
for (let a = 0; a < TWO_PI; a += 0.02) {
let xoff = map(cos(a), -1, 1, 0, 5) + angle;
let yoff = map(sin(a), -1, 1, 0, 5) + angle;
let r = map(noise(xoff, yoff), 0, 1, size * 0.5, size);
let x = r * cos(a);
let y = r * sin(a);
let hue = (angle * 50) % 360;
stroke(hue, 100, 100, alpha);
vertex(x, y);
}
endShape(CLOSE);
pop();
}
updatePosition(newX, newY) {
this.x = newX;
this.y = newY;
}
}
function keyPressed() {
if (keyCode == ENTER && gameState == "start") {
gameState = "game";
background(0);
noFill();
if(c == 0){
let fs = fullscreen();
fullscreen(!fs);
c++;
return;
}
}
if (gameState == "game") {
if (key === 'r' || key === 'R') {
gameState="start";
}
let noteFreq = notes[key.toUpperCase()];
if (noteFreq && !playingNotes[key]) {
let osc = new p5.Oscillator('sine');
osc.freq(noteFreq);
osc.start();
osc.amp(0.5, 0.1);
oscillators[key] = osc;
playingNotes[key] = true;
reverb.process(osc); // Process this oscillator through the reverb
}
if (key == "Q") {
stopAllSounds();
}
}
}
function keyReleased() {
if (gameState == "game") {
if (playingNotes[key] && oscillators[key]) {
let osc = oscillators[key];
osc.amp(0, 0.1); // Ramp down to silence before stopping
setTimeout(() => {
osc.stop();
osc.dispose();
delete oscillators[key];
delete playingNotes[key];
}, 100); // Allow some time for the amp to ramp down
}
}
}
function stopAllSounds() {
Object.keys(oscillators).forEach(key => {
let osc = oscillators[key];
osc.stop();
osc.dispose();
delete oscillators[key];
});
playingNotes = {};
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}