Skip to content

Commit

Permalink
not so great fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrolcl committed Aug 24, 2024
1 parent bc0ada8 commit 4bcb6f7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
32 changes: 19 additions & 13 deletions seqplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,19 @@ int SequencePlayer::loopStart() const
SequencePlayer::SequencePlayer()
: m_port(nullptr)
, m_songPositionTicks(0)
, m_echoResolution(50)
, m_loopEnabled(false)
, m_loopStart(0)
, m_loopEnd(0)
, m_echoResolution(50)
, m_pitchShift(0)
, m_volumeFactor(100)
, m_latestBeat(nullptr)
, m_firstBeat(nullptr)
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
qRegisterMetaType<std::chrono::milliseconds>();
qRegisterMetaType<std::uint64_t>();
#endif
initChannels();
}

Expand Down Expand Up @@ -131,12 +135,12 @@ void SequencePlayer::shutupSound()

void SequencePlayer::playEvent(MIDIEvent* ev)
{
static const std::type_info& textId = typeid(TextEvent);
static const std::type_info& tempoId = typeid(TempoEvent);
static const std::type_info& timeSigId = typeid(TimeSignatureEvent);
static const std::type_info& keySigId = typeid(KeySignatureEvent);
static const std::type_info& beatId = typeid(BeatEvent);
static const std::type_info& sysexId = typeid (SysExEvent);
static const std::type_info &textId = typeid(TextEvent);
static const std::type_info &tempoId = typeid(TempoEvent);
static const std::type_info &timeSigId = typeid(TimeSignatureEvent);
static const std::type_info &keySigId = typeid(KeySignatureEvent);
static const std::type_info &beatId = typeid(BeatEvent);
static const std::type_info &sysexId = typeid(SysExEvent);

if (m_port == nullptr)
return;
Expand Down Expand Up @@ -274,10 +278,10 @@ void SequencePlayer::playerLoop()
using namespace std::chrono;
using Clock = steady_clock;
using TimePoint = Clock::time_point;
static const std::type_info& beatId = typeid(BeatEvent);
static const std::type_info &beatId = typeid(BeatEvent);
int currentBar{0};
std::uint64_t echoTicks{0};
microseconds echoDelta{m_echoResolution}, eventTime{0};
microseconds deltaTime{microseconds::zero()}, echoDelta{m_echoResolution};
TimePoint currentTime{Clock::now()}, nextTime{currentTime}, nextEcho{currentTime},
startTime{currentTime};
emit songStarted();
Expand All @@ -300,9 +304,9 @@ void SequencePlayer::playerLoop()
}
}
if (ev->delta() > 0) {
eventTime = m_song.timeOfEvent(ev);
deltaTime = m_song.deltaTimeOfEvent(ev);
echoDelta = m_song.timeOfTicks(m_echoResolution);
nextTime = startTime + eventTime;
nextTime = currentTime + deltaTime;
nextEcho = currentTime + echoDelta;
while (nextEcho < nextTime) {
dispatcher->processEvents(eventFilter);
Expand All @@ -318,7 +322,8 @@ void SequencePlayer::playerLoop()
echoTicks = ev->tick();
m_songPositionTicks = echoTicks;
currentTime = Clock::now();
emit songEchoTime(duration_cast<milliseconds>(eventTime), echoTicks);
emit songEchoTime(duration_cast<milliseconds>(m_song.timeOfTicks(echoTicks)),
echoTicks);
}
playEvent(ev);
}
Expand All @@ -333,7 +338,8 @@ void SequencePlayer::playerLoop()

emit songStopped();
if (!m_song.hasMoreEvents()) {
//qDebug() << "Final Song Position:" << m_songPosition;
qDebug() << "Final Song Position:" << m_songPositionTicks
<< (currentTime - startTime).count() / 1e9;
emit songFinished();
}
dispatcher->processEvents(eventFilter);
Expand Down
7 changes: 6 additions & 1 deletion seqplayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
#include "sequence.h"
#include "events.h"

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Q_DECLARE_METATYPE(std::chrono::milliseconds)
Q_DECLARE_METATYPE(std::uint64_t)
#endif

class SequencePlayer : public QObject
{
Q_OBJECT
Expand Down Expand Up @@ -118,10 +123,10 @@ private slots:
Sequence m_song;
drumstick::rt::MIDIOutput* m_port;
std::uint64_t m_songPositionTicks;
std::uint64_t m_echoResolution;
bool m_loopEnabled;
int m_loopStart;
int m_loopEnd;
int m_echoResolution;
int m_pitchShift;
int m_volumeFactor;
int m_volume[drumstick::rt::MIDI_STD_CHANNELS];
Expand Down
8 changes: 4 additions & 4 deletions sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,8 @@ int Sequence::trackChannel(int track) const
void Sequence::timeCalculations()
{
m_ticks2micros = m_tempo / (m_division * m_tempoFactor);
qDebug() << Q_FUNC_INFO << "tempo:" << m_tempo << "div:" << m_division
<< "ticks2micros:" << m_ticks2micros;
// qDebug() << Q_FUNC_INFO << "tempo:" << m_tempo << "div:" << m_division
// << "ticks2micros:" << m_ticks2micros;
}

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

std::chrono::microseconds Sequence::timeOfTicks(const int ticks) const
std::chrono::microseconds Sequence::timeOfTicks(const std::uint64_t ticks) const
{
return std::chrono::microseconds(static_cast<std::uint64_t>(ticks * m_ticks2micros));
}
Expand Down Expand Up @@ -610,7 +610,7 @@ int Sequence::songLengthTicks() const
void Sequence::updateTempo(qreal newTempo)
{
if (m_tempo != newTempo) {
qDebug() << Q_FUNC_INFO << newTempo;
//qDebug() << Q_FUNC_INFO << newTempo;
m_tempo = newTempo;
timeCalculations();
}
Expand Down
2 changes: 1 addition & 1 deletion sequence.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Sequence : public QObject
int eventTime(MIDIEvent* ev) const;
std::chrono::microseconds timeOfEvent(MIDIEvent *ev) const;
std::chrono::microseconds deltaTimeOfEvent(MIDIEvent *ev) const;
std::chrono::microseconds timeOfTicks(const int ticks) const;
std::chrono::microseconds timeOfTicks(const uint64_t ticks) const;
bool hasMoreEvents();
int getFormat() const { return m_format; }
int getDivision() const { return m_division; }
Expand Down

0 comments on commit 4bcb6f7

Please sign in to comment.