Chris@49
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@52
|
4 Sonic Visualiser
|
Chris@52
|
5 An audio file viewer and annotation editor.
|
Chris@52
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@52
|
7 This file copyright 2006 Chris Cannam.
|
Chris@0
|
8
|
Chris@52
|
9 This program is free software; you can redistribute it and/or
|
Chris@52
|
10 modify it under the terms of the GNU General Public License as
|
Chris@52
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@52
|
12 License, or (at your option) any later version. See the file
|
Chris@52
|
13 COPYING included with this distribution for more information.
|
Chris@0
|
14 */
|
Chris@0
|
15
|
Chris@0
|
16 #include "BeatDetectTransform.h"
|
Chris@0
|
17
|
Chris@0
|
18 #include "model/DenseTimeValueModel.h"
|
Chris@0
|
19 #include "model/SparseOneDimensionalModel.h"
|
Chris@0
|
20
|
Chris@0
|
21 #include <iostream>
|
Chris@0
|
22 #include "dsp/onsets/DetectionFunction.h"
|
Chris@0
|
23 #include "dsp/tempotracking/TempoTrack.h"
|
Chris@0
|
24
|
Chris@0
|
25
|
Chris@0
|
26 BeatDetectTransform::BeatDetectTransform(Model *inputModel) :
|
Chris@0
|
27 Transform(inputModel)
|
Chris@0
|
28 {
|
Chris@0
|
29 // Step resolution for the detection function in seconds
|
Chris@0
|
30 double stepSecs = 0.01161;
|
Chris@0
|
31
|
Chris@0
|
32 // Step resolution for the detection function in samples
|
Chris@0
|
33 size_t stepSize = (size_t)floor((double)inputModel->getSampleRate() *
|
Chris@0
|
34 stepSecs);
|
Chris@0
|
35
|
Chris@0
|
36
|
Chris@0
|
37 // m_w->m_bdf->setResolution(stepSize);
|
Chris@0
|
38 // output->setResolution(stepSize);
|
Chris@0
|
39
|
Chris@0
|
40 std::cerr << "BeatDetectTransform::BeatDetectTransform: input sample rate " << inputModel->getSampleRate() << ", stepSecs " << stepSecs << ", stepSize " << stepSize << ", unrounded stepSize " << double(inputModel->getSampleRate()) * stepSecs << ", output sample rate " << inputModel->getSampleRate() / stepSize << ", unrounded output sample rate " << double(inputModel->getSampleRate()) / double(stepSize) << std::endl;
|
Chris@0
|
41
|
Chris@0
|
42 m_output = new SparseOneDimensionalModel(inputModel->getSampleRate(), 1);
|
Chris@0
|
43 }
|
Chris@0
|
44
|
Chris@0
|
45 BeatDetectTransform::~BeatDetectTransform()
|
Chris@0
|
46 {
|
Chris@0
|
47 // parent does it all
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 TransformName
|
Chris@0
|
51 BeatDetectTransform::getName()
|
Chris@0
|
52 {
|
Chris@0
|
53 return tr("Beats");
|
Chris@0
|
54 }
|
Chris@0
|
55
|
Chris@0
|
56 void
|
Chris@0
|
57 BeatDetectTransform::run()
|
Chris@0
|
58 {
|
Chris@0
|
59 SparseOneDimensionalModel *output = getOutput();
|
Chris@0
|
60 DenseTimeValueModel *input = getInput();
|
Chris@0
|
61 if (!input) return;
|
Chris@0
|
62
|
Chris@0
|
63 DFConfig config;
|
Chris@0
|
64
|
Chris@0
|
65 config.DFType = DF_COMPLEXSD;
|
Chris@0
|
66
|
Chris@0
|
67 // Step resolution for the detection function in seconds
|
Chris@0
|
68 config.stepSecs = 0.01161;
|
Chris@0
|
69
|
Chris@0
|
70 // Step resolution for the detection function in samples
|
Chris@0
|
71 config.stepSize = (unsigned int)floor((double)input->getSampleRate() *
|
Chris@0
|
72 config.stepSecs );
|
Chris@0
|
73
|
Chris@0
|
74 config.frameLength = 2 * config.stepSize;
|
Chris@0
|
75
|
Chris@0
|
76 unsigned int stepSize = config.stepSize;
|
Chris@0
|
77 unsigned int frameLength = config.frameLength;
|
Chris@0
|
78
|
Chris@0
|
79 // m_w->m_bdf->setResolution(stepSize);
|
Chris@0
|
80 output->setResolution(stepSize);
|
Chris@0
|
81
|
Chris@0
|
82 //Tempo Tracking Configuration Parameters
|
Chris@0
|
83 TTParams ttparams;
|
Chris@0
|
84
|
Chris@0
|
85 // Low Pass filter coefficients for detection function smoothing
|
Chris@0
|
86 double* aCoeffs = new double[3];
|
Chris@0
|
87 double* bCoeffs = new double[3];
|
Chris@0
|
88
|
Chris@0
|
89 aCoeffs[ 0 ] = 1;
|
Chris@0
|
90 aCoeffs[ 1 ] = -0.5949;
|
Chris@0
|
91 aCoeffs[ 2 ] = 0.2348;
|
Chris@0
|
92 bCoeffs[ 0 ] = 0.1600;
|
Chris@0
|
93 bCoeffs[ 1 ] = 0.3200;
|
Chris@0
|
94 bCoeffs[ 2 ] = 0.1600;
|
Chris@0
|
95
|
Chris@0
|
96 ttparams.winLength = 512;
|
Chris@0
|
97 ttparams.lagLength = 128;
|
Chris@0
|
98 ttparams.LPOrd = 2;
|
Chris@0
|
99 ttparams.LPACoeffs = aCoeffs;
|
Chris@0
|
100 ttparams.LPBCoeffs = bCoeffs;
|
Chris@0
|
101 ttparams.alpha = 9;
|
Chris@0
|
102 ttparams.WinT.post = 8;
|
Chris@0
|
103 ttparams.WinT.pre = 7;
|
Chris@0
|
104
|
Chris@0
|
105 ////////////////////////////////////////////////////////////
|
Chris@0
|
106 // DetectionFunction
|
Chris@0
|
107 ////////////////////////////////////////////////////////////
|
Chris@0
|
108 // Instantiate and configure detection function object
|
Chris@0
|
109
|
Chris@0
|
110 DetectionFunction df(config);
|
Chris@0
|
111
|
Chris@0
|
112 size_t origin = input->getStartFrame();
|
Chris@0
|
113 size_t frameCount = input->getEndFrame() - origin;
|
Chris@0
|
114 size_t blocks = (frameCount / stepSize);
|
Chris@0
|
115 if (blocks * stepSize < frameCount) ++blocks;
|
Chris@0
|
116
|
Chris@0
|
117 double *buffer = new double[frameLength];
|
Chris@0
|
118
|
Chris@0
|
119 // DF output with causal extension
|
Chris@0
|
120 unsigned int clen = blocks + ttparams.winLength;
|
Chris@0
|
121 double *dfOutput = new double[clen];
|
Chris@0
|
122
|
Chris@0
|
123 std::cerr << "Detecting beats at step size " << stepSize << "..." << std::endl;
|
Chris@0
|
124
|
Chris@0
|
125 for (size_t i = 0; i < clen; ++i) {
|
Chris@0
|
126
|
Chris@0
|
127 // std::cerr << "block " << i << "/" << clen << std::endl;
|
Chris@0
|
128 // std::cerr << ".";
|
Chris@0
|
129
|
Chris@0
|
130 if (i < blocks) {
|
Chris@0
|
131 size_t got = input->getValues(-1, //!!! needs to come from parent layer -- which is not supposed to be in scope at this point
|
Chris@0
|
132 origin + i * stepSize,
|
Chris@0
|
133 origin + i * stepSize + frameLength,
|
Chris@0
|
134 buffer);
|
Chris@0
|
135 while (got < frameLength) buffer[got++] = 0.0;
|
Chris@0
|
136 dfOutput[i] = df.process(buffer);
|
Chris@0
|
137 } else {
|
Chris@0
|
138 dfOutput[i] = 0.0;
|
Chris@0
|
139 }
|
Chris@0
|
140
|
Chris@0
|
141 // m_w->m_bdf->addPoint(SparseTimeValueModel::Point
|
Chris@0
|
142 // (i * stepSize, dfOutput[i],
|
Chris@0
|
143 // QString("%1").arg(dfOutput[i])));
|
Chris@0
|
144 // m_w->m_bdf->setCompletion(i * 99 / clen);
|
Chris@0
|
145 output->setCompletion(i * 99 / clen);
|
Chris@0
|
146
|
Chris@0
|
147 if (m_deleting) {
|
Chris@0
|
148 delete [] buffer;
|
Chris@0
|
149 delete [] dfOutput;
|
Chris@0
|
150 delete [] aCoeffs;
|
Chris@0
|
151 delete [] bCoeffs;
|
Chris@0
|
152 return;
|
Chris@0
|
153 }
|
Chris@0
|
154 }
|
Chris@0
|
155
|
Chris@0
|
156 // m_w->m_bdf->setCompletion(100);
|
Chris@0
|
157
|
Chris@0
|
158 // Tempo Track Object instantiation and configuration
|
Chris@0
|
159 TempoTrack tempoTracker(ttparams);
|
Chris@0
|
160
|
Chris@0
|
161 // Vector of detected onsets
|
Chris@0
|
162 vector<int> beats;
|
Chris@0
|
163
|
Chris@0
|
164 std::cerr << "Running tempo tracker..." << std::endl;
|
Chris@0
|
165
|
Chris@0
|
166 beats = tempoTracker.process(dfOutput, blocks);
|
Chris@0
|
167
|
Chris@0
|
168 delete [] buffer;
|
Chris@0
|
169 delete [] dfOutput;
|
Chris@0
|
170 delete [] aCoeffs;
|
Chris@0
|
171 delete [] bCoeffs;
|
Chris@0
|
172
|
Chris@0
|
173 for (size_t i = 0; i < beats.size(); ++i) {
|
Chris@0
|
174 // std::cerr << "Beat value " << beats[i] << ", multiplying out to " << beats[i] * stepSize << std::endl;
|
Chris@0
|
175 float bpm = 0.0;
|
Chris@0
|
176 int fdiff = 0;
|
Chris@0
|
177 if (i < beats.size() - 1) {
|
Chris@0
|
178 fdiff = (beats[i+1] - beats[i]) * stepSize;
|
Chris@0
|
179 // one beat is fdiff frames, so there are samplerate/fdiff bps,
|
Chris@0
|
180 // so 60*samplerate/fdiff bpm
|
Chris@0
|
181 if (fdiff > 0) {
|
Chris@0
|
182 bpm = (60.0 * input->getSampleRate()) / fdiff;
|
Chris@0
|
183 }
|
Chris@0
|
184 }
|
Chris@0
|
185 output->addPoint(SparseOneDimensionalModel::Point
|
Chris@0
|
186 (origin + beats[i] * stepSize, QString("%1").arg(bpm)));
|
Chris@0
|
187 if (m_deleting) return;
|
Chris@0
|
188 }
|
Chris@0
|
189
|
Chris@0
|
190 output->setCompletion(100);
|
Chris@0
|
191 }
|
Chris@0
|
192
|
Chris@0
|
193 DenseTimeValueModel *
|
Chris@0
|
194 BeatDetectTransform::getInput()
|
Chris@0
|
195 {
|
Chris@0
|
196 DenseTimeValueModel *dtvm =
|
Chris@0
|
197 dynamic_cast<DenseTimeValueModel *>(getInputModel());
|
Chris@0
|
198 if (!dtvm) {
|
Chris@0
|
199 std::cerr << "BeatDetectTransform::getInput: WARNING: Input model is not conformable to DenseTimeValueModel" << std::endl;
|
Chris@0
|
200 }
|
Chris@0
|
201 return dtvm;
|
Chris@0
|
202 }
|
Chris@0
|
203
|
Chris@0
|
204 SparseOneDimensionalModel *
|
Chris@0
|
205 BeatDetectTransform::getOutput()
|
Chris@0
|
206 {
|
Chris@0
|
207 return static_cast<SparseOneDimensionalModel *>(getOutputModel());
|
Chris@0
|
208 }
|
Chris@0
|
209
|