Comments (12)
It has to be the compile step configuration then. Only thing it could be honestly because p5 is installed (i checked), it is clearly not bundling the sound addon properly for some reason in this case. Sorry I can't be more help on this one.
For further issues, please raise a new issue though!
from react.
Will appreciate if someone could make a tiny demo to demonstrate how this bad boy could work. 🙏
Trying to get this to work by loading and playing a sound, https://p5js.org/examples/sound-load-and-play-sound.html
I can manage to import the library in but then all the functions/constructors can't be called.
from react.
I have a workaround that only works on function calls.
I added window.p5 = p5
after calling import * as p5 from "p5"
import * as ml5 from "ml5"
window.p5 = p5
import "p5/lib/addons/p5.sound"
Then I'll be able to use the p5.loadSound() function. However if I try to use new p5.Envelope() constructor it'll still throw me an error.
export default function sketch(p5) {
p5.setup = () => {
p5.createCanvas(400, 400)
song = p5.loadSound(url("./assets/hurry.mp3")) // Will work
env = new p5.Envelope() // Will error
...
from react.
Hi @vennsoh, sorry for the wait in reply, I can help you here for sure.
So actually what you did was correct, you have to have the import "p5/lib/addons/p5.sound";
statement. Since sound is an external library you add on to p5, it isn't exported by default from the p5
constructor you get given in the sketch function.
As for having to install p5
itself, that won't be necessary after the next version is eventually released to NPM as I fixed this in pull request #56 as a solution to issue #45.
Another thing: #42 brought up a similar question to this about how to use
p5.Vector
to and you can see the solution to using items like that here.
So, in short:
- If it is a p5 addon then you must
import
it - If you can't find a constructor like
p5.Vector
, you can probably access it by usingp5.constructor.[thing]
This is just because of how p5
itself is built and not a library issue although I agree it isn't ideal but kind of still makes sense I think overall since technically addons aren't part of the core library anyway so why should p5
include them by default which is the reason I guess they implemented things this way.
Lastly, a quick tip:
To view all available native constructors that are not addons you can run
console.log({ constructors: { ...p5.constructor } });
.
Hope all this helps and reach out again if you need further support, happy to help!
from react.
Omg, you're a godsend to this community! Thanks!
By just changing p5.Envelope() --> p5.constructor.Envelope()
has fixed my problem.
Although there's another problem with a recent Chrome update that requires getAudioContext() but it's well documented here how to fix that, https://github.com/processing/p5.js-sound/issues/249
Using this: https://p5js.org/reference/#/p5/userStartAudio
Can I seek more clarification on this line of sentence?
"As for having to install p5 itself, that won't be necessary after the next version is eventually released to NPM as I fixed this in pull request #56 as a solution to issue #45."
Do we mean:
- With react-p5-wrapper, if I want to use p5.sound in the future, I don't need to install p5 separately?
Also why do we need window.p5 = p5
for this to work? Can someone educate me?
from react.
Can I seek more clarification on this line of sentence?
"As for having to install p5 itself, that won't be necessary after the next version is eventually released to NPM as I fixed this in pull request #56 as a solution to issue #45."Do we mean:
- With react-p5-wrapper, if I want to use p5.sound in the future, I don't need to install p5 separately?
Exactly, p5 will be included as a dependency once the next version of this library is released on NPM. Sadly I can't say when exactly that will be but I would assume in the coming quarter if I had to guess. It does mean though that you shouldn't have to manually install p5.
What do you mean about the window.p5
part? I don't understand why you would have to do that.
from react.
Thanks @jamesrweb hm, weirdly if comment out window.p5 = p5
then I'll get this error.
I learn about this hack from this thread: https://stackoverflow.com/questions/39693208/using-p5-sound-js-in-instance-mode-p5-amplitude-not-a-constructor/44727829#44727829
Someone responded to how I can avoid doing window.p5 = p5 but I still get an error.
https://stackoverflow.com/questions/64887033/how-do-i-add-p5-sound-to-react-using-react-p5-wrapper/64922685#64922685
import * as P5Class from "p5"
window.p5 = P5Class // I need this otherwise it throws an error
import "p5/lib/addons/p5.sound"
export default function sketch(p5) {
p5.setup = () => {
p5.createCanvas(400, 400)
let env = new p5.constructor.Envelope()
...
from react.
Why do you need to do this in the first place? You get access to p5
as the parameter to your sketch
function. Can you explain?
from react.
I'm using Framer to do my prototype so I wonder if this is a Framer thing where to add something on top that's causing me needing this workaround.
I created a Framer prototype that demonstrate this. https://framer.com/projects/ml5-neuralNetwork--sPis0uXjFNsgMa1VcxMD-ikuQ0 (You need to register a free framer account to see the code + preview the prototype in browser.
I tried to create a codesandbox to demonstrate the issue too to make it easier.
https://codesandbox.io/s/vigorous-lamarr-px8fs?file=/src/Sketch.js
But then now I'm getting this issue? My code is working fine if I don't need to import p5.sound but once I did it, it throw an error. Might be a webpack thing that comes with Codesandbox default template, not sure.
from react.
@vennsoh, Can you share your code for that project and / or file?
from react.
Wait, have you tried running npm install
or deleting your node_modules
folder and reinstalling dependencies because for some reason your codesandbox example is attempting to import
from the @types
package and not p5
itself 🤔...
from react.
@vennsoh, Can you share your code for that project and / or file?
Yup the 2 links I have posted above are the code.
Framer: https://framer.com/projects/ml5-neuralNetwork--sPis0uXjFNsgMa1VcxMD-ikuQ0 (working)
Codesandbox: https://codesandbox.io/s/vigorous-lamarr-px8fs?file=/src/Sketch.js (error out)
And yes, I did try your suggestion of removing node_modules. And npm install again, I still need to use my workaround otherwise I get errors. For my Framer example that is working...
I need to have:
window:p5 = p5
and need to have it right afterimport * as P5Class from "p5"
import * as P5Class from "p5"
window.p5 = P5Class
import "p5/lib/addons/p5.sound"
- Then if I want to access the constructor of the p5.sound, I need to use, .constructor like
env = new p5.constructor.Envelope()
from react.
Related Issues (20)
- Migrate to Vite and Vitest
- Migrate to the @P5-wrapper npm org
- Loading image P5-react
- React's strict mode causes the canvas to be created twice HOT 4
- An example of exposing imperative methods from the sketch? HOT 9
- unexpected array behavior inside p5 sketch function HOT 1
- ReactP5Wrapper has additional pixels in height HOT 3
- Could not find a declaration file but the code works HOT 10
- Duplicate canvas when running in vite dev HOT 5
- TypeError: p5.loadSound is not a function HOT 3
- P5-wrapper size HOT 5
- Stop rerender of p5 component when redux variables update HOT 3
- Latest commits not in the npm registry HOT 5
- Issue building with Vite 5 HOT 16
- npm ERR! Usage Error: This project is configured to use npm HOT 2
- windowResized() not firing HOT 4
- Error in nextjs HOT 7
- Change p5 dependency version to minimum 1.4.1 HOT 9
- Examples directory doesn't exist, link broken on README HOT 3
- Module parse failed: Identifier 'XX' has already been declared (7762:41) HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react.