Commit 54d968c1 authored by Daniel Clarke's avatar Daniel Clarke

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