view src/AudioAnalysis.cpp @ 0:bcb0d40158f4

started audio file loader project - using oF_061
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Tue, 30 Aug 2011 20:18:34 +0100
parents
children ba2a17cf81bf
line wrap: on
line source
/*
 *  AudioAnalysis.cpp
 *  audioFileLoader4
 *
 *  Created by Andrew on 14/08/2011.
 *  Copyright 2011 QMUL. All rights reserved.
 *
 */

#include "AudioAnalysis.h"


/*
void testApp::initialiseVariables(){
	
	energyIndex = 0;
	//	frameIndex = 0;
	chromaIndex = 0;
	chromoGramm.initialise(FRAMESIZE,2048);//framesize 512 and hopsize 2048
}


//--------------------------------------------------------------
void testApp::draw(){
	switch (screenToDraw){
		case 0:
			if (drawSpectralDifferenceFunction)
				drawSpectralDifference(&chromaMatrix);
			else
				drawDoubleMatrix(&chromaMatrix);
			
			drawEnergyVectorFromPointer(&firstEnergyVector);
			break;
		case 1:
			//	audioHolder.drawAudioVectorMillis(1000, 1000+audioScale);
			audioHolder.drawAudioVectorSamples(audioHolder.playPosition, audioHolder.playPosition+audioHolder.audioScaleSamples);
			break;
	}
	
	
	
	//ofSetColor(255,0,0);
	//drawEnergyVectorFromPointer(&audioVector);
	
	ofSetColor(0xFFFFFF);
	ofLine(audioPosition*width, 0, audioPosition*width, height);
	
	ofDrawBitmapString(soundFileName,80,480);
	
}


void testApp::drawEnergyVectorFromPointer(DoubleVector* energyVec){
	
	ofSetColor(0xFF0066);
	float screenHeight = ofGetHeight() ;
	float screenWidth = ofGetWidth();  
	float heightFactor = 8;
	int i, j, startingFrame;
	startingFrame = currentPlayingFrame / scrollWidth;//i.e. number of scroll widths in
	startingFrame *= scrollWidth;
	
	for (i = 0; i < scrollWidth - 1; i++){
		j = i + startingFrame;
		if (j < (*energyVec).size())
			ofLine(i*screenWidth/scrollWidth, screenHeight - ((*energyVec)[j]*screenHeight/heightFactor),
				   screenWidth*(i+1)/scrollWidth, screenHeight - ((*energyVec)[j+1]*screenHeight/heightFactor));
		
	}
}

void testApp::drawSpectralDifference(DoubleMatrix* dMatrix){
	int matrixSize = (*dMatrix).size();
	if (matrixSize > 0){
		
		float screenHeight = ofGetHeight() ;
		float screenWidth = ofGetWidth();
		float heightFactor = 8;
		double difference;
		int i, j, startingFrame;
		startingFrame = currentPlayingFrame / scrollWidth;//i.e. number of scroll widths in
		startingFrame *= scrollWidth;//starting frame in terms of energy frames
		startingFrame /= CHROMA_CONVERSION_FACTOR; //in terms of chroma frames
		
		float chromoLength = scrollWidth/CHROMA_CONVERSION_FACTOR;
		for (i = 1; i < chromoLength; i++){//changed to add 1
			j = i + startingFrame;
			for (int y = 0;y < 12;y++){			
				
				if (j < matrixSize)
					difference = (*dMatrix)[j][11-y] - (*dMatrix)[j-1][11-y];
				else 
					difference = 0;
				
				if (difference < 0)
					difference = 0;//half wave rectify
				
				ofSetColor(0,0,255 * difference);//, 0;
				ofRect(i*screenWidth/chromoLength,y*screenHeight/12,screenWidth/chromoLength,screenHeight/12);
			}//end y
		}//end i
		
	}///end if matrix has content
	else{
		printf("Error - please load audio first");
	}
	
}


void testApp::drawDoubleMatrix(DoubleMatrix* dMatrix){
	//used to draw the chromagram matrix
	int matrixSize = (*dMatrix).size();
	if (matrixSize > 0){
		
		float screenHeight = ofGetHeight() ;
		float screenWidth = ofGetWidth();
		float heightFactor = 8;
		int i, j, startingFrame;
		startingFrame = currentPlayingFrame / scrollWidth;//i.e. number of scroll widths in
		startingFrame *= scrollWidth;//starting frame in terms of energy frames
		startingFrame /= CHROMA_CONVERSION_FACTOR; //in terms of chroma frames
		
		float chromoLength = scrollWidth/CHROMA_CONVERSION_FACTOR;
		for (i = 0; i < chromoLength; i++){
			j = i + startingFrame;
			for (int y = 0;y < 12;y++){
				
				if (j < matrixSize)
					ofSetColor(0,0,255 * (*dMatrix)[j][11-y]);
				else 
					ofSetColor(0,0,0);
				
				ofRect(i*screenWidth/chromoLength,y*screenHeight/12,screenWidth/chromoLength,screenHeight/12);
			}//end y
		}//end i
		
	}///end if matrix has content
	else{
		printf("Error - please load audio first");
	}
	
	
}





void testApp::processAudioToDoubleMatrix(Chromagram* chromaG, DoubleMatrix* myDoubleMatrix, DoubleVector* energyVector){
	
	myDoubleMatrix->clear();
	energyVector->clear();
	
	audioHolder.audioVector.clear();
	audioHolder.audioMatrix.clear();
	
	energyIndex = 0;

	chromaG->initialise(FRAMESIZE,2048);//framesize 512 and hopsize 2048 
	chromaG->maximumChromaValue = 0;
	
	
	// HERE IS THE CLASSIC LOADING FILE CODE
	//DEALS WITH MORE THAN MONO
	int channels = sfinfo.channels;
	int blocksize = FRAMESIZE;
	
	float buf [channels * blocksize] ;
	int k, m, readcount ;
	
	DoubleVector d;
	while ((readcount = sf_readf_float (infile, buf, blocksize)) > 0){
		for (k = 0 ; k < readcount ; k++){	
			d.clear();
			for (m = 0 ; m < channels ; m++){
				d.push_back(buf [k * channels + m]);
				//		 fprintf (outfile, " % 12.10f", buf [k * channels + m]) ;
				//		 fprintf (outfile, "\n") ;
				if (m == 0){
					//makes the vector hold the mono file
					//this is the one we use for chromagram analysis etc
					audioHolder.audioVector.push_back(buf[k * channels + 0]);
					frame[k] = buf[k*channels + 0];
				}
				
			} 
			audioHolder.audioMatrix.push_back(d);
			//storing the full soundfile in multiple channels in the audioMatrix
		}
		
		
		chromaG->processframe(frame);
		
		if (chromaG->chromaready)
		{
			DoubleVector d;
			
			for (int i = 0;i<12;i++){
				//chromoGramVector[chromaIndex][i] = chromoGramm.rawChroma[i] / chromoGramm.maximumChromaValue;
				d.push_back(chromaG->rawChroma[i]);// / chromaG->maximumChromaValue);	
				
			}	
			
			myDoubleMatrix->push_back(d);
			
			//There was a method to detect chord but deleted
			//	chord.C_Detect(chromoGramm.chroma,chromoGramm.chroma_low);
			//	rootChord[chromaIndex] = chord.root;
			
			
		}//end if chromagRamm ready
		
		//	frameIndex++;
		
		//get energy of the current frame and wait
		double energyValue = getEnergyOfFrame();
		energyVector->push_back(energyValue);
		
		
		
	}//end readcount
	
	
	
	printf("Max chroma value is %f \n", chromaG->maximumChromaValue);
	
	
	
	//normalise
	int length = myDoubleMatrix->size();
	printf("length of chromagram is %d frames\n", length);
	length = ((*myDoubleMatrix)[0]).size();
	printf("height of dmatrix is %d\n", length);
	
	for (int i = 0; i < myDoubleMatrix->size();i++){
		for (int j = 0; j < ((*myDoubleMatrix)[0]).size();j++){
			(*myDoubleMatrix)[i][j] /= chromaG->maximumChromaValue;	
		}
	}
	
	
	printf("size of energy vector is %d \n", (int) energyVector->size());
	
	totalNumberOfFrames = (int) energyVector->size();//frameIndex;//used to use this - but switch to energy vector's size instead
	
	//	printf("Total frames %i energy index %i and Chroma index %i \n", frameIndex, energyIndex, chromaIndex);
	
	printf("audio vector size is %i\n", (int) audioHolder.audioVector.size());
	audioHolder.length = (int) audioHolder.audioVector.size();
}

*/