robert@464: /* robert@464: * spear_parser.cpp v1.2 robert@464: * robert@464: * Created on: May 6, 2014 robert@464: * Author: Victor Zappi robert@464: */ robert@464: robert@464: #include "spear_parser.h" robert@464: robert@464: using namespace std; robert@464: robert@464: //#define DO_CHECKS robert@464: robert@464: //------------------------------------------------------------------------------------------------ robert@464: // partials robert@464: //------------------------------------------------------------------------------------------------ robert@464: robert@464: Partials::Partials() robert@464: { robert@464: partialFrequencies = NULL; robert@464: // partialAmplitudes = NULL; robert@464: // partialNumFrames = NULL; robert@464: // partialStartSample = NULL; robert@464: // partialEndSample = NULL; robert@464: // partialCurrentFrame = NULL; robert@464: // partialFreqDelta = NULL; robert@464: // partialAmpDelta = NULL; robert@464: robert@464: robert@464: activePartialNum = NULL; robert@464: // activePartials = NULL; robert@464: robert@464: currentSample = -1; robert@464: } robert@464: robert@464: Partials::~Partials() robert@464: { robert@464: if(partialFrequencies != NULL) // check on one is enough robert@464: { robert@464: if(partialFrequencies[0] != NULL) // check on one is enough robert@464: { robert@464: for(unsigned int i=0; i maxSample) robert@464: maxSample = endSample; robert@464: robert@464: // update data structure robert@464: partials.update(parIndex, frameNum); robert@464: robert@464: robert@464: //------------------------------------- robert@464: // frames robert@464: getline(fin, s); robert@464: token = strtok((char *)s.c_str(), " "); // frame time robert@464: frameIndex = -1; robert@464: robert@464: // unroll first iteration, so that in the following loop we save the check on the last frame to calculate increments robert@464: if(token) // all frames data are on one line, in groups of 3 entries robert@464: { robert@464: frameIndex++; robert@464: robert@464: endSample = fromTimeToSamples(atof(token)); robert@464: robert@464: token = strtok(0, " "); // frame frequency robert@464: prevFreq = atof(token); robert@464: partials.partialFrequencies[parIndex][frameIndex] = (float)prevFreq; robert@464: partials.partialFreqMean[parIndex] += prevFreq; // for frequency mean robert@464: robert@464: token = strtok(0, " "); // frame amplitude robert@464: prevAmp = atof(token); robert@464: partials.partialAmplitudes[parIndex][frameIndex] = (float)prevAmp; robert@464: robert@464: token = strtok(0, " "); // next frame frequency, to be checked robert@464: } robert@464: robert@464: // here the loop starts robert@464: while(token) // all frames data are on one line, in groups of 3 entries robert@464: { robert@464: frameIndex++; robert@464: missSampCnt = 0; robert@464: robert@464: startSample = fromTimeToSamples(atof(token)); robert@464: robert@464: token = strtok(0, " "); // frame frequency robert@464: freq = atof(token); robert@464: robert@464: token = strtok(0, " "); // frame amplitude robert@464: amp = atof(token); robert@464: // now we know all about the current frame, but we want to know if some frames are missing between this and the last one robert@464: robert@464: // while current frame sample is farther than one hopsize... robert@464: while(startSample > endSample+hopSize) robert@464: { robert@464: missSampCnt++; // ...one sample is missing robert@464: endSample += hopSize; // move to next hop robert@464: } robert@464: robert@464: // if frames are missing do interpolation and update indices robert@464: if(missSampCnt>0) robert@464: startSample = interpolateSamples(parIndex, &frameIndex, missSampCnt, endSample+hopSize, freq, amp, &prevFreq, &prevAmp); robert@464: robert@464: partials.partialFrequencies[parIndex][frameIndex] = (float)freq; robert@464: partials.partialFreqMean[parIndex] += freq; // for frequency mean robert@464: partials.setFreqDelta(parIndex, frameIndex-1, (freq-prevFreq)/hopSize); // freq delta between prev and current frame robert@464: prevFreq = freq; robert@464: robert@464: partials.partialAmplitudes[parIndex][frameIndex] = (float)amp; robert@464: partials.setAmpDelta(parIndex, frameIndex-1, (amp-prevAmp)/hopSize); // amp delta between prev and current frame robert@464: prevAmp = amp; robert@464: robert@464: endSample = startSample; robert@464: token = strtok(0, " "); // next frame frequency, to be checked robert@464: } robert@464: #ifdef DO_CHECKS robert@464: if(frameIndex != (frameNum-1)) robert@464: { robert@464: cout << "Parser Error: frame count mismatch on partial " << parIndex << ", bad file format" << endl; // exit if mismatch robert@464: cout << "frameIndex: " << frameIndex << endl; robert@464: cout << "frameNum: " << frameNum << endl; robert@464: return false; robert@464: } robert@464: #endif robert@464: robert@464: partials.partialFreqMean[parIndex] /= partials.partialNumFrames[parIndex]; // frequency mean robert@464: robert@464: getline(fin, s); // next partial line, to check robert@464: } robert@464: #ifdef DO_CHECKS robert@464: if(parIndex != (parNum-1)) robert@464: { robert@464: cout << "Parser Error: partial count mismatch, bad file format" << endl; // exit if mismatch robert@464: return false; robert@464: } robert@464: #endif robert@464: robert@464: partials.setHopNum(maxSample/hopSize); robert@464: robert@464: gettimeofday(&stop, NULL); robert@464: parserT = ( (stop.tv_sec*1000000+stop.tv_usec) - (start.tv_sec*1000000+start.tv_usec) ); robert@464: robert@464: gettimeofday(&start, NULL); robert@464: staticCalculations(); robert@464: gettimeofday(&stop, NULL); robert@464: staticT = ( (stop.tv_sec*1000000+stop.tv_usec) - (start.tv_sec*1000000+start.tv_usec) ); robert@464: robert@464: fin.close(); robert@464: robert@464: robert@464: printf("\n-----------------------\n"); robert@464: printf("\nFile: %s\n", filename); robert@464: printf("\n-----------------------\n"); robert@464: printf("Profiler\n"); robert@464: printf("-----------------------\n"); robert@464: printf("Hop size parser:\t\t%lu usec\n", hopSizeT); robert@464: printf("File parser:\t\t\t%lu usec\n", parserT); robert@464: printf("Static calculations:\t\t%lu usec\n", staticT); robert@464: printf("\n\nTotal:\t\t%lu usec\n", hopSizeT+parserT+staticT); robert@464: printf("-----------------------\n"); robert@464: robert@464: return true; robert@464: } robert@464: robert@464: robert@464: int Spear_parser::interpolateSamples(int parIndex, int *frameIndex, int missCnt, int nextSample, double nextFreq, double nextAmp, double *prevFreq, double *prevAmp) robert@464: { robert@464: int frame = *frameIndex; // current frame index robert@464: int sample = nextSample - (hopSize*(missCnt)); // move from next real frame sample to first missing frame sample robert@464: double freq = *prevFreq; // freq of the prev real frame robert@464: double freqStep = (nextFreq-*prevFreq)/(missCnt+1); // fixed freq step between hops, for missing frames [linear interpolation] robert@464: double deltaFreq = freqStep/hopSize; // fixed hop freq step in samples robert@464: double amp = *prevAmp; // same for amp... robert@464: double ampStep = (nextAmp-*prevAmp)/(missCnt+1); robert@464: double deltaAmp = ampStep/hopSize; robert@464: robert@464: // for each missing frame robert@464: for(int i=0; i=partials.partialStartSample[j]) && (frameSample partials.maxActiveParNum) robert@464: partials.maxActiveParNum = activeCnt; robert@464: robert@464: // copy indices robert@464: for(unsigned int k=0; k