xxxxxxxxxx
40
function mousePressed() {
Tone.start();
print("Audio started!");
}
function keyPressed() {
const fmSynth = new Tone.FMSynth().toDestination();
// Play a single note
// const note = "C5";
// play a random midi note
// const midiNote = int(random(0, 128));
// const note = Tone.Frequency(midiNote, "midi").toFrequency();
// play a random note within a set of notes
// const notes = [60, 63, 65, 67, 70 ];
// const midiNote = notes[int(random(0, notes.length))];
// const note = Tone.Frequency(midiNote, "midi").toFrequency();
// fmSynth.triggerAttackRelease(note, "2fn");
// play random note within a set of notes, multiple octaves
const notes = [0, 3, 5, 6, 10];
const root = 48;
const numOctaves = 2;
const midiNote = root + notes[int(random(0, notes.length))] +
12*int(random(0, numOctaves));
const note = Tone.Frequency(midiNote, "midi").toFrequency();
fmSynth.triggerAttackRelease(note, "2n");
}
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
text("Press any key to play a note.", 20, 30);
}