1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4     QM DSP Library
5 
6     Centre for Digital Music, Queen Mary, University of London.
7     This file copyright 2008-2009 Matthew Davies and QMUL.
8 
9     This program is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public License as
11     published by the Free Software Foundation; either version 2 of the
12     License, or (at your option) any later version.  See the file
13     COPYING included with this distribution for more information.
14 */
15 
16 
17 #ifndef TEMPOTRACKV2_H
18 #define TEMPOTRACKV2_H
19 
20 #include <vector>
21 using namespace std;
22 
23 //!!! Question: how far is this actually sample rate dependent?  I
24 // think it does produce plausible results for e.g. 48000 as well as
25 // 44100, but surely the fixed window sizes and comb filtering will
26 // make it prefer double or half time when run at e.g. 96000?
27 
28 class TempoTrackV2
29 {
30 public:
31     /**
32      * Construct a tempo tracker that will operate on beat detection
33      * function data calculated from audio at the given sample rate
34      * with the given frame increment.
35      *
36      * Currently the sample rate and increment are used only for the
37      * conversion from beat frame location to bpm in the tempo array.
38      */
39     TempoTrackV2(float sampleRate, size_t dfIncrement);
40     ~TempoTrackV2();
41 
42     // Returned beat periods are given in df increment units; inputtempo and tempi in bpm
calculateBeatPeriod(const vector<double> & df,vector<double> & beatPeriod,vector<double> & tempi)43     void calculateBeatPeriod(const vector<double> &df,
44                              vector<double> &beatPeriod,
45                              vector<double> &tempi) {
46         calculateBeatPeriod(df, beatPeriod, tempi, 120.0, false);
47     }
48 
49     // Returned beat periods are given in df increment units; inputtempo and tempi in bpm
50     // MEPD 28/11/12 Expose inputtempo and constraintempo parameters
51     // Note, if inputtempo = 120 and constraintempo = false, then functionality is as it was before
52     void calculateBeatPeriod(const vector<double> &df,
53                              vector<double> &beatPeriod,
54                              vector<double> &tempi,
55                              double inputtempo, bool constraintempo);
56 
57     // Returned beat positions are given in df increment units
calculateBeats(const vector<double> & df,const vector<double> & beatPeriod,vector<double> & beats)58     void calculateBeats(const vector<double> &df,
59                         const vector<double> &beatPeriod,
60                         vector<double> &beats) {
61         calculateBeats(df, beatPeriod, beats, 0.9, 4.0);
62     }
63 
64     // Returned beat positions are given in df increment units
65     // MEPD 28/11/12 Expose alpha and tightness parameters
66     // Note, if alpha = 0.9 and tightness = 4, then functionality is as it was before
67     void calculateBeats(const vector<double> &df,
68                         const vector<double> &beatPeriod,
69                         vector<double> &beats,
70                         double alpha, double tightness);
71 
72 private:
73     typedef vector<int> i_vec_t;
74     typedef vector<vector<int> > i_mat_t;
75     typedef vector<double> d_vec_t;
76     typedef vector<vector<double> > d_mat_t;
77 
78     float m_rate;
79     size_t m_increment;
80 
81     void adapt_thresh(d_vec_t &df);
82     double mean_array(const d_vec_t &dfin, int start, int end);
83     void filter_df(d_vec_t &df);
84     void get_rcf(const d_vec_t &dfframe, const d_vec_t &wv, d_vec_t &rcf);
85     void viterbi_decode(const d_mat_t &rcfmat, const d_vec_t &wv,
86                         d_vec_t &bp, d_vec_t &tempi);
87     double get_max_val(const d_vec_t &df);
88     int get_max_ind(const d_vec_t &df);
89     void normalise_vec(d_vec_t &df);
90 };
91 
92 #endif
93