File: LoopPointExtractor.cxx

package info (click to toggle)
clam 1.4.0-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 17,836 kB
  • ctags: 20,981
  • sloc: cpp: 92,504; python: 9,721; ansic: 1,602; xml: 444; sh: 239; makefile: 153; perl: 54; asm: 15
file content (248 lines) | stat: -rw-r--r-- 8,954 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248

#include "LoopPointExtractor.hxx"

namespace CLAM
{
float dist(feature_t *F1, feature_t *F2)
{
	if (*F1 == 0 || *F2 == 0) return 10.0;
	
	return fabs( (*F1-*F2) / *F2) * 100.0;
}

LoopPointExtractor::LoopPointExtractor()
 :	minimumStartAndEndFrameSeparation( DEFAULT_MINIMUM_FRAME_SEPARATION ),
	frameHopSize( DEFAULT_FRAME_HOPSIZE )
{
}

void LoopPointExtractor::extractOptimalLoopPoints(CLAM::List<CLAM::Frame>& framesArray,
													std::map<int,int>& loopPoints, 
													int startFrame, int endFrame)
{
	std::vector<CLAM::DistanceMeasure> emdVector;

	calculateEarthMoverDistances(framesArray, emdVector, startFrame, endFrame);

	std::vector<CLAM::DistanceMeasure> bestLoopsVector;

	selectBestLoopPoints(emdVector, bestLoopsVector);
	
	convertToMap(bestLoopsVector, loopPoints);
}

void LoopPointExtractor::convertToMap(std::vector<CLAM::DistanceMeasure>& bestLoopsVector, std::map<int,int>& loopPoints)
{
	for (int counter = 0; counter < bestLoopsVector.size(); counter++)
	{
		DistanceMeasure pDistanceMeasure = bestLoopsVector.at(counter);
		
		int frameA = pDistanceMeasure.getPositionA();
		int frameB = pDistanceMeasure.getPositionB();

		loopPoints[frameA] = frameB;
	}
}

void LoopPointExtractor::scaleWeights(TData* weights1Ptr, TData* scaledWeights1Ptr, int arraySize)
{
	double totals = 0.0;
	for (int x = 0; x < arraySize; x++)
		totals += weights1Ptr[x];
	double scaleFactor = 1.0 / totals;
	for (int x = 0; x < arraySize; x++)
		scaledWeights1Ptr[x] = scaleFactor * weights1Ptr[x];
}

void LoopPointExtractor::generateEqualWeights(TData* scaledWeights1Ptr, int arraySize)
{
	double weight = 1.0 / arraySize;
	for (int x = 0; x < arraySize; x++)
		scaledWeights1Ptr[x] = weight;
}

signature_t LoopPointExtractor::generateSignatureWithMagnitudes(SpectralPeakArray& spectralPeakArray)
{
	//TData* featuresPtr = spectralPeakArray.GetFreqBuffer().GetPtr();
	//TData* weightsPtr = spectralPeakArray.GetMagBuffer().GetPtr();
	TData* featuresPtr = spectralPeakArray.GetMagBuffer().GetPtr();
	TData scaledWeightsPtr[ spectralPeakArray.GetnPeaks() ];
	//scaleWeights(weightsPtr, scaledWeightsPtr, spectralPeakArray.GetnPeaks());
	int size = (spectralPeakArray.GetnPeaks() > 100) ? 100 : spectralPeakArray.GetnPeaks();
	generateEqualWeights(scaledWeightsPtr, size);
	signature_t theSignature = { size, featuresPtr, scaledWeightsPtr };
	
	return theSignature;
}

signature_t LoopPointExtractor::generateSignatureWithFrequenciesScaledByMagnitudes(SpectralPeakArray& spectralPeakArray)
{
	TData* featuresPtr = spectralPeakArray.GetFreqBuffer().GetPtr();
	TData* weightsPtr = spectralPeakArray.GetMagBuffer().GetPtr();
	TData scaledWeightsPtr[ spectralPeakArray.GetnPeaks() ];
	int size = (spectralPeakArray.GetnPeaks() > 100) ? 100 : spectralPeakArray.GetnPeaks();
	scaleWeights(weightsPtr, scaledWeightsPtr, size);
	signature_t theSignature = { size, featuresPtr, scaledWeightsPtr };
	
	return theSignature;
}

void LoopPointExtractor::calculateEarthMoverDistances(CLAM::List<CLAM::Frame>& framesArray, 
														std::vector<CLAM::DistanceMeasure>& emdVector,
														int startFrame, int endFrame)
{		
	std::cout << "loopMaker: calculating earth movers distances." << std::endl;
	
	int numberOfFrames = (endFrame == -1) ? framesArray.Size() : endFrame;
	
	for (int outer_counter = startFrame; outer_counter < numberOfFrames; outer_counter += frameHopSize)
	{
		for (int inner_counter = outer_counter + minimumStartAndEndFrameSeparation; inner_counter < numberOfFrames; inner_counter += frameHopSize)
		{
			SpectralPeakArray& spectralPeakArray1 = framesArray[outer_counter].GetSpectralPeakArray();
			signature_t s1 = generateSignatureWithFrequenciesScaledByMagnitudes(spectralPeakArray1);
	
			SpectralPeakArray& spectralPeakArray2 = framesArray[inner_counter].GetSpectralPeakArray();
			signature_t s2 = generateSignatureWithFrequenciesScaledByMagnitudes(spectralPeakArray2);
	
			DistanceMeasure aDistanceMeasure;
			aDistanceMeasure.setDistance( emd(&s1, &s2, dist, NULL, NULL) );
			aDistanceMeasure.setPositionA( outer_counter );
			aDistanceMeasure.setPositionB( inner_counter );
			emdVector.push_back( aDistanceMeasure );

			if (emdVector.size() % 1000 == 0)
				std::cout << "." << std::flush;
		}
		
		//std::cout << "Compared frame " << outer_counter << " to frame " << inner_counter << ". Distance: " << emdVector[outer_counter] << std::endl;
		//printf("Compared frame %i to frame %i. Found a distance of %21.20f\n", outer_counter, inner_counter, emdVector[outer_counter]);
	}

	std::cout << std::endl;
	std::cout << "Calculated " << emdVector.size() << " earth mover distances." << std::endl;

	int numberOfMinima = 10;
	TData minimaArray[ numberOfMinima ];
	int startingPoint = 120;
	//findMinima(emdVector, minimaArray, numberOfFrames, numberOfMinima, startingPoint);
	//nth_element(emdVector.begin(), emdVector.begin()+10, emdVector.end());
	sort(emdVector.begin(), emdVector.end());

	std::cout << "Sorted the array of earth mover distances." << std::endl;

	CLAM_DEACTIVATE_FAST_ROUNDING;
}

bool LoopPointExtractor::areStartAndEndFramesCloseTogether(int posA, int posB)
{
	// this ensures that the loop is at least a second long because with a 
	// hopzise of 256 samples, ceil(44100 / 256) = 173 frames
	int distanceBetweenStartAndEndFrames = abs(posA - posB);
	
	return (distanceBetweenStartAndEndFrames < minimumStartAndEndFrameSeparation);
}

bool LoopPointExtractor::areStartAndEndFramesCloseTogether(DistanceMeasure& pDistanceMeasure)
{
	return areStartAndEndFramesCloseTogether(pDistanceMeasure.getPositionA(), pDistanceMeasure.getPositionB());
}

bool LoopPointExtractor::isCloseToFrameAlreadyReported(DistanceMeasure& currentDistanceMeasure, std::vector<CLAM::DistanceMeasure>& reportedDistanceVector)
{
	int minimumFrameSeparation = 20;
	int oneSecondFrameSeparation = 173;
	for (int counter = 0; counter < reportedDistanceVector.size(); counter++)
	{
		DistanceMeasure& previousDistanceMeasure = reportedDistanceVector.at(counter);

		int changeInFrameAPosition = abs(currentDistanceMeasure.getPositionA() 
											- previousDistanceMeasure.getPositionA());
		int changeInFrameBPosition = abs(currentDistanceMeasure.getPositionB() 
											- previousDistanceMeasure.getPositionB());
		
		// if neither the start nor the end of the loop has changed by a lot,
		// this loop is too close to one already reported
		if (changeInFrameAPosition < minimumFrameSeparation 
				&& changeInFrameBPosition < minimumFrameSeparation)
			return true;

		// if the start of the loop hasn't changed much and the end of the 
		// loop hasn't changed by a lot, this loop is too close to one already
		// reported
		if (changeInFrameAPosition < minimumFrameSeparation
				&& changeInFrameBPosition < oneSecondFrameSeparation)
			return true;
			
		// if the end of the loop hasn't changed much and the start of the 
		// loop hasn't changed by a lot, this loop is too close to one already
		// reported
		if (changeInFrameBPosition < minimumFrameSeparation
				&& changeInFrameAPosition < oneSecondFrameSeparation)
			return true;
	}
	
	return false;
}

void LoopPointExtractor::selectBestLoopPoints(std::vector<CLAM::DistanceMeasure>& emdVector,
												std::vector<CLAM::DistanceMeasure>& bestLoopsVector)
{
	int numberOfStartAndEndFramesTooClose = 0;
	int numberOfLoopsTooCloseToReportedLoop = 0;
	
	for (int x = 0; x < emdVector.size(); x++)
	{
		DistanceMeasure& pDistanceMeasure = emdVector.at(x);
		
		int previousFrameAPosition;
		int previousFrameBPosition;
		bool isFirstIteration = true;

		if ( areStartAndEndFramesCloseTogether(pDistanceMeasure) )
		{
			numberOfStartAndEndFramesTooClose++;
			continue;
		}
		
		if ( isCloseToFrameAlreadyReported(pDistanceMeasure, bestLoopsVector) )
		{
			numberOfLoopsTooCloseToReportedLoop++;
			continue;
		}
				
		/*
		int frameA = startFrame + pDistanceMeasure.getPositionA();
		int frameB = startFrame + pDistanceMeasure.getPositionB();

		std::cout << "loopMaker: distance measurement: " << pDistanceMeasure.GetDistance();
		std::cout << ", position a: " << frameA << " (" << framesToMilliseconds(frameA) << " msecs)";
		std::cout << ", position b: " << frameB << " (" << framesToMilliseconds(frameB) << " msecs)";
		std::cout << ", counter: " << bestLoopsVector.size() << std::endl;
		*/
		
		bestLoopsVector.push_back(pDistanceMeasure);
		
		/*
		if (pDistanceMeasure.GetDistance() < 1)
			std::cout << "Distance measurement: " << pDistanceMeasure.GetDistance() << ", position: " << pDistanceMeasure.getPosition() << std::endl;
		else
			break;
		*/
		
		if (bestLoopsVector.size() > 100)
			break;
	}
	
	/*
	std::cout << "loopMaker: " << numberOfStartAndEndFramesTooClose;
	std::cout << " loops had start and frames that were too close together." << std::endl;
	std::cout << "loopMaker: " << numberOfLoopsTooCloseToReportedLoop;
	std::cout << " loops were too close to previously reported loops." << std::endl;
	*/
}


} //namespace CLAM