-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudiodatabase.cpp
298 lines (215 loc) · 7.25 KB
/
audiodatabase.cpp
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <iostream>
#include <unordered_map>
#include <list>
#include <vector>
#include <fstream>
#include <sstream>
#include "../common/globals.h"
#include "../common/audiodatabase.h"
#include "../common/cpu_helpers.h"
#include "../common/gpu_helpers.h"
using namespace std;
/* Constructor */
DB::DB(){
numFiles = 0;
}
int DB::initFromScratch(char * folderPath, int cudaMode){
/* Step 1: For all .wavs in this folder, assign a filename to each file ID */
DIR* dir;
dirent* pdir;
vector<string> files;
dir = opendir(folderPath);
pdir = readdir(dir);
int i=0;
while (pdir){
string filename = pdir->d_name;
if(filename.find(".wav") != string::npos){
fileNames.push_back(pdir->d_name);
i++;
}
pdir = readdir(dir);
}
numFiles = i;
/* Step 2: Compute hashes and their respective datapoints */
for(int i=0; i<fileNames.size(); i++){
char* filepath;
filepath = (char *) malloc(strlen(folderPath)+strlen(fileNames[i])+1);
strcpy(filepath, folderPath );
strcat(filepath, fileNames[i]);
Complex * audio;
int audioSize, numChunks;
wavToComplex(filepath , &audio, &audioSize);
numChunks = audioSize / CHUNK_SAMPLES;
unsigned long * hashes;
if(cudaMode == 0){
audioToHashes(audio, numChunks, &hashes);
}
else{
audioToHashes_CUDA(audio, numChunks, &hashes);
}
fileLengths.push_back(numChunks);
for(int j=0; j<numChunks; j++){
DataPoint dp;
dp.file_id = i;
dp.time = j;
unsigned long hash = hashes[j];
hashmap[hash].push_back(dp);
}
}
return 0;
}
/*
serializeToFiles
====================
Takes the contents of the database and serializes them to text files
that can be used to initialize the database again at
@param - folderPath: string containing input file for WAV
@return - 0 for success, 1 for error
*/
int DB::serializeToFiles(char * outputHashesFile, char * outputfileNamesFile){
fstream fileNamesFile(outputfileNamesFile, ios_base::out);
for(int i=0; i<fileNames.size(); i++){
fileNamesFile << i << " " << fileNames[i] << " " << fileLengths[i] << " " << endl;
}
fstream hashFile(outputHashesFile, ios_base::out);
for (auto it : hashmap){
long hash = it.first;
list<DataPoint> dpList = hashmap[hash];
hashFile << std::hex << hash;
for(auto &i : dpList){
DataPoint dp = i;
hashFile << std::dec << " ( " << dp.file_id << " " << dp.time << " )";
}
hashFile << endl;
}
fileNamesFile.close();
hashFile.close();
return 0;
}
/*
dbInitFromFile
====================
Initializes database from a previously serialized instance of a database.
This is useful so we don't have to perform FFTs and determine frequencies
from hours of audio all over again.
@param - hashesFile: file containing serialized hashmap
- fileNamesFile: file containing serialized fileIDs and fileNames
@return - 0 for success, 1 for error
*/
int DB::initFromFile(char * hashesFile, char * fileNamesFile){
//Step 1: Read in the fileNames and assign file IDs
fstream fstreamfileNames(fileNamesFile, ios_base::in);
if (!fstreamfileNames) {
cout << "Unable to open fileNames file";
exit(1); // terminate with error
}
int id;
int length;
string fname;
while ( fstreamfileNames >> id >> fname >> length ){
char * fnameCstr = new char[fname.length() + 1];
strcpy(fnameCstr, fname.c_str());
fileNames.push_back(fnameCstr);
fileLengths.push_back(length);
numFiles++;
}
fstreamfileNames.close();
//Step 2: Read in the hash table
fstream fstreamHashes(hashesFile, ios_base::in);
if (!fstreamHashes) {
cout << "Unable to open hashes file";
exit(1); // terminate with error
}
string line;
while (getline( fstreamHashes, line))
{
stringstream stream(line);
unsigned long hash;
stream >> std::hex >> hash >> std::dec;
list<DataPoint> dpList;
char openParentheses;
char closeParentheses;
unsigned int dataPointTime;
unsigned int dataPointFileId;
int i = 0;
while( stream >> openParentheses >> dataPointFileId >> dataPointTime >> closeParentheses){
if(openParentheses!='(' || closeParentheses!=')'){
cout << "HASHES text file not formatted correctly at line: '" << line << "'" << endl;
exit(1); // terminate with error
}
DataPoint * dp = new DataPoint;
dp->file_id = dataPointFileId;
dp->time = dataPointTime;
dpList.push_back(*dp);
}
hashmap[hash] = dpList;
}
return 0;
}
int DB::getBestMatchingSongNaive(int numHashes, unsigned long * hashes){
//NOTE: THIS IS A NAIVE IMPLEMENTATION... FOR BETTER ACCURACY, CONSIDER OFFSET
//Compute histogram
int * histogram = (int *) malloc(numFiles * sizeof(int)); //Histogram of what files match each audio chunk
for(int i=0; i<numFiles; i++) histogram[i] = 0;
int curOffset = 0;
cout << "NUM HASHES = " << numHashes << endl;
for(int i=0; i<numHashes; i++){
unsigned long hash = hashes[i];
list<DataPoint> datapoints = hashmap[hash];
for(auto dp : datapoints){
histogram[dp.file_id]++;
}
curOffset++;
}
//Get best song from the histogram
int best = 0;
int bestScore = 0;
cout << "---HISTOGRAM RESULTS---" << endl;
for(int i=0; i<numFiles; i++){
cout << "FILE ID " << i << " HAS SCORE OF " << histogram[i] << " ( " << fileNames[i] << " ) " <<endl;
if(histogram[i] > bestScore){
best = i;
bestScore = histogram[i];
}
}
cout << "The audiofile " << fileNames[best] << " was the BEST matching song!";
return best;
}
int DB::getBestMatchingSong(int numHashes, unsigned long * hashes){
int biggestFileLength = 0;
for(int i=0; i<fileLengths.size(); i++) if(fileLengths[i] > biggestFileLength) biggestFileLength = fileLengths[i];
//Compute histogram
int histogram[numFiles]; //Histogram of what files match each audio chunk
memset(histogram, 0, numFiles * sizeof(int));
int offsetHistory[numFiles][biggestFileLength]; //Keeps track of at what point in time did we last see the hashes
memset(offsetHistory, -1, sizeof(offsetHistory[0][0]) * numFiles * biggestFileLength);
for(int t=0; t<numHashes; t++){
bool histogramCandidates[numFiles]; //Histogram candidates for this iteration
memset(histogramCandidates, false, sizeof(bool)*numFiles);
unsigned long hash = hashes[t];
list<DataPoint> datapoints = hashmap[hash];
for(auto dp : datapoints){
if(dp.time > 0 && offsetHistory[dp.file_id][dp.time-1] == (t-1)){
histogramCandidates[dp.file_id] = true; //We found a candidate
}
offsetHistory[dp.file_id][dp.time] = t;
}
for(int i=0; i<numFiles; i++) if(histogramCandidates[i] == true) histogram[i]++;
}
//Get best song from the histogram
int best = 0;
int bestScore = 0;
cout << endl << "---HISTOGRAM RESULTS---" << endl;
for(int i=0; i<numFiles; i++){
cout << "FILE ID " << i << " HAS SCORE OF " << histogram[i] << " ( " << fileNames[i] << " ) " <<endl;
if(histogram[i] > bestScore){
best = i;
bestScore = histogram[i];
}
}
cout << endl << "The audiofile " << fileNames[best] << " was the BEST matching song! " << endl << endl;
return best;
}