Skip to content

Commit 7493cf5

Browse files
juli27Jojo-Schmitz
authored andcommitted
Small cleanup in TempoMap
Backport of musescore#32090
1 parent 9033744 commit 7493cf5

File tree

4 files changed

+42
-144
lines changed

4 files changed

+42
-144
lines changed

libmscore/pos.cpp

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ Pos::Pos()
2828
_type = TType::TICKS;
2929
_tick = 0;
3030
_frame = 0;
31-
sn = -1;
3231
_valid = false;
3332
}
3433

@@ -39,7 +38,6 @@ Pos::Pos(TempoMap* tl, TimeSigMap* sl)
3938
_type = TType::TICKS;
4039
_tick = 0;
4140
_frame = 0;
42-
sn = -1;
4341
_valid = false;
4442
}
4543

@@ -52,7 +50,6 @@ Pos::Pos(TempoMap* tl, TimeSigMap* sl, unsigned t, TType timeType)
5250
_tick = t;
5351
else
5452
_frame = t;
55-
sn = -1;
5653
_valid = true;
5754
}
5855

@@ -65,7 +62,6 @@ Pos::Pos(TempoMap* tl, TimeSigMap* sl, const QString& s)
6562
_tick = sig->bar2tick(m, b) + t;
6663
_type = TType::TICKS;
6764
_frame = 0;
68-
sn = -1;
6965
_valid = true;
7066
}
7167

@@ -76,7 +72,6 @@ Pos::Pos(TempoMap* tl, TimeSigMap* sl, int measure, int beat, int tick)
7672
_tick = sig->bar2tick(measure, beat) + tick;
7773
_type = TType::TICKS;
7874
_frame = 0;
79-
sn = -1;
8075
_valid = true;
8176
}
8277

@@ -104,7 +99,6 @@ Pos::Pos(TempoMap* tl, TimeSigMap* sl, int min, int sec, int frame, int subframe
10499
_type = TType::FRAMES;
105100
_tick = 0;
106101
_frame = (int)lrint(time * MScore::sampleRate);
107-
sn = -1;
108102
_valid = true;
109103
}
110104

@@ -119,11 +113,11 @@ void Pos::setType(TType t)
119113

120114
if (_type == TType::TICKS) {
121115
// convert from ticks to frames
122-
_frame = tempo->tick2time(_tick, _frame, &sn) * MScore::sampleRate;
116+
_frame = tempo->tick2time(_tick) * MScore::sampleRate;
123117
}
124118
else {
125119
// convert from frames to ticks
126-
_tick = tempo->time2tick(_frame / MScore::sampleRate, _tick, &sn);
120+
_tick = tempo->time2tick(_frame / MScore::sampleRate);
127121
}
128122
_type = t;
129123
}
@@ -138,7 +132,6 @@ Pos& Pos::operator+=(const Pos& a)
138132
_frame += a.frame();
139133
else
140134
_tick += a.tick();
141-
sn = -1; // invalidate cached values
142135
return *this;
143136
}
144137

@@ -152,7 +145,6 @@ Pos& Pos::operator-=(const Pos& a)
152145
_frame -= a.frame();
153146
else
154147
_tick -= a.tick();
155-
sn = -1; // invalidate cached values
156148
return *this;
157149
}
158150

@@ -166,7 +158,6 @@ Pos& Pos::operator+=(int a)
166158
_frame += a;
167159
else
168160
_tick += a;
169-
sn = -1; // invalidate cached values
170161
return *this;
171162
}
172163

@@ -180,7 +171,6 @@ Pos& Pos::operator-=(int a)
180171
_frame -= a;
181172
else
182173
_tick -= a;
183-
sn = -1; // invalidate cached values
184174
return *this;
185175
}
186176

@@ -263,7 +253,7 @@ bool Pos::operator!=(const Pos& s) const
263253
unsigned Pos::tick() const
264254
{
265255
if (_type == TType::FRAMES)
266-
_tick = tempo->time2tick(_frame / MScore::sampleRate, _tick, &sn);
256+
_tick = tempo->time2tick(_frame / MScore::sampleRate);
267257
return _tick;
268258
}
269259

@@ -288,9 +278,8 @@ unsigned Pos::frame() const
288278
void Pos::setTick(unsigned pos)
289279
{
290280
_tick = pos;
291-
sn = -1;
292281
if (_type == TType::FRAMES)
293-
_frame = tempo->tick2time(pos, &sn) * MScore::sampleRate;
282+
_frame = tempo->tick2time(pos) * MScore::sampleRate;
294283
_valid = true;
295284
}
296285

@@ -301,9 +290,8 @@ void Pos::setTick(unsigned pos)
301290
void Pos::setFrame(unsigned pos)
302291
{
303292
_frame = pos;
304-
sn = -1;
305293
if (_type == TType::TICKS)
306-
_tick = tempo->time2tick(pos/MScore::sampleRate, &sn);
294+
_tick = tempo->time2tick(pos/MScore::sampleRate);
307295
_valid = true;
308296
}
309297

@@ -325,7 +313,6 @@ void Pos::write(XmlWriter& xml, const char* name) const
325313

326314
void Pos::read(XmlReader& e)
327315
{
328-
sn = -1;
329316
if (e.hasAttribute("tick")) {
330317
_tick = e.intAttribute("tick");
331318
_type = TType::TICKS;
@@ -376,7 +363,7 @@ void PosLen::dump(int n) const
376363

377364
void Pos::dump(int /*n*/) const
378365
{
379-
qDebug("Pos(%s, sn=%d, ", type() == TType::FRAMES ? "Frames" : "Ticks", sn);
366+
qDebug("Pos(%s, ", type() == TType::FRAMES ? "Frames" : "Ticks");
380367
switch(type()) {
381368
case TType::FRAMES:
382369
qDebug("samples=%d)", _frame);
@@ -431,7 +418,7 @@ void PosLen::setLenTick(unsigned len)
431418
_lenTick = len;
432419
sn = -1;
433420
if (type() == TType::FRAMES)
434-
_lenFrame = tempo->tick2time(len, &sn) * MScore::sampleRate;
421+
_lenFrame = tempo->tick2time(len) * MScore::sampleRate;
435422
else
436423
_lenTick = len;
437424
}
@@ -444,7 +431,7 @@ void PosLen::setLenFrame(unsigned len)
444431
{
445432
sn = -1;
446433
if (type() == TType::TICKS)
447-
_lenTick = tempo->time2tick(len/MScore::sampleRate, &sn);
434+
_lenTick = tempo->time2tick(len/MScore::sampleRate);
448435
else
449436
_lenFrame = len;
450437
}
@@ -456,7 +443,7 @@ void PosLen::setLenFrame(unsigned len)
456443
unsigned PosLen::lenTick() const
457444
{
458445
if (type() == TType::FRAMES)
459-
_lenTick = tempo->time2tick(_lenFrame/MScore::sampleRate, _lenTick, &sn);
446+
_lenTick = tempo->time2tick(_lenFrame/MScore::sampleRate);
460447
return _lenTick;
461448
}
462449

@@ -467,7 +454,7 @@ unsigned PosLen::lenTick() const
467454
unsigned PosLen::lenFrame() const
468455
{
469456
if (type() == TType::TICKS)
470-
_lenFrame = tempo->tick2time(_lenTick, _lenFrame, &sn) * MScore::sampleRate;
457+
_lenFrame = tempo->tick2time(_lenTick) * MScore::sampleRate;
471458
return _lenFrame;
472459
}
473460

@@ -478,7 +465,6 @@ unsigned PosLen::lenFrame() const
478465
Pos PosLen::end() const
479466
{
480467
Pos pos(*this);
481-
pos.invalidSn();
482468
switch(type()) {
483469
case TType::FRAMES:
484470
pos.setFrame(pos.frame() + _lenFrame);

libmscore/pos.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ enum class TType : char { TICKS, FRAMES };
4242
class Pos {
4343
TType _type;
4444
bool _valid;
45-
mutable int sn;
4645
mutable unsigned _tick;
4746
mutable unsigned _frame;
4847

@@ -72,8 +71,6 @@ class Pos {
7271
Pos upSnapped(int) const;
7372
Pos downSnapped(int) const;
7473

75-
void invalidSn() { sn = -1; }
76-
7774
TType type() const { return _type; }
7875
void setType(TType t);
7976

libmscore/tempo.cpp

Lines changed: 20 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -14,51 +14,16 @@
1414
#include "tempo.h"
1515

1616
namespace Ms {
17-
18-
//---------------------------------------------------------
19-
// TEvent
20-
//---------------------------------------------------------
21-
22-
TEvent::TEvent()
23-
{
24-
type = TempoType::INVALID;
25-
tempo = 0.0;
26-
pause = 0.0;
27-
time = 0.0;
28-
}
29-
30-
TEvent::TEvent(const TEvent& e)
31-
{
32-
type = e.type;
33-
tempo = e.tempo;
34-
pause = e.pause;
35-
time = e.time;
36-
}
37-
38-
TEvent::TEvent(qreal t, qreal p, TempoType tp)
17+
TEvent::TEvent(const qreal t, const double p, const TempoType tp)
18+
: type(tp), tempo(t), pause(p)
3919
{
40-
type = tp;
41-
tempo = t;
42-
pause = p;
43-
time = 0.0;
4420
}
4521

4622
bool TEvent::valid() const
4723
{
4824
return !(!type);
4925
}
5026

51-
//---------------------------------------------------------
52-
// TempoMap
53-
//---------------------------------------------------------
54-
55-
TempoMap::TempoMap()
56-
{
57-
_tempo = 2.0; // default fixed tempo in beat per second
58-
_tempoSN = 1;
59-
_relTempo = 1.0;
60-
}
61-
6227
//---------------------------------------------------------
6328
// setPause
6429
//---------------------------------------------------------
@@ -114,7 +79,6 @@ void TempoMap::normalize()
11479
tick = e->first;
11580
tempo = e->second.tempo;
11681
}
117-
++_tempoSN;
11882
}
11983

12084
//---------------------------------------------------------
@@ -136,7 +100,6 @@ void TempoMap::dump() const
136100
void TempoMap::clear()
137101
{
138102
std::map<int,TEvent>::clear();
139-
++_tempoSN;
140103
}
141104

142105
//---------------------------------------------------------
@@ -152,7 +115,6 @@ void TempoMap::clearRange(int tick1, int tick2)
152115
if (first == last)
153116
return;
154117
erase(first, last);
155-
++_tempoSN;
156118
}
157119

158120
//---------------------------------------------------------
@@ -176,25 +138,6 @@ qreal TempoMap::tempo(int tick) const
176138
return i->second.tempo;
177139
}
178140

179-
//---------------------------------------------------------
180-
// del
181-
//---------------------------------------------------------
182-
183-
void TempoMap::del(int tick)
184-
{
185-
auto e = find(tick);
186-
if (e == end()) {
187-
qDebug("TempoMap::del event at (%d): not found", tick);
188-
// abort();
189-
return;
190-
}
191-
// don't delete event if still being used for pause
192-
if (e->second.type & TempoType::PAUSE)
193-
e->second.type = TempoType::PAUSE;
194-
else
195-
erase(e);
196-
normalize();
197-
}
198141

199142
//---------------------------------------------------------
200143
// setRelTempo
@@ -212,34 +155,21 @@ void TempoMap::setRelTempo(qreal val)
212155

213156
void TempoMap::delTempo(int tick)
214157
{
215-
del(tick);
216-
++_tempoSN;
217-
}
218-
219-
//---------------------------------------------------------
220-
// tick2time
221-
//---------------------------------------------------------
222-
223-
qreal TempoMap::tick2time(int tick, qreal time, int* sn) const
224-
{
225-
return (*sn == _tempoSN) ? time : tick2time(tick, sn);
226-
}
227-
228-
//---------------------------------------------------------
229-
// time2tick
230-
// return cached value t if list did not change
231-
//---------------------------------------------------------
232-
233-
int TempoMap::time2tick(qreal time, int t, int* sn) const
234-
{
235-
return (*sn == _tempoSN) ? t : time2tick(time, sn);
158+
auto e = find(tick);
159+
if (e == end()) {
160+
qDebug("TempoMap::del event at (%d): not found", tick);
161+
// abort();
162+
return;
163+
}
164+
// don't delete event if still being used for pause
165+
if (e->second.type & TempoType::PAUSE)
166+
e->second.type = TempoType::PAUSE;
167+
else
168+
erase(e);
169+
normalize();
236170
}
237171

238-
//---------------------------------------------------------
239-
// tick2time
240-
//---------------------------------------------------------
241-
242-
qreal TempoMap::tick2time(int tick, int* sn) const
172+
qreal TempoMap::tick2time(int tick) const
243173
{
244174
qreal time = 0.0;
245175
qreal delta = qreal(tick);
@@ -271,8 +201,7 @@ qreal TempoMap::tick2time(int tick, int* sn) const
271201
}
272202
else
273203
qDebug("TempoMap: empty");
274-
if (sn)
275-
*sn = _tempoSN;
204+
276205
time += delta / (MScore::division * tempo * _relTempo);
277206
return time;
278207
}
@@ -281,14 +210,12 @@ qreal TempoMap::tick2time(int tick, int* sn) const
281210
// time2tick
282211
//---------------------------------------------------------
283212

284-
int TempoMap::time2tick(qreal time, int* sn) const
213+
int TempoMap::time2tick(qreal time) const
285214
{
286215
int tick = 0;
287-
qreal delta = time;
288-
qreal tempo = _tempo;
216+
qreal delta = 0.0;
217+
qreal tempo = 2.0;
289218

290-
delta = 0.0;
291-
tempo = 2.0;
292219
for (auto e = begin(); e != end(); ++e) {
293220
// if in a pause period, wait on previous tick
294221
if ((time <= e->second.time) && (time > e->second.time - e->second.pause)) {
@@ -303,8 +230,7 @@ int TempoMap::time2tick(qreal time, int* sn) const
303230
}
304231
delta = time - delta;
305232
tick += (int)lrint(delta * _relTempo * MScore::division * tempo);
306-
if (sn)
307-
*sn = _tempoSN;
233+
308234
return tick;
309235
}
310236

0 commit comments

Comments
 (0)