I have a p5js application which loads and plays a sound file. It was worked fine for a very long time, but suddenly it’s started throwing errors when I try to play the file.
I’ve stripped the code down to its basics, as well as trying it with different sound files, formats, browsers etc, but it breaks in all of them.
As soon as I call audioStream.play()
I get the following error:
Uncaught TypeError: Cannot read property 'length' of undefined
at RingBuffer.push (a95166f8-a660-483a-99b5-aafd118d4566:75)
at AudioWorkletProcessor.process (a95166f8-a660-483a-99b5-aafd118d4566:170)
I’m speculating, due to the undefined error, that something may be trying to access the audio stream before it is fully instantiated, but after it’s loaded.
The error happens outside the flow of my program, I’m guessing in a timeout or something, so I can’t debug it from a point in my code. However, If I use:
console.log('play');
audioStream.play();
console.log('playing');
Then the error occurs between the two log statements, so it’s something that’s triggered by calling play()
on the p5 SoundFile
object.
Here is my code, stripped down as much as possible, which replicates the problem:
const audioFile = '../audio/Track.wav';
let audioStream;
let playing = false;
window.setup = () => {
createCanvas(640, 480);
background(0,128,0)
audioStream = loadSound(audioFile, function () {
console.log('loaded', audioFile);
});
console.log('loading', audioFile);
};
window.draw = () => {
if (!playing && audioStream.isLoaded()) {
playing = true;
console.log('play');
audioStream.play();
console.log('playing');
}
};