forked from KDE/rattlesnake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metronome.cpp
96 lines (78 loc) · 1.84 KB
/
metronome.cpp
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
#include "metronome.h"
#include <QDebug>
Metronome::Metronome(QObject *parent)
: QObject(parent)
{
m_hitTimer.setSingleShot(false);
setBpm(120);
connect(&m_hitTimer, &QTimer::timeout, this, &Metronome::hit);
connect(&m_hitTimer, &QTimer::timeout, this, &Metronome::playHitSound);
connect(&m_hitTimer, &QTimer::timeout, this, [=] {
m_hitCount++;
});
addNote(Sounds::Click);
}
int Metronome::bpm() const
{
return m_bpm;
}
void Metronome::setBpm(int bpm)
{
m_bpm = bpm;
Q_EMIT bpmChanged();
const int intervalMsecs = (60 / double(m_bpm)) * 1000;
m_hitTimer.setInterval(int(intervalMsecs));
}
void Metronome::playHitSound()
{
const int i = m_hitCount % m_notes.size();
// To display it in the gui
setCurrentIndex(i);
qDebug() << "Sound index" << i << m_notes.at(i)->soundFile();
m_mediaPlayer.setMedia(m_notes.at(i)->soundFile());
m_mediaPlayer.setVolume(m_notes.at(i)->volume());
m_mediaPlayer.setPosition(0);
m_mediaPlayer.play();
}
int Metronome::currentIndex() const
{
return m_currentIndex;
}
void Metronome::setCurrentIndex(int curentIndex)
{
qDebug() << "Current index" << currentIndex();
m_currentIndex = curentIndex;
Q_EMIT currentIndexChanged();
}
QVector<Note*> Metronome::notes() const
{
return m_notes;
}
void Metronome::addNote(const Sounds sound)
{
auto *note = new Note(this);
note->setSound(sound);
m_notes.push_back(note);
Q_EMIT notesChanged();
}
void Metronome::removeNote(const int index)
{
if (index < 1) {
return;
}
m_notes.removeAt(index);
Q_EMIT notesChanged();
}
void Metronome::start()
{
m_hitTimer.start();
Q_EMIT runningChanged();
}
void Metronome::stop()
{
m_hitTimer.stop();
Q_EMIT runningChanged();
}
bool Metronome::running() const {
return m_hitTimer.isActive();
}