xxxxxxxxxx
37
let fmSynth;
const noteValues = [60, 63, 67, 70, 72];
let isPlaying = false;
function mousePressed() {
Tone.start();
print("Audio started!");
// const midiNote = int(random(0, 90));
}
function setup() {
createCanvas(400, 400);
background(0);
fmSynth = new Tone.FMSynth().toDestination();
}
function draw() {
if (mouseIsPressed) {
if (!isPlaying) {
isPlaying = true;
const midiNote = noteValues[int(random(0, noteValues.length))];
const freq = Tone.Frequency(midiNote, "midi").toFrequency();
fmSynth.triggerAttack(freq);
}
noStroke();
fill(0, 255, 255, 50);
translate(mouseX, mouseY);
rotate(random(-PI, PI));
square(0, 0, 50);
} else {
if (isPlaying) {
isPlaying = false;
// release note
fmSynth.triggerRelease();
}
}
}