Skip to content

Instantly share code, notes, and snippets.

@claytical
Created November 3, 2015 14:47
Show Gist options
  • Save claytical/7fb940cc067ea8b8e64b to your computer and use it in GitHub Desktop.
Save claytical/7fb940cc067ea8b8e64b to your computer and use it in GitHub Desktop.
Record and Playback Audio in p5js
var mic;
var micOn;
var recorder;
var recording;
var soundFile;
function setup() {
// uncomment this line to make the canvas the full size of the window
createCanvas(windowWidth, windowHeight);
// the volume is a number between 0 and 1
mic = new p5.AudioIn();
micOn = false;
//create a new recorder object
recorder = new p5.SoundRecorder();
//set the recorder to listen to the mic object
recorder.setInput(mic);
//create a new soundfile object to playback and save the recording
soundFile = new p5.SoundFile();
}
function draw() {
// draw stuff here
background(255);
soundFile.setVolume(1);
if (micOn) {
//it's a recording indicator :)
noStroke();
fill(255,0,0);
ellipse(50, 50, 25, 25);
}
}
function keyPressed() {
soundFile.play();
console.log("Playing Sound");
}
function mousePressed() {
micOn = !micOn;
if (micOn) {
mic.start();
recorder.record(soundFile);
}
else {
mic.stop();
recorder.stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment