Skip to content
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

Unable to use p5 audio library with p5 and canvas-sketch #160

Open
jobarbo opened this issue Jul 14, 2022 · 3 comments
Open

Unable to use p5 audio library with p5 and canvas-sketch #160

jobarbo opened this issue Jul 14, 2022 · 3 comments

Comments

@jobarbo
Copy link

jobarbo commented Jul 14, 2022

Hi Matt, I’m currently trying to get realtime audio through p5-sound with canvas-sketch but I can’t seem to make it work. When I call for const mic = new p5.AudioIn();
It says that p5.AudioIn is not a constructor. If I add it through a require with the path, it says that p5 is undefined. I looked on the web and it seem to say that to make it work I have to add the script tag in the html but there is none in canvas-sketch file system. I was wondering if you had any tip. It does the same thing with p5.DOM. Thanks!

@jusebin
Copy link

jusebin commented Jul 29, 2022

I can't too, I spend severals hours to fix this, but nothing happen
I subscribe here if someone got the solution !

@prltagustina
Copy link

prltagustina commented Jul 29, 2022 via email

@cdaein
Copy link

cdaein commented Jan 20, 2023

This is somewhat hacky and not an ideal way but it works 🤷

const canvasSketch = require("canvas-sketch");
const p5 = require("p5");
window.p5 = p5;
require("p5/lib/addons/p5.sound");

const sketch = ({ p5 }) => {
  const mySynth = new p5.constructor.MonoSynth();

  p5.getAudioContext().suspend();
  p5.mousePressed = () => {
    p5.userStartAudio();
    mySynth.play();
  };

  return ({ context, width, height }) => {
    context.fillStyle = `#aaa`;
    context.fillRect(0, 0, width, height);
  };
};

const settings = {
  dimensions: [600, 600],
  p5: p5,
};

canvasSketch(sketch, settings);

If it throws an error with undefined addModule, add --https in CLI when you start the server.

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

No branches or pull requests

4 participants