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 #ifndef QM_DSP_TEMPOTRACKV2_H
17 #define QM_DSP_TEMPOTRACKV2_H
18 
19 #include <vector>
20 
21 //!!! Question: how far is this actually sample rate dependent?  I
22 // think it does produce plausible results for e.g. 48000 as well as
23 // 44100, but surely the fixed window sizes and comb filtering will
24 // make it prefer double or half time when run at e.g. 96000?
25 
26 class TempoTrackV2
27 {
28 public:
29     /**
30      * Construct a tempo tracker that will operate on beat detection
31      * function data calculated from audio at the given sample rate
32      * with the given frame increment.
33      *
34      * Currently the sample rate and increment are used only for the
35      * conversion from beat frame location to bpm in the tempo array.
36      */
37     TempoTrackV2(float sampleRate, int dfIncrement);
38     ~TempoTrackV2();
39 
40     // Returned beat periods are given in df increment units; inputtempo and tempi in bpm
calculateBeatPeriod(const std::vector<double> & df,std::vector<double> & beatPeriod,std::vector<double> & tempi)41     void calculateBeatPeriod(const std::vector<double> &df,
42                              std::vector<double> &beatPeriod,
43                              std::vector<double> &tempi) {
44         calculateBeatPeriod(df, beatPeriod, tempi, 120.0, false);
45     }
46 
47     // Returned beat periods are given in df increment units; inputtempo and tempi in bpm
48     // MEPD 28/11/12 Expose inputtempo and constraintempo parameters
49     // Note, if inputtempo = 120 and constraintempo = false, then functionality is as it was before
50     void calculateBeatPeriod(const std::vector<double> &df,
51                              std::vector<double> &beatPeriod,
52                              std::vector<double> &tempi,
53                              double inputtempo, bool constraintempo);
54 
55     // Returned beat positions are given in df increment units
calculateBeats(const std::vector<double> & df,const std::vector<double> & beatPeriod,std::vector<double> & beats)56     void calculateBeats(const std::vector<double> &df,
57                         const std::vector<double> &beatPeriod,
58                         std::vector<double> &beats) {
59         calculateBeats(df, beatPeriod, beats, 0.9, 4.0);
60     }
61 
62     // Returned beat positions are given in df increment units
63     // MEPD 28/11/12 Expose alpha and tightness parameters
64     // Note, if alpha = 0.9 and tightness = 4, then functionality is as it was before
65     void calculateBeats(const std::vector<double> &df,
66                         const std::vector<double> &beatPeriod,
67                         std::vector<double> &beats,
68                         double alpha, double tightness);
69 
70 private:
71     typedef std::vector<int> i_vec_t;
72     typedef std::vector<std::vector<int> > i_mat_t;
73     typedef std::vector<double> d_vec_t;
74     typedef std::vector<std::vector<double> > d_mat_t;
75 
76     float m_rate;
77     int m_increment;
78 
79     void adapt_thresh(d_vec_t &df);
80     double mean_array(const d_vec_t &dfin, int start, int end);
81     void filter_df(d_vec_t &df);
82     void get_rcf(const d_vec_t &dfframe, const d_vec_t &wv, d_vec_t &rcf);
83     void viterbi_decode(const d_mat_t &rcfmat, const d_vec_t &wv,
84                         d_vec_t &bp, d_vec_t &tempi);
85     double get_max_val(const d_vec_t &df);
86     int get_max_ind(const d_vec_t &df);
87     void normalise_vec(d_vec_t &df);
88 };
89 
90 #endif
91