summaryrefslogtreecommitdiff
path: root/plugin/src/Caelestia/beattracker.hpp
blob: fda0ddc79e8fd7e24af9e920359c5fe547afa9a8 (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
#pragma once

#include <QAudioSource>
#include <QIODevice>
#include <QObject>
#include <aubio/aubio.h>
#include <qqmlintegration.h>

namespace caelestia {

class BeatTracker : public QObject {
    Q_OBJECT
    QML_ELEMENT
    QML_SINGLETON

    Q_PROPERTY(smpl_t bpm READ bpm NOTIFY bpmChanged)
    Q_PROPERTY(int refCount READ refCount WRITE setRefCount NOTIFY refCountChanged)

public:
    explicit BeatTracker(uint_t sampleRate = 44100, uint_t hopSize = 512, QObject* parent = nullptr);
    ~BeatTracker();

    [[nodiscard]] smpl_t bpm() const;

    [[nodiscard]] int refCount() const;
    void setRefCount(int refCount);

signals:
    void bpmChanged();
    void refCountChanged();
    void beat(smpl_t bpm);

private:
    QAudioSource* m_source;
    QIODevice* m_device;

    aubio_tempo_t* m_tempo;
    fvec_t* m_in;
    fvec_t* m_out;
    uint_t m_hopSize;

    smpl_t m_bpm;
    int m_refCount;

    void start();
    void stop();
    void process();
    void handleStateChanged(QtAudio::State state) const;
};

} // namespace caelestia