Commit 0c43df36 authored by Daniel Clarke's avatar Daniel Clarke

Fixed global init problem and did stuff I can't remember

parent ced57668
# change '../../em++' to path to em++ in emscriten folder
# change '../../em++' to path to em++ in emscripten folder
EMSCR=../../em++
# ----------------------------------------
......
......@@ -33330,25 +33330,107 @@ maximJs.maxiArray.prototype.clear = (function(useSq) {
this.length = 0;
});
maximJs.maxiTools = (function() {});
maximJs.maxiTools.arrayOfObj = (function(obj, num) {
maximJs.maxiTools.prototype.arrayOfObj = (function(obj, num) {
var array = [];
for (var i = 0; i < num; i++) {
array.push(new obj);
}
return array;
});
maximJs.maxiTools.getArrayAsVectorDbl = (function(arrayIn) {
maximJs.maxiTools.prototype.getArrayAsVectorDbl = (function(arrayIn) {
var vecOut = new Module.VectorDouble;
for (var i = 0; i < arrayIn.length; i++) {
vecOut.push_back(arrayIn[i]);
}
return vecOut;
});
maximJs.maxiTools.loadSample = (function(url, samplePlayer, contextIn) {
maximJs.maxiAudio = (function() {
this.numChannels = 2;
this.output = 0;
this.context = null;
this.source = null;
this.analyser = null;
this.jsProcessor = null;
this.initDone = false;
});
maximJs.maxiAudio.prototype.play = (function() {});
maximJs.maxiAudio.prototype.setup = (function() {
console.log("non-overrided setup");
});
maximJs.maxiAudio.prototype.getNumChannels = (function() {
return this.numChannels;
});
maximJs.maxiAudio.prototype.setNumChannels = (function(isArray, numChannels_) {
this.numChannels = numChannels_;
this.outputIsArray(isArray, numChannels_);
this.resetAudio();
});
maximJs.maxiAudio.prototype.outputIsArray = (function(isArray) {
if (isArray) {
this.output = new Array(this.numChannels);
for (var i = 0; i < this.numChannels; i++) {
this.output[i] = 0;
}
} else {
this.output = 0;
}
});
maximJs.maxiAudio.prototype.process = (function(event) {
var numChannels = event.outputBuffer.numberOfChannels;
var outputLength = event.outputBuffer.getChannelData(0).length;
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() {
this.context = new (window.AudioContext || window.webkitAudioContext);
this.source = this.context.createBufferSource();
this.jsProcessor = this.context.createScriptProcessor(4096, this.numChannels, this.numChannels);
this.jsProcessor.onaudioprocess = (function(event) {
var numChannels = event.outputBuffer.numberOfChannels;
var outputLength = event.outputBuffer.getChannelData(0).length;
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);
this.analyser = this.context.createAnalyser();
this.analyser.fftSize = 2048;
this.source.connect(this.jsProcessor);
this.jsProcessor.connect(this.analyser);
this.analyser.connect(this.context.destination);
this.initDone = true;
});
maximJs.maxiAudio.prototype.resetAudio = (function() {
if (this.initDone) {
this.source.disconnect();
this.jsProcessor.disconnect();
this.analyser.disconnect();
}
this.init();
});
maximJs.maxiAudio.prototype.loadSample = (function(url, samplePlayer, contextIn) {
var data = [];
var context;
if (!contextIn) {
context = new (window.AudioContext || window.webkitAudioContext);
context = this.context;
} else {
context = contextIn;
}
......@@ -33375,74 +33457,6 @@ maximJs.maxiTools.loadSample = (function(url, samplePlayer, contextIn) {
});
request.send();
});
maximJs.maxiAudio = (function() {
var numChannels = 2;
var output = 0;
this.context = null;
this.source = null;
this.analyser = null;
this.jsProcessor = null;
var initDone = false;
this.play = (function() {});
this.setup = (function() {
console.log("non-overrided setup");
});
this.getNumChannels = (function() {
return numChannels;
});
this.setNumChannels = (function(isArray, numChannels_) {
numChannels = numChannels_;
this.outputIsArray(isArray, numChannels_);
this.resetAudio();
});
this.outputIsArray = (function(isArray) {
if (isArray) {
maximJs.maxiAudio.output = new Array(numChannels);
for (var i = 0; i < numChannels; i++) {
maximJs.maxiAudio.output[i] = 0;
}
} else {
maximJs.maxiAudio.output = 0;
}
});
var process = (function(event) {
var numChannels = event.outputBuffer.numberOfChannels;
var outputLength = event.outputBuffer.getChannelData(0).length;
for (var i = 0; i < outputLength; ++i) {
maximJs.maxiAudio.play();
if (maximJs.maxiAudio.output instanceof Array) {
for (var channel = 0; channel < numChannels; channel++) {
event.outputBuffer.getChannelData(channel)[i] = maximJs.maxiAudio.output[channel];
}
} else {
for (var channel = 0; channel < numChannels; channel++) {
event.outputBuffer.getChannelData(channel)[i] = maximJs.maxiAudio.output;
}
}
}
});
this.resetAudio = (function() {
if (initDone) {
this.source.disconnect();
this.jsProcessor.disconnect();
this.analyser.disconnect();
}
this.init();
});
this.init = (function() {
this.context = new (window.AudioContext || window.webkitAudioContext);
this.source = this.context.createBufferSource();
this.jsProcessor = this.context.createScriptProcessor(4096, numChannels, numChannels);
this.jsProcessor.onaudioprocess = process;
this.analyser = this.context.createAnalyser();
this.analyser.fftSize = 2048;
this.source.connect(this.jsProcessor);
this.jsProcessor.connect(this.analyser);
this.analyser.connect(this.context.destination);
initDone = true;
});
return this;
})();
......@@ -44,12 +44,11 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<!-- Our javascript code -->
<script type="text/javascript">
maximJs.maxiAudio.init();
maximJs.maxiAudio.init();
var samplePlayer = new maximJs.maxiSample();
var mySine = new maximJs.maxiOsc();
maximJs.maxiTools.loadSample("audio/beat2.wav", samplePlayer);
maximJs.maxiAudio.loadSample("audio/beat2.wav", samplePlayer);
// maximJs.maxiTools.loadSample("audio/beat2.wav", samplePlayer, maximJs.maxiAudio.context);
maximJs.maxiAudio.play = function(){
......
......@@ -44,14 +44,14 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<!-- Our javascript code -->
<script type="text/javascript">
var audio = new maximJs.maxiAudio();
// initialise audio
maximJs.maxiAudio.init();
audio.init();
var mySine = new maximJs.maxiOsc();
maximJs.maxiAudio.play = function(){
output = mySine.sinewave(440);
audio.play = function(){
this.output = mySine.sinewave(440);
}
......
......@@ -43,8 +43,55 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<script src="../maxiLib.js"></script>
<!-- <script src="../maxi_webAudio.js"></script> -->
<!-- Our javascript code -->
<script type="text/javascript">
</head>
<body>
<h1> Two Tones </h1>
<canvas id="myCanvas" width="700" height="700" style="border:1px solid #c3c3c3;">
<!-- It's a good idea to warn people whose browsers don't support the tag -->
Your browser does not support the canvas element.
</canvas>
<script type="text/javascript">
var context;
window.onload = setup;
function setup(){
// fft.setup(fftSize, 512, 256);
// ifft.setup(fftSize, 512, 256);
// mfcc.setup(512, 42, 13, 20, 20000, sampleRate);
// This gets the window in the browser
var canvas=document.getElementById("myCanvas");
// This creates a 2d drawing context in it.
context=canvas.getContext("2d");
//finally, we return setInterval, which wants a function to call,
//and a period in milliseconds to wait between calling it.
return setInterval(draw, 40);
}
//The stuff inside the script tag is very similar to processing code.
function draw() {
//This is basically the same as any other 2D processing draw routine.
//clear the screen
context.clearRect(0,0,700,700);
context.beginPath();
context.fillStyle="#FF0000";
context.rect(0,0,50,50);
context.fill();
// context.stroke();
context.closePath();
}
// initialise audio
maximJs.maxiAudio.init();
......@@ -55,16 +102,10 @@ var myOtherSine = new maximJs.maxiOsc();
maximJs.maxiAudio.play = function(){
// these two sines will beat together. They're now a bit too loud though..
output = mySine.sinewave(440) + myOtherSine.sinewave(441);
}
</script>
</head>
<body>
<h1> Two Tones </h1>
<p>
These sines are taking a beating...
</p>
......
......@@ -1659,6 +1659,21 @@ double maxiDyn::compress(double input) {
return output*(1+log(ratio));
}
void maxiDyn::setAttack(double attackMS) {
attack = pow( 0.01, 1.0 / ( attackMS * maxiSettings::sampleRate * 0.001 ) );
}
void maxiDyn::setRelease(double releaseMS) {
release = pow( 0.01, 1.0 / ( releaseMS * maxiSettings::sampleRate * 0.001 ) );
}
void maxiDyn::setThreshold(double thresholdI) {
threshold = thresholdI;
}
void maxiDyn::setRatio(double ratioF) {
ratio = ratioF;
}
/* Lots of people struggle with the envelope generators so here's a new easy one.
It takes mental numbers for attack and release tho. Basically, they're exponentials.
......@@ -1830,21 +1845,7 @@ void maxiEnv::setDecay(double decayMS) {
}
void maxiDyn::setAttack(double attackMS) {
attack = pow( 0.01, 1.0 / ( attackMS * maxiSettings::sampleRate * 0.001 ) );
}
void maxiDyn::setRelease(double releaseMS) {
release = pow( 0.01, 1.0 / ( releaseMS * maxiSettings::sampleRate * 0.001 ) );
}
void maxiDyn::setThreshold(double thresholdI) {
threshold = thresholdI;
}
void maxiDyn::setRatio(double ratioF) {
ratio = ratioF;
}
double convert::mtof(int midinote) {
......
......@@ -119,14 +119,14 @@ public:
valindex = index;
}
int getValindex() const{
return valindex;
}
void setAmplitude(double amp){
amplitude = amp;
}
int getValindex() const{
return valindex;
}
double getAmplitude() const{
return amplitude;
}
......@@ -178,15 +178,15 @@ public:
void setCutoff(double cut){
cutoff = cut;
}
void setResonance(double res){
resonance = res;
}
double getCutoff() const{
return cutoff;
}
void setResonance(double res){
resonance = res;
}
double getResonance() const{
return resonance;
}
......@@ -247,6 +247,8 @@ public:
inline T value() {
return val;
}
};
......@@ -486,6 +488,13 @@ public:
// ------------------------------------------------
// getters/setters
// int getTrigger() const{
// return trigger;
// }
// void setTrigger(int trigger){
// this->trigger = trigger;
// }
// ------------------------------------------------
};
......
......@@ -308,7 +308,7 @@ public:
char* myData;
tempVec
//tempVec
// different vars for use with js
vector<double> tempVec;
......
......@@ -127,7 +127,7 @@ maximJs.maxiArray.prototype.clear = function(useSq){
// tools
maximJs.maxiTools = function(){};
maximJs.maxiTools.arrayOfObj = function(obj, num){
maximJs.maxiTools.prototype.arrayOfObj = function(obj, num){
var array = [];
for(var i = 0; i < num; i++){
......@@ -136,7 +136,7 @@ maximJs.maxiTools.arrayOfObj = function(obj, num){
return array;
};
maximJs.maxiTools.getArrayAsVectorDbl = function(arrayIn){
maximJs.maxiTools.prototype.getArrayAsVectorDbl = function(arrayIn){
var vecOut = new Module.VectorDouble();
for(var i = 0; i < arrayIn.length; i++){
vecOut.push_back(arrayIn[i]);
......@@ -145,13 +145,144 @@ maximJs.maxiTools.getArrayAsVectorDbl = function(arrayIn){
return vecOut;
};
maximJs.maxiTools.loadSample = function(url, samplePlayer, contextIn) {
// ------------------------------------------------
maximJs.maxiAudio = function() {
this.numChannels = 2;
this.output = 0;
this.context = null;
this.source = null;
this.analyser = null;
this.jsProcessor = null;
this.initDone = false;
};
maximJs.maxiAudio.play = function(){};
// don't really need setup??
maximJs.maxiAudio.prototype.setup = function(){
console.log("non-overrided setup");
};
maximJs.maxiAudio.prototype.getNumChannels = function(){
return this.numChannels;
};
// isArray should be second param really
// set num channels and set output as an array
// use this if you want to change number of channels
maximJs.maxiAudio.prototype.setNumChannels = function(isArray, numChannels_){
this.numChannels = numChannels_;
this.outputIsArray(isArray, numChannels_);
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){
if(isArray){
this.output = new Array(this.numChannels);
for(var i = 0; i < this.numChannels; i++){
this.output[i] = 0;
}
} else {
this.output = 0;
}
};
// 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);
// 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(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)
;
this.analyser = this.context.createAnalyser();
this.analyser.fftSize = 2048;
// Connect the processing graph: source -> jsProcessor -> analyser -> destination
this.source.connect(this.jsProcessor);
this.jsProcessor.connect(this.analyser);
this.analyser.connect(this.context.destination);
this.initDone = true;
};
maximJs.maxiAudio.prototype.resetAudio = function(){
if(this.initDone){
this.source.disconnect();
this.jsProcessor.disconnect();
this.analyser.disconnect();
}
this.init();
};
// option to load sample if a different context is used
maximJs.maxiAudio.prototype.loadSample = function(url, samplePlayer, contextIn) {
var data = [];
var context;
if(!contextIn){
// context = GetContext();
context = new (window.AudioContext || window.webkitAudioContext)();
context = this.context;
} else {
context = contextIn;
}
......@@ -198,106 +329,3 @@ maximJs.maxiTools.loadSample = function(url, samplePlayer, contextIn) {
request.send();
};
// ------------------------------------------------
maximJs.maxiAudio = (function() {
var numChannels = 2;
var output = 0;
this.context = null;
this.source = null;
this.analyser = null;
this.jsProcessor = null;
var initDone = false;
this.play = function(){};
// don't really need setup??
this.setup = function(){
console.log("non-overrided setup");
};
this.getNumChannels = function(){
return numChannels;
};
// isArray should be second param really
// set num channels and set output as an array
// use this if you want to change number of channels
this.setNumChannels = function(isArray, numChannels_){
numChannels = numChannels_;
this.outputIsArray(isArray, numChannels_);
this.resetAudio();
};
// use this if you want to keep num of outputs but change
// method e.g. array or not
this.outputIsArray = function(isArray){
if(isArray){
maximJs.maxiAudio.output = new Array(numChannels);
for(var i = 0; i < numChannels; i++){
maximJs.maxiAudio.output[i] = 0;
}
} else {
maximJs.maxiAudio.output = 0;
}
};
// function handling audio processing
// called byjsProcessor
var process = function(event) {
var numChannels = event.outputBuffer.numberOfChannels;
var outputLength = event.outputBuffer.getChannelData(0).length;
for (var i = 0; i < outputLength; ++i) {
maximJs.maxiAudio.play();
if(maximJs.maxiAudio.output instanceof Array){
for (var channel = 0; channel < numChannels; channel++) {
event.outputBuffer.getChannelData(channel)[i] = maximJs.maxiAudio.output[channel];
}
}
else
{
for (var channel = 0; channel < numChannels; channel++) {
event.outputBuffer.getChannelData(channel)[i] = maximJs.maxiAudio.output;
}
}
}
};
this.resetAudio = function(){
if(initDone){
this.source.disconnect();
this.jsProcessor.disconnect();
this.analyser.disconnect();
}
this.init();
};
this.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, numChannels, numChannels);
this.jsProcessor.onaudioprocess = process;
this.analyser = this.context.createAnalyser();
this.analyser.fftSize = 2048;
// Connect the processing graph: source -> jsProcessor -> analyser -> destination
this.source.connect(this.jsProcessor);
this.jsProcessor.connect(this.analyser);
this.analyser.connect(this.context.destination);
initDone = true;
};
return this;
}());
\ No newline at end of file
// might adopt some strange functions from Module?
// var maximJs = Module;
// alternatively...
// probably better this way
// no inheritance of Module stuff...
var maximJs = {};
maximJs.maxiSettings = Module.maxiSettings;
maximJs.maxiOsc = Module.maxiOsc;
maximJs.maxiEnvelope = Module.maxiEnvelope;
maximJs.maxiDelayline = Module.maxiDelayline;
maximJs.maxiFilter = Module.maxiFilter;
maximJs.maxiMix = Module.maxiMix;
maximJs.maxiLagExp = Module.maxiLagExp;
maximJs.maxiSample = Module.maxiSample;
maximJs.maxiMap = Module.maxiMap;
maximJs.maxiDyn = Module.maxiDyn;
maximJs.maxiEnv = Module.maxiEnv;
maximJs.convert = Module.convert;
maximJs.maxiDistortion = Module.maxiDistortion;
maximJs.maxiFlanger = Module.maxiFlanger;
maximJs.maxiChorus = Module.maxiChorus;
maximJs.maxiDCBlocker = Module.maxiDCBlocker;
maximJs.maxiSVF = Module.maxiSVF;
maximJs.maxiEnvelopeFollower = Module.maxiEnvelopeFollower;
maximJs.maxiKick = Module.maxiKick;
maximJs.maxiSnare = Module.maxiSnare;
maximJs.maxiHats = Module.maxiHats;
maximJs.maxiClock = Module.maxiClock;
// LIBS
// FFT-ish
maximJs.maxiFFT = Module.maxiFFT;
maximJs.maxiIFFT = Module.maxiIFFT;
maximJs.maxiMFCC = Module.maxiMFCC;
maximJs.maxiFFTOctaveAnalyzer = Module.maxiFFTOctaveAnalyzer;
// Grains
maximJs.maxiTimestretch = Module.maxiTimestretch;
maximJs.maxiPitchShift = Module.maxiPitchShift;
maximJs.maxiPitchStretch = Module.maxiPitchStretch;
// ------------------------------------------------
// maxiArray - could extend Array object?
// cheaty array method to avoid mixing vector terminology with arrays