Posting Sketches with Sound

Sketches using our new template-all.zip should work here on WordPress using the p5-embed class as usual. Here are some tests.

synthtest

var myTone;

function setup() {
    createCanvas(300, 300);
    useSound();
}

function soundSetup() { // setup for audio generation
    myTone = new p5.Oscillator();
    myTone.setType('sine');
    myTone.freq(800);
    myTone.start();
}

function draw() {
    background(200);
    fill(0);
    ellipse(mouseX, mouseY, 20, 20);
    myTone.amp(constrain(mouseX / width, 0, 1));
    myTone.freq(constrain(200 + 1000 * (mouseY / height), 200, 1200));
}

wooptest

var bgcol = 200; // gray
var woop; // this will hold a sound

function preload() {
    woop = loadSound("https://courses.ideate.cmu.edu/15-104/f2019/wp-content/uploads/2019/10/woop.wav");
}

function setup() {
    createCanvas(200, 100);
    frameRate(10); // controls "flash" time
    useSound();
}

function soundSetup() { // setup for audio generation
    woop.setVolume(0.5);
}

function draw() {
    background(bgcol);
    bgcol = 200; // restore flash
    fill(0);
    textFont("Arial", 24);
    text("click me", 50, 50);
}

function mousePressed() {
    woop.play();
    bgcol = 255; // bright flash
}