comparison MatchFeeder.cpp @ 14:cdead4a52755

Make feeder/matcher able to return feature vectors
author Chris Cannam
date Fri, 10 Oct 2014 12:04:54 +0100
parents a02321c31884
children a82276091bbd
comparison
equal deleted inserted replaced
13:66082cc488c3 14:cdead4a52755
13 License, or (at your option) any later version. See the file 13 License, or (at your option) any later version. See the file
14 COPYING included with this distribution for more information. 14 COPYING included with this distribution for more information.
15 */ 15 */
16 16
17 #include "MatchFeeder.h" 17 #include "MatchFeeder.h"
18
19 using std::vector;
18 20
19 MatchFeeder::MatchFeeder(Matcher *m1, Matcher *m2) : 21 MatchFeeder::MatchFeeder(Matcher *m1, Matcher *m2) :
20 pm1(m1), pm2(m2) 22 pm1(m1), pm2(m2)
21 { 23 {
22 fftSize = m1->fftSize; 24 fftSize = m1->fftSize;
47 // one per input stream. When the match-feeder function is 49 // one per input stream. When the match-feeder function is
48 // entered, it knows that it has at least one block in each queue. 50 // entered, it knows that it has at least one block in each queue.
49 // It loops, processing up to one block per matcher, until a queue 51 // It loops, processing up to one block per matcher, until a queue
50 // is empty. Then it returns, to be called again with more data. 52 // is empty. Then it returns, to be called again with more data.
51 53
54 prepare(input);
55
56 while (!q1.empty() && !q2.empty()) {
57 // std::cerr << "MatchFeeder::feed: q1 " << q1.size() << " q2 " << q2.size() << std::endl;
58 (void)feedBlock();
59 }
60 }
61
62 MatchFeeder::Features
63 MatchFeeder::feedAndGetFeatures(const float *const *input)
64 {
65 prepare(input);
66
67 Features all;
68
69 while (!q1.empty() && !q2.empty()) {
70 Features ff = feedBlock();
71 all.f1.insert(all.f1.end(), ff.f1.begin(), ff.f1.end());
72 all.f2.insert(all.f2.end(), ff.f2.begin(), ff.f2.end());
73 }
74
75 return all;
76 }
77
78 void
79 MatchFeeder::prepare(const float *const *input)
80 {
52 float *block = new float[fftSize+2]; 81 float *block = new float[fftSize+2];
53 for (size_t i = 0; i < fftSize+2; ++i) { 82 for (size_t i = 0; i < fftSize+2; ++i) {
54 block[i] = input[0][i]; 83 block[i] = input[0][i];
55 } 84 }
56 q1.push(block); 85 q1.push(block);
58 block = new float[fftSize+2]; 87 block = new float[fftSize+2];
59 for (size_t i = 0; i < fftSize+2; ++i) { 88 for (size_t i = 0; i < fftSize+2; ++i) {
60 block[i] = input[1][i]; 89 block[i] = input[1][i];
61 } 90 }
62 q2.push(block); 91 q2.push(block);
63
64 while (!q1.empty() && !q2.empty()) {
65 // std::cerr << "MatchFeeder::feed: q1 " << q1.size() << " q2 " << q2.size() << std::endl;
66 feedBlock();
67 }
68 } 92 }
69 93
70 void 94 MatchFeeder::Features
71 MatchFeeder::feedBlock() 95 MatchFeeder::feedBlock()
72 { 96 {
97 Features ff;
98 vector<double> f1, f2;
99
73 if (pm1->frameCount < pm1->blockSize) { // fill initial block 100 if (pm1->frameCount < pm1->blockSize) { // fill initial block
74 // std::cerr << "feeding initial block" << std::endl; 101 // std::cerr << "feeding initial block" << std::endl;
75 feed1(); 102 f1 = feed1();
76 feed2(); 103 f2 = feed2();
77 } 104 }
78 //!!! } else if (pm1->atEnd) { 105 //!!! } else if (pm1->atEnd) {
79 // feed2(); 106 // feed2();
80 //!!! } else if (pm2->atEnd) 107 //!!! } else if (pm2->atEnd)
81 // feed1(); 108 // feed1();
82 else if (pm1->runCount >= Matcher::MAX_RUN_COUNT) { // slope constraints 109 else if (pm1->runCount >= Matcher::MAX_RUN_COUNT) { // slope constraints
83 // std::cerr << "pm1 too slopey" << std::endl; 110 // std::cerr << "pm1 too slopey" << std::endl;
84 feed2(); 111 f2 = feed2();
85 } else if (pm2->runCount >= Matcher::MAX_RUN_COUNT) { 112 } else if (pm2->runCount >= Matcher::MAX_RUN_COUNT) {
86 // std::cerr << "pm2 too slopey" << std::endl; 113 // std::cerr << "pm2 too slopey" << std::endl;
87 feed1(); 114 f1 = feed1();
88 } else { 115 } else {
89 switch (finder->getExpandDirection 116 switch (finder->getExpandDirection
90 (pm1->frameCount-1, pm2->frameCount-1)) { 117 (pm1->frameCount-1, pm2->frameCount-1)) {
91 case ADVANCE_THIS: 118 case ADVANCE_THIS:
92 // std::cerr << "finder says ADVANCE_THIS" << std::endl; 119 // std::cerr << "finder says ADVANCE_THIS" << std::endl;
93 feed1(); 120 f1 = feed1();
94 break; 121 break;
95 case ADVANCE_OTHER: 122 case ADVANCE_OTHER:
96 // std::cerr << "finder says ADVANCE_OTHER" << std::endl; 123 // std::cerr << "finder says ADVANCE_OTHER" << std::endl;
97 feed2(); 124 f2 = feed2();
98 break; 125 break;
99 case ADVANCE_BOTH: 126 case ADVANCE_BOTH:
100 // std::cerr << "finder says ADVANCE_BOTH" << std::endl; 127 // std::cerr << "finder says ADVANCE_BOTH" << std::endl;
101 feed1(); 128 f1 = feed1();
102 feed2(); 129 f2 = feed2();
103 break; 130 break;
104 } 131 }
105 } 132 }
133
134 if (!f1.empty()) ff.f1.push_back(f1);
135 if (!f2.empty()) ff.f2.push_back(f2);
136 return ff;
106 } 137 }
107 138
108 void 139 vector<double>
109 MatchFeeder::feed1() 140 MatchFeeder::feed1()
110 { 141 {
111 // std::cerr << "feed1" << std::endl; 142 // std::cerr << "feed1" << std::endl;
112 float *block = q1.front(); 143 float *block = q1.front();
113 q1.pop(); 144 q1.pop();
116 } 147 }
117 for (size_t i = 0; i <= fftSize/2; ++i) { 148 for (size_t i = 0; i <= fftSize/2; ++i) {
118 imBuffer[i] = block[i*2+1]; 149 imBuffer[i] = block[i*2+1];
119 } 150 }
120 delete[] block; 151 delete[] block;
121 pm1->processFrame(reBuffer, imBuffer); 152 return pm1->processFrame(reBuffer, imBuffer);
122 } 153 }
123 154
124 void 155 vector<double>
125 MatchFeeder::feed2() 156 MatchFeeder::feed2()
126 { 157 {
127 // std::cerr << "feed2" << std::endl; 158 // std::cerr << "feed2" << std::endl;
128 float *block = q2.front(); 159 float *block = q2.front();
129 q2.pop(); 160 q2.pop();
132 } 163 }
133 for (size_t i = 0; i <= fftSize/2; ++i) { 164 for (size_t i = 0; i <= fftSize/2; ++i) {
134 imBuffer[i] = block[i*2+1]; 165 imBuffer[i] = block[i*2+1];
135 } 166 }
136 delete[] block; 167 delete[] block;
137 pm2->processFrame(reBuffer, imBuffer); 168 return pm2->processFrame(reBuffer, imBuffer);
138 } 169 }
139 170