Why is p5.js preload so slow?

I load a few mp3 files that are 2 minutes long.

function preload() {
	
	sound_dancer_1_l = loadSound('data/GH010103_1-L.mp3');
	sound_dancer_1_r = loadSound('data/GH010103_1-R.mp3');
	sound_dancer_2_l = loadSound('data/GH010103_2-L.mp3');
	sound_dancer_2_r = loadSound('data/GH010103_2-R.mp3');
	sound_dancer_3_l = loadSound('data/GH010103_3-L.mp3');
	sound_dancer_3_r = loadSound('data/GH010103_3-R.mp3');
	sound_dancer_4_l = loadSound('data/GH010103_4-L.mp3');
	sound_dancer_4_r = loadSound('data/GH010103_4-R.mp3');

}

It takes around 5 seconds on my localhost, which instead should have been instant.
Is there anyway to speed it up?

If I don’t preload them and start play early on then I get an error and I have no sound at all.

1 Like

preload() is a p5js optional callback which delays the sketch until everything is processed there: :snail:

You can invoke p5::loadSound() outside preload() & check it’s ready w/ p5.SoundFile::isLoaded(): :sound:

1 Like

Thanks, I just used html5 audio in the end.

i got preload working with no glitches.

var myRec = new p5.SpeechRec('en-US', parseResult);
myRec.continuous = true;
myRec.interimResults = false; //was getting detection errors with this turned on, not sure what the fuck this is
myRec.onEnd = restart;
var songs = [];
var songNames = ['one.wav', 'two.m4a', 'three.m4a', 'come_closer.m4a', 'four.wav', 'five.m4a'];
var songCount = songNames.length;
var currentSong = 0;
var song;
var startT;
var fiveSeconds = 5000;

function restart() { // the dirty hack
    myRec.start();
}

function preload() {
    soundFormats('wav', 'm4a');
    for (let i = 0; i < songNames.length; i++) {
        songs.push(loadSound('audioFiles/' + songNames[i]));
    }
}

function setup() {
    frameRate(1);
    createCanvas(600, 600);
    background(255, 255, 255);
    fill(0, 0, 0, 255);
    myRec.start();
    startT = millis();
}
1 Like