Digital Audio Recording

Click the link or highlight the code and copy it.  

Then, go to p5js.org and paste the code in the editor. 

Hit the "run" button, watch what happens. 

Make adjustments to the code, the hit the "run" button again. 

Repeat.  :) 

LINK TO WORKING CODE


let mic, recorder, soundFile;

let isRecording = false;


function setup() {

  createCanvas(400, 200);

  

  mic = new p5.AudioIn();

  mic.start();

  

  recorder = new p5.SoundRecorder();

  recorder.setInput(mic);

  

  soundFile = new p5.SoundFile();

  

  // Create buttons for recording and playback

  let recordButton = createButton('Record');

  recordButton.mousePressed(toggleRecording);

  

  let playButton = createButton('Play');

  playButton.mousePressed(playSound);

}


function toggleRecording() {

  if (isRecording) {

    recorder.stop();

    isRecording = false;

    console.log('Recording stopped');

  } else {

    recorder.record(soundFile);

    isRecording = true;

    console.log('Recording started');

  }

}


function playSound() {

  soundFile.play();

}


function draw() {

  background(220);

  

  if (isRecording) {

    fill(255, 0, 0);

    ellipse(width / 2, height / 2, 50, 50);

  } else {

    fill(0, 255, 0);

    ellipse(width / 2, height / 2, 50, 50);

  }

}