Juan Sebastian Silva Juan Sebastian Silva - 3 months ago 18
Javascript Question

How to convert p5.js soundFile to file javascript class?

I'm recording with p5.js library but i need to convert the soundFile object to a javascript file object to send it to an audio server.

how can archieve this?

my code is something like this:

mic = new p5.AudioIn();
mic.start();
recorder = new p5.SoundRecorder();
recorder.setInput(mic);
soundFile = new p5.SoundFile();

$("#record").on("click", function{
recorder.record(soundFile);
})

$("#stop").on("click", function{
recorder.stop();
})
$("#send").on("click", function{
//here i need to convert soundFile object to file object
myuploadfile(file)
})

Answer

The p5.sound library creates a Blob object before download it as .wav, you can see this on p5.prototype.writeFile.

The difference between a Blob and a file is just 2 extra properties: a date and a name, so, if you want to "convert" your blob to a file, just add this 2 properties:

function blobToFile(theBlob, fileName){
    //A Blob() is almost a File() - it's just missing the two properties below which we will add
    theBlob.lastModifiedDate = new Date();
    theBlob.name = fileName;
    return theBlob;
}

After that, you can manage the output as a javascript file

Comments