diff options
Diffstat (limited to 'client/lib/util.js')
| -rw-r--r-- | client/lib/util.js | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/client/lib/util.js b/client/lib/util.js index 31c3d3a..e47b343 100644 --- a/client/lib/util.js +++ b/client/lib/util.js @@ -54,6 +54,30 @@ function requestAudioContext (fn) { } } +function dataURItoBlob(dataURI) { + // convert base64 to raw binary data held in a string + // doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this + var byteString = atob(dataURI.split(',')[1]); + + // separate out the mime component + var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0] + + // write the bytes of the string to an ArrayBuffer + var ab = new ArrayBuffer(byteString.length); + + // create a view into the buffer + var ia = new Uint8Array(ab); + + // set the bytes of the buffer to the correct values + for (var i = 0; i < byteString.length; i++) { + ia[i] = byteString.charCodeAt(i); + } + + // write the ArrayBuffer to a blob, and you're done + var blob = new Blob([ab], {type: mimeString}); + return blob; + +} function ftom(f) { // return (Math.log(f) - Math.log(261.626)) / Math.log(2) + 4.0 return 69 + 12 * Math.log2(f / 440) @@ -68,5 +92,5 @@ function tap (fn) { } } -export { choice, mod, browser, requestAudioContext, ftom, mtof, tap } +export { choice, mod, browser, requestAudioContext, ftom, mtof, tap, dataURItoBlob } |
