rev: ddebd5730c64ee9767e9f153f03973022193d700 minibpm/src/MiniBpm.h -rw-r--r-- 3.6 KiB View raw Log this file
ddebd5730c64 — Chris Cannam Move explanatory comment to cpp file 7 years ago
                                                                                
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
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    MiniBPM
    A fixed-tempo BPM estimator for music audio
    Copyright 2012 Particular Programs Ltd.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.

    Alternatively, if you have a valid commercial licence for MiniBPM
    obtained by agreement with the copyright holders, you may
    redistribute and/or modify it under the terms described in that
    licence.

    If you wish to distribute code using MiniBPM under terms other
    than those of the GNU General Public License, you must obtain a
    valid commercial licence before doing so.
*/

#ifndef _BQ_MINI_BPM_H_
#define _BQ_MINI_BPM_H_

#include <vector>

namespace breakfastquay {

/**
 * A fixed-tempo BPM estimator, self-contained and implemented in a
 * single C++ file.
 *
 * This may be used in two ways: either call estimateTempoOfSamples()
 * with a single in-memory buffer of all audio samples, or (if the
 * input data is streamed or cannot fit in memory) call process()
 * repeatedly with consecutive sample blocks of any size, followed by
 * estimateTempo() when all samples have been supplied.
 *
 * A single channel of audio only may be supplied (multi-channel is
 * not supported). To process multi-channel audio, average the
 * channels first.
 */
class MiniBPM
{
public:
    MiniBPM(float sampleRate);
    ~MiniBPM();

    /**
     * Set the range of valid tempi. The default is 55-190bpm.
     */
    void setBPMRange(double min, double max);
    void getBPMRange(double &min, double &max) const;

    /**
     * Set the number of beats per bar, if known. If unknown, leave at
     * the default (which is 4).
     */
    void setBeatsPerBar(int bpb);
    int getBeatsPerBar() const;

    /**
     * Return the estimated tempo in bpm of the music audio in the
     * given sequence of samples. nsamples contains the number of
     * samples. If the tempo cannot be estimated because the clip is
     * too short, return 0.
     *
     * You should use either this function, or a series of process()
     * calls followed by an estimateTempo() call. Do not call both
     * process() and estimateTempoOfSamples() on the same estimator
     * object.
     */
    double estimateTempoOfSamples(const float *samples, int nsamples);

    /**
     * Supply a single block of audio for processing. The block may be
     * of any length. Blocks are assumed to be contiguous
     * (i.e. without overlap).
     */
    void process(const float *samples, int nsamples);

    /**
     * Return the estimated tempo in bpm of the music audio in the
     * sequence of samples previously supplied through a series of
     * calls to process(). If the tempo cannot be estimated because
     * the clip is too short, return 0.
     */
    double estimateTempo();

    /**
     * Return all candidate tempi for the last tempo estimation that
     * was carried out, in order of likelihood (best first). The value
     * returned from estimateTempo or estimateTempoOfSamples will
     * therefore be the first in the returned sequence.
     */
    std::vector<double> getTempoCandidates() const;

    /**
     * Prepare the object to carry out another tempo estimation on a
     * new audio clip. You can either call this between uses, or
     * simply destroy this object and construct a new one.
     */
    void reset();

private:
    class D;
    D *m_d;
};

}

#endif