@@ -244,6 +244,18 @@ define(function (require) {
244244 this . _jsNode = null ;
245245 } ;
246246
247+ /**
248+ * Export a usable url for blob object.
249+ *
250+ * @method saveSoundToBlob
251+ * @param {p5.SoundFile } soundFile p5.SoundFile that you wish to export
252+ */
253+ p5 . prototype . saveSoundToBlob = function ( soundFile ) {
254+ const dataView = convertToWav ( soundFile )
255+ const audioBlob = new Blob ( [ dataView ] , { type : 'audio/wav' } )
256+ return URL . createObjectURL ( audioBlob )
257+ }
258+
247259 /**
248260 * Save a p5.SoundFile as a .wav audio file.
249261 *
@@ -252,6 +264,13 @@ define(function (require) {
252264 * @param {String } name name of the resulting .wav file.
253265 */
254266 p5 . prototype . saveSound = function ( soundFile , name ) {
267+ const dataView = convertToWav ( soundFile )
268+ p5 . prototype . writeFile ( [ dataView ] , name , 'wav' ) ;
269+ } ;
270+
271+ // helper methods to convert audio file as .wav format,
272+ // will use as saving .wav file and saving blob object
273+ function convertToWav ( soundFile ) {
255274 var leftChannel , rightChannel ;
256275 leftChannel = soundFile . buffer . getChannelData ( 0 ) ;
257276
@@ -297,9 +316,8 @@ define(function (require) {
297316 index += 2 ;
298317 }
299318
300- p5 . prototype . writeFile ( [ view ] , name , 'wav' ) ;
301- } ;
302-
319+ return view
320+ }
303321
304322 // helper methods to save waves
305323 function interleave ( leftChannel , rightChannel ) {
0 commit comments