Commit 54d968c1 authored by Dr-Dan's avatar Dr-Dan

fixed some problems with analysis example

parent d0ab8933
This diff is collapsed.
...@@ -40,8 +40,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ...@@ -40,8 +40,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<!-- <link rel="stylesheet" type="text/css" href="javascript-processing_files/simple.css"> --> <!-- <link rel="stylesheet" type="text/css" href="javascript-processing_files/simple.css"> -->
<!-- <script src="../libs/maxiLib_maxiFFT.js"></script> -->
<script src="../maxiLib.js"></script> <script src="../maxiLib.js"></script>
<!-- Our javascript code --> <!-- Our javascript code -->
...@@ -68,12 +66,20 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ...@@ -68,12 +66,20 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
var audio = new maximJs.maxiAudio(); var audio = new maximJs.maxiAudio();
audio.init(); audio.init();
var fftSize = 1024;
var sampleRate = 44100;
var fft = new maximJs.maxiFFT(); var fft = new maximJs.maxiFFT();
var mfcc = new maximJs.maxiMFCC(); var mfcc = new maximJs.maxiMFCC();
var oct = new maximJs.maxiFFTOctaveAnalyzer(); var oct = new maximJs.maxiFFTOctaveAnalyzer();
var nAverages = 12; var nAverages = 12;
fft.setup(fftSize, 512, 256);
mfcc.setup(512, 42, 13, 20, 20000, sampleRate);
oct.setup(sampleRate, fftSize/2, nAverages);
// for storing fft values // for storing fft values
/* /*
required as passing a vector from one class required as passing a vector from one class
...@@ -81,20 +87,26 @@ to another isn't currently working ...@@ -81,20 +87,26 @@ to another isn't currently working
*/ */
var magnitudes = new Module.VectorFloat(); var magnitudes = new Module.VectorFloat();
var magnitudesDB = new Module.VectorFloat(); var magnitudesDB = new Module.VectorFloat();
var phases = new Module.VectorFloat();
// will store mfcc output // will store mfcc output
var mfccs = new Module.VectorDouble(); var mfccs = new Module.VectorDouble();
for(var i = 0; i < 13; ++i){
mfccs.push_back(0);
}
for(var i = 0; i < fftSize/2; ++i){
magnitudes.push_back(0);
}
var osc = new maximJs.maxiOsc(); var osc = new maximJs.maxiOsc();
// if you want to play a sample... // if you want to play a sample...
// var samplePlayer = new maximJs.maxiSample(); var samplePlayer = new maximJs.maxiSample();
// audio.loadSample("audio/beat2.wav", samplePlayer); audio.loadSample("./audio/beat2.wav", samplePlayer);
var fftSize = 1024;
var sampleRate = 44100;
var magMult = 6; var magMult = 6;
...@@ -124,18 +136,8 @@ var pitchHist = []; ...@@ -124,18 +136,8 @@ var pitchHist = [];
window.onload = setup; window.onload = setup;
function setup(){ function setup(){
fft.setup(fftSize, 512, 256);
// ifft.setup(fftSize, 512, 256);
mfcc.setup(512, 42, 13, 20, 20000, sampleRate);
oct.setup(sampleRate, fftSize/2, nAverages);
for(var i = 0; i < 13; ++i){
mfccs.push_back(0);
}
for(var i = 0; i < fftSize/2; ++i){
magnitudes.push_back(0);
}
// This gets the window in the browser // This gets the window in the browser
var canvas=document.getElementById("myCanvas"); var canvas=document.getElementById("myCanvas");
...@@ -152,30 +154,29 @@ audio.play = function(){ ...@@ -152,30 +154,29 @@ audio.play = function(){
// create wave for fft // create wave for fft
var wave = osc.square(220); var wave = osc.square(220);
// if(samplePlayer.isReady()){ // or use a sample
// var wave = samplePlayer.play(); // var wave = samplePlayer.isReady() ? samplePlayer.play() : 0.0;
// }
// process wave // process wave
if(fft.process(wave)){ if(fft.process(wave)){
fft.magsToDB(); fft.magsToDB();
// sorry, this is not nice // // sorry, this is not nice
Module.vectorTools.clearVectorFloat(magnitudes); Module.vectorTools.clearVectorFloat(magnitudes);
Module.vectorTools.clearVectorFloat(magnitudesDB); Module.vectorTools.clearVectorFloat(magnitudesDB);
for(var i = 0; i < fftSize/2; ++i){
magnitudes.push_back(fft.getMagnitude(i));
magnitudesDB.push_back(fft.getMagnitudeDB(i));
}
// pass magnitudes to mfcc and store in mfccs vector for(var i = 0; i < fftSize/2; ++i){
mfcc.mfcc(magnitudes, mfccs); magnitudes.push_back(fft.getMagnitude(i));
oct.calculate(magnitudesDB); magnitudesDB.push_back(fft.getMagnitudeDB(i));
} }
this.output = wave; // // pass magnitudes to mfcc and store in mfccs vector
mfcc.mfcc(magnitudes, mfccs);
oct.calculate(magnitudesDB);
}
this.output = wave;
} }
...@@ -246,40 +247,39 @@ for(var i=0; i < amtAvgs; i++) { ...@@ -246,40 +247,39 @@ for(var i=0; i < amtAvgs; i++) {
</script> </script>
<pre class="prettyprint lang-js linenums:true" id="quine" style="border:4px solid #88c" > <pre class="prettyprint lang-js linenums:true" id="quine" style="border:4px solid #88c" >
var audio = new maximJs.maxiAudio(); var audio = new maximJs.maxiAudio();
audio.init(); audio.init();
var fft = new maximJs.maxiFFT(); var fft = new maximJs.maxiFFT();
var mfcc = new maximJs.maxiMFCC(); var mfcc = new maximJs.maxiMFCC();
var oct = new maximJs.maxiFFTOctaveAnalyzer(); var oct = new maximJs.maxiFFTOctaveAnalyzer();
var nAverages = 12; var nAverages = 12;
// for storing fft values // for storing fft values
/* /*
required as passing a vector from one class required as passing a vector from one class
to another isn't currently working to another isn't currently working
*/ */
var magnitudes = new Module.VectorFloat(); var magnitudes = new Module.VectorFloat();
var magnitudesDB = new Module.VectorFloat(); var magnitudesDB = new Module.VectorFloat();
var phases = new Module.VectorFloat();
// will store mfcc output // will store mfcc output
var mfccs = new Module.VectorDouble(); var mfccs = new Module.VectorDouble();
var osc = new maximJs.maxiOsc(); var osc = new maximJs.maxiOsc();
// if you want to play a sample... // if you want to play a sample...
// var samplePlayer = new maximJs.maxiSample(); // var samplePlayer = new maximJs.maxiSample();
// audio.loadSample("audio/beat2.wav", samplePlayer); // audio.loadSample("./audio/beat2.wav", samplePlayer);
var fftSize = 1024; var fftSize = 1024;
var sampleRate = 44100; var sampleRate = 44100;
var magMult = 6; var magMult = 6;
var fftDraw = { var fftDraw = {
barsBottom: 600, barsBottom: 600,
barsLeft: 50, barsLeft: 50,
barsSize:1, barsSize:1,
...@@ -287,17 +287,17 @@ var fftDraw = { ...@@ -287,17 +287,17 @@ var fftDraw = {
}; };
var mfccDraw = { var mfccDraw = {
barsBottom: 350, barsBottom: 350,
barsLeft: 350, barsLeft: 350,
barsSize:10, barsSize:10,
magMult: 24 magMult: 24
}; };
var octDraw = { var octDraw = {
barsBottom: 150, barsBottom: 150,
barsLeft: 350, barsLeft: 350,
barsSize:10, barsSize:10,
magMult: 0.5 magMult: 0.5
}; };
var amtAvgs = 15; var amtAvgs = 15;
...@@ -305,58 +305,56 @@ var pitchHist = []; ...@@ -305,58 +305,56 @@ var pitchHist = [];
window.onload = setup; window.onload = setup;
function setup(){ function setup(){
fft.setup(fftSize, 512, 256); fft.setup(fftSize, 512, 256);
// ifft.setup(fftSize, 512, 256); mfcc.setup(512, 42, 13, 20, 20000, sampleRate);
mfcc.setup(512, 42, 13, 20, 20000, sampleRate); oct.setup(sampleRate, fftSize/2, nAverages);
oct.setup(sampleRate, fftSize/2, nAverages);
for(var i = 0; i < 13; ++i){ for(var i = 0; i < 13; ++i){
mfccs.push_back(0); mfccs.push_back(0);
} }
for(var i = 0; i < fftSize/2; ++i){ for(var i = 0; i < fftSize/2; ++i){
magnitudes.push_back(0); magnitudes.push_back(0);
} }
// This gets the window in the browser // This gets the window in the browser
var canvas=document.getElementById("myCanvas"); var canvas=document.getElementById("myCanvas");
// This creates a 2d drawing context in it. // This creates a 2d drawing context in it.
context=canvas.getContext("2d"); context=canvas.getContext("2d");
//finally, we return setInterval, which wants a function to call, //finally, we return setInterval, which wants a function to call,
//and a period in milliseconds to wait between calling it. //and a period in milliseconds to wait between calling it.
return setInterval(draw, 40); return setInterval(draw, 40);
} }
audio.play = function(){ audio.play = function(){
// create wave for fft // create wave for fft
var wave = osc.square(220); var wave = osc.square(220);
// if(samplePlayer.isReady()){ // or use a sample
// var wave = samplePlayer.play(); // var wave = samplePlayer.isReady() ? samplePlayer.play() : 0.0;
// }
// process wave // process wave
if(fft.process(wave)){ if(fft.process(wave)){
fft.magsToDB(); fft.magsToDB();
// sorry, this is not nice // // sorry, this is not nice
Module.vectorTools.clearVectorFloat(magnitudes); Module.vectorTools.clearVectorFloat(magnitudes);
Module.vectorTools.clearVectorFloat(magnitudesDB); Module.vectorTools.clearVectorFloat(magnitudesDB);
for(var i = 0; i < fftSize/2; ++i){ for(var i = 0; i < fftSize/2; ++i){
magnitudes.push_back(fft.getMagnitude(i)); magnitudes.push_back(fft.getMagnitude(i));
magnitudesDB.push_back(fft.getMagnitudeDB(i)); magnitudesDB.push_back(fft.getMagnitudeDB(i));
} }
// // pass magnitudes to mfcc and store in mfccs vector
mfcc.mfcc(magnitudes, mfccs);
oct.calculate(magnitudesDB);
}
this.output = wave;
// pass magnitudes to mfcc and store in mfccs vector
mfcc.mfcc(magnitudes, mfccs);
oct.calculate(magnitudesDB);
}
this.output = wave;
} }
...@@ -372,14 +370,14 @@ context.font = "30px Arial"; ...@@ -372,14 +370,14 @@ context.font = "30px Arial";
context.fillText("FFT",70,200); context.fillText("FFT",70,200);
for(var i=0; i < fftSize / 2; i++) { for(var i=0; i < fftSize / 2; i++) {
context.beginPath(); context.beginPath();
context.rect( context.rect(
fftDraw.barsLeft + i, fftDraw.barsLeft + i,
fftDraw.barsBottom, fftDraw.barsBottom,
fftDraw.barsSize, fftDraw.barsSize,
-(fft.getMagnitude(i) * fftDraw.magMult)); -(fft.getMagnitude(i) * fftDraw.magMult));
context.fill(); context.fill();
context.closePath(); context.closePath();
} }
// mfcc // mfcc
...@@ -387,41 +385,41 @@ context.fillStyle="rgba(0, 220, 0, 0.5)" ...@@ -387,41 +385,41 @@ context.fillStyle="rgba(0, 220, 0, 0.5)"
context.font = "30px Arial"; context.font = "30px Arial";
context.fillText("MFCC",350,250); context.fillText("MFCC",350,250);
for(var i=0; i < 13; i++) { for(var i=0; i < 13; i++) {
context.beginPath(); context.beginPath();
context.rect( context.rect(
mfccDraw.barsLeft + i*mfccDraw.barsSize, mfccDraw.barsLeft + i*mfccDraw.barsSize,
mfccDraw.barsBottom, mfccDraw.barsBottom,
mfccDraw.barsSize, mfccDraw.barsSize,
(mfccs.get(i) * mfccDraw.magMult)); (mfccs.get(i) * mfccDraw.magMult));
context.fill(); context.fill();
context.closePath(); context.closePath();
} }
// octave analyser // octave analyser
var j = 0; var j = 0;
for(var i=0; i < amtAvgs; ++i) { for(var i=0; i < amtAvgs; ++i) {
pitchHist[i] = 0; pitchHist[i] = 0;
} }
for (var i = 0; i < oct.nAverages; ++i) { for (var i = 0; i < oct.nAverages; ++i) {
pitchHist[j] += oct.getAverage(i); pitchHist[j] += oct.getAverage(i);
j++; j++;
j = j % amtAvgs; j = j % amtAvgs;
} }
context.fillStyle="rgba(0, 0, 255, 0.5)" context.fillStyle="rgba(0, 0, 255, 0.5)"
context.font = "30px Arial"; context.font = "30px Arial";
context.fillText("OCT",350,50); context.fillText("OCT",350,50);
for(var i=0; i < amtAvgs; i++) { for(var i=0; i < amtAvgs; i++) {
context.beginPath(); context.beginPath();
context.rect( context.rect(
octDraw.barsLeft + i*(octDraw.barsSize*1.5), octDraw.barsLeft + i*(octDraw.barsSize*1.5),
octDraw.barsBottom, octDraw.barsBottom,
octDraw.barsSize, octDraw.barsSize,
-(pitchHist[i] * octDraw.magMult)); -(pitchHist[i] * octDraw.magMult));
context.fill(); context.fill();
context.closePath(); context.closePath();
} }
} }
</pre> </pre>
......
...@@ -51,10 +51,10 @@ void maxiFFT::setup(int _fftSize, int _windowSize, int _hopSize) { ...@@ -51,10 +51,10 @@ void maxiFFT::setup(int _fftSize, int _windowSize, int _hopSize) {
hopSize = _hopSize; hopSize = _hopSize;
buffer = (float *) malloc(fftSize * sizeof(float)); buffer = (float *) malloc(fftSize * sizeof(float));
// magnitudes = (float *) malloc(bins * sizeof(float)); // magnitudes = (float *) malloc(bins * sizeof(float));
magnitudes.reserve(bins); magnitudes.resize(bins);
magnitudesDB = (float *) malloc(bins * sizeof(float)); magnitudesDB = (float *) malloc(bins * sizeof(float));
// phases = (float *) malloc(bins * sizeof(float)); // phases = (float *) malloc(bins * sizeof(float));
phases.reserve(bins); phases.resize(bins);
avgPower = new float; avgPower = new float;
memset(buffer, 0, fftSize * sizeof(float)); memset(buffer, 0, fftSize * sizeof(float));
// memset(magnitudes, 0, bins * sizeof(float)); // memset(magnitudes, 0, bins * sizeof(float));
......
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