Skip to content

Commit

Permalink
Merge pull request #312 from wahengchang/master
Browse files Browse the repository at this point in the history
Added saveSoundToBlob() to soundRecorder.js
  • Loading branch information
therewasaguy authored Aug 7, 2018
2 parents 9736982 + a7d02cc commit feb4bd3
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/soundRecorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,18 @@ define(function (require) {
this._jsNode = null;
};

/**
* Export a usable url for blob object.
*
* @method saveSoundToBlob
* @param {p5.SoundFile} soundFile p5.SoundFile that you wish to export
*/
p5.prototype.saveSoundToBlob = function(soundFile) {
const dataView = convertToWav(soundFile)
const audioBlob = new Blob([dataView], {type: 'audio/wav'})
return URL.createObjectURL(audioBlob)
}

/**
* Save a p5.SoundFile as a .wav audio file.
*
Expand All @@ -252,6 +264,13 @@ define(function (require) {
* @param {String} name name of the resulting .wav file.
*/
p5.prototype.saveSound = function(soundFile, name) {
const dataView = convertToWav(soundFile)
p5.prototype.writeFile( [ dataView ], name, 'wav');
};

// helper methods to convert audio file as .wav format,
// will use as saving .wav file and saving blob object
function convertToWav(soundFile){
var leftChannel, rightChannel;
leftChannel = soundFile.buffer.getChannelData(0);

Expand Down Expand Up @@ -297,9 +316,8 @@ define(function (require) {
index += 2;
}

p5.prototype.writeFile( [ view ], name, 'wav');
};

return view
}

// helper methods to save waves
function interleave(leftChannel, rightChannel) {
Expand Down

0 comments on commit feb4bd3

Please sign in to comment.