Skip to content

Add Recorder class #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

lvdopqt
Copy link

@lvdopqt lvdopqt commented Apr 7, 2025

This PR relates to this issue #11

Changes:

  • This PR adds Recorder from Tone.js
  • It adds the call to the Recorder.js from the app.js

It can be used like this:

let osc, recorder;
let recording = false;
let recordStartTime = 0;
let recordDuration = 3000; // in ms

function setup() {
  let cnv = createCanvas(200, 100);
  textAlign(CENTER, CENTER);
  textSize(16);
  osc = new p5.Oscillator('sine');
  osc.amp(0.5);
  recorder = new p5.Recorder();
  osc.connect(recorder);
  cnv.mousePressed(startRecording);
}

async function startRecording() {
  if (recording) return;
  recording = true;
  recordStartTime = millis();
  recorder.start();
  osc.start();

  setTimeout(async () => {
    let audioBlob = await recorder.stop();
    recorder.download(audioBlob, 'recording.webm');
    osc.stop();
    recording = false;
  }, recordDuration);
}

function draw() {
  background(220);
  
  if (recording) {
    let remaining = Math.ceil((recordDuration - (millis() - recordStartTime)) / 1000);
    remaining = max(0, remaining);
    text(`Recording: ${remaining}`, width / 2, height / 2);
  } else {
    text('Click to record', width / 2, height / 2);
  }
}

@lvdopqt
Copy link
Author

lvdopqt commented Apr 7, 2025

@ogbabydiesal is there a PR template for this repo? 🤔

@lvdopqt lvdopqt mentioned this pull request Apr 7, 2025
@lvdopqt lvdopqt marked this pull request as ready for review April 7, 2025 14:01
@lvdopqt lvdopqt changed the title Add Recorder.js Add Recorder class Apr 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants