view src/SoundFileLoader.cpp @ 2:fa2af670b5c5 tip

SoundFileLoader might have moved
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Fri, 06 Jan 2012 00:23:26 +0000
parents
children
line wrap: on
line source
/*
 *  SoundFileLoader.cpp
 *  audioFileLoaderSVN1
 *
 *  Created by Andrew on 04/09/2011.
 *  Copyright 2011 QMUL. All rights reserved.
 *
 */

#include "SoundFileLoader.h"


SoundFileLoader::SoundFileLoader(){
	sfinfo.format = 0;
	
	chromaG = &chromoGramm;
	
}

void SoundFileLoader::loadLibSndFile(const char *infilename){
	
	if (!sf_close(infile)){
		printf("closed sndfile okay \n");
	}
	
	// Open Input File with lib snd file
    if (! (infile = sf_open (infilename, SFM_READ, &sfinfo)))
    {   // Open failed
        printf ("SF OPEN routine Not able to open input file %s.\n", infilename) ;
        // Print the error message from libsndfile. 
        puts (sf_strerror (NULL)) ;
		
	} else{
		printf("SF OPEN : file %s okay, ", infilename);
		printf("number of channels is %i\n", sfinfo.channels);
		//sndfileInfoString = "Opened okay ";
		
	};
	
	processAudioToDoubleMatrix();
	
}




void SoundFileLoader::processAudioToDoubleMatrix(){
	
	
	
	chromaAnalysis.chromaMatrix.clear();
	chromaAnalysis.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);	
				
			}	
			
			chromaAnalysis.chromaMatrix.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();
		chromaAnalysis.energyVector.push_back(energyValue);
		
		
		
	}//end readcount
	
	
	
	printf("Max chroma value is %f \n", chromaG->maximumChromaValue);
	
	
	
	//normalise
	int length = chromaAnalysis.chromaMatrix.size();
	printf("length of chromagram is %d frames\n", length);
	length = (chromaAnalysis.chromaMatrix[0]).size();
	printf("height of dmatrix is %d\n", length);
	
	for (int i = 0; i < chromaAnalysis.chromaMatrix.size();i++){
		for (int j = 0; j < (chromaAnalysis.chromaMatrix[0]).size();j++){
			chromaAnalysis.chromaMatrix[i][j] /= chromaG->maximumChromaValue;	
		}
	}
	
	
	printf("size of energy vector is %d \n", (int) chromaAnalysis.energyVector.size());
	
	totalNumberOfFrames = (int) chromaAnalysis.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();
}



double SoundFileLoader::getEnergyOfFrame(){
	
	float totalEnergyInFrame = 0;
	
	for (int i = 0;i<FRAMESIZE;i++){
		
		totalEnergyInFrame += (frame[i] * frame[i]);
		
	}
	totalEnergyInFrame = sqrt(totalEnergyInFrame);
	
	return totalEnergyInFrame;
}