Skip to content

Commit 14d9a0d

Browse files
committed
fix for #20
1 parent 33cacc3 commit 14d9a0d

File tree

4 files changed

+54
-60
lines changed

4 files changed

+54
-60
lines changed

ChangeLog

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2024-08-23
2+
* Fix for GH ticket #20
3+
14
2024-08-20
25
* Fix for GH ticket #22
36

seqplayer.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,14 @@ void SequencePlayer::playEvent(MIDIEvent* ev)
272272
void SequencePlayer::playerLoop()
273273
{
274274
using namespace std::chrono;
275-
typedef system_clock Clock;
275+
typedef steady_clock Clock;
276+
using TimePoint = Clock::time_point;
276277
static const std::type_info& beatId = typeid(BeatEvent);
277278
int currentBar{ 0 };
278279
long echoPosition{ 0 }, echoTicks{ 0 };
279-
milliseconds deltaTime{0}, echoDelta{ m_echoResolution };
280-
Clock::time_point currentTime{ Clock::now() },
281-
nextTime{ currentTime }, nextEcho{ currentTime };
280+
microseconds echoDelta{m_echoResolution}, eventTime{0};
281+
TimePoint currentTime{Clock::now()}, nextTime{currentTime}, nextEcho{currentTime},
282+
startTime{currentTime};
282283
emit songStarted();
283284
QAbstractEventDispatcher* dispatcher = thread()->eventDispatcher();
284285
QEventLoop::ProcessEventsFlags eventFilter = QEventLoop::ExcludeUserInputEvents;
@@ -298,9 +299,9 @@ void SequencePlayer::playerLoop()
298299
}
299300
}
300301
if (ev->delta() > 0) {
301-
deltaTime = m_song.deltaTimeOfEvent(ev);
302+
eventTime = m_song.timeOfEvent(ev);
302303
echoDelta = m_song.timeOfTicks(m_echoResolution);
303-
nextTime = currentTime + deltaTime;
304+
nextTime = startTime + eventTime;
304305
nextEcho = currentTime + echoDelta;
305306
echoPosition = m_songPosition;
306307
while (nextEcho < nextTime) {
@@ -315,10 +316,9 @@ void SequencePlayer::playerLoop()
315316
dispatcher->processEvents(eventFilter);
316317
std::this_thread::sleep_until(nextTime);
317318
echoTicks = ev->tick();
318-
m_songPosition += deltaTime.count();
319+
m_songPosition = eventTime.count();
319320
currentTime = Clock::now();
320-
emit songEchoTime(m_songPosition, ev->tick());
321-
//qDebug() << "echo:" << m_songPosition << ev->tick() << deltaTime.count();
321+
emit songEchoTime(m_songPosition, echoTicks);
322322
}
323323
playEvent(ev);
324324
}

sequence.cpp

+37-46
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,31 @@
2525
using namespace drumstick::File;
2626
using namespace drumstick::rt;
2727

28-
Sequence::Sequence(QObject *parent) : QObject(parent),
29-
m_rmid(nullptr),
30-
m_smf(nullptr),
31-
m_wrk(nullptr),
32-
m_uchardetErrors(0),
33-
m_format(0),
34-
m_numTracks(0),
35-
m_ticksDuration(0),
36-
m_division(-1),
37-
m_pos(0),
38-
m_curTrack(-1),
39-
m_beatMax(0),
40-
m_barCount(0),
41-
m_beatCount(0),
42-
m_lowestMidiNote(127),
43-
m_highestMidiNote(0),
44-
m_tempo(500000.0),
45-
m_tempoFactor(1.0),
46-
m_ticks2millis(0),
47-
m_duration(0),
48-
m_lastBeat(0),
49-
m_beatLength(0),
50-
m_tick(0),
51-
m_codec(nullptr)
28+
Sequence::Sequence(QObject *parent)
29+
: QObject(parent)
30+
, m_rmid(nullptr)
31+
, m_smf(nullptr)
32+
, m_wrk(nullptr)
33+
, m_uchardetErrors(0)
34+
, m_format(0)
35+
, m_numTracks(0)
36+
, m_ticksDuration(0)
37+
, m_division(-1)
38+
, m_pos(0)
39+
, m_curTrack(-1)
40+
, m_beatMax(0)
41+
, m_barCount(0)
42+
, m_beatCount(0)
43+
, m_lowestMidiNote(127)
44+
, m_highestMidiNote(0)
45+
, m_tempo(500000.0)
46+
, m_tempoFactor(1.0)
47+
, m_ticks2micros(0)
48+
, m_duration(0)
49+
, m_lastBeat(0)
50+
, m_beatLength(0)
51+
, m_tick(0)
52+
, m_codec(nullptr)
5253
{
5354
m_rmid = new Rmidi(this);
5455
connect(m_rmid, &Rmidi::signalRiffData, this, &Sequence::dataHandler);
@@ -530,8 +531,9 @@ int Sequence::trackChannel(int track) const
530531

531532
void Sequence::timeCalculations()
532533
{
533-
m_ticks2millis = m_tempo / (1000.0 * m_division * m_tempoFactor);
534-
//qDebug() << Q_FUNC_INFO << "tempo:" << m_tempo << "div:" << m_division << "ticks2millis:" << m_ticks2millis;
534+
m_ticks2micros = m_tempo / (m_division * m_tempoFactor);
535+
// qDebug() << Q_FUNC_INFO << "tempo:" << m_tempo << "div:" << m_division
536+
// << "ticks2micros:" << m_ticks2micros;
535537
}
536538

537539
qreal Sequence::tempoFactor() const
@@ -557,14 +559,19 @@ MIDIEvent *Sequence::nextEvent()
557559
return 0;
558560
}
559561

560-
std::chrono::milliseconds Sequence::deltaTimeOfEvent(MIDIEvent *ev) const
562+
std::chrono::microseconds Sequence::timeOfEvent(MIDIEvent *ev) const
561563
{
562-
return std::chrono::milliseconds(std::lround(ev->delta() * m_ticks2millis));
564+
return std::chrono::microseconds(static_cast<long>(ev->tick() * m_ticks2micros));
563565
}
564566

565-
std::chrono::milliseconds Sequence::timeOfTicks(const int ticks) const
567+
std::chrono::microseconds Sequence::deltaTimeOfEvent(MIDIEvent *ev) const
566568
{
567-
return std::chrono::milliseconds(std::lround(ticks * m_ticks2millis));
569+
return std::chrono::microseconds(static_cast<long>(ev->delta() * m_ticks2micros));
570+
}
571+
572+
std::chrono::microseconds Sequence::timeOfTicks(const int ticks) const
573+
{
574+
return std::chrono::microseconds(static_cast<long>(ticks * m_ticks2micros));
568575
}
569576

570577
bool Sequence::hasMoreEvents()
@@ -588,22 +595,6 @@ void Sequence::setTickPosition(long tick) {
588595
m_pos = m_list.count() -1 ;
589596
}
590597

591-
void Sequence::setTimePosition(long time) {
592-
long lastTime = 0;
593-
for(int i=0; i<m_list.count(); ++i) {
594-
MIDIEvent* ev = m_list[i];
595-
long deltaTicks = ev->delta();
596-
long deltaMillis = std::lround(m_ticks2millis * deltaTicks);
597-
long eventMillis = lastTime + deltaMillis;
598-
if (eventMillis > time) {
599-
m_pos = i > 0 ? i -1 : 0;
600-
return;
601-
}
602-
lastTime = eventMillis;
603-
}
604-
m_pos = m_list.count() -1 ;
605-
}
606-
607598
qreal Sequence::currentTempo() const
608599
{
609600
return m_tempo / m_tempoFactor;

sequence.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,20 @@ class Sequence : public QObject
6464
void setTempoFactor(const qreal factor);
6565
MIDIEvent *nextEvent();
6666
int eventTime(MIDIEvent* ev) const;
67-
std::chrono::milliseconds deltaTimeOfEvent(MIDIEvent* ev) const;
68-
std::chrono::milliseconds timeOfTicks(const int ticks) const;
67+
std::chrono::microseconds timeOfEvent(MIDIEvent *ev) const;
68+
std::chrono::microseconds deltaTimeOfEvent(MIDIEvent *ev) const;
69+
std::chrono::microseconds timeOfTicks(const int ticks) const;
6970
bool hasMoreEvents();
7071
int getFormat() const { return m_format; }
7172
int getDivision() const { return m_division; }
7273
bool isEmpty();
7374
void resetPosition();
7475
void setTickPosition(long tick);
75-
void setTimePosition(long time);
7676
qreal currentTempo() const;
7777
QString getName() const { return m_lblName; }
7878
int songLengthTicks() const;
7979
void updateTempo(qreal newTempo);
80-
qreal ticks2millis() const { return m_ticks2millis; }
80+
qreal ticks2micros() const { return m_ticks2micros; }
8181

8282
bool channelUsed(int channel);
8383
QString channelLabel(int channel);
@@ -220,7 +220,7 @@ public slots:
220220
int m_highestMidiNote;
221221
qreal m_tempo;
222222
qreal m_tempoFactor;
223-
qreal m_ticks2millis;
223+
qreal m_ticks2micros;
224224
qreal m_duration;
225225
qint64 m_lastBeat;
226226
qint64 m_beatLength;

0 commit comments

Comments
 (0)