c@279
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
c@279
|
2
|
c@279
|
3 /*
|
c@279
|
4 QM DSP Library
|
c@279
|
5
|
c@279
|
6 Centre for Digital Music, Queen Mary, University of London.
|
c@279
|
7 This file copyright 2008-2009 Matthew Davies and QMUL.
|
c@279
|
8 All rights reserved.
|
c@279
|
9 */
|
c@279
|
10
|
c@279
|
11 #ifndef DOWNBEAT_H
|
c@279
|
12 #define DOWNBEAT_H
|
c@279
|
13
|
c@279
|
14 #include <vector>
|
c@279
|
15
|
c@279
|
16 #include "dsp/rateconversion/Decimator.h"
|
c@279
|
17
|
c@279
|
18 using std::vector;
|
c@279
|
19
|
c@279
|
20 /**
|
c@279
|
21 * This class takes an input audio signal and a sequence of beat
|
c@279
|
22 * locations (calculated e.g. by TempoTrackV2) and estimates which of
|
c@279
|
23 * the beat locations are downbeats (first beat of the bar).
|
c@279
|
24 *
|
c@279
|
25 * The input audio signal is expected to have been downsampled to a
|
c@279
|
26 * very low sampling rate (e.g. 2700Hz). A utility function for
|
c@279
|
27 * downsampling and buffering incoming block-by-block audio is
|
c@279
|
28 * provided.
|
c@279
|
29 */
|
c@279
|
30 class DownBeat
|
c@279
|
31 {
|
c@279
|
32 public:
|
c@279
|
33 /**
|
c@279
|
34 * Construct a downbeat locator that will operate on audio at the
|
c@279
|
35 * downsampled by the given decimation factor from the given
|
c@279
|
36 * original sample rate, plus beats extracted from the same audio
|
c@279
|
37 * at the given original sample rate with the given frame
|
c@279
|
38 * increment.
|
c@279
|
39 *
|
c@279
|
40 * decimationFactor must be a power of two no greater than 64, and
|
c@279
|
41 * dfIncrement must be a multiple of decimationFactor.
|
c@279
|
42 */
|
c@279
|
43 DownBeat(float originalSampleRate,
|
c@279
|
44 size_t decimationFactor,
|
c@279
|
45 size_t dfIncrement);
|
c@279
|
46 ~DownBeat();
|
c@279
|
47
|
c@279
|
48 /**
|
c@279
|
49 * Estimate which beats are down-beats.
|
c@279
|
50 *
|
c@279
|
51 * audio contains the input audio stream after downsampling, and
|
c@279
|
52 * audioLength contains the number of samples in this downsampled
|
c@279
|
53 * stream.
|
c@279
|
54 *
|
c@279
|
55 * beats contains a series of beat positions expressed in
|
c@279
|
56 * multiples of the df increment at the audio's original sample
|
c@279
|
57 * rate, as described to the constructor.
|
c@279
|
58 *
|
c@279
|
59 * The returned downbeat array contains a series of indices to the
|
c@279
|
60 * beats array.
|
c@279
|
61 */
|
c@279
|
62 void findDownBeats(const double *audio, // downsampled
|
c@279
|
63 size_t audioLength, // after downsampling
|
c@279
|
64 const vector<double> &beats,
|
c@279
|
65 vector<int> &downbeats);
|
c@279
|
66
|
c@279
|
67 /**
|
c@279
|
68 * For your downsampling convenience: call this function
|
c@279
|
69 * repeatedly with input audio blocks containing dfIncrement
|
c@279
|
70 * samples at the original sample rate, to decimate them to the
|
c@279
|
71 * downsampled rate and buffer them within the DownBeat class.
|
c@279
|
72 *
|
c@279
|
73 * Call getBufferedAudio() to retrieve the results after all
|
c@279
|
74 * blocks have been processed.
|
c@279
|
75 */
|
c@279
|
76 void pushAudioBlock(const double *audio);
|
c@279
|
77
|
c@279
|
78 /**
|
c@279
|
79 * Retrieve the accumulated audio produced by pushAudioBlock calls.
|
c@279
|
80 */
|
c@279
|
81 const double *getBufferedAudio(size_t &length) const;
|
c@279
|
82
|
c@279
|
83 private:
|
c@279
|
84 typedef vector<int> i_vec_t;
|
c@279
|
85 typedef vector<vector<int> > i_mat_t;
|
c@279
|
86 typedef vector<double> d_vec_t;
|
c@279
|
87 typedef vector<vector<double> > d_mat_t;
|
c@279
|
88
|
c@279
|
89 void makeDecimators();
|
c@279
|
90 double measureSpecDiff(d_vec_t oldspec, d_vec_t newspec);
|
c@279
|
91
|
c@279
|
92 float m_rate;
|
c@279
|
93 size_t m_factor;
|
c@279
|
94 size_t m_increment;
|
c@279
|
95 Decimator *m_decimator1;
|
c@279
|
96 Decimator *m_decimator2;
|
c@279
|
97 double *m_buffer;
|
c@279
|
98 double *m_decbuf;
|
c@279
|
99 size_t m_bufsiz;
|
c@279
|
100 size_t m_buffill;
|
c@279
|
101 size_t m_beatframesize;
|
c@279
|
102 double *m_beatframe;
|
c@279
|
103 double *m_fftRealOut;
|
c@279
|
104 double *m_fftImagOut;
|
c@279
|
105 };
|
c@279
|
106
|
c@279
|
107 #endif
|