Commit 0c43df36 authored by Dr-Dan's avatar Dr-Dan

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++ EMSCR=../../em++
# ---------------------------------------- # ----------------------------------------
......
...@@ -33330,25 +33330,107 @@ maximJs.maxiArray.prototype.clear = (function(useSq) { ...@@ -33330,25 +33330,107 @@ maximJs.maxiArray.prototype.clear = (function(useSq) {
this.length = 0; this.length = 0;
}); });
maximJs.maxiTools = (function() {}); maximJs.maxiTools = (function() {});
maximJs.maxiTools.arrayOfObj = (function(obj, num) { maximJs.maxiTools.prototype.arrayOfObj = (function(obj, num) {
var array = []; var array = [];
for (var i = 0; i < num; i++) { for (var i = 0; i < num; i++) {
array.push(new obj); array.push(new obj);
} }
return array; return array;
}); });
maximJs.maxiTools.getArrayAsVectorDbl = (function(arrayIn) { maximJs.maxiTools.prototype.getArrayAsVectorDbl = (function(arrayIn) {
var vecOut = new Module.VectorDouble; var vecOut = new Module.VectorDouble;
for (var i = 0; i < arrayIn.length; i++) { for (var i = 0; i < arrayIn.length; i++) {
vecOut.push_back(arrayIn[i]); vecOut.push_back(arrayIn[i]);
} }
return vecOut; 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 data = [];
var context; var context;
if (!contextIn) { if (!contextIn) {
context = new (window.AudioContext || window.webkitAudioContext); context = this.context;
} else { } else {
context = contextIn; context = contextIn;
} }
...@@ -33375,74 +33457,6 @@ maximJs.maxiTools.loadSample = (function(url, samplePlayer, contextIn) { ...@@ -33375,74 +33457,6 @@ maximJs.maxiTools.loadSample = (function(url, samplePlayer, contextIn) {
}); });
request.send(); 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. ...@@ -44,12 +44,11 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<!-- Our javascript code --> <!-- Our javascript code -->
<script type="text/javascript"> <script type="text/javascript">
maximJs.maxiAudio.init(); maximJs.maxiAudio.init();
maximJs.maxiAudio.init();
var samplePlayer = new maximJs.maxiSample(); var samplePlayer = new maximJs.maxiSample();
var mySine = new maximJs.maxiOsc(); 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.maxiTools.loadSample("audio/beat2.wav", samplePlayer, maximJs.maxiAudio.context);
maximJs.maxiAudio.play = function(){ maximJs.maxiAudio.play = function(){
......
...@@ -44,14 +44,14 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ...@@ -44,14 +44,14 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<!-- Our javascript code --> <!-- Our javascript code -->
<script type="text/javascript"> <script type="text/javascript">
var audio = new maximJs.maxiAudio();
// initialise audio // initialise audio
maximJs.maxiAudio.init(); audio.init();
var mySine = new maximJs.maxiOsc(); var mySine = new maximJs.maxiOsc();
maximJs.maxiAudio.play = function(){ audio.play = function(){
output = mySine.sinewave(440); this.output = mySine.sinewave(440);
} }
......
...@@ -43,8 +43,55 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ...@@ -43,8 +43,55 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<script src="../maxiLib.js"></script> <script src="../maxiLib.js"></script>
<!-- <script src="../maxi_webAudio.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 // initialise audio
maximJs.maxiAudio.init(); maximJs.maxiAudio.init();
...@@ -55,16 +102,10 @@ var myOtherSine = new maximJs.maxiOsc(); ...@@ -55,16 +102,10 @@ var myOtherSine = new maximJs.maxiOsc();
maximJs.maxiAudio.play = function(){ maximJs.maxiAudio.play = function(){
// these two sines will beat together. They're now a bit too loud though.. // these two sines will beat together. They're now a bit too loud though..
output = mySine.sinewave(440) + myOtherSine.sinewave(441); output = mySine.sinewave(440) + myOtherSine.sinewave(441);
} }
</script> </script>
</head>
<body>
<h1> Two Tones </h1>
<p> <p>
These sines are taking a beating... These sines are taking a beating...
</p> </p>
......
...@@ -1659,6 +1659,21 @@ double maxiDyn::compress(double input) { ...@@ -1659,6 +1659,21 @@ double maxiDyn::compress(double input) {
return output*(1+log(ratio)); 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. /* 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. It takes mental numbers for attack and release tho. Basically, they're exponentials.
...@@ -1830,21 +1845,7 @@ void maxiEnv::setDecay(double decayMS) { ...@@ -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) { double convert::mtof(int midinote) {
......
...@@ -119,14 +119,14 @@ public: ...@@ -119,14 +119,14 @@ public:
valindex = index; valindex = index;
} }
int getValindex() const{
return valindex;
}
void setAmplitude(double amp){ void setAmplitude(double amp){
amplitude = amp; amplitude = amp;
} }
int getValindex() const{
return valindex;
}
double getAmplitude() const{ double getAmplitude() const{
return amplitude; return amplitude;
} }
...@@ -178,15 +178,15 @@ public: ...@@ -178,15 +178,15 @@ public:
void setCutoff(double cut){ void setCutoff(double cut){
cutoff = cut; cutoff = cut;
} }
void setResonance(double res){
resonance = res;
}
double getCutoff() const{ double getCutoff() const{
return cutoff; return cutoff;
} }
void setResonance(double res){
resonance = res;
}
double getResonance() const{ double getResonance() const{
return resonance; return resonance;
} }
...@@ -247,6 +247,8 @@ public: ...@@ -247,6 +247,8 @@ public:
inline T value() { inline T value() {
return val; return val;
} }
}; };
...@@ -486,6 +488,13 @@ public: ...@@ -486,6 +488,13 @@ public:
// ------------------------------------------------ // ------------------------------------------------
// getters/setters // getters/setters
// int getTrigger() const{
// return trigger;
// }
// void setTrigger(int trigger){
// this->trigger = trigger;
// }
// ------------------------------------------------ // ------------------------------------------------
}; };
......
...@@ -308,7 +308,7 @@ public: ...@@ -308,7 +308,7 @@ public:
char* myData; char* myData;
tempVec //tempVec
// different vars for use with js // different vars for use with js
vector<double> tempVec; vector<double> tempVec;
......
...@@ -127,7 +127,7 @@ maximJs.maxiArray.prototype.clear = function(useSq){ ...@@ -127,7 +127,7 @@ maximJs.maxiArray.prototype.clear = function(useSq){
// tools // tools
maximJs.maxiTools = function(){}; maximJs.maxiTools = function(){};
maximJs.maxiTools.arrayOfObj = function(obj, num){ maximJs.maxiTools.prototype.arrayOfObj = function(obj, num){
var array = []; var array = [];
for(var i = 0; i < num; i++){ for(var i = 0; i < num; i++){
...@@ -136,7 +136,7 @@ maximJs.maxiTools.arrayOfObj = function(obj, num){ ...@@ -136,7 +136,7 @@ maximJs.maxiTools.arrayOfObj = function(obj, num){
return array; return array;
}; };
maximJs.maxiTools.getArrayAsVectorDbl = function(arrayIn){ maximJs.maxiTools.prototype.getArrayAsVectorDbl = function(arrayIn){
var vecOut = new Module.VectorDouble(); var vecOut = new Module.VectorDouble();
for(var i = 0; i < arrayIn.length; i++){ for(var i = 0; i < arrayIn.length; i++){
vecOut.push_back(arrayIn[i]); vecOut.push_back(arrayIn[i]);
...@@ -145,13 +145,144 @@ maximJs.maxiTools.getArrayAsVectorDbl = function(arrayIn){ ...@@ -145,13 +145,144 @@ maximJs.maxiTools.getArrayAsVectorDbl = function(arrayIn){
return vecOut; 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);