Mercurial > hg > precise-onset-detection
changeset 5:1e636a3511fb
added beat annotations
author | Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk> |
---|---|
date | Mon, 06 Jan 2014 18:10:15 +0000 |
parents | 93b9a9471011 |
children | eb29c6b6dff8 |
files | src/BeatAnnotations.cpp src/BeatAnnotations.h |
diffstat | 2 files changed, 175 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/BeatAnnotations.cpp Mon Jan 06 18:10:15 2014 +0000 @@ -0,0 +1,142 @@ +/* + * BeatAnnotations.cpp + * BeatAnnotationViewer + * + * Created by Andrew on 31/10/2013. + * Copyright 2013 QMUL. All rights reserved. + * + */ + +#include "sndfile.h" +#include "BeatAnnotations.h" +//Beat tracking +#include "BTrack.h" +#include <Math.h> +#include "df.h" +#include "BeatWriter.h" + +BeatAnnotations::BeatAnnotations(){ +} + +void BeatAnnotations::loadBeatsFromAnnotations(std::string filename){ + reader.readInFile(filename); +} + +int BeatAnnotations::processAudioForBeatTimes(std::string audiofile){ + + BeatWriter writer; + + bool writeOutput = false;//write output to txt + + // static double frame[FRAMESIZE]; // to hold a single frame + double buffer[FRAMESIZE*2]; + double dfval; + + for (int i = 0;i < (FRAMESIZE*2);i++) + { + buffer[i] = 0; + } + + df *detfun; + BTrack b; + + b.initialise(FRAMESIZE); + + detfun = new df(1,(FRAMESIZE*2),0); + + SNDFILE *infile, *outfile ; // define input and output sound files + + SF_INFO sfinfo ; // struct to hold info about sound file + int readcount ; // counts number of samples read from sound file + const char *infilename = audiofile.c_str(); + //"/Users/andrew/Music/Station To Station 2/3-03 Panic In Detroit (Live Nassau Coliseum '76).wav";//"ledzep.wav" ; // input file name + // const char *outfilename = "output.wav" ; // output file name + + if (writeOutput) + writer.openFile("/Users/andrew/testFile.txt"); + + // Open Input File + if (! (infile = sf_open (infilename, SFM_READ, &sfinfo))) + { // Open failed + printf ("Not able to open input file %s.\n", infilename) ; + /* Print the error message from libsndfile. */ + puts (sf_strerror (NULL)) ; + return 1; + } ; + + printf("opened '%s'\n", audiofile.c_str()); + + //STEREO OKAY + + //HERE IS THE CLASSIC LOADING FILE CODE + //DEALS WITH MORE THAN MONO + int channels = sfinfo.channels; + int blocksize = FRAMESIZE; + + float buf [channels * blocksize] ; + float frame[blocksize]; + + int k, m; + readcount = 1; + int counter = 0; + + //DoubleVector d; + while ((readcount = sf_readf_float (infile, buf, blocksize)) > 0){ + for (k = 0 ; k < readcount ; k++){ + //d.clear(); + frame[k] = 0; + + for (m = 0 ; m < channels ; m++){ + frame[k] += buf[k*channels + 0];//sum the channels together + //d.push_back(buf [k * channels + m]); + } + + frame[k] /= channels;//average of the channels + } + + //processing here + //add to our buffer for sending to Btrack + for (int i = 0; i< FRAMESIZE;i++) + { + buffer[i] = buffer[i+FRAMESIZE]; + buffer[i+FRAMESIZE] = frame[i]; + } + + dfval = detfun->compute(buffer); // compute detection function sample + + printf("det val %i: %f\n", counter, dfval); + + b.process(dfval); // process df sample in beat tracker + + if (b.playbeat == 1) + { + printf("BEAT:beat at frame %i, seconds %f\n", counter, (counter*FRAMESIZE/44100.)); + + if (writeOutput) + writer.writeBeatTime((counter*FRAMESIZE/44100.)); + } else { + printf("not beat\n"); + } + counter++; + + //was sf_write_double(outfile, frame, readcount) ; + + }//end readcount + //END STEREO OKAY + + // Close input file + sf_close (infile); + + if (writeOutput) + writer.closeFile(); + + delete detfun; + + return 0; + +} + +void BeatAnnotations::draw(){ + +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/BeatAnnotations.h Mon Jan 06 18:10:15 2014 +0000 @@ -0,0 +1,33 @@ +/* + * BeatAnnotations.h + * BeatAnnotationViewer + * + * Created by Andrew on 31/10/2013. + * Copyright 2013 QMUL. All rights reserved. + * + */ + + + +#ifndef BEAT_ANNOTATIONS_H +#define BEAT_ANNOTATIONS_H + + +#define FRAMESIZE 512 +#include "NoteDataReader.h" + +class BeatAnnotations{ +public: + BeatAnnotations(); + + void draw(); + + void loadBeatsFromAnnotations(std::string filename); + int processAudioForBeatTimes(std::string audiofile); + + //vars + typedef std::vector<double> DoubleVector; + DoubleVector beatTimes;///beats in seconds + NoteDataReader reader; +}; +#endif \ No newline at end of file