Mercurial > hg > audio-file-loader
changeset 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 | ba2a17cf81bf |
children | |
files | src/SoundFileLoader.cpp src/SoundFileLoader.h |
diffstat | 2 files changed, 224 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SoundFileLoader.cpp Fri Jan 06 00:23:26 2012 +0000 @@ -0,0 +1,167 @@ +/* + * 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; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SoundFileLoader.h Fri Jan 06 00:23:26 2012 +0000 @@ -0,0 +1,57 @@ +/* + * SoundFileLoader.h + * audioFileLoaderSVN1 + * + * Created by Andrew on 04/09/2011. + * Copyright 2011 QMUL. All rights reserved. + * + */ + +#ifndef SOUND_FILE_LOADER_H +#define SOUND_FILE_LOADER_H + + +#include "fftw3.h" +#include "ofMain.h" +#include "sndfile.h" +#include "AudioFile.h" +#include "AudioAnalysis.h" +#include "chromaGram.h" +#include "ChordDetect.h" + +#define FRAMESIZE 512 +#define ENERGY_LENGTH 80000 +#define CHROMA_LENGTH 12000 +#define CHROMA_CONVERSION_FACTOR 16 //16 times as many frames in energy as in chroma + + +//this does a chromagram analysis and aubio onset analysis +//held in double matrix and doubleVector respectively +//these are dynamic vectors, so size set by what's needed for the file + +class SoundFileLoader{ + +public: + SoundFileLoader(); + float frame[FRAMESIZE]; + + void loadLibSndFile(const char *infilename); + + AudioFile audioHolder; + AudioAnalysis chromaAnalysis; + Chromagram chromoGramm; + Chromagram* chromaG; + + typedef std::vector<double> DoubleVector; + typedef std::vector<DoubleVector> DoubleMatrix; + + void processAudioToDoubleMatrix(); + double getEnergyOfFrame(); + + int totalNumberOfFrames; + + SNDFILE *infile; // define input and output sound files + SF_INFO sfinfo ; // struct to hold info about sound file + +}; +#endif