p5.js-sound icon indicating copy to clipboard operation
p5.js-sound copied to clipboard

AudioContext bug in Safari causes endless error for `currentTime`

Open ffd8 opened this issue 3 years ago • 0 comments

Just ran into a strange bug with p5.sound (v1.0.1) on Safari (13.1.2, yes old.. MacOS 10.13.6) – but I believe others have run into a very similar issue with currentTime often returning a null or 0.

Testing right now with multiple iFrames that load p5/p5sound (both min versions), no problem is found in Chrome or Firefox, but with Safari, it stumbles with the following error:

TypeError: null is not an object (evaluating 'c.a.createGain')

Which is then followed by an endless (100x times per second and counting) error:

TypeError: null is not an object (evaluating 'this._context.currentTime')

I found that I could prevent this when editing the p5.sound.min.js file, replacing: return this._context.currentTime » with » if(this._context !== null){return this._context.currentTime} (no idea where this is in the src??)

That stopped the issue! and my sound demo, using an oscillator still works just fine. My guess is that there's some order of events issue and the audioContext isn't ready by the time a call for gain is made, but somehow eventually is, though it was still bound to a 'broken' context that causes the error?

In general, I wish that p5.sound would wait until being used to initialize itself – since it's suggested by p5/editor to always attach even if not used. Prior to version 0.73, it was quietly included.. then with the WebWorker/worklet (sorry, not so familiar with exact process) – it caused the issue for beginners, that a p5 project now had to be run localhost (server) or on https. This has been brought up before ( #500 ) and would make beginners lives much easier if the worklet sat passive until needed. Perhaps it could wait for the first call to a function like loadSound() or new p5.Oscillator('sine') (any initiator function) to check if the context is setup and if not, establish it. It would also help to make a difference between using the mic/input or just output (where an https connection doesn't seem to be needed?).

ffd8 avatar Jan 23 '22 16:01 ffd8