Skip to content
Snippets Groups Projects
Commit efdb9bd5 authored by Jakub Fiala's avatar Jakub Fiala
Browse files

loadSample now works with base64-encoded samples

parent ced57668
Branches
No related merge requests found
......@@ -6,4 +6,5 @@ maxiLib/sed_addText.sh
maxiLib/sed_replace.sh
maxiLib/sed_text_stuff/
maximilian_examples_cpp/
old_stuff/
\ No newline at end of file
old_stuff/
.DS_Store
......@@ -8493,8 +8493,8 @@ Module.asmLibraryArg = {
var asm = (function(global,env,buffer) {
 
'use asm';
var Int8View = global.Int8Array;
var Int16View = global.Int16Array;
var Int32View = global.Int32Array;
......@@ -33344,6 +33344,133 @@ maximJs.maxiTools.getArrayAsVectorDbl = (function(arrayIn) {
}
return vecOut;
});
maximJs.maxiTools.getBase64 = function(str) {
if (str.indexOf(';base64,') != -1 ) {
var dataStart = str.indexOf(';base64,') + 8;
return str.slice(dataStart).match(/^([A-Za-z0-9+\/]{4})*([A-Za-z0-9+\/]{4}|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{2}==)$/) ? str.slice(dataStart) : false;
}
else return false;
}
maximJs.maxiTools._keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
maximJs.maxiTools.removePaddingFromBase64 = function(input) {
var lkey = maximJs.maxiTools._keyStr.indexOf(input.charAt(input.length - 1));
if(lkey == 64){
return input.substring(0,input.length - 1);
}
return input;
}
maximJs.maxiTools.loadSample = function(url, samplePlayer, contextIn) {
var data = [];
var context;
if(!contextIn){
context = new (window.AudioContext || window.webkitAudioContext)();
} else {
context = contextIn;
}
samplePlayer.clear();
var b64 = maximJs.maxiTools.getBase64(url);
if (b64) {
var ab_bytes = (b64.length/4) * 3;
var arrayBuffer = new ArrayBuffer(ab_bytes);
var bytes = parseInt((b64.length / 4) * 3, 10);
var uarray;
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
var j = 0;
uarray = new Uint8Array(arrayBuffer);
b64 = b64.replace(/[^A-Za-z0-9\+\/\=]/g, "");
for (i=0; i<bytes; i+=3) {
enc1 = this._keyStr.indexOf(b64.charAt(j++));
enc2 = this._keyStr.indexOf(b64.charAt(j++));
enc3 = this._keyStr.indexOf(b64.charAt(j++));
enc4 = this._keyStr.indexOf(b64.charAt(j++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
uarray[i] = chr1;
if (enc3 != 64) uarray[i+1] = chr2;
if (enc4 != 64) uarray[i+2] = chr3;
}
context.decodeAudioData(
arrayBuffer,
function(buffer) {
data = buffer.getChannelData(0);
if(data){
var myBufferData = new Module.VectorDouble();
for(var n = 0; n < data.length; n++){
myBufferData.push_back(data[n]);
}
samplePlayer.setSample(myBufferData);
}
},
function(buffer) {
console.log("Error decoding source!");
}
);
}
else {
var request = new XMLHttpRequest();
request.addEventListener("load",
function (evt) {
console.log("The transfer is complete.");
});
request.open("GET", url, true);
request.responseType = "arraybuffer";
request.onload = function() {
context.decodeAudioData(
request.response,
function(buffer) {
data = buffer.getChannelData(0);
if(data){
var myBufferData = new Module.VectorDouble();
for(var n = 0; n < data.length; n++){
myBufferData.push_back(data[n]);
}
samplePlayer.setSample(myBufferData);
}
},
function(buffer) {
console.log("Error decoding source!");
}
);
};
request.send();
}
};
maximJs.maxiTools.loadSample = (function(url, samplePlayer, contextIn) {
var data = [];
var context;
<!--
Copyright 2010, Google Inc.
All rights reserved.
......@@ -54,7 +53,7 @@ maximJs.maxiTools.loadSample("audio/beat2.wav", samplePlayer);
maximJs.maxiAudio.play = function(){
// this is necessary as file loading may not complete in setup
if(samplePlayer.isReady()){
if(samplePlayer.isReady()){
output = samplePlayer.play();//just play the file. Looping is default for all play functions.
// output=samplePlayer.play(0.69) ;//play the file with a speed setting. 1. is normal speed.
// output=samplePlayer.play(0.5,0,44100);//linear interpolationplay with a frequency input, start point and end point. Useful for syncing.
......@@ -72,7 +71,7 @@ maximJs.maxiAudio.play = function(){
<h1> Sample Example </h1>
<p>
</p>
<pre class="prettyprint lang-js linenums:true" id="quine" style="border:4px solid #88c" >
......@@ -85,7 +84,7 @@ loadSample("./beat2.wav", samplePlayer);
maximJs.maxiAudio.play = function(){
// this is necessary as file loading may not complete in setup
if(samplePlayer.isReady()){
if(samplePlayer.isReady()){
output = samplePlayer.play();//just play the file. Looping is default for all play functions.
// output=samplePlayer.play(0.69) ;//play the file with a speed setting. 1. is normal speed.
// output=samplePlayer.play(0.5,0,44100);//linear interpolationplay with a frequency input, start point and end point. Useful for syncing.
......@@ -93,4 +92,4 @@ maximJs.maxiAudio.play = function(){
}
}
</pre>
</body></html>
</body></html>
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
/*
Copyright notice for the base64 to arraybuffer conversion algorithm.
Copyright (c) 2011, Daniel Guerrero
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL DANIEL GUERRERO BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// might adopt some strange functions from Module?
// var maximJs = Module;
......@@ -71,7 +95,7 @@ maximJs.maxiArray = function maxiArray(){
// // reset length var
// this.length = this.vec.size();
// }
// }
// };
};
......@@ -145,6 +169,29 @@ maximJs.maxiTools.getArrayAsVectorDbl = function(arrayIn){
return vecOut;
};
maximJs.maxiTools.getBase64 = function(str) {
//check if the string is a data URI
if (str.indexOf(';base64,') != -1 ) {
//see where the actual data begins
var dataStart = str.indexOf(';base64,') + 8;
//check if the data is base64-encoded, if yes, return it
// taken from
// http://stackoverflow.com/a/8571649
return str.slice(dataStart).match(/^([A-Za-z0-9+\/]{4})*([A-Za-z0-9+\/]{4}|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{2}==)$/) ? str.slice(dataStart) : false;
}
else return false;
}
maximJs.maxiTools._keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
maximJs.maxiTools.removePaddingFromBase64 = function(input) {
var lkey = maximJs.maxiTools._keyStr.indexOf(input.charAt(input.length - 1));
if(lkey == 64){
return input.substring(0,input.length - 1);
}
return input;
}
maximJs.maxiTools.loadSample = function(url, samplePlayer, contextIn) {
var data = [];
var context;
......@@ -158,19 +205,45 @@ maximJs.maxiTools.loadSample = function(url, samplePlayer, contextIn) {
samplePlayer.clear();
// Load asynchronously
var request = new XMLHttpRequest();
request.addEventListener("load",
function (evt) {
console.log("The transfer is complete.");
});
request.open("GET", url, true);
request.responseType = "arraybuffer";
//check if url is actually a base64-encoded string
var b64 = maximJs.maxiTools.getBase64(url);
if (b64) {
//convert to arraybuffer
//modified version of this:
// https://github.com/danguer/blog-examples/blob/master/js/base64-binary.js
var ab_bytes = (b64.length/4) * 3;
var arrayBuffer = new ArrayBuffer(ab_bytes);
var bytes = parseInt((b64.length / 4) * 3, 10);
var uarray;
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
var j = 0;
uarray = new Uint8Array(arrayBuffer);
b64 = b64.replace(/[^A-Za-z0-9\+\/\=]/g, "");
for (i=0; i<bytes; i+=3) {
//get the 3 octects in 4 ascii chars
enc1 = this._keyStr.indexOf(b64.charAt(j++));
enc2 = this._keyStr.indexOf(b64.charAt(j++));
enc3 = this._keyStr.indexOf(b64.charAt(j++));
enc4 = this._keyStr.indexOf(b64.charAt(j++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
uarray[i] = chr1;
if (enc3 != 64) uarray[i+1] = chr2;
if (enc4 != 64) uarray[i+2] = chr3;
}
request.onload = function() {
context.decodeAudioData(
request.response,
arrayBuffer,
function(buffer) {
// source.buffer = buffer;
// source.loop = true;
......@@ -193,10 +266,52 @@ maximJs.maxiTools.loadSample = function(url, samplePlayer, contextIn) {
function(buffer) {
console.log("Error decoding source!");
}
);
}
else {
// Load asynchronously
var request = new XMLHttpRequest();
request.addEventListener("load",
function (evt) {
console.log("The transfer is complete.");
});
request.open("GET", url, true);
request.responseType = "arraybuffer";
request.onload = function() {
context.decodeAudioData(
request.response,
function(buffer) {
// source.buffer = buffer;
// source.loop = true;
// source.start(0);
data = buffer.getChannelData(0);
if(data){
var myBufferData = new Module.VectorDouble();
// Module.vectorTools.clearVectorDbl(myBufferData);
for(var n = 0; n < data.length; n++){
myBufferData.push_back(data[n]);
}
samplePlayer.setSample(myBufferData/*, context.sampleRate*/);
}
},
function(buffer) {
console.log("Error decoding source!");
}
);
};
};
request.send();
}
request.send();
};
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment