andrew@0: #include "testApp.h" andrew@0: #include "stdio.h" andrew@0: #include "aubio.h" andrew@0: #include andrew@0: #include andrew@0: #include andrew@0: #include andrew@0: andrew@2: andrew@2: andrew@0: //FIX CHORDS IN THE NEW POINTER VERSION OF THE PROCESS AUDIO FN andrew@0: andrew@0: //BUG IN WHETHER SECOND SONG LOADED OR NOT andrew@0: andrew@0: andrew@0: //CHECK THAT DTW will not call beyond the limits of the two chroma if different sizes andrew@0: andrew@0: //CALC DTW on different sizes andrew@0: andrew@0: //CHECK CORRECT BEST ALIGNMENT METHOD FOR DTW andrew@0: andrew@0: //RE-DO DRAW SO THAT IT DOES NOT CALCULATE EVERY POINT BUT DOES IN SQUARE CHUNKS andrew@0: andrew@0: //UPDATE START FRAME SO ALIGNMENT IS ALWAYS ON SCREEN andrew@0: //-------------------------------------------------------------- andrew@4: andrew@4: /* andrew@4: Main functions here: andrew@4: andrew@4: Load file from a dialogue box with LibSndFile andrew@4: We then iterate through all the samples and call our relevant functions in the timeWarp object - i.e. onset, chroma andrew@4: and do the calculations for similarity and alignment andrew@4: andrew@4: andrew@4: TO DO: andrew@4: Find continual alignment between the playing file and the non-playing file andrew@4: - show the current location in the energy amd chroma draw fn for the non-playing file andrew@4: andrew@4: simplify the loading procedure andrew@4: andrew@4: */ andrew@4: andrew@4: andrew@0: void testApp::setup(){ andrew@0: andrew@16: doCausalAlignment = true; andrew@16: andrew@0: ofBackground(255,255,255); andrew@0: andrew@0: // 2 output channels, andrew@0: // 0 input channels andrew@0: // 22050 samples per second andrew@0: // 256 samples per buffer andrew@0: // 4 num buffers (latency) andrew@0: andrew@0: andrew@0: // ofSetDataPathRoot("../bin/data/"); andrew@0: andrew@0: //DONT NEED ANY OF THIS andrew@0: sampleRate = 44100; andrew@0: phase = 0; andrew@0: phaseAdder = 0.0f; andrew@0: phaseAdderTarget = 0.0f; andrew@0: volume = 0.1f; andrew@0: bNoise = false; andrew@0: lAudio = new float[256]; andrew@0: rAudio = new float[256]; andrew@0: ofSoundStreamSetup(2,0,this, sampleRate,256, 4); andrew@0: //UNTIL HERE andrew@0: andrew@0: andrew@0: ofSetFrameRate(60); andrew@0: andrew@0: andrew@0: fvec_t * my_fvec_t; andrew@0: aubio_onset_t* my_aubio_result; andrew@0: aubio_onsetdetection_t * my_onset_detection; andrew@0: andrew@5: scrollWidth = 1600; andrew@5: conversionFactor = 1.0;//CHROMA_CONVERSION_FACTOR; andrew@5: chromoLength = (float) scrollWidth / conversionFactor;// CHROMA_CONVERSION_FACTOR; andrew@5: // chromaConversionRatio = scrollWidth / (float) conversionFactor; andrew@5: // printf("initial CHROMOLENGTH %f , conv ratio %f\n", chromoLength, chromaConversionRatio); andrew@0: andrew@0: sfinfo.format = 0; andrew@0: andrew@0: moveOn = true; andrew@2: andrew@2: andrew@2: chromaG.initialise(FRAMESIZE, CHROMAGRAM_FRAMESIZE); andrew@2: onset = new OnsetDetectionFunction(512,1024,6,1); andrew@2: andrew@0: //loading audio files andrew@6: //loadSoundFiles(); andrew@6: //this os load soundfiles andrew@0: andrew@6: string fullFileName = "/Users/andrew/Documents/work/programming/of_preRelease_v0061_osx_FAT/apps/my_openFrameworks/chromaReader13audioTimeWarp3/bin/data/sound/Bach_short1.wav"; andrew@11: secondFileName = "/Users/andrew/Documents/work/programming/of_preRelease_v0061_osx_FAT/apps/my_openFrameworks/chromaReader13audioTimeWarp3/bin/data/sound/Bach_short2.wav"; andrew@11: andrew@4: andrew@6: loadNewAudio(fullFileName); andrew@6: /* andrew@6: const char *infile_name = fullFileName.c_str();// "../../../data/sound/Bach_short1.wav"; // andrew@6: andrew@6: loadLibSndFile(infile_name); andrew@6: andrew@6: string loadfilename = fullFileName;//"sound/Bach_short1.wav";// andrew@6: loadedAudio.loadSound(loadfilename); andrew@6: playingAudio = &loadedAudio; andrew@6: //end load soninf ifiels andrew@6: andrew@6: processAudioToDoubleMatrix(&tw.chromaMatrix, &tw.firstEnergyVector); andrew@6: andrew@6: */ andrew@0: andrew@7: // secondFileName = "../../../data/sound/Bach_short2.wav"; andrew@7: andrew@7: andrew@0: andrew@0: loadSecondAudio(secondFileName);//i.e. load same as first file andrew@0: andrew@0: backwardsAlignmentIndex = 0;//remember that this goes backwards! andrew@0: andrew@0: tw.initialiseVariables(); andrew@1: andrew@4: calculateSimilarityAndAlignment(); andrew@1: andrew@0: //set not to play andrew@0: audioPlaying = false; andrew@0: andrew@0: drawSecondMatrix = false; andrew@0: drawSpectralDifferenceFunction = false; andrew@0: drawSimilarity = true; andrew@0: andrew@0: screenHeight = ofGetHeight() ; andrew@0: screenWidth = ofGetWidth(); andrew@0: andrew@14: andrew@0: initialiseVariables(); andrew@0: andrew@0: andrew@0: } andrew@8: andrew@0: andrew@0: andrew@4: void testApp::calculateSimilarityAndAlignment(){ andrew@8: andrew@14: tw.initialiseVariables(); andrew@14: andrew@4: //here is the main TimeWarp similarity matrix calc, the minimum alignment matrix via dtw and then the backwards path estimate andrew@5: double timeBefore = ofGetElapsedTimef(); andrew@5: tw.calculateChromaSimilarityMatrix(&tw.chromaMatrix, &tw.secondMatrix, &tw.chromaSimilarityMatrix); andrew@15: andrew@5: double elapsedTime = ofGetElapsedTimef() - timeBefore; andrew@12: printf("CHROMA SIMILARITY ONLY TAKES %2.2f seconds\n", elapsedTime); andrew@5: andrew@12: dontDoJunkAlignment(); andrew@13: andrew@16: if (doCausalAlignment) andrew@16: calculateCausalAlignment(); andrew@16: else{ andrew@16: //tw.calculateAlignmentMatrix(tw.firstChromaEnergyMatrix, tw.secondChromaEnergyMatrix, &tw.alignmentMeasureMatrix); andrew@16: tw.calculateAlignmentMatrix(tw.chromaMatrix, tw.secondMatrix, &tw.alignmentMeasureMatrix); andrew@16: tw.calculateMinimumAlignmentPath(&tw.alignmentMeasureMatrix, &tw.backwardsAlignmentPath, false); andrew@16: } andrew@15: andrew@13: backwardsAlignmentIndex = tw.backwardsAlignmentPath[0].size()-1; andrew@15: printf("index size is %i\n", backwardsAlignmentIndex); andrew@14: andrew@14: andrew@16: andrew@4: andrew@13: setConversionRatio(); andrew@12: andrew@13: printVariousMatrixInfo(); andrew@13: } andrew@13: andrew@14: void testApp::doPathBugCheck(){ andrew@14: //bug check andrew@15: if (tw.forwardsAlignmentPath.size() > 0 && tw.forwardsAlignmentPath[1][0] != 0){ andrew@15: tw.forwardsAlignmentPath[1][0] = 0;//sometimes is large rndm number andrew@14: printf("BUG IN FORWARDS PATH FIXED!!!\n"); andrew@14: } andrew@14: } andrew@14: andrew@13: andrew@16: void testApp::calculateCausalAlignment(){ andrew@16: calculateForwardsAlignment(); andrew@16: doPathBugCheck(); andrew@16: tw.copyForwardsPathToBackwardsPath(); andrew@16: } andrew@16: andrew@13: andrew@13: void testApp::setConversionRatio(){ andrew@5: conversionFactor = (int) round(tw.firstEnergyVector.size() / tw.chromaMatrix.size()); andrew@8: chromaConversionRatio = (int) round(tw.firstEnergyVector.size() / tw.chromaMatrix.size()); andrew@5: chromoLength = scrollWidth / (float)conversionFactor;// CHROMA_CONVERSION_FACTOR; andrew@5: printf("scrollwidth %i, conversion factor %i, chromo length %i\n", scrollWidth, (int)conversionFactor, (int)chromoLength); andrew@13: } andrew@13: andrew@13: void testApp::printVariousMatrixInfo(){ andrew@5: andrew@13: printf("large SIM SIZE%i, chrom size %ix%i\n", (int)tw.similarityMatrix.size(), (int)tw.chromaMatrix.size(), (int)tw.chromaMatrix[0].size()); andrew@12: if (tw.similarityMatrix.size() > 0 && tw.chromaSimilarityMatrix.size() > 0){ andrew@13: printf("SIM SIZE %i, and %i \n",(int) tw.similarityMatrix.size(), (int) tw.similarityMatrix[0].size()); andrew@13: printf("chomra sim size %i, and %i\n", (int) tw.chromaSimilarityMatrix.size(), (int)tw.chromaSimilarityMatrix[0].size()); andrew@12: } andrew@13: andrew@13: andrew@5: //tw.printBackwardsPath(0, tw.forwardsAlignmentPath[0].size()-1, &tw.forwardsAlignmentPath); andrew@5: andrew@13: andrew@5: printf("backwards path size is [0]:%i, [1]%i, \n", (int)tw.backwardsAlignmentPath[0].size(), (int)tw.backwardsAlignmentPath[1].size()); andrew@5: andrew@5: printf("backwards path size is %i, FORWARDS SIZE IS %i\n", (int)tw.backwardsAlignmentPath[0].size(), (int)tw.forwardsAlignmentPath[0].size()); andrew@14: //printf("BACKWARDS PATH::\n"); andrew@14: //tw.printBackwardsPath(0, (int)tw.backwardsAlignmentPath[0].size(), &tw.backwardsAlignmentPath); andrew@4: } andrew@4: andrew@0: void testApp::initialiseVariables(){ andrew@0: andrew@0: chromaIndex = 0; andrew@0: // chromoGramm.initialise(FRAMESIZE,2048);//framesize 512 and hopsize 2048 andrew@0: audioPosition = 0; andrew@5: if (tw.backwardsAlignmentPath.size() > 0) andrew@5: backwardsAlignmentIndex = tw.backwardsAlignmentPath[0].size()-1;//go back to beginning for drawing andrew@0: andrew@0: numberOfScrollWidthsForFirstFile = 0; andrew@0: numberOfScrollWidthsForSecondFile = 0; andrew@0: andrew@0: (*playingAudio).setPaused(true); andrew@0: audioPlaying = false; andrew@0: audioPaused = true; andrew@0: andrew@14: // tw.initialiseVariables(); andrew@8: andrew@0: } andrew@0: andrew@0: andrew@13: void testApp::calculateForwardsAlignment(){ andrew@13: andrew@13: tw.forwardsAlignmentPath.clear(); andrew@13: //causal part andrew@16: int hopsize = 200; andrew@14: int frameSize = 800; andrew@13: int startFrameY = 0; andrew@13: int startFrameX = 0; andrew@13: tw.anchorPoints.clear(); andrew@13: for (int startFrameX = 0;startFrameX < tw.firstEnergyVector.size(); startFrameX += hopsize){//tw.firstChromaEnergyMatrix.size() andrew@13: //replace this with a while startFrame < end of file andrew@13: tw.addAnchorPoints(startFrameX, startFrameY); andrew@13: andrew@13: //NEED TO ASSUME WE DON'T HAVE andrew@13: double timeBefore = ofGetElapsedTimef(); andrew@13: printf("PART SIM: startFrameX %i, startFrameY: %i\n", startFrameX, startFrameY); andrew@15: //NEW FUNCTION - calls only the energy and uses the stored chromagram andrew@13: andrew@13: tw.calculatePartJointSimilarityMatrix(&tw.firstEnergyVector, &tw.secondEnergyVector, &tw.chromaSimilarityMatrix, &tw.tmpSimilarityMatrix, andrew@13: startFrameX, startFrameY, startFrameX+frameSize, startFrameY + 3*frameSize); andrew@13: andrew@13: andrew@13: andrew@13: andrew@14: // printf("TMP size of tmp sim is %i\n", (int)tw.tmpSimilarityMatrix.size()); andrew@13: double elapsedTime = ofGetElapsedTimef() - timeBefore; andrew@14: // printf("PART SIM MATRIX CAL TAKES %f\n", elapsedTime); andrew@13: andrew@13: //change part sim to have a y limit too andrew@13: andrew@13: andrew@13: //check if we can not calculate alignment matrix for chunks of the sim matrix where it is off diagonal andrew@13: andrew@13: tw.calculatePartAlignmentMatrix(tw.tmpSimilarityMatrix.size()-1, tw.tmpSimilarityMatrix[0].size()-1, &tw.tmpAlignmentMeasureMatrix, &tw.tmpSimilarityMatrix); andrew@13: andrew@13: //get alignment measure minimum andrew@13: //find minimum path between only the section we are interested in andrew@13: //write new function to find minimum backwards path from the index we choose (not the final corner) andrew@13: // int myIndex = tw.findMinimumOfMatrixColumn(tw.tmpAlignmentMeasureMatrix, tw.tmpSimilarityMatrix.size()-1); andrew@13: /* printf("my index is %i\n", myIndex); andrew@13: for (int i = 0;i< tw.alignmentMeasureMatrix[tw.similarityMatrix.size()-1].size() - 1;i++){ andrew@13: printf("Alignment[%i] : %f\n", i, tw.alignmentMeasureMatrix[tw.similarityMatrix.size()-1][i]); andrew@13: } andrew@13: */ andrew@13: printf("\n CALC PART ALIGNMENT MIN PATH\n"); andrew@13: tw.calculateMinimumAlignmentPath(&tw.tmpAlignmentMeasureMatrix, &tw.tmpBackwardsPath, true);//true is for greedy calculation andrew@13: printf("\n PART ALIGNMENT GENERATES THIS BACKWARDS PATH:: \n"); andrew@13: tw.extendForwardAlignmentPath(hopsize, &tw.tmpBackwardsPath, startFrameX, startFrameY); andrew@13: andrew@13: startFrameY = tw.forwardsAlignmentPath[1][(tw.forwardsAlignmentPath[0].size()-1)]; andrew@13: andrew@13: }//end for startFrameX andrew@13: andrew@13: } andrew@13: andrew@0: //-------------------------------------------------------------- andrew@0: void testApp::update(){ andrew@0: textString = "energy index ["; andrew@0: textString += ofToString(xIndex); andrew@0: textString += "] = "; andrew@0: textString += ofToString(energy[xIndex]); andrew@0: andrew@4: // chordString = "Chord : "; andrew@4: // chordString += ofToString(rootChord[currentPlayingFrame/conversionFactor]);//CHROMA_CONVERSION_FACTOR]); andrew@0: andrew@8: audioPosition = (*playingAudio).getPosition(); andrew@0: if (firstAudioFilePlaying){ andrew@8: audioPosition *= tw.firstEnergyVector.size(); andrew@0: updateAlignmentPathIndex(0); andrew@0: } andrew@0: else { andrew@8: audioPosition *= tw.secondEnergyVector.size(); andrew@0: updateAlignmentPathIndex(1); andrew@0: } andrew@0: andrew@0: //if(!audioPaused) andrew@0: //printScoreForRow(audioPosition/CHROMA_CONVERSION_FACTOR, (audioPosition/CHROMA_CONVERSION_FACTOR)+10); andrew@4: andrew@0: andrew@0: currentPlayingFrame = audioPosition; andrew@0: audioPosition = (int) audioPosition % scrollWidth ; andrew@0: audioPosition /= scrollWidth; andrew@0: andrew@0: ofSoundUpdate(); andrew@0: andrew@0: andrew@0: } andrew@0: andrew@0: void testApp::updateAlignmentPathIndex(int identifier){ andrew@1: andrew@9: andrew@5: if (tw.backwardsAlignmentPath.size() > 0){ andrew@9: andrew@9: if (backwardsAlignmentIndex >= tw.backwardsAlignmentPath[identifier].size()) andrew@9: backwardsAlignmentIndex = tw.backwardsAlignmentPath[identifier].size()-1; andrew@9: andrew@1: //this is the alignment where we are currently playing - i.e. switching between files andrew@0: andrew@8: int chromaPosition = audioPosition;///conversionFactor;//CHROMA_CONVERSION_FACTOR; andrew@0: andrew@9: while (tw.backwardsAlignmentPath[identifier][backwardsAlignmentIndex] < chromaPosition && backwardsAlignmentIndex > 0) andrew@0: { andrew@0: backwardsAlignmentIndex--; andrew@0: } andrew@5: } andrew@0: andrew@0: } andrew@0: andrew@0: //-------------------------------------------------------------- andrew@0: void testApp::draw(){ andrew@0: andrew@5: if (drawSimilarity){ andrew@5: //drawSimilarityMatrix(); andrew@5: drawChromaSimilarityMatrix();//new fn with alignment causal including energy vector andrew@5: //but chroma sim used for representation andrew@5: andrew@5: } andrew@0: else andrew@5: drawDoubleMatrix(&tw.tmpSimilarityMatrix); andrew@5: //drawChromoGram(); andrew@0: andrew@0: andrew@0: } andrew@0: andrew@0: andrew@0: void testApp::drawEnergyVectorFromPointer(DoubleVector* energyVec){ andrew@0: andrew@0: float screenHeight = ofGetHeight() ; andrew@0: float screenWidth = ofGetWidth(); andrew@2: float heightFactor = 1; andrew@0: int i, j, startingFrame; andrew@0: startingFrame = currentPlayingFrame / scrollWidth;//i.e. number of scroll widths in andrew@0: startingFrame *= scrollWidth; andrew@0: andrew@0: for (i = 0; i < scrollWidth - 1; i++){ andrew@2: j = min(i + startingFrame, (int)energyVec->size()-1); andrew@0: ofLine(i*screenWidth/scrollWidth, screenHeight - ((*energyVec)[j]*screenHeight/heightFactor), andrew@0: screenWidth*(i+1)/scrollWidth, screenHeight - ((*energyVec)[j+1]*screenHeight/heightFactor)); andrew@0: andrew@0: } andrew@0: } andrew@0: andrew@0: void testApp::drawSpectralDifference(DoubleMatrix* dMatrix){ andrew@0: if ((*dMatrix).size()>0){ andrew@0: andrew@0: float screenHeight = ofGetHeight() ; andrew@0: float screenWidth = ofGetWidth(); andrew@0: float heightFactor = 8; andrew@0: double difference; andrew@0: int i, j, startingFrame; andrew@0: startingFrame = currentPlayingFrame / scrollWidth;//i.e. number of scroll widths in andrew@0: startingFrame *= scrollWidth;//starting frame in terms of energy frames andrew@4: startingFrame /= conversionFactor;// CHROMA_CONVERSION_FACTOR; //in terms of chroma frames andrew@0: andrew@0: andrew@0: for (i = 1; i < chromoLength; i++){//changed to add 1 andrew@2: j = min(i + startingFrame, (int) dMatrix->size()-1 );//in case out of range andrew@0: for (int y = 0;y < 12;y++){ andrew@0: difference = (*dMatrix)[j][11-y] - (*dMatrix)[j-1][11-y]; andrew@0: if (difference < 0) andrew@0: difference = 0;//half wave rectify andrew@0: andrew@0: ofSetColor(0,0,255 * difference);//, 0; andrew@0: ofRect(i*screenWidth/chromoLength,y*screenHeight/12,screenWidth/chromoLength,screenHeight/12); andrew@0: }//end y andrew@0: }//end i andrew@0: andrew@0: }///end if matrix has content andrew@0: else{ andrew@0: printf("Error - please load audio first"); andrew@0: } andrew@0: andrew@0: } andrew@0: andrew@0: andrew@0: void testApp::drawChromoGram(){ andrew@0: andrew@0: DoubleMatrix* dptr; andrew@0: DoubleVector* eptr; andrew@0: string whichFileString; andrew@0: andrew@0: if (drawSecondMatrix){ andrew@0: andrew@0: dptr = &tw.secondMatrix; andrew@0: eptr = &tw.secondEnergyVector; andrew@0: andrew@0: whichFileString = "second file"; andrew@0: andrew@0: }else { andrew@0: andrew@0: dptr = &tw.chromaMatrix; andrew@0: eptr = &tw.firstEnergyVector; andrew@0: whichFileString = "first file"; andrew@0: } andrew@0: andrew@0: andrew@0: andrew@0: if (drawSpectralDifferenceFunction) andrew@0: drawSpectralDifference(dptr); andrew@0: else andrew@0: drawDoubleMatrix(dptr); andrew@0: andrew@0: ofSetColor(0xFF6666); andrew@0: drawEnergyVectorFromPointer(eptr); andrew@0: andrew@0: ofDrawBitmapString(textString,80,480); andrew@0: andrew@0: andrew@0: ofSetColor(0xFFFFFF); andrew@0: ofLine(audioPosition*width, 0, audioPosition*width, height); andrew@0: andrew@0: andrew@0: ofDrawBitmapString(chordString,80,580); andrew@0: andrew@0: ofDrawBitmapString(soundFileName,80,480); andrew@0: andrew@0: ofDrawBitmapString(whichFileString,80,80); andrew@0: andrew@0: } andrew@0: andrew@0: void testApp::drawDoubleMatrix(DoubleMatrix* dMatrix){ andrew@5: andrew@5: andrew@0: if ((*dMatrix).size()>0){ andrew@5: int matrixWidth = (*dMatrix).size(); andrew@5: int matrixHeight = (*dMatrix)[0].size(); andrew@5: andrew@0: float screenHeight = ofGetHeight() ; andrew@0: float screenWidth = ofGetWidth(); andrew@5: /* float heightFactor = 8; andrew@0: int i, j, startingFrame; andrew@0: startingFrame = currentPlayingFrame / scrollWidth;//i.e. number of scroll widths in andrew@0: startingFrame *= scrollWidth;//starting frame in terms of energy frames andrew@4: startingFrame /= conversionFactor;//CHROMA_CONVERSION_FACTOR; //in terms of chroma frames andrew@0: andrew@5: andrew@5: float chromoLength = scrollWidth/conversionFactor;// CHROMA_CONVERSION_FACTOR; andrew@5: for (i = 0; i < chromoLength; i++){ andrew@5: j = min(i + startingFrame, (int) dMatrix->size()-1 ) ; andrew@5: for (int y = 0;y < 12;y++){ andrew@5: ofSetColor(0,0,255 * (*dMatrix)[j][11-y]);//, 0; andrew@5: ofRect(i*screenWidth/chromoLength,y*screenHeight/12,screenWidth/chromoLength,screenHeight/12); andrew@5: }//end y andrew@5: }//end i andrew@5: andrew@5: */ andrew@5: andrew@5: float ratio = max(matrixWidth/screenWidth,matrixHeight/screenHeight); andrew@5: for (int x = 0; x < screenWidth; x+= 5){ andrew@5: //j = min(i + startingFrame, (int) dMatrix->size()-1 ) ; andrew@5: for (int y = 0;y < screenHeight;y+= 5){ andrew@5: int xIndex = (int)(x*ratio); andrew@5: int yIndex = (int)(y*ratio); andrew@5: andrew@5: if (xIndex < matrixWidth && yIndex < matrixHeight) andrew@5: ofSetColor(0,0,255 * (*dMatrix)[xIndex][yIndex]);//, 0; andrew@5: else andrew@5: ofSetColor(0,0,0); andrew@5: andrew@5: ofRect(x,y,5,5);//screenWidth/matrixWidth,screenHeight/matrixHeight); andrew@0: }//end y andrew@0: }//end i andrew@0: andrew@0: }///end if matrix has content andrew@0: else{ andrew@0: printf("Error - please load audio first"); andrew@0: } andrew@0: andrew@0: andrew@0: } andrew@0: andrew@0: andrew@0: void testApp::drawSimilarityMatrix(){ andrew@0: andrew@0: int simHeight = (tw.similarityMatrix[0]).size(); andrew@0: int simWidth = tw.similarityMatrix.size(); andrew@0: andrew@4: int sizeOfMatrix = (int) tw.similarityMatrix.size();//tw.chromaMatrix.size(); andrew@4: int sizeOfSecondMatrix = (int) tw.similarityMatrix[0].size(); andrew@0: andrew@5: //in chromagram frames andrew@5: int startingXframe = tw.chromaSimilarityMatrix.size() / (scrollWidth/conversionFactor); andrew@5: int startingYframe = tw.chromaSimilarityMatrix[0].size() / (scrollWidth/conversionFactor); andrew@5: andrew@8: andrew@5: if (tw.backwardsAlignmentPath.size() > 0){ andrew@5: startingXframe = tw.backwardsAlignmentPath[0][backwardsAlignmentIndex] / (scrollWidth/conversionFactor); andrew@5: startingYframe = tw.backwardsAlignmentPath[1][backwardsAlignmentIndex] / (scrollWidth/conversionFactor); andrew@5: } andrew@8: andrew@4: startingXframe = startingXframe * scrollWidth/conversionFactor; andrew@4: startingYframe = startingYframe * scrollWidth/conversionFactor; andrew@8: andrew@8: // int startingFrame = findStartWidthFrame(); andrew@8: // startingFrame = numberOfScrollWidthsForFirstFile * scrollWidth/conversionFactor; andrew@8: andrew@8: andrew@0: //need to fix for second file too andrew@0: andrew@0: int *indexOfAlignmentPathTested; andrew@5: int lengthOfPath = 0; andrew@5: if (tw.backwardsAlignmentPath.size() > 0) andrew@5: lengthOfPath = tw.backwardsAlignmentPath[0].size()-1; andrew@5: andrew@0: indexOfAlignmentPathTested = &lengthOfPath; andrew@0: andrew@0: int xcoord; andrew@0: for (int x = 0;x < screenWidth;x++) andrew@0: { andrew@0: for (int y =0;y < screenHeight;y++){ andrew@0: andrew@0: xcoord = (x / screenWidth) * chromoLength;//was simWidth andrew@0: //xcoord += startingFrame; andrew@0: xcoord += startingXframe; andrew@0: andrew@0: int ycoord = y * chromoLength/ screenHeight; andrew@0: //ycoord += startingFrame; andrew@0: ycoord += startingYframe; andrew@0: andrew@0: int colour = 0; andrew@0: //int ycoord = y * simHeight/ screenHeight; andrew@0: //y += startingFrame; andrew@0: if (xcoord < sizeOfMatrix && ycoord < sizeOfSecondMatrix) andrew@0: colour = tw.similarityMatrix[xcoord][ycoord]*255; andrew@0: andrew@5: andrew@0: andrew@0: ofSetColor(colour,0,0); andrew@0: andrew@0: ofRect(x,y,1,1); andrew@0: andrew@0: } andrew@0: } andrew@0: andrew@5: andrew@5: andrew@5: ofSetColor(0,255,255); andrew@5: // drawAlignmentPath(startingXframe, startingYframe, &tw.tmpBackwardsPath); andrew@4: drawForwardsAlignmentPath(startingXframe, startingYframe); andrew@0: andrew@5: andrew@5: // ofSetColor(0,0,255); andrew@5: // drawAlignmentPath(startingXframe, startingYframe, &tw.backwardsAlignmentPath); andrew@5: andrew@5: andrew@0: //SET TEXT andrew@5: ofSetColor(255 ,255,255); andrew@0: string textString; andrew@0: textString = "width : "; andrew@0: textString += ofToString(simWidth); andrew@0: andrew@0: textString += " height : "; andrew@0: textString += ofToString(simHeight); andrew@0: andrew@8: // textString += " startframe : "; andrew@8: // textString += ofToString(startingFrame); andrew@0: andrew@0: textString += " Xframe : "; andrew@0: textString += ofToString(startingXframe); andrew@0: andrew@0: textString += " Yframe : "; andrew@0: textString += ofToString(startingYframe); andrew@0: andrew@0: textString += " currentFrame : "; andrew@0: textString += ofToString(currentPlayingFrame); andrew@0: andrew@0: textString += " scrollwidth: "; andrew@0: textString += ofToString(scrollWidth); andrew@0: andrew@0: textString += " xcoord: "; andrew@0: textString += ofToString(xcoord); andrew@0: andrew@0: textString += " Clength: "; andrew@0: textString += ofToString(chromoLength); andrew@0: andrew@0: textString += " no.Scrolls: "; andrew@0: textString += ofToString(numberOfScrollWidthsForFirstFile); andrew@0: //END SET TEXT andrew@0: andrew@5: ofSetColor(0x0000FF);// && tw.backwardsAlignmentPath.size() > 0 andrew@5: if (firstAudioFilePlaying){// && tw.alignmentMeasureMatrix.size() > 0 andrew@5: ofLine(audioPosition*screenWidth, 0, audioPosition*screenWidth, height); andrew@0: checkIfAudioPositionExceedsWidthForFirstFile(); andrew@5: // drawAlignmentmeasureValues(currentPlayingFrame); andrew@0: } andrew@0: else{ andrew@0: ofLine(0, audioPosition*screenHeight, screenWidth, audioPosition*screenHeight); andrew@0: } andrew@0: andrew@0: ofDrawBitmapString(textString,80,580); andrew@0: andrew@0: ofDrawBitmapString(userInfoString,80,80); andrew@0: andrew@0: } andrew@0: andrew@0: andrew@5: andrew@5: andrew@5: void testApp::drawChromaSimilarityMatrix(){ andrew@5: //scrollwidth is width in terms of the similarity matrix size (or energy size if we are being accurate) andrew@5: //need to get rid of dependency on sim matirx and only use chrom sim matrix andrew@5: andrew@5: // int simHeight = (tw.similarityMatrix[0]).size(); andrew@5: // int simWidth = tw.similarityMatrix.size(); andrew@5: andrew@5: // int sizeOfMatrix = (int) tw.similarityMatrix.size();//tw.chromaMatrix.size(); andrew@5: // int sizeOfSecondMatrix = (int) tw.similarityMatrix[0].size(); andrew@5: andrew@5: float chromogramScrollWidth = (scrollWidth/conversionFactor); andrew@5: //frames needed in energy still andrew@5: //in chromagram frames andrew@5: int startingXframe = (tw.firstEnergyVector.size() / scrollWidth); andrew@8: int startingYframe = (tw.secondEnergyVector.size() / scrollWidth);//secondMatrix andrew@5: andrew@5: // printf("DRAW SIM SIZE start frames %i x %i \n", startingXframe, startingYframe); andrew@8: if (tw.backwardsAlignmentPath.size() > 0 ){ andrew@8: startingXframe = (tw.backwardsAlignmentPath[0][backwardsAlignmentIndex]/ scrollWidth); andrew@9: startingYframe = max(0 , (int)(tw.backwardsAlignmentPath[1][backwardsAlignmentIndex]/ scrollWidth));//*conversionFactor andrew@5: //FIX THE 1 - ASDDED AS DEBUG andrew@5: // printf("alignment index %i, VERSUS DRAW SIM SIZE %i x %i \n", backwardsAlignmentIndex, startingXframe, startingYframe); andrew@5: } andrew@5: andrew@5: //PROBLEM IS THAT THE y value startYframe is not correctly incremented andrew@5: andrew@8: //tmp andrew@8: // startingXframe = 0; andrew@8: // startingYframe = 0; andrew@5: andrew@8: // int startingFrame = findStartWidthFrame(); andrew@8: // startingFrame = numberOfScrollWidthsForFirstFile * scrollWidth/conversionFactor; andrew@5: andrew@5: startingXframe *= scrollWidth;// /conversionFactor; andrew@5: startingYframe *= scrollWidth;// /conversionFactor; andrew@5: //need to fix for second file too andrew@5: andrew@5: int *indexOfAlignmentPathTested; andrew@5: int lengthOfPath = 0; andrew@5: if (tw.backwardsAlignmentPath.size() > 0) andrew@5: lengthOfPath = tw.backwardsAlignmentPath[0].size()-1; andrew@5: andrew@5: indexOfAlignmentPathTested = &lengthOfPath; andrew@5: andrew@5: int xcoord, ycoord; andrew@5: for (int x = 0;x < screenWidth;x++) andrew@5: { andrew@5: for (int y =0;y < screenHeight;y++){ andrew@5: andrew@5: xcoord = (x / screenWidth) * scrollWidth;//i.e. chromoLength * conversionFactor; andrew@5: xcoord += startingXframe; andrew@5: andrew@5: int xChromaCoord = xcoord / conversionFactor; andrew@5: andrew@5: ycoord = y * chromoLength * conversionFactor/ screenHeight; andrew@5: ycoord += startingYframe; andrew@5: andrew@5: int yChromaCoord = ycoord / conversionFactor; andrew@5: andrew@5: int colour = 0;//0x006644; andrew@5: andrew@5: andrew@5: /* if (xcoord < tw.similarityMatrix.size() && ycoord < tw.similarityMatrix[0].size()){// andrew@5: //nb not optimised andrew@5: colour = tw.similarityMatrix[xcoord][ycoord]*255; andrew@5: } andrew@5: */ andrew@5: if (xChromaCoord < tw.chromaSimilarityMatrix.size() && yChromaCoord < tw.chromaSimilarityMatrix[0].size()){ andrew@5: colour = tw.chromaSimilarityMatrix[xChromaCoord][yChromaCoord]*255; andrew@5: } andrew@5: andrew@5: andrew@5: ofSetColor(colour,0,0); andrew@5: andrew@5: ofRect(x,y,1,1); andrew@5: andrew@5: } andrew@5: } andrew@5: andrew@5: andrew@5: andrew@5: // ofSetColor(0,0,255); andrew@5: // drawAlignmentPath(startingXframe, startingYframe, &tw.backwardsAlignmentPath); andrew@5: //ALIGNMENT PATH doesnt work yet - scae rpobllem andrew@5: andrew@5: andrew@5: ofSetColor(0,255,255); andrew@5: // drawAlignmentPath(startingXframe, startingYframe, &tw.tmpBackwardsPath); andrew@8: andrew@5: drawForwardsAlignmentPathOnChromaSimilarity(startingXframe, startingYframe); andrew@5: andrew@10: drawAnchorPointsOnChromaSimilarity(startingXframe, startingYframe); andrew@5: andrew@5: andrew@5: //SET TEXT andrew@5: ofSetColor(255 ,255,255); andrew@5: string textString; andrew@5: /* textString = "width : "; andrew@5: textString += ofToString(simWidth); andrew@5: andrew@5: textString += " height : "; andrew@5: textString += ofToString(simHeight); andrew@5: */ andrew@8: // textString += " startframe : "; andrew@8: // textString += ofToString(startingFrame); andrew@5: andrew@5: textString += " Xframe : "; andrew@5: textString += ofToString(startingXframe); andrew@5: andrew@5: textString += " back[0] "; andrew@5: textString += ofToString(tw.backwardsAlignmentPath[0][backwardsAlignmentIndex]); andrew@5: andrew@5: textString += " Yframe : "; andrew@5: textString += ofToString(startingYframe); andrew@5: andrew@5: andrew@5: textString += " back[1] "; andrew@5: textString += ofToString(tw.backwardsAlignmentPath[1][backwardsAlignmentIndex]); andrew@5: andrew@5: andrew@5: textString += " currentFrame : "; andrew@5: textString += ofToString(currentPlayingFrame); andrew@5: andrew@5: andrew@5: textString += " backalign index : "; andrew@5: textString += ofToString(backwardsAlignmentIndex); andrew@5: andrew@5: andrew@5: textString += " scrollwidth: "; andrew@5: textString += ofToString(scrollWidth); andrew@5: andrew@5: textString += " xcoord: "; andrew@5: textString += ofToString(xcoord); andrew@5: andrew@5: textString += " Clength: "; andrew@5: textString += ofToString(chromoLength); andrew@5: andrew@5: textString += " no.Scrolls: "; andrew@5: textString += ofToString(numberOfScrollWidthsForFirstFile); andrew@5: //END SET TEXT andrew@5: andrew@5: ofSetColor(0x0000FF);// && tw.backwardsAlignmentPath.size() > 0 andrew@5: if (firstAudioFilePlaying){// && tw.alignmentMeasureMatrix.size() > 0 andrew@5: ofLine(audioPosition*screenWidth, 0, audioPosition*screenWidth, height); andrew@5: checkIfAudioPositionExceedsWidthForFirstFile(); andrew@5: // drawAlignmentmeasureValues(currentPlayingFrame); andrew@5: } andrew@5: else{ andrew@5: ofLine(0, audioPosition*screenHeight, screenWidth, audioPosition*screenHeight); andrew@5: } andrew@5: andrew@5: ofDrawBitmapString(textString,80,580); andrew@5: andrew@5: ofDrawBitmapString(userInfoString,80,80); andrew@5: andrew@5: } andrew@5: andrew@5: andrew@5: andrew@5: andrew@5: andrew@5: andrew@5: andrew@5: andrew@5: andrew@5: void testApp::drawAlignmentmeasureValues(const int& startingYframe){ andrew@5: //draw values: andrew@5: /* andrew@5: int xcoord = currentPlayingFrame / conversionFactor; andrew@5: ofSetColor(255, 255, 255); andrew@5: for (int y = 0;y < chromoLength; y+=max(1, (int)(20 * chromoLength / screenHeight))){ andrew@5: andrew@5: float value = tw.alignmentMeasureMatrix[xcoord][y+startingYframe]; andrew@5: int ycoord = y * screenHeight/chromoLength; andrew@5: ofDrawBitmapString(ofToString(value, 2) , audioPosition*screenWidth , ycoord); andrew@5: } andrew@5: */ andrew@5: } andrew@5: andrew@0: void testApp::checkIfAudioPositionExceedsWidthForFirstFile() andrew@0: { andrew@0: if (currentPlayingFrame > scrollWidth*(numberOfScrollWidthsForFirstFile+1)) andrew@0: numberOfScrollWidthsForFirstFile++; andrew@0: } andrew@0: andrew@0: int testApp::findStartWidthFrame(){ andrew@0: int startingFrame; andrew@8: /* andrew@0: startingFrame = currentPlayingFrame / scrollWidth;//i.e. number of scroll widths in andrew@0: startingFrame *= scrollWidth;//starting frame in terms of energy frames andrew@4: startingFrame /= conversionFactor;// CHROMA_CONVERSION_FACTOR; andrew@8: */ andrew@0: return startingFrame; andrew@0: } andrew@0: andrew@5: void testApp::drawAlignmentPath(int startingChromaXFrame, int startingChromaYFrame, IntMatrix* backPath){ andrew@5: // if (tw.alignmentMeasureMatrix.size() > 0){ andrew@5: if ((*backPath).size() > 0){ andrew@0: //draw alignment path andrew@0: int endingChromaXFrame = startingChromaXFrame + chromoLength; andrew@0: int endingChromaYFrame = startingChromaYFrame + chromoLength; andrew@0: andrew@0: float chromoWidth = screenWidth / chromoLength; andrew@0: float chromoHeight = screenHeight / chromoLength; andrew@0: andrew@5: int index = (*backPath)[0].size()-1; andrew@0: //OPTIMISE XXX andrew@0: andrew@0: andrew@5: while ((*backPath)[0][index] < startingChromaXFrame){ andrew@0: index --; andrew@0: } andrew@0: andrew@0: int printIndex = index; andrew@5: int backAlign = (*backPath)[0][index]; andrew@0: int printxcoord; andrew@0: int xcoord; andrew@0: andrew@5: while ((*backPath)[0][index] < endingChromaXFrame) { andrew@5: // xcoord = min((int)(tw.similarityMatrix.size())-1,(*backPath)[0][index]); andrew@5: xcoord = (*backPath)[0][index]; andrew@5: //int ycoord = min((int)(*backPath)[1][index], (int)(tw.alignmentMeasureMatrix[0].size())-1); andrew@5: int ycoord = (int)(*backPath)[1][index]; andrew@5: printxcoord = xcoord; andrew@5: // int colour = tw.similarityMatrix[xcoord][ycoord]*255; andrew@0: andrew@5: //float value = 0; andrew@5: //if (xcoord < tw.alignmentMeasureMatrix.size() && ycoord < tw.alignmentMeasureMatrix[xcoord].size()) andrew@5: //float value = tw.alignmentMeasureMatrix[xcoord][ycoord] ; andrew@5: andrew@0: andrew@0: xcoord -= startingChromaXFrame; andrew@0: ycoord -= startingChromaYFrame; andrew@5: andrew@0: ofRect(xcoord*chromoWidth, ycoord*chromoHeight, chromoWidth, chromoHeight); andrew@5: andrew@0: index--; andrew@0: } andrew@0: andrew@0: // drawHoverAlignmentValues(); andrew@5: // printf("ALIGN score :[%i] : %f \n", backwardsAlignmentPath[1][backwardsAlignmentIndex], ureMatrix[ backwardsAlignmentPath[0][backwardsAlignmentIndex] ][ (int) backwardsAlignmentPath[1][backwardsAlignmentIndex] ]); andrew@0: andrew@5: /* andrew@0: //SET TEXT andrew@0: string textString; andrew@0: textString = "ALIGNMENT PATH "; andrew@0: andrew@0: textString += "backward A index "; andrew@0: textString += ofToString(backwardsAlignmentIndex); andrew@0: andrew@0: textString += " starting X frame "; andrew@0: textString += ofToString(startingChromaXFrame); andrew@0: andrew@0: textString += " initial xcoord "; andrew@0: textString += ofToString(printxcoord); andrew@0: andrew@0: textString += " first index "; andrew@0: textString += ofToString(printIndex); andrew@0: andrew@0: textString += " backalign[index] "; andrew@0: textString += ofToString(backAlign); andrew@0: andrew@0: textString += " final xcoord "; andrew@0: textString += ofToString(xcoord); andrew@0: andrew@5: */ andrew@0: andrew@0: andrew@0: ofSetColor(255,255,255); andrew@0: ofDrawBitmapString(textString,80,640); andrew@5: }//end if alignment path > 0 andrew@5: //else{ andrew@5: // printf("al measure mat not in range \n"); andrew@5: // } andrew@0: } andrew@0: andrew@0: andrew@0: andrew@0: andrew@0: andrew@0: andrew@4: andrew@4: void testApp::drawForwardsAlignmentPath(int startingChromaXFrame, int startingChromaYFrame){ andrew@5: andrew@5: //using starting frame in energy vector andrew@5: andrew@4: if (tw.forwardsAlignmentPath.size() > 0){ andrew@4: int endingChromaXFrame = startingChromaXFrame + chromoLength; andrew@4: int endingChromaYFrame = startingChromaYFrame + chromoLength; andrew@4: andrew@4: float chromoWidth = screenWidth / chromoLength; andrew@4: float chromoHeight = screenHeight / chromoLength; andrew@4: andrew@4: int index = 0; andrew@4: //OPTIMISE XXX andrew@4: andrew@4: while (tw.forwardsAlignmentPath[0][index] < startingChromaXFrame){ andrew@4: //get to NOW andrew@4: index ++; andrew@4: } andrew@4: andrew@4: int xcoord; andrew@12: //replacing tw.similarityMatrix.size() by first e vec below andrew@4: andrew@4: while (index < tw.forwardsAlignmentPath[0].size() && tw.forwardsAlignmentPath[0][index] < endingChromaXFrame) { andrew@12: xcoord = min((int)(tw.firstEnergyVector.size())-1,tw.forwardsAlignmentPath[0][index]); andrew@5: int ycoord = (int)tw.forwardsAlignmentPath[1][index]; andrew@5: andrew@5: if (tw.alignmentMeasureMatrix.size() > 0) andrew@5: ycoord = min((int)tw.forwardsAlignmentPath[1][index], (int)(tw.alignmentMeasureMatrix[0].size())-1); andrew@4: andrew@4: // printxcoord = xcoord; andrew@4: int colour = 255;//tw.similarityMatrix[xcoord][ycoord]*255; andrew@4: //float value = tw.alignmentMeasureMatrix[xcoord][ycoord] ; andrew@4: andrew@4: xcoord -= startingChromaXFrame; andrew@4: ycoord -= startingChromaYFrame; andrew@4: ofSetColor(0,colour,0); andrew@4: ofRect(xcoord*chromoWidth, ycoord*chromoHeight, chromoWidth, chromoHeight); andrew@4: index++; andrew@4: }//end while andrew@4: andrew@4: }//end if forwards path exista andrew@4: andrew@4: } andrew@4: andrew@4: andrew@4: andrew@4: andrew@4: andrew@5: void testApp::drawForwardsAlignmentPathOnChromaSimilarity(const int& startingXFrame, const int& startingYFrame){ andrew@5: andrew@5: //using starting frame in energy vector andrew@5: andrew@5: if (tw.forwardsAlignmentPath.size() > 0){ andrew@5: int endingChromaXFrame = startingXFrame + scrollWidth; andrew@5: int endingChromaYFrame = startingYFrame + scrollWidth; andrew@5: andrew@5: float energyVectorWidth = screenWidth / scrollWidth; andrew@5: float energyVectorHeight = screenHeight / scrollWidth; andrew@5: andrew@10: int index = max(0, (int)tw.backwardsAlignmentPath[0].size() - backwardsAlignmentIndex); andrew@5: //OPTIMISE XXX andrew@10: while (index > 0 && tw.forwardsAlignmentPath[0][index] > startingXFrame){ andrew@10: //get to NOW andrew@10: index --; andrew@10: } andrew@5: andrew@5: while (tw.forwardsAlignmentPath[0][index] < startingXFrame){ andrew@5: //get to NOW andrew@5: index ++; andrew@5: } andrew@5: andrew@5: int xcoord; andrew@12: //replacing tw.similarityMatrix.size() by fiorst e vec andrew@5: while (index < tw.forwardsAlignmentPath[0].size() && tw.forwardsAlignmentPath[0][index] < endingChromaXFrame) { andrew@12: xcoord = min((int)(tw.firstEnergyVector.size())-1,tw.forwardsAlignmentPath[0][index]); andrew@5: int ycoord = (int)tw.forwardsAlignmentPath[1][index]; andrew@5: andrew@5: if (tw.alignmentMeasureMatrix.size() > 0) andrew@5: ycoord = min((int)tw.forwardsAlignmentPath[1][index], (int)(tw.alignmentMeasureMatrix[0].size())-1); andrew@5: andrew@5: // printxcoord = xcoord; andrew@5: int colour = 255;//tw.similarityMatrix[xcoord][ycoord]*255; andrew@5: //float value = tw.alignmentMeasureMatrix[xcoord][ycoord] ; andrew@5: andrew@5: xcoord -= startingXFrame; andrew@5: ycoord -= startingYFrame; andrew@5: ofSetColor(0,colour,0); andrew@5: ofRect(xcoord*energyVectorWidth, ycoord*energyVectorHeight, energyVectorWidth, energyVectorHeight); andrew@5: index++; andrew@5: }//end while andrew@5: andrew@5: }//end if forwards path exista andrew@5: andrew@5: } andrew@5: andrew@5: andrew@5: andrew@10: void testApp::drawAnchorPointsOnChromaSimilarity(const int& startingXFrame, const int& startingYFrame){ andrew@10: andrew@10: //using starting frame in energy vector andrew@10: andrew@10: if (tw.anchorPoints.size() > 0){ andrew@10: int endingChromaXFrame = startingXFrame + scrollWidth; andrew@10: int endingChromaYFrame = startingYFrame + scrollWidth; andrew@10: andrew@10: float energyVectorWidth = screenWidth / scrollWidth; andrew@10: float energyVectorHeight = screenHeight / scrollWidth; andrew@10: andrew@10: int index = 0; andrew@10: andrew@10: while (index < tw.anchorPoints.size() && tw.anchorPoints[index][0] < startingXFrame){ andrew@10: index ++; andrew@10: } andrew@10: andrew@10: int xcoord; andrew@10: andrew@10: while (index < tw.anchorPoints.size() && tw.anchorPoints[index][0] < endingChromaXFrame) { andrew@12: xcoord = min((int)(tw.firstEnergyVector.size())-1,tw.anchorPoints[index][0]); andrew@10: int ycoord = (int)tw.anchorPoints[index][1]; andrew@10: andrew@10: if (tw.alignmentMeasureMatrix.size() > 0) andrew@10: ycoord = min((int)tw.anchorPoints[index][1], (int)(tw.alignmentMeasureMatrix[0].size())-1); andrew@10: andrew@10: int colour = 255;//tw.similarityMatrix[xcoord][ycoord]*255; andrew@10: //float value = tw.alignmentMeasureMatrix[xcoord][ycoord] ; andrew@10: andrew@10: xcoord -= startingXFrame; andrew@10: ycoord -= startingYFrame; andrew@10: ofSetColor(colour,colour,colour); andrew@10: ofRect((xcoord-1)*energyVectorWidth, (ycoord-1)*energyVectorHeight, 3*energyVectorWidth, 3*energyVectorHeight); andrew@10: index++; andrew@10: }//end while andrew@10: andrew@10: }//end if andrew@10: andrew@10: } andrew@10: andrew@10: andrew@10: andrew@5: andrew@5: andrew@4: andrew@0: void testApp::loadSoundFiles(){ andrew@0: andrew@0: //assume libsndfile looks in the folder where the app is run andrew@0: //therefore ../../../ gets to the bin folder andrew@0: //we then need data/sounds/to get to the sound folder andrew@0: //this is different to the usual OF default folder andrew@0: //was const char andrew@4: const char *infilename = "../../../data/sound/Bach_short1.wav"; andrew@0: loadLibSndFile(infilename); andrew@0: andrew@4: string loadfilename = "sound/Bach_short1.wav";//PicturesMixer6.aif"; andrew@0: loadedAudio.loadSound(loadfilename); andrew@0: playingAudio = &loadedAudio; andrew@0: andrew@0: } andrew@0: andrew@0: void testApp::loadLibSndFile(const char *infilename){ andrew@0: andrew@0: if (!sf_close(infile)){ andrew@0: printf("closed sndfile okay \n"); andrew@0: } andrew@0: andrew@0: // Open Input File with lib snd file andrew@0: if (! (infile = sf_open (infilename, SFM_READ, &sfinfo))) andrew@0: { // Open failed andrew@0: printf ("SF OPEN routine Not able to open input file %s.\n", infilename) ; andrew@0: // Print the error message from libsndfile. andrew@0: puts (sf_strerror (NULL)) ; andrew@0: andrew@0: } else{ andrew@0: printf("SF OPEN opened file %s okay.\n", infilename); andrew@0: sndfileInfoString = "Opened okay "; andrew@0: andrew@0: }; andrew@0: andrew@0: } andrew@0: andrew@1: void testApp::processAudioToDoubleMatrix(DoubleMatrix* myDoubleMatrix, DoubleVector* energyVector){ andrew@0: //wendy andrew@0: myDoubleMatrix->clear(); andrew@0: energyVector->clear(); andrew@1: andrew@0: andrew@1: andrew@1: chromaG.initialise(FRAMESIZE, CHROMAGRAM_FRAMESIZE);//framesize 512 and hopsize 2048 - already done andrew@1: chromaG.maximumChromaValue = 1; andrew@2: double maximumEnergyValue = 1; andrew@0: andrew@0: int readcount = 1; // counts number of samples read from sound file andrew@0: printf("processing audio from doublematrix \n"); andrew@0: printf("readcount %i", readcount); andrew@14: while(readcount != 0 && moveOn == true && energyVector->size() < FILE_LIMIT) andrew@0: { andrew@0: andrew@0: // read FRAMESIZE samples from 'infile' and save in 'data' andrew@0: readcount = sf_read_float(infile, frame, FRAMESIZE); andrew@0: andrew@2: double doubleFrame[FRAMESIZE]; andrew@2: for (int k = 0;k< FRAMESIZE;k++){ andrew@2: doubleFrame[k] = frame[k]; andrew@2: } andrew@2: andrew@2: andrew@2: andrew@1: //8192 samples per chroma frame //processing frame - downsampled to 11025Hz andrew@1: chromaG.processframe(frame); andrew@0: andrew@1: if (chromaG.chromaready) andrew@0: { andrew@0: DoubleVector d; andrew@0: andrew@0: for (int i = 0;i<12;i++){ andrew@1: d.push_back(chromaG.rawChroma[i]);// / chromaG->maximumChromaValue); andrew@0: andrew@0: } andrew@0: //this would do chord detection andrew@0: andrew@0: myDoubleMatrix->push_back(d); andrew@1: //so now is storing at d[i][current_index] andrew@0: andrew@0: }//end if chromagRamm ready andrew@0: andrew@0: andrew@2: // double energyValue = getEnergyOfFrame(); andrew@2: double energyValue = onset->getDFsample(doubleFrame); andrew@0: energyVector->push_back(energyValue); andrew@2: if (energyValue > maximumEnergyValue) andrew@2: maximumEnergyValue = energyValue; andrew@4: andrew@0: andrew@0: }//end while readcount andrew@0: andrew@1: printf("Max chroma value is %f \n", chromaG.maximumChromaValue); andrew@2: printf("length of chromagram is %d frames\n", (int)myDoubleMatrix->size()); andrew@4: printf("height of dmatrix is %d\n", (int)(*myDoubleMatrix)[0].size()); andrew@4: //normalise chroma matrix andrew@0: for (int i = 0; i < myDoubleMatrix->size();i++){ andrew@0: for (int j = 0; j < ((*myDoubleMatrix)[0]).size();j++){ andrew@1: //non-causal normalisation andrew@1: (*myDoubleMatrix)[i][j] /= chromaG.maximumChromaValue; andrew@0: } andrew@0: } andrew@0: andrew@4: andrew@4: printf("size of energy vector is %d \n", (int)energyVector->size()); andrew@4: //non causal normalisation andrew@2: for (int i = 0; i < energyVector->size();i++){ andrew@2: (*energyVector)[i] /= maximumEnergyValue; andrew@2: } andrew@2: andrew@4: // totalNumberOfFrames = (int)energyVector->size(); andrew@4: chromaConversionRatio = myDoubleMatrix->size() / (int)energyVector->size(); andrew@0: andrew@0: // int size = myDoubleMatrix->size() * CHROMA_CONVERSION_FACTOR; andrew@0: andrew@0: } andrew@0: andrew@0: andrew@0: //-------------------------------------------------------------- andrew@0: void testApp::keyPressed (int key){ andrew@0: if (key == '-'){ andrew@0: volume -= 0.05; andrew@0: volume = MAX(volume, 0); andrew@0: } else if (key == '+'){ andrew@0: volume += 0.05; andrew@0: volume = MIN(volume, 1); andrew@0: } andrew@0: andrew@0: if (key == OF_KEY_DOWN){ andrew@0: if (scrollWidth > 600) andrew@0: scrollWidth += 400; andrew@0: else andrew@0: scrollWidth *= 2; andrew@0: andrew@4: chromoLength = scrollWidth/conversionFactor;// CHROMA_CONVERSION_FACTOR; andrew@0: } andrew@0: andrew@0: if (key == OF_KEY_UP){ andrew@0: if (scrollWidth > 600) andrew@0: scrollWidth -= 400; andrew@0: else andrew@0: scrollWidth /= 2; andrew@0: andrew@4: chromoLength = scrollWidth/conversionFactor;// CHROMA_CONVERSION_FACTOR; andrew@0: } andrew@0: andrew@0: if (key == OF_KEY_LEFT){ andrew@0: andrew@2: (*playingAudio).setSpeed(-4); andrew@0: backwardsAlignmentIndex = tw.backwardsAlignmentPath[0].size()-1; andrew@0: } andrew@0: andrew@0: if (key == OF_KEY_RIGHT){ andrew@0: andrew@2: (*playingAudio).setSpeed(4); andrew@0: } andrew@0: andrew@0: if (key == OF_KEY_RETURN){ andrew@0: loadedAudio.stop(); andrew@0: audioPlaying = false; andrew@0: audioPaused = true; andrew@0: initialiseVariables(); andrew@0: } andrew@0: andrew@0: if (key == ' '){ andrew@0: if (!audioPlaying) { andrew@0: (*playingAudio).play(); andrew@0: (*playingAudio).setPaused(false); andrew@0: secondAudio.play(); andrew@0: secondAudio.setPaused(true); andrew@0: andrew@0: firstAudioFilePlaying = true; andrew@0: andrew@0: audioPlaying = true; andrew@0: audioPaused = false; andrew@0: } andrew@0: else{ andrew@0: audioPaused = !audioPaused; andrew@0: (*playingAudio).setPaused(audioPaused); andrew@0: } andrew@0: andrew@0: } andrew@0: andrew@0: if (key == 'p'){ andrew@0: swapBetweenPlayingFilesUsingAlignmentMatch(); andrew@0: andrew@0: } andrew@0: andrew@0: if (key == 'o'){ andrew@8: tw.clearVectors(); andrew@8: andrew@0: openNewAudioFileWithdialogBox(); andrew@0: } andrew@0: andrew@0: if (key == 'l'){ andrew@0: //open audio file andrew@0: string *filePtr, secondFileName; andrew@0: filePtr = &secondFileName; andrew@0: //so filePtr points to secondFileName andrew@0: andrew@0: if (getFilenameFromDialogBox(filePtr)){ andrew@0: printf("Loaded name okay :\n'%s' \n", secondFileName.c_str()); andrew@0: } andrew@0: andrew@0: loadSecondAudio(secondFileName); andrew@0: andrew@8: initialiseVariables(); andrew@8: backwardsAlignmentIndex = 0; andrew@8: andrew@4: calculateSimilarityAndAlignment(); andrew@4: andrew@8: andrew@8: andrew@0: } andrew@0: andrew@14: if (key == 'f'){ andrew@8: tw.printBackwardsPath(0, (int) tw.forwardsAlignmentPath[0].size(), &tw.forwardsAlignmentPath); andrew@8: } andrew@8: andrew@14: if (key == 'b'){ andrew@14: tw.printBackwardsPath(0, (int) tw.backwardsAlignmentPath[0].size(), &tw.backwardsAlignmentPath); andrew@14: } andrew@14: andrew@8: andrew@0: if (key == 's'){ andrew@0: drawSimilarity = !drawSimilarity; andrew@0: } andrew@0: andrew@0: andrew@0: if (key == 'm'){ andrew@0: drawSecondMatrix = !drawSecondMatrix; andrew@0: } andrew@0: andrew@0: if (key == 'd'){ andrew@0: drawSpectralDifferenceFunction = !drawSpectralDifferenceFunction; andrew@0: } andrew@0: andrew@0: } andrew@0: andrew@0: //-------------------------------------------------------------- andrew@0: void testApp::keyReleased (int key){ andrew@0: if (key == OF_KEY_LEFT || OF_KEY_RIGHT){ andrew@0: (*playingAudio).setSpeed(1); andrew@5: if (tw.backwardsAlignmentPath.size() > 0) andrew@0: backwardsAlignmentIndex = tw.backwardsAlignmentPath[0].size()-1; andrew@0: } andrew@0: andrew@0: } andrew@0: andrew@0: void testApp::openNewAudioFileWithdialogBox(){ andrew@0: andrew@0: //open audio file andrew@0: string *filePtr; andrew@0: filePtr = &soundFileName; andrew@0: andrew@0: if (getFilenameFromDialogBox(filePtr)){ andrew@0: printf("Mainfile: Loaded name okay :\n'%s' \n", soundFileName.c_str()); andrew@0: } andrew@0: andrew@0: //openFileDialogBox(); - replaced this lone by call to openFile Dialoguebox andrew@0: loadNewAudio(soundFileName); andrew@0: andrew@0: } andrew@0: andrew@0: //-------------------------------------------------------------- andrew@0: void testApp::mouseMoved(int x, int y ){ andrew@0: width = ofGetWidth(); andrew@0: pan = (float)x / (float)width; andrew@0: float height = (float)ofGetHeight(); andrew@0: float heightPct = ((height-y) / height); andrew@0: targetFrequency = 2000.0f * heightPct; andrew@0: phaseAdderTarget = (targetFrequency / (float) sampleRate) * TWO_PI; andrew@0: xIndex = (int)(pan*ENERGY_LENGTH); andrew@0: } andrew@0: andrew@0: //-------------------------------------------------------------- andrew@0: void testApp::mouseDragged(int x, int y, int button){ andrew@0: width = ofGetWidth(); andrew@0: pan = (float)x / (float)width; andrew@0: } andrew@0: andrew@0: //-------------------------------------------------------------- andrew@0: void testApp::mousePressed(int x, int y, int button){ andrew@0: bNoise = true; andrew@4: //moveOn = true; andrew@0: } andrew@0: andrew@0: andrew@0: //-------------------------------------------------------------- andrew@0: void testApp::mouseReleased(int x, int y, int button){ andrew@0: bNoise = false; andrew@0: } andrew@0: andrew@0: //-------------------------------------------------------------- andrew@0: void testApp::windowResized(int w, int h){ andrew@0: width = w; andrew@0: height = h; andrew@0: screenHeight = ofGetHeight() ; andrew@0: screenWidth = ofGetWidth(); andrew@0: andrew@0: } andrew@0: //-------------------------------------------------------------- andrew@0: void testApp::audioRequested (float * output, int bufferSize, int nChannels){ andrew@0: //pan = 0.5f; andrew@0: float leftScale = 1 - pan; andrew@0: float rightScale = pan; andrew@0: andrew@0: // sin (n) seems to have trouble when n is very large, so we andrew@0: // keep phase in the range of 0-TWO_PI like this: andrew@0: while (phase > TWO_PI){ andrew@0: phase -= TWO_PI; andrew@0: } andrew@0: andrew@0: andrew@0: } andrew@0: andrew@0: andrew@0: //-------------------------------------------------------------- andrew@0: andrew@0: andrew@0: bool testApp::getFilenameFromDialogBox(string* fileNameToSave){ andrew@0: //this uses a pointer structure within the loader and returns true if the dialogue box was used successfully andrew@0: // first, create a string that will hold the URL andrew@0: string URL; andrew@0: andrew@0: // openFile(string& URL) returns 1 if a file was picked andrew@0: // returns 0 when something went wrong or the user pressed 'cancel' andrew@0: int response = ofxFileDialogOSX::openFile(URL); andrew@0: if(response){ andrew@0: // now you can use the URL andrew@0: *fileNameToSave = URL; andrew@0: //printf("\n filename is %s \n", soundFileName.c_str()); andrew@0: return true; andrew@0: } andrew@0: else { andrew@0: // soundFileName = "OPEN canceled. "; andrew@0: printf("\n open file cancelled \n"); andrew@0: return false; andrew@0: } andrew@0: andrew@0: } andrew@0: andrew@0: void testApp::openFileDialogBox(){ andrew@0: andrew@0: // first, create a string that will hold the URL andrew@0: string URL; andrew@0: andrew@0: // openFile(string& URL) returns 1 if a file was picked andrew@0: // returns 0 when something went wrong or the user pressed 'cancel' andrew@0: int response = ofxFileDialogOSX::openFile(URL); andrew@0: if(response){ andrew@0: // now you can use the URL andrew@0: soundFileName = URL;//"URL to open: \n "+URL; andrew@0: }else { andrew@0: soundFileName = "OPEN cancelled. "; andrew@0: } andrew@0: andrew@0: andrew@0: andrew@0: } andrew@0: andrew@0: andrew@0: void testApp::loadNewAudio(string soundFileName){ andrew@7: //need to add in clear fns andrew@7: // tw.chromaMatrix.clear(); andrew@7: // tw.firstEnergyVector.clear(); andrew@6: andrew@0: loadedAudio.loadSound(soundFileName); andrew@6: playingAudio = &loadedAudio; andrew@0: //snd file method andrew@0: const char *infilename = soundFileName.c_str() ; andrew@0: loadLibSndFile(infilename); andrew@0: andrew@7: // loadFirstAudioFile(); andrew@7: processAudioToDoubleMatrix(&tw.chromaMatrix, &tw.firstEnergyVector); andrew@0: audioPlaying = false; andrew@0: } andrew@0: andrew@0: andrew@0: andrew@0: void testApp::loadFirstAudioFile(){ andrew@14: andrew@1: processAudioToDoubleMatrix(&tw.chromaMatrix, &tw.firstEnergyVector); andrew@14: andrew@0: andrew@0: } andrew@0: andrew@6: void testApp::loadSecondAudio(string sndFileName){ andrew@0: andrew@6: secondAudio.loadSound(sndFileName); andrew@0: andrew@6: const char *infilenme = sndFileName.c_str() ; andrew@6: loadLibSndFile(infilenme); andrew@0: andrew@14: andrew@14: processAudioToDoubleMatrix(&tw.secondMatrix, &tw.secondEnergyVector); andrew@0: andrew@0: } andrew@0: andrew@0: andrew@0: void testApp::swapBetweenPlayingFilesUsingAlignmentMatch(){ andrew@8: float tmpConvFac = conversionFactor; andrew@8: conversionFactor = 1.0; andrew@0: ofSoundUpdate(); andrew@0: //swapping between files andrew@8: //printf("current playing (energy scale) frame was %i \n", currentPlayingFrame); andrew@0: float oldPosition = (*playingAudio).getPosition(); andrew@11: printf("\n playing position is %f \n", oldPosition);//and conv factor %f \n", (*playingAudio).getPosition(), conversionFactor); andrew@11: float currentPlayingFileLength; andrew@11: float newFileLength; andrew@0: //(*playingAudio).stop(); andrew@0: (*playingAudio).setPaused(true); andrew@0: int newIndicator; andrew@0: if (firstAudioFilePlaying){ andrew@0: playingAudio = &secondAudio; andrew@0: newIndicator = 1; andrew@11: currentPlayingFileLength = tw.firstEnergyVector.size(); andrew@11: newFileLength = tw.secondEnergyVector.size(); andrew@0: } andrew@0: else{ andrew@0: playingAudio = &loadedAudio; andrew@0: newIndicator = 0; andrew@11: currentPlayingFileLength = tw.secondEnergyVector.size(); andrew@11: newFileLength = tw.firstEnergyVector.size(); andrew@0: } andrew@4: andrew@8: //printf("new indicator %i \n", newIndicator); andrew@11: printf("playing pos according to energy frames is %f; ", andrew@8: (currentPlayingFrame/((float)tw.backwardsAlignmentPath[1-newIndicator][0])) );//* conversionFactor)) );//CHROMA_CONVERSION_FACTOR andrew@11: printf("Current frame %f, predicts new frame to be roughly %f \n", andrew@11: (oldPosition*tw.backwardsAlignmentPath[newIndicator][0]), (oldPosition*tw.backwardsAlignmentPath[1-newIndicator][0])); andrew@11: printf("file lenbgths are now %i and other %i\n", tw.backwardsAlignmentPath[newIndicator][0], tw.backwardsAlignmentPath[1-newIndicator][0]); andrew@11: printf("compared to energy vec lengths %i and %i\n", tw.firstEnergyVector.size(), tw.secondEnergyVector.size()); andrew@0: andrew@11: currentChromaFrame = round(oldPosition * currentPlayingFileLength); andrew@11: // currentChromaFrame = currentPlayingFrame;// / conversionFactor; andrew@9: printf("current chroma frame %i \n", currentChromaFrame);//and using energy frames would have been %i \n", currentChromaFrame, currentPlayingFrame / conversionFactor);//CHROMA_CONVERSION_FACTOR); andrew@4: andrew@4: int matchingFrame = findMatchFromAlignment(firstAudioFilePlaying); andrew@11: float relativePosition = matchingFrame / newFileLength;// tw.backwardsAlignmentPath[newIndicator][0]; andrew@0: //i.e. the position as float [0,1] 0:beginning, 1 is end andrew@0: andrew@0: (*playingAudio).setPaused(false); andrew@0: (*playingAudio).setPosition(relativePosition); andrew@0: andrew@11: printf("matching frame is %i \n", matchingFrame, tw.backwardsAlignmentPath[newIndicator][0]); andrew@9: printf("new playing position is %f and back align index %i \n", (*playingAudio).getPosition(), backwardsAlignmentIndex); andrew@0: andrew@0: firstAudioFilePlaying = !firstAudioFilePlaying; andrew@0: andrew@8: conversionFactor = tmpConvFac; andrew@0: } andrew@0: andrew@0: int testApp::findMatchFromAlignment(bool whichFileToTest){ andrew@0: //could use technique from middle of file and go either way to reduce latency for long search? andrew@0: //- (not that this is a problem yet) andrew@0: int indicator; andrew@0: if (whichFileToTest) andrew@0: indicator = 0; andrew@0: else andrew@0: indicator = 1; andrew@0: andrew@0: int oppositeIndicator = 1 - indicator; andrew@0: andrew@9: int frame = backwardsAlignmentIndex;// tw.backwardsAlignmentPath[indicator].size()-1; andrew@0: andrew@9: while (tw.backwardsAlignmentPath[indicator][frame] > currentChromaFrame && backwardsAlignmentIndex < tw.backwardsAlignmentPath[indicator].size()-1){ andrew@9: frame++; andrew@9: } andrew@0: andrew@9: while (tw.backwardsAlignmentPath[indicator][frame] < currentChromaFrame && frame > 0){ andrew@0: frame--; andrew@0: } andrew@0: //printf("frame found is %i \n", frame); andrew@0: int frameToSwitchTo = tw.backwardsAlignmentPath[oppositeIndicator][frame]; andrew@0: andrew@0: float calculatedPosition = (currentChromaFrame / (float) tw.backwardsAlignmentPath[indicator][0]); andrew@0: andrew@0: printf("(length was %i)\n", tw.backwardsAlignmentPath[indicator][0]); andrew@0: andrew@0: printf("compares to position calculated from chroma length %f \n", calculatedPosition); andrew@0: printf("current frame %i maps to new frame %i \n", currentChromaFrame, frameToSwitchTo); andrew@0: printf("relative position of new frame is %f \n", (frameToSwitchTo / (float) tw.backwardsAlignmentPath[oppositeIndicator][0]) ); andrew@0: return frameToSwitchTo; andrew@0: andrew@0: } andrew@0: andrew@0: void testApp::printSimilarityMatrix(int sizeToPrint){ andrew@0: andrew@0: printf("\n _ _ _ _\n"); andrew@0: printf("Similarity Matrix \n"); andrew@0: int i,j; andrew@0: DoubleVector d; andrew@0: int rowSize = sizeToPrint; andrew@0: andrew@0: for (int j = 0;j < rowSize;j++){ andrew@0: printf("row %i : ", j); andrew@0: andrew@0: for (i = 0;i < rowSize;i++){ andrew@0: printf("%f , ", tw.similarityMatrix[i][j] ); andrew@0: } andrew@0: printf("\n"); andrew@0: } andrew@0: printf("...............\n"); andrew@0: andrew@0: } andrew@0: andrew@0: andrew@8: andrew@8: void testApp::printAlignmentMatrix(const DoubleMatrix& alignmentMatrix){ andrew@0: andrew@8: int size = alignmentMatrix.size(); andrew@0: printf("\n _ _ _ _\n"); andrew@0: printf("align size is %i \n", size); andrew@0: andrew@0: int i,j; andrew@0: DoubleVector d; andrew@8: int rowSize = alignmentMatrix.size(); andrew@8: d = alignmentMatrix[0];//choose initial size andrew@0: andrew@0: for (int j = 0;j < d.size();j++){ andrew@0: printf("row %i : ", j); andrew@0: andrew@0: for (i = 0;i < rowSize;i++){ andrew@8: d = alignmentMatrix[i]; andrew@0: andrew@0: // printf("row %i , col %i, val : %f \n", i, j, alignmentMeasureMatrix[i][j] ); andrew@8: printf("%f , ", alignmentMatrix[i][j] ); andrew@0: } andrew@0: printf("\n"); andrew@0: } andrew@0: printf("...............\n"); andrew@0: andrew@0: } andrew@0: andrew@0: andrew@0: void testApp::printScoreForRow(int row, int max){ andrew@0: printf("alignment scores row %i \n", row); andrew@0: float minimum = tw.alignmentMeasureMatrix[row][0]; andrew@0: int minimumIndex = 0; andrew@0: for (int i =0;i < max;i++){ andrew@0: printf("[%i] %f ", i, tw.alignmentMeasureMatrix[row][i]); andrew@0: if (tw.alignmentMeasureMatrix[row][i] < minimum) andrew@0: { andrew@0: minimum = tw.alignmentMeasureMatrix[row][i] ; andrew@0: minimumIndex = i; andrew@0: } andrew@0: printf(" \n"); andrew@0: } andrew@0: printf("Minimum [%i] : %f \n", minimumIndex, minimum); andrew@0: printf("ALIGN score :[%i] : %f \n", tw.backwardsAlignmentPath[1][backwardsAlignmentIndex], tw.alignmentMeasureMatrix[tw.backwardsAlignmentPath[0][backwardsAlignmentIndex] ][ (int) tw.backwardsAlignmentPath[1][backwardsAlignmentIndex] ]); andrew@0: andrew@0: } andrew@0: andrew@0: andrew@0: andrew@0: andrew@0: andrew@0: andrew@12: void testApp::dontDoJunkAlignment(){ andrew@12: andrew@12: if (1 == 2){ andrew@12: //dont do joint as matrix is too large - energyvec x energyvec andrew@12: //needs doing in parts andrew@12: double timeBefore = ofGetElapsedTimef(); andrew@12: tw.calculateJointSimilarityMatrix(&tw.firstEnergyVector, &tw.secondEnergyVector, &tw.chromaSimilarityMatrix, &tw.similarityMatrix); andrew@12: double elapsedTime = ofGetElapsedTimef() - timeBefore; andrew@12: printf("JOINT SIMILARITY TAKES %6.2f seconds\n", elapsedTime); andrew@12: //this has been replaced by the joint function andrew@12: } andrew@12: andrew@12: andrew@12: andrew@12: // the big similarity measure andrew@12: //timeBefore = ofGetElapsedTimef(); andrew@12: //tw.calculateSimilarityMatrixWithPointers(&tw.firstChromaEnergyMatrix, &tw.secondChromaEnergyMatrix, &tw.similarityMatrix); andrew@12: //elapsedTime = ofGetElapsedTimef() - timeBefore; andrew@12: //printf("CREATING BIG SIMILARITY MATRIX TAKES %f\n", elapsedTime); andrew@12: //bug is here above - needed to make sim matrix visible andrew@12: andrew@12: //new fn andrew@12: // tw.calculateSimilarityMatrixFromChromagramAndEnergy(&tw.firstEnergyVector, &tw.chromaMatrix, &tw.secondEnergyVector, tw.secondMatrix, &tw.similarityMatrix); andrew@12: // tw.calculateAlignmentMatrix(tw.firstChromaEnergyMatrix, tw.secondChromaEnergyMatrix, &tw.alignmentMeasureMatrix); andrew@12: // tw.calculateMinimumAlignmentPath(&tw.alignmentMeasureMatrix, &tw.backwardsAlignmentPath, false); andrew@12: andrew@12: } andrew@0: andrew@0: andrew@0: andrew@0: andrew@0: andrew@0: andrew@0: /* andrew@5: void testApp::pushSimCoxde(){ andrew@5: /* for (int i = 0;i < tw.tmpSimilarityMatrix.size();i++){ andrew@5: DoubleVector v; andrew@5: v = tw.tmpSimilarityMatrix[i]; andrew@5: tw.similarityMatrix.push_back(v); andrew@5: } andrew@5: */ andrew@5: andrew@5: /* andrew@0: void testApp::calculateSimilarityMatrix(){ andrew@0: similarityMatrix.clear(); andrew@0: printf("calculating similarity matrix..."); andrew@0: userInfoString = "calculating similarity matrix..."; andrew@0: andrew@0: double distance, firstSum, secondSum; andrew@0: andrew@0: for (int x = 0;x < tw.chromaMatrix.size();x++){ andrew@0: DoubleVector d; andrew@0: for (int y = 0;y < tw.secondMatrix.size();y++){ andrew@0: andrew@0: //d.push_back( drand48() ); andrew@0: andrew@0: distance = 0; andrew@0: firstSum = 0; andrew@0: secondSum = 0; andrew@0: for (int z = 0;z < chromaMatrix[x].size();z++){//z is the twelve chromagram values andrew@0: andrew@0: distance += chromaMatrix[x][z] * secondMatrix[y][z]; andrew@0: andrew@0: firstSum += chromaMatrix[x][z] * chromaMatrix[x][z]; andrew@0: secondSum += secondMatrix[y][z] * secondMatrix[y][z]; andrew@0: } andrew@0: andrew@0: if (firstSum > 0 && secondSum > 0) andrew@0: distance /= sqrt(firstSum * secondSum); andrew@0: andrew@0: andrew@0: d.push_back( distance); andrew@0: andrew@0: } //end for y andrew@0: andrew@0: similarityMatrix.push_back(d); andrew@0: andrew@0: }//end for x andrew@0: userInfoString += "; size ="; andrew@0: userInfoString += ofToString(similarityMatrix.size() , 0); andrew@0: printf("..sim size: %i, height: %i \n", similarityMatrix.size(), (chromaMatrix[0]).size()); andrew@0: andrew@0: }//end self sim andrew@0: andrew@0: */ andrew@0: /* andrew@0: void testApp::calculateAlignmentMatrix(){ andrew@0: andrew@0: //initialise alignment andrew@0: alignmentMeasureMatrix.clear(); andrew@0: DoubleVector d; andrew@0: d.push_back(getDistance(0,0)); andrew@0: alignmentMeasureMatrix.push_back(d); andrew@0: andrew@0: bool chromaCalculated = false; andrew@0: bool secondCalculated = false; andrew@0: andrew@0: while (!chromaCalculated || !secondCalculated) { andrew@0: andrew@0: if (!chromaCalculated) andrew@0: chromaCalculated = extendAlignmentAlong(); andrew@0: andrew@0: if (!secondCalculated) andrew@0: secondCalculated = extendAlignmentUp(); andrew@0: andrew@0: } andrew@0: andrew@0: } andrew@0: andrew@0: bool testApp::extendAlignmentUp(){ andrew@0: DoubleVector d; andrew@0: d = alignmentMeasureMatrix[0]; andrew@0: int heightSize = d.size(); andrew@0: if (heightSize < secondMatrix.size()){ andrew@0: //then we haven't finished yet andrew@0: for (int i = 0;i < alignmentMeasureMatrix.size();i++){ andrew@0: double value = getDistance(i, heightSize); andrew@0: value += getMinimum(i, heightSize, value); andrew@0: alignmentMeasureMatrix[i].push_back(value); andrew@0: } andrew@0: } andrew@0: if (alignmentMeasureMatrix[0].size() == secondMatrix.size()) andrew@0: return true; andrew@0: else andrew@0: return false; andrew@0: andrew@0: } andrew@0: andrew@0: andrew@0: bool testApp::extendAlignmentAlong(){ andrew@0: DoubleVector d; andrew@0: int widthSize = alignmentMeasureMatrix.size(); andrew@0: if (widthSize < chromaMatrix.size()){ andrew@0: //then we can extend along andrew@0: double value = getDistance(widthSize, 0); andrew@0: value += getMinimum(widthSize, 0, value); andrew@0: andrew@0: d.push_back(value); andrew@0: alignmentMeasureMatrix.push_back(d); andrew@0: andrew@0: for (int j = 1;j < alignmentMeasureMatrix[widthSize - 1].size();j++){ andrew@0: value = getDistance(widthSize, j); andrew@0: value += getMinimum(widthSize, j, value); andrew@0: alignmentMeasureMatrix[widthSize].push_back(value); andrew@0: } andrew@0: andrew@0: //alignmentMeasureMatrix.push_back(d); andrew@0: } andrew@0: andrew@0: if (alignmentMeasureMatrix.size() == chromaMatrix.size()) andrew@0: return true; andrew@0: else andrew@0: return false; andrew@0: andrew@0: } andrew@0: andrew@0: andrew@0: void testApp::calculateMinimumAlignmentPath(){ andrew@0: //this requires one pass of the DTW algorithm and then works backwards from (N,M) andrew@0: //to find the optimal path to (0,0), where N and M are the lengths of the two chromoVectors respectively andrew@0: //minimumAlignmentPath.clear(); andrew@0: backwardsAlignmentPath.clear(); andrew@0: andrew@0: printf("Finding minimum Path \n"); andrew@0: IntVector v; andrew@0: v.push_back(chromaMatrix.size()-1); andrew@0: backwardsAlignmentPath.push_back(v); andrew@0: v.clear(); andrew@0: v.push_back(secondMatrix.size()-1); andrew@0: backwardsAlignmentPath.push_back(v); andrew@0: //so now backwards path[0][0] = size(chroma) and path[1][0] = size(secondMatrix) andrew@0: printf("backwards path %i : %i \n", backwardsAlignmentPath[0][0], backwardsAlignmentPath[1][0]); andrew@0: andrew@0: andrew@0: int indexOfBackwardsPath = 0; andrew@0: while (!findPreviousMinimumInBackwardsPath()) { andrew@0: indexOfBackwardsPath++; andrew@0: printf("backwards path %i : %i \n", backwardsAlignmentPath[0][indexOfBackwardsPath], backwardsAlignmentPath[1][indexOfBackwardsPath]); andrew@0: andrew@0: } andrew@0: //printf("final index of backwards path is %i and i is %i \n", backwardsAlignmentPath[0].size()-1, indexOfBackwardsPath); andrew@0: andrew@0: backwardsAlignmentIndex = backwardsAlignmentPath[0].size()-1;//remember that this goes backwards! andrew@0: andrew@0: } andrew@0: andrew@0: andrew@0: bool testApp::findPreviousMinimumInBackwardsPath(){ andrew@0: int chromaPosition, secondPosition; andrew@0: int i,j; andrew@0: i = backwardsAlignmentPath[0][backwardsAlignmentPath[0].size()-1]; andrew@0: j = backwardsAlignmentPath[1][backwardsAlignmentPath[1].size()-1]; andrew@0: andrew@0: double newMinimum; andrew@0: double *ptr; andrew@0: ptr = &newMinimum; andrew@0: newMinimum = alignmentMeasureMatrix[i][j]; andrew@0: DoubleVector d; andrew@0: andrew@0: andrew@0: bool finishedAligning = true; andrew@0: andrew@0: if (i > 0){ andrew@0: if (testForNewAlignmentMinimum(ptr, i-1, j)){ andrew@0: chromaPosition = i-1; andrew@0: secondPosition = j; andrew@0: finishedAligning = false; andrew@0: } andrew@0: andrew@0: if (j>0 && testForNewAlignmentMinimum(ptr, i-1, j-1)){ andrew@0: chromaPosition = i-1; andrew@0: secondPosition = j-1; andrew@0: finishedAligning = false; andrew@0: } andrew@0: } andrew@0: andrew@0: if (j > 0 && testForNewAlignmentMinimum(ptr, i, j-1)){ andrew@0: chromaPosition = i; andrew@0: secondPosition = j-1; andrew@0: //newMinimum = alignmentMeasureMatrix[chromaPosition][secondPosition]; andrew@0: finishedAligning = false; andrew@0: } andrew@0: andrew@0: if (!finishedAligning){ andrew@0: backwardsAlignmentPath[0].push_back(chromaPosition); andrew@0: backwardsAlignmentPath[1].push_back(secondPosition); andrew@0: } andrew@0: andrew@0: return finishedAligning; andrew@0: andrew@0: } andrew@0: andrew@0: andrew@0: andrew@0: bool testApp::testForNewAlignmentMinimum(double *previousMinimum, int i, int j){ andrew@0: bool newMinimumFound = false; andrew@0: if (alignmentMeasureMatrix[i][j] < *previousMinimum){ andrew@0: *previousMinimum = alignmentMeasureMatrix[i][j]; andrew@0: newMinimumFound = true; andrew@0: } andrew@0: andrew@0: return newMinimumFound; andrew@0: } andrew@0: */ andrew@4: andrew@4: andrew@4: /* andrew@4: int testApp::findMinimumOfVector(DoubleVector *d){ andrew@4: int minimumIndex = 0; andrew@4: double minimumValue = (*d)[0]; andrew@4: for (int i = 0;i < d->size();i++){ andrew@4: if ((*d)[i] < minimumValue){ andrew@4: minimumIndex = i; andrew@4: minimumValue = (*d)[i]; andrew@4: } andrew@4: } andrew@4: andrew@4: return minimumIndex; andrew@4: } andrew@4: */ andrew@4: /* andrew@4: double testApp::getDistance(int i, int j){ andrew@4: return (1 - similarityMatrix[i][j]); andrew@4: } andrew@4: andrew@4: double testApp::getMinimum(int i, int j, float newValue){ andrew@4: double minimumValue = 0; andrew@4: andrew@4: if (i > 0){ andrew@4: minimumValue = tw.alignmentMeasureMatrix[i-1][j]; andrew@4: if (j > 0){ andrew@4: minimumValue = min(minimumValue, alignmentMeasureMatrix[i-1][j-1] + newValue ) ;//penalises diagonal by 2 andrew@4: minimumValue = min(minimumValue, alignmentMeasureMatrix[i][j-1]); andrew@4: } andrew@4: } andrew@4: else{//i.e. i == 0 andrew@4: if (j > 0) andrew@4: minimumValue = tw.alignmentMeasureMatrix[i][j-1]; andrew@4: } andrew@4: andrew@4: return minimumValue; andrew@4: } andrew@4: andrew@4: */ andrew@4: