Ludovico Vianello- video B

let osc;
let playing = false;

function setup() {
createCanvas(1024, 1024);

// Creiamo un oscillatore sinusoidale
osc = new p5.Oscillator(‘sine’);
}

function draw() {
background(220, 50, 80);

// Mappiamo il mouse
// X controlla la frequenza (Pitch): da 100Hz a 1000Hz
let freq = map(mouseX, 0, width, 100, 1000);
// Y controlla l’ampiezza (Volume): da 1 (max) a 0 (min)
let amp = map(mouseY, 0, height, 1, 0);

if (playing) {
osc.freq(freq, 0.1); // 0.1 è il tempo di transizione per non sentire “clic”
osc.amp(amp, 0.1);
}

// Visualizzazione grafica semplice
fill(255);
noStroke();
ellipse(mouseX, mouseY, 50, 50);

// Testo informativo
textAlign(CENTER);
text(‘Clicca per accendere/spegnere’, width/2, height – 50);
text(‘Freq: ‘ + floor(freq) + ‘Hz’, width/2, height – 30);
}

function mousePressed() {
if (!playing) {
osc.start();
playing = true;
} else {
osc.stop();
playing = false;
}
}

Commenti

Lascia un commento