Commit d0ab8933 authored by Daniel Clarke's avatar Daniel Clarke

combining changes with base64 stuff

parents 7fabd29d 0fc6adb4
......@@ -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
This diff is collapsed.
<!--
Copyright 2010, Google Inc.
All rights reserved.
......@@ -54,12 +53,12 @@ audio.loadSample("audio/beat2.wav", samplePlayer);
audio.play = function(){
// this is necessary as file loading may not complete in setup
if(samplePlayer.isReady()){
this.output = samplePlayer.play();//just play the file. Looping is default for all play functions.
if(samplePlayer.isReady()){
this.output = samplePlayer.play();//just play the file. Looping is default for all play functions.
// this.output=samplePlayer.play(0.69) ;//play the file with a speed setting. 1. is normal speed.
// this.output=samplePlayer.play(0.5,0,44100);//linear interpolationplay with a frequency input, start point and end point. Useful for syncing.
// this.output=samplePlayer.play(0.5,0,44100);//linear interpolationplay with a frequency input, start point and end point. Useful for syncing.
// this.output=samplePlayer.play4(0.5,0,44100);//cubic interpolation play with a frequency input, start point and end point. Useful for syncing.
}
}
}
......@@ -72,7 +71,7 @@ audio.play = function(){
<h1> Sample Example </h1>
<p>
</p>
<pre class="prettyprint lang-js linenums:true" id="quine" style="border:4px solid #88c" >
......@@ -87,12 +86,12 @@ audio.loadSample("audio/beat2.wav", samplePlayer);
audio.play = function(){
// this is necessary as file loading may not complete in setup
if(samplePlayer.isReady()){
this.output = samplePlayer.play();//just play the file. Looping is default for all play functions.
if(samplePlayer.isReady()){
this.output = samplePlayer.play();//just play the file. Looping is default for all play functions.
// this.output=samplePlayer.play(0.69) ;//play the file with a speed setting. 1. is normal speed.
// this.output=samplePlayer.play(0.5,0,44100);//linear interpolationplay with a frequency input, start point and end point. Useful for syncing.
// this.output=samplePlayer.play(0.5,0,44100);//linear interpolationplay with a frequency input, start point and end point. Useful for syncing.
// this.output=samplePlayer.play4(0.5,0,44100);//cubic interpolation play with a frequency input, start point and end point. Useful for syncing.
}
}
}
</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();
// }
// }
// };
};
......@@ -146,7 +170,28 @@ 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;
}
// ------------------------------------------------
......@@ -158,7 +203,7 @@ maximJs.maxiAudio = function() {
this.source = null;
this.analyser = null;
this.jsProcessor = null;
this.bufferSize = 1024;
this.initDone = false;
};
......@@ -184,6 +229,11 @@ maximJs.maxiAudio.prototype.setNumChannels = function(isArray, numChannels_){
this.resetAudio();
};
maximJs.maxiAudio.prototype.setBufferSize = function(newBufferSize) {
this.bufferSize = newBufferSize;
this.resetAudio();
};
// use this if you want to keep num of outputs but change
// method e.g. array or not
maximJs.maxiAudio.prototype.outputIsArray = function(isArray){
......@@ -198,61 +248,34 @@ maximJs.maxiAudio.prototype.outputIsArray = function(isArray){
}
};
/*
// function handling audio processing
// called byjsProcessor
maximJs.maxiAudio.prototype.process = function(event) {
var numChannels = event.outputBuffer.numberOfChannels;
var outputLength = event.outputBuffer.getChannelData(0).length;
// console.log("n");
for (var i = 0; i < outputLength; ++i) {
this.play();
if(self.output instanceof Array){
for (var channel = 0; channel < numChannels; channel++) {
event.outputBuffer.getChannelData(channel)[i] = this.output[channel];
}
}
else
{
for (var channel = 0; channel < numChannels; channel++) {
event.outputBuffer.getChannelData(channel)[i] = this.output;
}
}
}
}
.bind(this)
;
*/
maximJs.maxiAudio.prototype.init = function() {
// Temporary patch until all browsers support unprefixed context.
this.context = new (window.AudioContext || window.webkitAudioContext)();
this.source = this.context.createBufferSource();
this.jsProcessor = this.context.createScriptProcessor(4096, this.numChannels, this.numChannels);
this.jsProcessor = this.context.createScriptProcessor(this.bufferSize, this.numChannels, this.numChannels);
// var process = this.process;
this.jsProcessor.onaudioprocess = function(event) {
var numChannels = event.outputBuffer.numberOfChannels;
var outputLength = event.outputBuffer.getChannelData(0).length;
// console.log("n");
for (var i = 0; i < outputLength; ++i) {
this.play();
if(this.output instanceof Array){
for (var channel = 0; channel < numChannels; channel++) {
event.outputBuffer.getChannelData(channel)[i] = this.output[channel];
var numChannels = event.outputBuffer.numberOfChannels;
var outputLength = event.outputBuffer.getChannelData(0).length;
// console.log("n");
for (var i = 0; i < outputLength; ++i) {
this.play();
if(this.output instanceof Array){
for (var channel = 0; channel < numChannels; channel++) {
event.outputBuffer.getChannelData(channel)[i] = this.output[channel];
}
}
}
else
{
for (var channel = 0; channel < numChannels; channel++) {
event.outputBuffer.getChannelData(channel)[i] = this.output;
}
}
}
}
else
{
for (var channel = 0; channel < numChannels; channel++) {
event.outputBuffer.getChannelData(channel)[i] = this.output;
}
}
}
}
.bind(this)
;
......@@ -291,19 +314,47 @@ maximJs.maxiAudio.prototype.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);
//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);
b64 = maximJs.maxiTools.removePaddingFromBase64(maximJs.maxiTools.removePaddingFromBase64(b64));
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 = maximJs.maxiTools._keyStr.indexOf(b64.charAt(j++));
enc2 = maximJs.maxiTools._keyStr.indexOf(b64.charAt(j++));
enc3 = maximJs.maxiTools._keyStr.indexOf(b64.charAt(j++));
enc4 = maximJs.maxiTools._keyStr.indexOf(b64.charAt(j++));
request.responseType = "arraybuffer";
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;
......@@ -326,8 +377,50 @@ maximJs.maxiAudio.prototype.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();
};
Markdown is supported
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