annotate src/MatchFeeder.cpp @ 60:faa523be20f9 refactors_no_float

Update both Feeders so as to recognise the end of one input before the other has ended. MatchFeeder does this by detecting trailing silence (as both its inputs are technically the same length since the shorter is zero-padded) and reporting that to Finder. MatchFeatureFeeder simply recognises missing features at the end and won't queue them.
author Chris Cannam
date Fri, 14 Nov 2014 13:53:58 +0000
parents 6a5d165e5ea4
children 19a93b15fcc3 a540137d393b
rev   line source
cannam@0 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@0 2
cannam@0 3 /*
cannam@0 4 Vamp feature extraction plugin using the MATCH audio alignment
cannam@0 5 algorithm.
cannam@0 6
cannam@0 7 Centre for Digital Music, Queen Mary, University of London.
cannam@0 8 This file copyright 2007 Simon Dixon, Chris Cannam and QMUL.
cannam@0 9
cannam@0 10 This program is free software; you can redistribute it and/or
cannam@0 11 modify it under the terms of the GNU General Public License as
cannam@0 12 published by the Free Software Foundation; either version 2 of the
cannam@0 13 License, or (at your option) any later version. See the file
cannam@0 14 COPYING included with this distribution for more information.
cannam@0 15 */
cannam@0 16
cannam@0 17 #include "MatchFeeder.h"
cannam@0 18
Chris@14 19 using std::vector;
Chris@14 20
cannam@0 21 MatchFeeder::MatchFeeder(Matcher *m1, Matcher *m2) :
Chris@60 22 pm1(m1), pm2(m2), n(0), lastIn1(0), lastIn2(0)
cannam@0 23 {
Chris@43 24 fftSize = m1->m_params.fftSize;
cannam@0 25 finder = new Finder(m1, m2);
cannam@0 26 reBuffer = new double[fftSize/2+1];
cannam@0 27 imBuffer = new double[fftSize/2+1];
cannam@0 28 }
cannam@0 29
cannam@0 30 MatchFeeder::~MatchFeeder()
cannam@0 31 {
cannam@0 32 delete[] imBuffer;
cannam@0 33 delete[] reBuffer;
cannam@0 34 while (!q1.empty()) {
cannam@0 35 delete[] q1.front();
cannam@0 36 q1.pop();
cannam@0 37 }
cannam@0 38 while (!q2.empty()) {
cannam@0 39 delete[] q2.front();
cannam@0 40 q2.pop();
cannam@0 41 }
cannam@0 42 delete finder;
cannam@0 43 }
cannam@0 44
cannam@0 45 void
cannam@0 46 MatchFeeder::feed(const float *const *input)
cannam@0 47 {
cannam@0 48 // We maintain two FIFO queues of audio data frame block pointers,
cannam@0 49 // one per input stream. When the match-feeder function is
cannam@0 50 // entered, it knows that it has at least one block in each queue.
cannam@0 51 // It loops, processing up to one block per matcher, until a queue
cannam@0 52 // is empty. Then it returns, to be called again with more data.
cannam@0 53
Chris@14 54 prepare(input);
Chris@14 55
Chris@60 56 while (!q1.empty() || !q2.empty()) {
Chris@14 57 // std::cerr << "MatchFeeder::feed: q1 " << q1.size() << " q2 " << q2.size() << std::endl;
Chris@14 58 (void)feedBlock();
Chris@14 59 }
Chris@14 60 }
Chris@14 61
Chris@14 62 MatchFeeder::Features
Chris@14 63 MatchFeeder::feedAndGetFeatures(const float *const *input)
Chris@14 64 {
Chris@14 65 prepare(input);
Chris@14 66
Chris@14 67 Features all;
Chris@14 68
Chris@60 69 while (!q1.empty() || !q2.empty()) {
Chris@14 70 Features ff = feedBlock();
Chris@14 71 all.f1.insert(all.f1.end(), ff.f1.begin(), ff.f1.end());
Chris@14 72 all.f2.insert(all.f2.end(), ff.f2.begin(), ff.f2.end());
Chris@14 73 }
Chris@14 74
Chris@14 75 return all;
Chris@14 76 }
Chris@14 77
Chris@14 78 void
Chris@14 79 MatchFeeder::prepare(const float *const *input)
Chris@14 80 {
Chris@60 81 float threshold = 1e-5;
Chris@60 82
cannam@0 83 float *block = new float[fftSize+2];
Chris@60 84 float rms = 0;
Chris@60 85
cannam@0 86 for (size_t i = 0; i < fftSize+2; ++i) {
cannam@0 87 block[i] = input[0][i];
Chris@60 88 rms += block[i] * block[i];
Chris@60 89 }
Chris@60 90 rms = sqrtf(rms / (fftSize+2));
Chris@60 91 if (rms > threshold) {
Chris@60 92 lastIn1 = n;
cannam@0 93 }
cannam@0 94 q1.push(block);
cannam@0 95
cannam@0 96 block = new float[fftSize+2];
Chris@60 97 rms = 0;
Chris@60 98
cannam@0 99 for (size_t i = 0; i < fftSize+2; ++i) {
cannam@0 100 block[i] = input[1][i];
Chris@60 101 rms += block[i] * block[i];
Chris@60 102 }
Chris@60 103 rms = sqrtf(rms / (fftSize+2));
Chris@60 104 if (rms > threshold) {
Chris@60 105 lastIn2 = n;
cannam@0 106 }
cannam@0 107 q2.push(block);
Chris@60 108
Chris@60 109 ++n;
Chris@60 110 finder->setDurations(lastIn1, lastIn2);
cannam@0 111 }
cannam@0 112
Chris@14 113 MatchFeeder::Features
cannam@0 114 MatchFeeder::feedBlock()
cannam@0 115 {
Chris@14 116 Features ff;
Chris@14 117 vector<double> f1, f2;
Chris@14 118
Chris@60 119 if (q1.empty()) {
Chris@60 120 feed2();
Chris@60 121 } else if (q2.empty()) {
Chris@60 122 feed1();
Chris@60 123 } else if (pm1->m_frameCount < pm1->m_blockSize) { // fill initial block
cannam@0 124 // std::cerr << "feeding initial block" << std::endl;
Chris@14 125 f1 = feed1();
Chris@14 126 f2 = feed2();
Chris@60 127 } else if (pm1->m_runCount >= pm1->m_params.maxRunCount) { // slope constraints
cannam@0 128 // std::cerr << "pm1 too slopey" << std::endl;
Chris@14 129 f2 = feed2();
Chris@43 130 } else if (pm2->m_runCount >= pm2->m_params.maxRunCount) {
cannam@0 131 // std::cerr << "pm2 too slopey" << std::endl;
Chris@14 132 f1 = feed1();
cannam@0 133 } else {
cannam@0 134 switch (finder->getExpandDirection
Chris@43 135 (pm1->m_frameCount-1, pm2->m_frameCount-1)) {
cannam@0 136 case ADVANCE_THIS:
cannam@0 137 // std::cerr << "finder says ADVANCE_THIS" << std::endl;
Chris@14 138 f1 = feed1();
cannam@0 139 break;
cannam@0 140 case ADVANCE_OTHER:
cannam@0 141 // std::cerr << "finder says ADVANCE_OTHER" << std::endl;
Chris@14 142 f2 = feed2();
cannam@0 143 break;
cannam@0 144 case ADVANCE_BOTH:
cannam@0 145 // std::cerr << "finder says ADVANCE_BOTH" << std::endl;
Chris@14 146 f1 = feed1();
Chris@14 147 f2 = feed2();
cannam@0 148 break;
cannam@0 149 }
cannam@0 150 }
Chris@14 151
Chris@14 152 if (!f1.empty()) ff.f1.push_back(f1);
Chris@14 153 if (!f2.empty()) ff.f2.push_back(f2);
Chris@14 154 return ff;
cannam@0 155 }
cannam@0 156
Chris@14 157 vector<double>
cannam@0 158 MatchFeeder::feed1()
cannam@0 159 {
cannam@0 160 // std::cerr << "feed1" << std::endl;
cannam@0 161 float *block = q1.front();
cannam@0 162 q1.pop();
cannam@0 163 for (size_t i = 0; i <= fftSize/2; ++i) {
cannam@0 164 reBuffer[i] = block[i*2];
cannam@0 165 }
cannam@0 166 for (size_t i = 0; i <= fftSize/2; ++i) {
cannam@0 167 imBuffer[i] = block[i*2+1];
cannam@0 168 }
cannam@0 169 delete[] block;
Chris@21 170 return pm1->consumeFrame(reBuffer, imBuffer);
cannam@0 171 }
cannam@0 172
Chris@14 173 vector<double>
cannam@0 174 MatchFeeder::feed2()
cannam@0 175 {
cannam@0 176 // std::cerr << "feed2" << std::endl;
cannam@0 177 float *block = q2.front();
cannam@0 178 q2.pop();
cannam@0 179 for (size_t i = 0; i <= fftSize/2; ++i) {
cannam@0 180 reBuffer[i] = block[i*2];
cannam@0 181 }
cannam@0 182 for (size_t i = 0; i <= fftSize/2; ++i) {
cannam@0 183 imBuffer[i] = block[i*2+1];
cannam@0 184 }
cannam@0 185 delete[] block;
Chris@21 186 return pm2->consumeFrame(reBuffer, imBuffer);
cannam@0 187 }
cannam@0 188