Skip to content

Commit 4bcb6f7

Browse files
committed
not so great fix
1 parent bc0ada8 commit 4bcb6f7

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed

seqplayer.cpp

+19-13
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,19 @@ int SequencePlayer::loopStart() const
9090
SequencePlayer::SequencePlayer()
9191
: m_port(nullptr)
9292
, m_songPositionTicks(0)
93+
, m_echoResolution(50)
9394
, m_loopEnabled(false)
9495
, m_loopStart(0)
9596
, m_loopEnd(0)
96-
, m_echoResolution(50)
9797
, m_pitchShift(0)
9898
, m_volumeFactor(100)
9999
, m_latestBeat(nullptr)
100100
, m_firstBeat(nullptr)
101101
{
102+
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
103+
qRegisterMetaType<std::chrono::milliseconds>();
104+
qRegisterMetaType<std::uint64_t>();
105+
#endif
102106
initChannels();
103107
}
104108

@@ -131,12 +135,12 @@ void SequencePlayer::shutupSound()
131135

132136
void SequencePlayer::playEvent(MIDIEvent* ev)
133137
{
134-
static const std::type_info& textId = typeid(TextEvent);
135-
static const std::type_info& tempoId = typeid(TempoEvent);
136-
static const std::type_info& timeSigId = typeid(TimeSignatureEvent);
137-
static const std::type_info& keySigId = typeid(KeySignatureEvent);
138-
static const std::type_info& beatId = typeid(BeatEvent);
139-
static const std::type_info& sysexId = typeid (SysExEvent);
138+
static const std::type_info &textId = typeid(TextEvent);
139+
static const std::type_info &tempoId = typeid(TempoEvent);
140+
static const std::type_info &timeSigId = typeid(TimeSignatureEvent);
141+
static const std::type_info &keySigId = typeid(KeySignatureEvent);
142+
static const std::type_info &beatId = typeid(BeatEvent);
143+
static const std::type_info &sysexId = typeid(SysExEvent);
140144

141145
if (m_port == nullptr)
142146
return;
@@ -274,10 +278,10 @@ void SequencePlayer::playerLoop()
274278
using namespace std::chrono;
275279
using Clock = steady_clock;
276280
using TimePoint = Clock::time_point;
277-
static const std::type_info& beatId = typeid(BeatEvent);
281+
static const std::type_info &beatId = typeid(BeatEvent);
278282
int currentBar{0};
279283
std::uint64_t echoTicks{0};
280-
microseconds echoDelta{m_echoResolution}, eventTime{0};
284+
microseconds deltaTime{microseconds::zero()}, echoDelta{m_echoResolution};
281285
TimePoint currentTime{Clock::now()}, nextTime{currentTime}, nextEcho{currentTime},
282286
startTime{currentTime};
283287
emit songStarted();
@@ -300,9 +304,9 @@ void SequencePlayer::playerLoop()
300304
}
301305
}
302306
if (ev->delta() > 0) {
303-
eventTime = m_song.timeOfEvent(ev);
307+
deltaTime = m_song.deltaTimeOfEvent(ev);
304308
echoDelta = m_song.timeOfTicks(m_echoResolution);
305-
nextTime = startTime + eventTime;
309+
nextTime = currentTime + deltaTime;
306310
nextEcho = currentTime + echoDelta;
307311
while (nextEcho < nextTime) {
308312
dispatcher->processEvents(eventFilter);
@@ -318,7 +322,8 @@ void SequencePlayer::playerLoop()
318322
echoTicks = ev->tick();
319323
m_songPositionTicks = echoTicks;
320324
currentTime = Clock::now();
321-
emit songEchoTime(duration_cast<milliseconds>(eventTime), echoTicks);
325+
emit songEchoTime(duration_cast<milliseconds>(m_song.timeOfTicks(echoTicks)),
326+
echoTicks);
322327
}
323328
playEvent(ev);
324329
}
@@ -333,7 +338,8 @@ void SequencePlayer::playerLoop()
333338

334339
emit songStopped();
335340
if (!m_song.hasMoreEvents()) {
336-
//qDebug() << "Final Song Position:" << m_songPosition;
341+
qDebug() << "Final Song Position:" << m_songPositionTicks
342+
<< (currentTime - startTime).count() / 1e9;
337343
emit songFinished();
338344
}
339345
dispatcher->processEvents(eventFilter);

seqplayer.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
#include "sequence.h"
2727
#include "events.h"
2828

29+
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
30+
Q_DECLARE_METATYPE(std::chrono::milliseconds)
31+
Q_DECLARE_METATYPE(std::uint64_t)
32+
#endif
33+
2934
class SequencePlayer : public QObject
3035
{
3136
Q_OBJECT
@@ -118,10 +123,10 @@ private slots:
118123
Sequence m_song;
119124
drumstick::rt::MIDIOutput* m_port;
120125
std::uint64_t m_songPositionTicks;
126+
std::uint64_t m_echoResolution;
121127
bool m_loopEnabled;
122128
int m_loopStart;
123129
int m_loopEnd;
124-
int m_echoResolution;
125130
int m_pitchShift;
126131
int m_volumeFactor;
127132
int m_volume[drumstick::rt::MIDI_STD_CHANNELS];

sequence.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,8 @@ int Sequence::trackChannel(int track) const
534534
void Sequence::timeCalculations()
535535
{
536536
m_ticks2micros = m_tempo / (m_division * m_tempoFactor);
537-
qDebug() << Q_FUNC_INFO << "tempo:" << m_tempo << "div:" << m_division
538-
<< "ticks2micros:" << m_ticks2micros;
537+
// qDebug() << Q_FUNC_INFO << "tempo:" << m_tempo << "div:" << m_division
538+
// << "ticks2micros:" << m_ticks2micros;
539539
}
540540

541541
qreal Sequence::tempoFactor() const
@@ -571,7 +571,7 @@ std::chrono::microseconds Sequence::deltaTimeOfEvent(MIDIEvent *ev) const
571571
return std::chrono::microseconds(static_cast<std::uint64_t>(ev->delta() * m_ticks2micros));
572572
}
573573

574-
std::chrono::microseconds Sequence::timeOfTicks(const int ticks) const
574+
std::chrono::microseconds Sequence::timeOfTicks(const std::uint64_t ticks) const
575575
{
576576
return std::chrono::microseconds(static_cast<std::uint64_t>(ticks * m_ticks2micros));
577577
}
@@ -610,7 +610,7 @@ int Sequence::songLengthTicks() const
610610
void Sequence::updateTempo(qreal newTempo)
611611
{
612612
if (m_tempo != newTempo) {
613-
qDebug() << Q_FUNC_INFO << newTempo;
613+
//qDebug() << Q_FUNC_INFO << newTempo;
614614
m_tempo = newTempo;
615615
timeCalculations();
616616
}

sequence.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Sequence : public QObject
6969
int eventTime(MIDIEvent* ev) const;
7070
std::chrono::microseconds timeOfEvent(MIDIEvent *ev) const;
7171
std::chrono::microseconds deltaTimeOfEvent(MIDIEvent *ev) const;
72-
std::chrono::microseconds timeOfTicks(const int ticks) const;
72+
std::chrono::microseconds timeOfTicks(const uint64_t ticks) const;
7373
bool hasMoreEvents();
7474
int getFormat() const { return m_format; }
7575
int getDivision() const { return m_division; }

0 commit comments

Comments
 (0)