-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmatcher.cpp
More file actions
133 lines (121 loc) · 3.87 KB
/
matcher.cpp
File metadata and controls
133 lines (121 loc) · 3.87 KB
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
// cl /EHsc /O2 /openmp matcher.cpp Landmark.cpp Database.cpp lib/WavReader.cpp lib/Timing.cpp lib/ReadAudio.cpp lib/BmpReader.cpp lib/Signal.cpp lib/utils.cpp lib/Sound.cpp .\PeakFinder.cpp .\PeakFinderDejavu.cpp Analyzer.cpp
#include <cmath>
#include <stdio.h>
#include <cstdint>
#include <algorithm>
#include <string>
#include <stdexcept>
#include <fstream>
#include <sstream>
#include <omp.h>
#include "lib/Timing.hpp"
#include "Landmark.hpp"
#include "lib/utils.hpp"
#include "Analyzer.hpp"
#include "Database.hpp"
#include "PeakFinderDejavu.hpp"
int processQuery(
std::string name,
Analyzer &analyzer,
const Database &db,
match_t *scores
) {
Timing tm;
try {
std::vector<Landmark> lms = analyzer.fingerprint_file(name.c_str());
int which = db.query_landmarks(lms, scores);
return which;
}
catch (std::runtime_error x) {
LOG_ERROR("%s", x.what());
}
return -1;
}
int main(int argc, char const *argv[]) {
if (argc < 4) {
printf("Usage: ./matcher <query list> <database dir> <result file>\n");
return 1;
}
Timing timing;
std::ifstream flist(argv[1]);
if (!flist) {
printf("cannot read query list!\n");
return 1;
}
init_logger("matcher");
std::string line;
std::vector<std::string> queryList;
while (std::getline(flist, line)) {
queryList.push_back(line);
}
flist.close();
LOG_DEBUG("read query list %.3fs", timing.getRunTime() * 0.001);
Database db;
if (db.load(argv[2])) {
LOG_FATAL("cannot load database");
return 1;
}
int nSongs = db.songList.size();
std::ofstream fout(argv[3]);
if (!fout) {
LOG_FATAL("cannot write result!");
return 1;
}
std::ofstream fout_csv(argv[3] + std::string(".csv"));
if (!fout_csv) {
LOG_FATAL("cannot write result!");
return 1;
}
fout_csv << "query,answer,score,time,part_scores\n";
std::ofstream fout_bin( argv[3] + std::string(".bin"), std::ios::binary );
if (!fout_bin) {
LOG_FATAL("cannot write result!");
return 1;
}
std::vector<int> result(queryList.size());
std::vector<match_t> scores(nSongs);
LOG_DEBUG("load database %.3fs", timing.getRunTime() * 0.001);
LandmarkBuilder builder;
{
Analyzer analyzer;
analyzer.REPEAT_COUNT = 4;
analyzer.peak_finder = new PeakFinderDejavu();
analyzer.landmark_builder = new LandmarkBuilder();
for (int i = 0; i < queryList.size(); i++) {
std::string name = queryList[i];
LOG_DEBUG("File: %s", name.c_str());
result[i] = processQuery(name, analyzer, db, scores.data());
if (result[i] >= 0 && result[i] < nSongs) {
double score = scores[result[i]].score;
int hop = analyzer.peak_finder->FFT_SIZE - analyzer.peak_finder->NOVERLAP;
double framesecs = double(hop) / analyzer.SAMPLE_RATE;
for (int j = 0; j < nSongs; j++) {
double guess = (scores[j].offset - (1<<db.T1_BITS)) * framesecs;
if (guess < -6) {
guess = scores[j].offset * framesecs;
}
scores[j].offset = guess;
}
double match_time = scores[result[i]].offset;
LOG_INFO("%s\t%s\tscore=%f\ttime=%.2f", name.c_str(), db.songList[result[i]].c_str(), score, match_time);
fout << queryList[i] << '\t' << db.songList[result[i]] << '\n';
fout_csv << queryList[i] << ',' << db.songList[result[i]] << ',' << score << ',' << match_time << ',' << '\n';
}
else {
LOG_ERROR("error querying %s", name.c_str());
fout << queryList[i] << '\t' << "error\n";
fout_csv << queryList[i] << ",error,0,0,\n";
}
fout.flush();
fout_csv.flush();
fout_bin.write((char*)scores.data(), sizeof(scores[0]) * scores.size());
fout_bin.flush();
}
delete analyzer.peak_finder;
delete analyzer.landmark_builder;
}
fout.close();
fout_bin.close();
LOG_INFO("Total time: %.3fs", timing.getRunTime() * 0.001);
return 0;
}