view src/BeatAnnotations.cpp @ 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
children
line wrap: on
line source
/*
 *  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(){

}