1 //=======================================================================
2 /** @file BTrack.h
3  *  @brief BTrack - a real-time beat tracker
4  *  @author Zach Banks, Adam Stark
5  *  @copyright Copyright (C) 2015 Zach Banks
6  *  @copyright Copyright (C) 2008-2014  Queen Mary University of London
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21 //=======================================================================
22 
23 #ifndef __BTRACK_H
24 #define __BTRACK_H
25 
26 #include "OnsetDetectionFunction.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 //=======================================================================
33 /** The main beat tracking class and the interface to the BTrack
34  * beat tracking algorithm. The algorithm can process either
35  * audio frames or onset detection function samples and also
36  * contains some static functions for calculating beat times in seconds
37  */
38 struct btrack {
39     struct odf odf;
40     int frameSize;
41     double invSampleRate;
42     double * onsetDF;                       /**< to hold onset detection function */
43     double * cumulativeScore;               /**< to hold cumulative score */
44     double * w1;
45 
46     double resampledOnsetDF[512];           /**< to hold resampled detection function */
47     double acf[512];                        /**<  to hold autocorrelation function */
48     double weightingVector[128];            /**<  to hold weighting vector */
49     double combFilterBankOutput[128];       /**<  to hold comb filter output */
50     double tempoObservationVector[41];      /**<  to hold tempo version of comb filter output */
51 
52     double delta[41];                       /**<  to hold final tempo candidate array */
53     double prevDelta[41];                   /**<  previous delta */
54     double prevDeltaFixed[41];              /**<  fixed tempo version of previous delta */
55 
56     double tempoTransitionMatrix[41][41];   /**<  tempo transition matrix */
57 
58 	//=======================================================================
59     // parameters
60 
61     double tightness;                       /**< the tightness of the weighting used to calculate cumulative score */
62     double alpha;                           /**< the mix between the current detection function sample and the cumulative score's "momentum" */
63     double beatPeriod;                      /**< the beat period, in detection function samples */
64     double tempo;                           /**< the tempo in beats per minute */
65     double estimatedTempo;                  /**< the current tempo estimation being used by the algorithm */
66     double latestCumulativeScoreValue;      /**< holds the latest value of the cumulative score function */
67     double latestODF;                       /**< holds the latest value of the onset detection function*/
68     double latestConfidence;                /**< holds the latest confidence value, the ratio between max score & min score in the last beat */
69     double tempoToLagFactor;                /**< factor for converting between lag and tempo */
70     int m0;                                 /**< indicates when the next point to predict the next beat is */
71     int beatCounter;                        /**< keeps track of when the next beat is - will be zero when the beat is due, and is set elsewhere in the algorithm to be positive once a beat prediction is made */
72     int hopSize;                            /**< the hop size being used by the algorithm */
73     int onsetDFBufferSize;                  /**< the onset detection function buffer size */
74     int tempoFixed;                         /**< indicates whether the tempo should be fixed or not */
75     int beatDueInFrame;                     /**< indicates whether a beat is due in the current frame */
76 
77 };
78 
79 /** Constructor taking both hopSize and frameSize
80 * @param hop_size the hop size in audio samples
81 * @param frame_size the frame size in audio samples
82 */
83 int btrack_init(struct btrack * bt, int hop_size, int frame_size, int sample_rate);
84 void btrack_del(struct btrack * bt);
85 
86 void btrack_process_audio_frame(struct btrack * bt, const btrack_chunk_t * frame);
87 void btrack_process_fft_frame(struct btrack * bt, const btrack_chunk_t * fft_frame);
88 void btrack_process_odf_sample(struct btrack * bt, double odf_sample);
89 
90 int btrack_beat_due_in_current_frame(const struct btrack * bt);
91 double btrack_get_bpm(const struct btrack * bt);
92 double btrack_get_latest_score(const struct btrack * bt);
93 double btrack_get_latest_odf(const struct btrack * bt);
94 double btrack_get_latest_confidence(const struct btrack * bt);
95 int btrack_get_frames_until_beat(const struct btrack * bt);
96 double btrack_get_time_until_beat(const struct btrack * bt);
97 
98 void btrack_set_bpm(struct btrack * bt, double bpm);
99 void btrack_fix_bpm(struct btrack * bt, double bpm);
100 void btrack_nofix_bpm(struct btrack * bt);
101 
102 void btrack_set_hop_size(struct btrack * bt, int hop_size);
103 
104 #ifdef __cplusplus
105 }
106 #endif
107 
108 #endif
109