Skip to content

Commit a8607ec

Browse files
committed
simple time processing optimization
when playing a song with single tempo, time calculation can be optimized for better accuracy.
1 parent 25b42c5 commit a8607ec

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

seqplayer.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -278,16 +278,18 @@ void SequencePlayer::playerLoop()
278278
using Clock = steady_clock;
279279
using TimePoint = Clock::time_point;
280280
static const std::type_info &beatId = typeid(BeatEvent);
281+
bool useSimpleTimeProcess = m_song.simpleTimeProcess();
281282
int currentBar{0};
282283
quint64 echoTicks{0};
283-
microseconds deltaTime{microseconds::zero()}, echoDelta{m_echoResolution};
284+
microseconds echoDelta{m_echoResolution};
284285
TimePoint currentTime{Clock::now()}, nextTime{currentTime}, nextEcho{currentTime},
285286
startTime{currentTime};
286287
emit songStarted();
287288
QAbstractEventDispatcher* dispatcher = thread()->eventDispatcher();
288289
QEventLoop::ProcessEventsFlags eventFilter = QEventLoop::ExcludeUserInputEvents;
289290
dispatcher->processEvents(eventFilter);
290291
m_songPositionTicks = 0;
292+
//qDebug() << "using simple time process: " << useSimpleTimeProcess
291293

292294
#ifdef WIN32
293295
timeBeginPeriod(1);
@@ -303,9 +305,12 @@ void SequencePlayer::playerLoop()
303305
}
304306
}
305307
if (ev->delta() > 0) {
306-
deltaTime = m_song.deltaTimeOfEvent(ev);
308+
if (useSimpleTimeProcess) {
309+
nextTime = startTime + m_song.timeOfEvent(ev);
310+
} else {
311+
nextTime = currentTime + m_song.deltaTimeOfEvent(ev);
312+
}
307313
echoDelta = m_song.timeOfTicks(m_echoResolution);
308-
nextTime = currentTime + deltaTime;
309314
nextEcho = currentTime + echoDelta;
310315
while (nextEcho < nextTime) {
311316
dispatcher->processEvents(eventFilter);

sequence.cpp

+28-10
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ void Sequence::clear()
292292
m_highestMidiNote = 0;
293293
m_curTrack = 0;
294294
m_duration = 0;
295-
m_initialTempo = 0;
296-
m_firstTime = true;
295+
m_initialTempo = 5e5;
296+
m_numTempoChanges = 0;
297297
m_codec = nullptr;
298298
m_infoMap.clear();
299299
for(int i=0; i<MIDI_STD_CHANNELS; ++i) {
@@ -313,10 +313,9 @@ void Sequence::clear()
313313
m_trkChannel.clear();
314314
m_currentFile.clear();
315315
m_currentFileFull.clear();
316-
while (!m_list.isEmpty()) {
317-
delete m_list.takeFirst();
318-
}
319316
m_loadingErrors.clear();
317+
qDeleteAll(m_list);
318+
m_list.clear();
320319
}
321320

322321
bool Sequence::isEmpty()
@@ -425,6 +424,22 @@ int Sequence::errorsCount() const
425424
return m_loadingErrors.size();
426425
}
427426

427+
qsizetype Sequence::size() const
428+
{
429+
return m_list.size();
430+
}
431+
432+
qreal Sequence::initialTempo() const
433+
{
434+
return m_initialTempo;
435+
}
436+
437+
bool Sequence::simpleTimeProcess() const
438+
{
439+
//qDebug() << Q_FUNC_INFO << m_numTempoChanges;
440+
return (m_numTempoChanges <= 1);
441+
}
442+
428443
QString Sequence::duration() const
429444
{
430445
std::chrono::microseconds us = timeOfTicks(m_ticksDuration);
@@ -623,13 +638,8 @@ int Sequence::songLengthTicks() const
623638
void Sequence::updateTempo(qreal newTempo)
624639
{
625640
if (m_tempo != newTempo) {
626-
//qDebug() << Q_FUNC_INFO << newTempo;
627641
m_tempo = newTempo;
628642
timeCalculations();
629-
if (m_firstTime) {
630-
m_initialTempo = newTempo;
631-
m_firstTime = false;
632-
};
633643
}
634644
}
635645

@@ -896,6 +906,10 @@ void Sequence::smfTempoEvent(int tempo)
896906
if (ev->tick() == 0) {
897907
updateTempo(tempo);
898908
}
909+
if (m_numTempoChanges == 0) {
910+
m_initialTempo = tempo;
911+
};
912+
m_numTempoChanges++;
899913
}
900914

901915
void Sequence::smfTimeSigEvent(int b0, int b1, int b2, int b3)
@@ -1231,6 +1245,10 @@ void Sequence::wrkTempoEvent(long time, int tempo)
12311245
if (time == 0) {
12321246
updateTempo(ev->tempo());
12331247
}
1248+
if (m_numTempoChanges == 0) {
1249+
m_initialTempo = ev->tempo();
1250+
};
1251+
m_numTempoChanges++;
12341252
}
12351253

12361254
void Sequence::wrkTrackPatch(int track, int patch)

sequence.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,9 @@ class Sequence : public QObject
124124
QString loadingErrors() const;
125125
int errorsCount() const;
126126

127-
qsizetype size() const { return m_list.size(); }
128-
qreal initialTempo() const { return m_initialTempo; }
127+
qsizetype size() const;
128+
qreal initialTempo() const;
129+
bool simpleTimeProcess() const;
129130
QString duration() const;
130131

131132
signals:
@@ -293,8 +294,8 @@ public slots:
293294
QMap<int, int> m_trkChannel;
294295
QMap<QString, QString> m_infoMap;
295296
QList<QString> m_loadingErrors;
296-
bool m_firstTime{true};
297-
qreal m_initialTempo{0};
297+
int m_numTempoChanges{0};
298+
qreal m_initialTempo{5e5};
298299
};
299300

300301
#endif // SEQUENCE_H

0 commit comments

Comments
 (0)