summaryrefslogtreecommitdiffstats
path: root/src/track/beatgrid.h
blob: f1f09faf288016eeefb07340ad2d00b9671abfbf (plain)
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
#ifndef BEATGRID_H
#define BEATGRID_H

#include <QMutex>

#include "track/track.h"
#include "track/beats.h"
#include "proto/beats.pb.h"

#define BEAT_GRID_1_VERSION "BeatGrid-1.0"
#define BEAT_GRID_2_VERSION "BeatGrid-2.0"

namespace mixxx {

// BeatGrid is an implementation of the Beats interface that implements an
// infinite grid of beats, aligned to a song simply by a starting offset of the
// first beat and the song's average beats-per-minute.
class BeatGrid final : public Beats {
  public:
    // Construct a BeatGrid. If a more accurate sample rate is known, provide it
    // in the iSampleRate parameter -- otherwise pass 0.
    BeatGrid(const Track& track, SINT iSampleRate);
    // Construct a BeatGrid. If a more accurate sample rate is known, provide it
    // in the iSampleRate parameter -- otherwise pass 0. The BeatGrid will be
    // deserialized from the byte array.
    BeatGrid(const Track& track, SINT iSampleRate,
             const QByteArray& byteArray);
    ~BeatGrid() override = default;

    // Initializes the BeatGrid to have a BPM of dBpm and the first beat offset
    // of dFirstBeatSample. Does not generate an updated() signal, since it is
    // meant for initialization.
    void setGrid(double dBpm, double dFirstBeatSample);

    // The following are all methods from the Beats interface, see method
    // comments in beats.h

    Beats::CapabilitiesFlags getCapabilities() const override {
        return BEATSCAP_TRANSLATE | BEATSCAP_SCALE | BEATSCAP_SETBPM;
    }

    QByteArray toByteArray() const override;
    BeatsPointer clone() const override;
    QString getVersion() const override;
    QString getSubVersion() const override;
    virtual void setSubVersion(QString subVersion);

    ////////////////////////////////////////////////////////////////////////////
    // Beat calculations
    ////////////////////////////////////////////////////////////////////////////

    double findNextBeat(double dSamples) const override;
    double findPrevBeat(double dSamples) const override;
    bool findPrevNextBeats(double dSamples,
                           double* dpPrevBeatSamples,
                           double* dpNextBeatSamples) const override;
    double findClosestBeat(double dSamples) const override;
    double findNthBeat(double dSamples, int n) const override;
    std::unique_ptr<BeatIterator> findBeats(double startSample, double stopSample) const override;
    bool hasBeatInRange(double startSample, double stopSample) const override;
    double getBpm() const override;
    double getBpmRange(double startSample, double stopSample) const override;
    double getBpmAroundPosition(double curSample, int n) const override;

    ////////////////////////////////////////////////////////////////////////////
    // Beat mutations
    ////////////////////////////////////////////////////////////////////////////

    void addBeat(double dBeatSample) override;
    void removeBeat(double dBeatSample) override;
    void translate(double dNumSamples) override;
    void scale(enum BPMScale scale) override;
    void setBpm(double dBpm) override;

    SINT getSampleRate() const override {
        return m_iSampleRate;
    }

  private:
    BeatGrid(const BeatGrid& other);
    double firstBeatSample() const;
    double bpm() const;

    void readByteArray(const QByteArray& byteArray);
    // For internal use only.
    bool isValid() const;

    mutable QMutex m_mutex;
    // The sub-version of this beatgrid.
    QString m_subVersion;
    // The number of samples per second
    SINT m_iSampleRate;
    // Data storage for BeatGrid
    mixxx::track::io::BeatGrid m_grid;
    // The length of a beat in samples
    double m_dBeatLength;
};

} // namespace mixxx

#endif /* BEATGRID_H */