File: BPMDetect.h

package info (click to toggle)
djplay 0.5.0-3.1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,448 kB
  • ctags: 1,390
  • sloc: cpp: 13,382; sh: 7,104; makefile: 814; sed: 16
file content (159 lines) | stat: -rw-r--r-- 5,026 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/******************************************************************************
 *
 * Beats-per-minute (BPM) detection routine
 *
 * Author        : Copyright (c) Olli Parviainen
 * Author e-mail : oparviai @ iki.fi
 * File created  : 11-Jan-2003
 *
 * Last changed  : $Date: 2003/10/13 00:35:13 $
 * File revision : $Revision: 1.1 $
 *
 * $Id: BPMDetect.h,v 1.1 2003/10/13 00:35:13 melanie_t Exp $
 *
 * License :
 *
 *  SoundTouch sound processing library
 *  Copyright (c) Olli Parviainen
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *****************************************************************************/

#ifndef _BPMDetect_H_
#define _BPMDetect_H_

#include <qthread.h>

/// Minimum allowed BPM rate
#define MIN_BPM 30

/// Maximum allowed BPM rate
#define MAX_BPM 230


class StreamSource;
class QWidget;

class QBPMEvent : public QEvent
{
public:
    enum Type {LoaderEvent = 1002};
    QBPMEvent(Type t=LoaderEvent);
    ~QBPMEvent();

	float bpm();
	void setBpm(float i_bpm);
protected:
	float s_bpm;
};

/// Internal class prototype 
class FIFOSampleBuffer;

/// Class for calculating BPM rate for audio data.
class BPMDetect : public QThread
{
protected:
    /// Auto-correlation accumulator bins.
    float *xcorr;
    
    /// Amplitude envelope sliding average approximation level accumulator
    float envelopeAccu;

    /// RMS volume sliding average approximation level accumulator
    float RMSVolumeAccu;

    /// Sample average counter.
    int decimateCount;

    /// Sample average accumulator for FIFO-like decimation.
    int decimateSum;

    /// Decimate sound by this coefficient to reach approx. 500 Hz.
    int decimateBy;

    /// Auto-correlation window length
    int windowLen;

    /// Beginning of auto-correlation window: Autocorrelation isn't being updated for
    /// the first these many correlation bins.
    int windowStart;
 
    /// FIFO-buffer for decimated processing samples.
    FIFOSampleBuffer *buffer;

    /// Initialize the class for WAV file.
    void init(StreamSource *source);

    /// Updates auto-correlation function for given number of decimated samples that 
    /// are read from the internal 'buffer' pipe (samples aren't removed from the pipe 
    /// though).
    void updateXCorr(const int process_samples      /// How many samples are processed.
                     );

    /// Decimates samples to approx. 500 Hz.
    ///
    /// \return Number of output samples.
    int decimate(short *dest,       ///< Destination buffer
                 const short *src,  ///< Source sample buffer
                 int numsamples     ///< Number of source samples.
                 );

    /// Reads and prepares a block of samples for processing. Stereo sound is converted 
    /// to mono and sound data is then decimated down to approx. 500 Hz.
    ///
    /// \return Number of samples moved to outBuffer.
    int inputSamples(StreamSource *source,  ///< WAV file where data is read from
                     short *outBuffer       ///< Buffer for samples.
                     );

    /// Processes block of samples: Envelopes the samples and then
    /// updates the autocorrelation estimation.
    void processBlock(short *samples,   ///< Pointer to input/working data buffer
                      int numSamples    ///< Number of samples in buffer
                      );

    /// Calculates amplitude envelope for the buffer of samples.
    /// Result is outputted in 'samples.
    void calcEnvelope(short *samples,   ///< Pointer to input/output data buffer
                      int numsamples    ///< Number of samples in buffer
                      );

	StreamSource *s_source;

    /// Analyzes the WAV file data and returns BPM rate. Notice that the
    /// WAV audio data has to be in 16bit sample format.
    ///
    /// \return Beats-per-minute rate, or zero if detection failed.
	float detectBpm(StreamSource *source);

	float s_res;

	bool s_abort;
	QWidget *s_parent;
public:
    /// Constructor.
	BPMDetect(QWidget *parent, StreamSource *source);

    /// Destructor.
	virtual ~BPMDetect();

	void run(void);
	float result();
	void abort();
};

#endif // _BPMDetect_H_