Skip to content

Commit c2da0de

Browse files
Merge pull request #32090 from juli27/cleanupTempo
Small cleanup in `TempoMap`
2 parents 5b9cac7 + 3d4e748 commit c2da0de

File tree

2 files changed

+33
-121
lines changed

2 files changed

+33
-121
lines changed

src/engraving/dom/tempo.cpp

Lines changed: 19 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -31,50 +31,16 @@
3131
using namespace mu;
3232

3333
namespace mu::engraving {
34-
//---------------------------------------------------------
35-
// TEvent
36-
//---------------------------------------------------------
37-
38-
TEvent::TEvent()
39-
{
40-
type = TempoType::INVALID;
41-
tempo = 0.0;
42-
pause = 0.0;
43-
time = 0.0;
44-
}
45-
46-
TEvent::TEvent(const TEvent& e)
47-
{
48-
type = e.type;
49-
tempo = e.tempo;
50-
pause = e.pause;
51-
time = e.time;
52-
}
53-
54-
TEvent::TEvent(BeatsPerSecond t, double p, TempoType tp)
34+
TEvent::TEvent(const BeatsPerSecond t, const double p, const TempoType tp)
35+
: type(tp), tempo(t), pause(p)
5536
{
56-
type = tp;
57-
tempo = t;
58-
pause = p;
59-
time = 0.0;
6037
}
6138

6239
bool TEvent::valid() const
6340
{
6441
return !(!type);
6542
}
6643

67-
//---------------------------------------------------------
68-
// TempoMap
69-
//---------------------------------------------------------
70-
71-
TempoMap::TempoMap()
72-
{
73-
m_tempo = 2.0; // default fixed tempo in beat per second
74-
m_tempoSN = 1;
75-
m_tempoMultiplier = 1.0;
76-
}
77-
7844
//---------------------------------------------------------
7945
// setPause
8046
//---------------------------------------------------------
@@ -135,7 +101,6 @@ void TempoMap::normalize()
135101
tick = e->first;
136102
tempo = e->second.tempo.val;
137103
}
138-
++m_tempoSN;
139104
}
140105

141106
//---------------------------------------------------------
@@ -159,7 +124,6 @@ void TempoMap::clear()
159124
{
160125
std::map<int, TEvent>::clear();
161126
m_pauses.clear();
162-
++m_tempoSN;
163127
}
164128

165129
//---------------------------------------------------------
@@ -183,7 +147,6 @@ void TempoMap::clearRange(int tick1, int tick2)
183147
}
184148

185149
erase(first, last);
186-
++m_tempoSN;
187150
}
188151

189152
//---------------------------------------------------------
@@ -224,27 +187,6 @@ double TempoMap::pauseSecs(int tick) const
224187
return muse::value(m_pauses, tick, 0.0);
225188
}
226189

227-
//---------------------------------------------------------
228-
// del
229-
//---------------------------------------------------------
230-
231-
void TempoMap::del(int tick)
232-
{
233-
auto e = find(tick);
234-
if (e == end()) {
235-
LOGD("TempoMap::del event at (%d): not found", tick);
236-
// abort();
237-
return;
238-
}
239-
// don't delete event if still being used for pause
240-
if (e->second.type & TempoType::PAUSE) {
241-
e->second.type = TempoType::PAUSE;
242-
} else {
243-
erase(e);
244-
}
245-
normalize();
246-
}
247-
248190
BeatsPerSecond TempoMap::tempoMultiplier() const
249191
{
250192
return m_tempoMultiplier;
@@ -272,34 +214,22 @@ bool TempoMap::setTempoMultiplier(BeatsPerSecond val)
272214

273215
void TempoMap::delTempo(int tick)
274216
{
275-
del(tick);
276-
++m_tempoSN;
277-
}
278-
279-
//---------------------------------------------------------
280-
// tick2time
281-
//---------------------------------------------------------
282-
283-
double TempoMap::tick2time(int tick, double time, int* sn) const
284-
{
285-
return (*sn == m_tempoSN) ? time : tick2time(tick, sn);
286-
}
287-
288-
//---------------------------------------------------------
289-
// time2tick
290-
// return cached value t if list did not change
291-
//---------------------------------------------------------
292-
293-
int TempoMap::time2tick(double time, int t, int* sn) const
294-
{
295-
return (*sn == m_tempoSN) ? t : time2tick(time, sn);
217+
auto e = find(tick);
218+
if (e == end()) {
219+
LOGD("TempoMap::del event at (%d): not found", tick);
220+
// abort();
221+
return;
222+
}
223+
// don't delete event if still being used for pause
224+
if (e->second.type & TempoType::PAUSE) {
225+
e->second.type = TempoType::PAUSE;
226+
} else {
227+
erase(e);
228+
}
229+
normalize();
296230
}
297231

298-
//---------------------------------------------------------
299-
// tick2time
300-
//---------------------------------------------------------
301-
302-
double TempoMap::tick2time(int tick, int* sn) const
232+
double TempoMap::tick2time(int tick) const
303233
{
304234
double time = 0.0;
305235
double delta = double(tick);
@@ -329,9 +259,7 @@ double TempoMap::tick2time(int tick, int* sn) const
329259
} else {
330260
LOGD("TempoMap: empty");
331261
}
332-
if (sn) {
333-
*sn = m_tempoSN;
334-
}
262+
335263
time += delta / (Constants::DIVISION * tempo.val * m_tempoMultiplier.val);
336264
return time;
337265
}
@@ -340,7 +268,7 @@ double TempoMap::tick2time(int tick, int* sn) const
340268
// time2tick
341269
//---------------------------------------------------------
342270

343-
int TempoMap::time2tick(double time, int* sn) const
271+
int TempoMap::time2tick(double time) const
344272
{
345273
int tick = 0;
346274
double delta = time;
@@ -363,9 +291,7 @@ int TempoMap::time2tick(double time, int* sn) const
363291
}
364292
delta = time - delta;
365293
tick += lrint(delta * m_tempoMultiplier.val * Constants::DIVISION * tempo.val);
366-
if (sn) {
367-
*sn = m_tempoSN;
368-
}
294+
369295
return tick;
370296
}
371297
}

src/engraving/dom/tempo.h

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include "../types/bps.h"
3131

3232
namespace mu::engraving {
33-
static constexpr int TEMPO_PRECISION = 6;
33+
inline constexpr int TEMPO_PRECISION = 6;
3434

3535
enum class TempoType : char {
3636
INVALID = 0x0, PAUSE = 0x1, FIX = 0x2, RAMP = 0x4
@@ -39,19 +39,14 @@ enum class TempoType : char {
3939
typedef muse::Flags<TempoType> TempoTypes;
4040
DECLARE_OPERATORS_FOR_FLAGS(TempoTypes)
4141

42-
//---------------------------------------------------------
43-
// Tempo Event
44-
//---------------------------------------------------------
45-
4642
struct TEvent {
47-
TempoTypes type;
48-
BeatsPerSecond tempo; // beats per second
49-
double pause = 0.0; // pause in seconds
50-
double time = 0.0; // precomputed time for tick in sec
51-
52-
TEvent();
53-
TEvent(const TEvent& e);
54-
TEvent(BeatsPerSecond bps, double seconds, TempoType t);
43+
TempoTypes type = TempoType::INVALID;
44+
BeatsPerSecond tempo = 0.0;
45+
double pause = 0.0; // pause in seconds
46+
double time = 0.0; // precomputed time for tick in sec
47+
48+
TEvent() = default;
49+
TEvent(BeatsPerSecond, double pauseInSeconds, TempoType);
5550
bool valid() const;
5651

5752
bool operator ==(const TEvent& other) const
@@ -63,16 +58,13 @@ struct TEvent {
6358
}
6459
};
6560

66-
//---------------------------------------------------------
67-
// Tempomap
68-
//---------------------------------------------------------
69-
7061
class TempoMap : public std::map<int, TEvent>
7162
{
7263
OBJECT_ALLOCATOR(engraving, TempoMap)
7364

7465
public:
75-
TempoMap();
66+
TempoMap() = default;
67+
7668
void clear();
7769
void clearRange(int tick1, int tick2);
7870

@@ -82,11 +74,8 @@ class TempoMap : public std::map<int, TEvent>
8274
BeatsPerSecond multipliedTempo(int tick) const;
8375
double pauseSecs(int tick) const;
8476

85-
double tick2time(int tick, int* sn = 0) const;
86-
double tick2time(int tick, double time, int* sn) const;
87-
int time2tick(double time, int* sn = 0) const;
88-
int time2tick(double time, int tick, int* sn) const;
89-
int tempoSN() const { return m_tempoSN; }
77+
double tick2time(int tick) const;
78+
int time2tick(double time) const;
9079

9180
void setTempo(int t, BeatsPerSecond);
9281
void setPause(int t, double);
@@ -96,13 +85,10 @@ class TempoMap : public std::map<int, TEvent>
9685
bool setTempoMultiplier(BeatsPerSecond val);
9786

9887
private:
99-
10088
void normalize();
101-
void del(int tick);
10289

103-
int m_tempoSN = 0; // serial no to track tempo changes
104-
BeatsPerSecond m_tempo; // tempo if not using tempo list (beats per second)
105-
BeatsPerSecond m_tempoMultiplier;
90+
BeatsPerSecond m_tempo = 2.0; // tempo if not using tempo list (beats per second)
91+
BeatsPerSecond m_tempoMultiplier = 1.0;
10692

10793
std::unordered_map<int, double> m_pauses;
10894
};

0 commit comments

Comments
 (0)