annotate transform/BeatDetectionFunctionTransform.cpp @ 52:d397ea0a79f5

* Update licensing rubric for GPL
author Chris Cannam
date Mon, 20 Mar 2006 15:10:07 +0000
parents 39ae3dee27b9
children
rev   line source
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 "BeatDetectionFunctionTransform.h"
Chris@0 17
Chris@0 18 #include "model/DenseTimeValueModel.h"
Chris@0 19 #include "model/SparseTimeValueModel.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 BeatDetectionFunctionTransform::BeatDetectionFunctionTransform(Model *inputModel) :
Chris@0 27 Transform(inputModel)
Chris@0 28 {
Chris@0 29 m_output = new SparseTimeValueModel(inputModel->getSampleRate(), 1,
Chris@0 30 0.0, 0.0, false);
Chris@0 31 }
Chris@0 32
Chris@0 33 BeatDetectionFunctionTransform::~BeatDetectionFunctionTransform()
Chris@0 34 {
Chris@0 35 // parent does it all
Chris@0 36 }
Chris@0 37
Chris@0 38 TransformName
Chris@0 39 BeatDetectionFunctionTransform::getName()
Chris@0 40 {
Chris@0 41 return tr("Beat Detection Function");
Chris@0 42 }
Chris@0 43
Chris@0 44 void
Chris@0 45 BeatDetectionFunctionTransform::run()
Chris@0 46 {
Chris@0 47 SparseTimeValueModel *output = getOutput();
Chris@0 48 DenseTimeValueModel *input = getInput();
Chris@0 49 if (!input) {
Chris@0 50 std::cerr << "BeatDetectionFunctionTransform::run: WARNING: Input model is not conformable to DenseTimeValueModel" << std::endl;
Chris@0 51 return;
Chris@0 52 }
Chris@0 53
Chris@0 54 DFConfig config;
Chris@0 55
Chris@0 56 config.DFType = DF_COMPLEXSD;
Chris@0 57
Chris@0 58 // Step resolution for the detection function in seconds
Chris@0 59 config.stepSecs = 0.01161;
Chris@0 60
Chris@0 61 // Step resolution for the detection function in samples
Chris@0 62 config.stepSize = (unsigned int)floor((double)input->getSampleRate() *
Chris@0 63 config.stepSecs );
Chris@0 64
Chris@0 65 config.frameLength = 2 * config.stepSize;
Chris@0 66
Chris@0 67 unsigned int stepSize = config.stepSize;
Chris@0 68 unsigned int frameLength = config.frameLength;
Chris@0 69
Chris@0 70 output->setResolution(stepSize);
Chris@0 71
Chris@0 72 //Tempo Tracking Configuration Parameters
Chris@0 73 TTParams ttparams;
Chris@0 74
Chris@0 75 // Low Pass filter coefficients for detection function smoothing
Chris@0 76 double* aCoeffs = new double[3];
Chris@0 77 double* bCoeffs = new double[3];
Chris@0 78
Chris@0 79 aCoeffs[ 0 ] = 1;
Chris@0 80 aCoeffs[ 1 ] = -0.5949;
Chris@0 81 aCoeffs[ 2 ] = 0.2348;
Chris@0 82 bCoeffs[ 0 ] = 0.1600;
Chris@0 83 bCoeffs[ 1 ] = 0.3200;
Chris@0 84 bCoeffs[ 2 ] = 0.1600;
Chris@0 85
Chris@0 86 ttparams.winLength = 512;
Chris@0 87 ttparams.lagLength = 128;
Chris@0 88 ttparams.LPOrd = 2;
Chris@0 89 ttparams.LPACoeffs = aCoeffs;
Chris@0 90 ttparams.LPBCoeffs = bCoeffs;
Chris@0 91 ttparams.alpha = 9;
Chris@0 92 ttparams.WinT.post = 8;
Chris@0 93 ttparams.WinT.pre = 7;
Chris@0 94
Chris@0 95 ////////////////////////////////////////////////////////////
Chris@0 96 // DetectionFunction
Chris@0 97 ////////////////////////////////////////////////////////////
Chris@0 98 // Instantiate and configure detection function object
Chris@0 99
Chris@0 100 DetectionFunction df(config);
Chris@0 101
Chris@0 102 size_t origin = input->getStartFrame();
Chris@0 103 size_t frameCount = input->getEndFrame() - origin;
Chris@0 104 size_t blocks = (frameCount / stepSize);
Chris@0 105 if (blocks * stepSize < frameCount) ++blocks;
Chris@0 106
Chris@0 107 double *buffer = new double[frameLength];
Chris@0 108
Chris@0 109 // DF output with causal extension
Chris@0 110 unsigned int clen = blocks + ttparams.winLength;
Chris@0 111 double *dfOutput = new double[clen];
Chris@0 112
Chris@0 113 std::cerr << "Running beat detection function at step size " << stepSize << "..." << std::endl;
Chris@0 114
Chris@0 115 for (size_t i = 0; i < clen; ++i) {
Chris@0 116
Chris@0 117 // std::cerr << "block " << i << "/" << clen << std::endl;
Chris@0 118 // std::cerr << ".";
Chris@0 119
Chris@0 120 if (i < blocks) {
Chris@0 121 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 122 origin + i * stepSize,
Chris@0 123 origin + i * stepSize + frameLength,
Chris@0 124 buffer);
Chris@0 125 while (got < frameLength) buffer[got++] = 0.0;
Chris@0 126 dfOutput[i] = df.process(buffer);
Chris@0 127 } else {
Chris@0 128 dfOutput[i] = 0.0;
Chris@0 129 }
Chris@0 130
Chris@0 131 output->addPoint(SparseTimeValueModel::Point
Chris@0 132 (i * stepSize, dfOutput[i],
Chris@0 133 QString("%1").arg(dfOutput[i])));
Chris@0 134 // m_w->m_bdf->setCompletion(i * 99 / clen);
Chris@0 135 output->setCompletion(i * 99 / clen);
Chris@0 136
Chris@0 137 if (m_deleting) {
Chris@0 138 delete [] buffer;
Chris@0 139 delete [] dfOutput;
Chris@0 140 delete [] aCoeffs;
Chris@0 141 delete [] bCoeffs;
Chris@0 142 return;
Chris@0 143 }
Chris@0 144 }
Chris@0 145
Chris@0 146 output->setCompletion(100);
Chris@0 147 }
Chris@0 148
Chris@0 149 DenseTimeValueModel *
Chris@0 150 BeatDetectionFunctionTransform::getInput()
Chris@0 151 {
Chris@0 152 return dynamic_cast<DenseTimeValueModel *>(getInputModel());
Chris@0 153 }
Chris@0 154
Chris@0 155 SparseTimeValueModel *
Chris@0 156 BeatDetectionFunctionTransform::getOutput()
Chris@0 157 {
Chris@0 158 return static_cast<SparseTimeValueModel *>(getOutputModel());
Chris@0 159 }
Chris@0 160