andrew@5: /* andrew@5: * BeatAnnotations.cpp andrew@5: * BeatAnnotationViewer andrew@5: * andrew@5: * Created by Andrew on 31/10/2013. andrew@5: * Copyright 2013 QMUL. All rights reserved. andrew@5: * andrew@5: */ andrew@5: andrew@5: #include "sndfile.h" andrew@5: #include "BeatAnnotations.h" andrew@5: //Beat tracking andrew@5: #include "BTrack.h" andrew@5: #include andrew@5: #include "df.h" andrew@5: #include "BeatWriter.h" andrew@5: andrew@5: BeatAnnotations::BeatAnnotations(){ andrew@5: } andrew@5: andrew@5: void BeatAnnotations::loadBeatsFromAnnotations(std::string filename){ andrew@5: reader.readInFile(filename); andrew@5: } andrew@5: andrew@5: int BeatAnnotations::processAudioForBeatTimes(std::string audiofile){ andrew@5: andrew@5: BeatWriter writer; andrew@5: andrew@5: bool writeOutput = false;//write output to txt andrew@5: andrew@5: // static double frame[FRAMESIZE]; // to hold a single frame andrew@5: double buffer[FRAMESIZE*2]; andrew@5: double dfval; andrew@5: andrew@5: for (int i = 0;i < (FRAMESIZE*2);i++) andrew@5: { andrew@5: buffer[i] = 0; andrew@5: } andrew@5: andrew@5: df *detfun; andrew@5: BTrack b; andrew@5: andrew@5: b.initialise(FRAMESIZE); andrew@5: andrew@5: detfun = new df(1,(FRAMESIZE*2),0); andrew@5: andrew@5: SNDFILE *infile, *outfile ; // define input and output sound files andrew@5: andrew@5: SF_INFO sfinfo ; // struct to hold info about sound file andrew@5: int readcount ; // counts number of samples read from sound file andrew@5: const char *infilename = audiofile.c_str(); andrew@5: //"/Users/andrew/Music/Station To Station 2/3-03 Panic In Detroit (Live Nassau Coliseum '76).wav";//"ledzep.wav" ; // input file name andrew@5: // const char *outfilename = "output.wav" ; // output file name andrew@5: andrew@5: if (writeOutput) andrew@5: writer.openFile("/Users/andrew/testFile.txt"); andrew@5: andrew@5: // Open Input File andrew@5: if (! (infile = sf_open (infilename, SFM_READ, &sfinfo))) andrew@5: { // Open failed andrew@5: printf ("Not able to open input file %s.\n", infilename) ; andrew@5: /* Print the error message from libsndfile. */ andrew@5: puts (sf_strerror (NULL)) ; andrew@5: return 1; andrew@5: } ; andrew@5: andrew@5: printf("opened '%s'\n", audiofile.c_str()); andrew@5: andrew@5: //STEREO OKAY andrew@5: andrew@5: //HERE IS THE CLASSIC LOADING FILE CODE andrew@5: //DEALS WITH MORE THAN MONO andrew@5: int channels = sfinfo.channels; andrew@5: int blocksize = FRAMESIZE; andrew@5: andrew@5: float buf [channels * blocksize] ; andrew@5: float frame[blocksize]; andrew@5: andrew@5: int k, m; andrew@5: readcount = 1; andrew@5: int counter = 0; andrew@5: andrew@5: //DoubleVector d; andrew@5: while ((readcount = sf_readf_float (infile, buf, blocksize)) > 0){ andrew@5: for (k = 0 ; k < readcount ; k++){ andrew@5: //d.clear(); andrew@5: frame[k] = 0; andrew@5: andrew@5: for (m = 0 ; m < channels ; m++){ andrew@5: frame[k] += buf[k*channels + 0];//sum the channels together andrew@5: //d.push_back(buf [k * channels + m]); andrew@5: } andrew@5: andrew@5: frame[k] /= channels;//average of the channels andrew@5: } andrew@5: andrew@5: //processing here andrew@5: //add to our buffer for sending to Btrack andrew@5: for (int i = 0; i< FRAMESIZE;i++) andrew@5: { andrew@5: buffer[i] = buffer[i+FRAMESIZE]; andrew@5: buffer[i+FRAMESIZE] = frame[i]; andrew@5: } andrew@5: andrew@5: dfval = detfun->compute(buffer); // compute detection function sample andrew@5: andrew@5: printf("det val %i: %f\n", counter, dfval); andrew@5: andrew@5: b.process(dfval); // process df sample in beat tracker andrew@5: andrew@5: if (b.playbeat == 1) andrew@5: { andrew@5: printf("BEAT:beat at frame %i, seconds %f\n", counter, (counter*FRAMESIZE/44100.)); andrew@5: andrew@5: if (writeOutput) andrew@5: writer.writeBeatTime((counter*FRAMESIZE/44100.)); andrew@5: } else { andrew@5: printf("not beat\n"); andrew@5: } andrew@5: counter++; andrew@5: andrew@5: //was sf_write_double(outfile, frame, readcount) ; andrew@5: andrew@5: }//end readcount andrew@5: //END STEREO OKAY andrew@5: andrew@5: // Close input file andrew@5: sf_close (infile); andrew@5: andrew@5: if (writeOutput) andrew@5: writer.closeFile(); andrew@5: andrew@5: delete detfun; andrew@5: andrew@5: return 0; andrew@5: andrew@5: } andrew@5: andrew@5: void BeatAnnotations::draw(){ andrew@5: andrew@5: } andrew@5: