Merge branch 'windows' of git.ardour.org:ardour/ardour into windows
[ardour.git] / libs / qm-dsp / dsp / tempotracking / TempoTrackV2.h
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
22 using std::vector;
23
24 //!!! Question: how far is this actually sample rate dependent?  I
25 // think it does produce plausible results for e.g. 48000 as well as
26 // 44100, but surely the fixed window sizes and comb filtering will
27 // make it prefer double or half time when run at e.g. 96000?
28
29 class TempoTrackV2  
30 {
31 public:
32     /**
33      * Construct a tempo tracker that will operate on beat detection
34      * function data calculated from audio at the given sample rate
35      * with the given frame increment.
36      *
37      * Currently the sample rate and increment are used only for the
38      * conversion from beat frame location to bpm in the tempo array.
39      */
40     TempoTrackV2(float sampleRate, size_t dfIncrement);
41     ~TempoTrackV2();
42
43     // Returned beat periods are given in df increment units; tempi in bpm
44     void calculateBeatPeriod(const vector<double> &df,
45                              vector<double> &beatPeriod,
46                              vector<double> &tempi);
47
48     // Returned beat positions are given in df increment units
49     void calculateBeats(const vector<double> &df,
50                         const vector<double> &beatPeriod,
51                         vector<double> &beats);
52
53 private:
54     typedef vector<int> i_vec_t;
55     typedef vector<vector<int> > i_mat_t;
56     typedef vector<double> d_vec_t;
57     typedef vector<vector<double> > d_mat_t;
58
59     float m_rate;
60     size_t m_increment;
61
62     void adapt_thresh(d_vec_t &df);
63     double mean_array(const d_vec_t &dfin, int start, int end);
64     void filter_df(d_vec_t &df);
65     void get_rcf(const d_vec_t &dfframe, const d_vec_t &wv, d_vec_t &rcf);
66     void viterbi_decode(const d_mat_t &rcfmat, const d_vec_t &wv,
67                         d_vec_t &bp, d_vec_t &tempi);
68     double get_max_val(const d_vec_t &df);
69     int get_max_ind(const d_vec_t &df);
70     void normalise_vec(d_vec_t &df);
71 };
72
73 #endif