Skip to content

Commit

Permalink
fix for #20
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrolcl committed Aug 22, 2024
1 parent 33cacc3 commit 14d9a0d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 60 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
2024-08-23
* Fix for GH ticket #20

2024-08-20
* Fix for GH ticket #22

Expand Down
18 changes: 9 additions & 9 deletions seqplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,14 @@ void SequencePlayer::playEvent(MIDIEvent* ev)
void SequencePlayer::playerLoop()
{
using namespace std::chrono;
typedef system_clock Clock;
typedef steady_clock Clock;
using TimePoint = Clock::time_point;
static const std::type_info& beatId = typeid(BeatEvent);
int currentBar{ 0 };
long echoPosition{ 0 }, echoTicks{ 0 };
milliseconds deltaTime{0}, echoDelta{ m_echoResolution };
Clock::time_point currentTime{ Clock::now() },
nextTime{ currentTime }, nextEcho{ currentTime };
microseconds echoDelta{m_echoResolution}, eventTime{0};
TimePoint currentTime{Clock::now()}, nextTime{currentTime}, nextEcho{currentTime},
startTime{currentTime};
emit songStarted();
QAbstractEventDispatcher* dispatcher = thread()->eventDispatcher();
QEventLoop::ProcessEventsFlags eventFilter = QEventLoop::ExcludeUserInputEvents;
Expand All @@ -298,9 +299,9 @@ void SequencePlayer::playerLoop()
}
}
if (ev->delta() > 0) {
deltaTime = m_song.deltaTimeOfEvent(ev);
eventTime = m_song.timeOfEvent(ev);
echoDelta = m_song.timeOfTicks(m_echoResolution);
nextTime = currentTime + deltaTime;
nextTime = startTime + eventTime;
nextEcho = currentTime + echoDelta;
echoPosition = m_songPosition;
while (nextEcho < nextTime) {
Expand All @@ -315,10 +316,9 @@ void SequencePlayer::playerLoop()
dispatcher->processEvents(eventFilter);
std::this_thread::sleep_until(nextTime);
echoTicks = ev->tick();
m_songPosition += deltaTime.count();
m_songPosition = eventTime.count();
currentTime = Clock::now();
emit songEchoTime(m_songPosition, ev->tick());
//qDebug() << "echo:" << m_songPosition << ev->tick() << deltaTime.count();
emit songEchoTime(m_songPosition, echoTicks);
}
playEvent(ev);
}
Expand Down
83 changes: 37 additions & 46 deletions sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,31 @@
using namespace drumstick::File;
using namespace drumstick::rt;

Sequence::Sequence(QObject *parent) : QObject(parent),
m_rmid(nullptr),
m_smf(nullptr),
m_wrk(nullptr),
m_uchardetErrors(0),
m_format(0),
m_numTracks(0),
m_ticksDuration(0),
m_division(-1),
m_pos(0),
m_curTrack(-1),
m_beatMax(0),
m_barCount(0),
m_beatCount(0),
m_lowestMidiNote(127),
m_highestMidiNote(0),
m_tempo(500000.0),
m_tempoFactor(1.0),
m_ticks2millis(0),
m_duration(0),
m_lastBeat(0),
m_beatLength(0),
m_tick(0),
m_codec(nullptr)
Sequence::Sequence(QObject *parent)
: QObject(parent)
, m_rmid(nullptr)
, m_smf(nullptr)
, m_wrk(nullptr)
, m_uchardetErrors(0)
, m_format(0)
, m_numTracks(0)
, m_ticksDuration(0)
, m_division(-1)
, m_pos(0)
, m_curTrack(-1)
, m_beatMax(0)
, m_barCount(0)
, m_beatCount(0)
, m_lowestMidiNote(127)
, m_highestMidiNote(0)
, m_tempo(500000.0)
, m_tempoFactor(1.0)
, m_ticks2micros(0)
, m_duration(0)
, m_lastBeat(0)
, m_beatLength(0)
, m_tick(0)
, m_codec(nullptr)
{
m_rmid = new Rmidi(this);
connect(m_rmid, &Rmidi::signalRiffData, this, &Sequence::dataHandler);
Expand Down Expand Up @@ -530,8 +531,9 @@ int Sequence::trackChannel(int track) const

void Sequence::timeCalculations()
{
m_ticks2millis = m_tempo / (1000.0 * m_division * m_tempoFactor);
//qDebug() << Q_FUNC_INFO << "tempo:" << m_tempo << "div:" << m_division << "ticks2millis:" << m_ticks2millis;
m_ticks2micros = m_tempo / (m_division * m_tempoFactor);
// qDebug() << Q_FUNC_INFO << "tempo:" << m_tempo << "div:" << m_division
// << "ticks2micros:" << m_ticks2micros;
}

qreal Sequence::tempoFactor() const
Expand All @@ -557,14 +559,19 @@ MIDIEvent *Sequence::nextEvent()
return 0;
}

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

std::chrono::milliseconds Sequence::timeOfTicks(const int ticks) const
std::chrono::microseconds Sequence::deltaTimeOfEvent(MIDIEvent *ev) const
{
return std::chrono::milliseconds(std::lround(ticks * m_ticks2millis));
return std::chrono::microseconds(static_cast<long>(ev->delta() * m_ticks2micros));
}

std::chrono::microseconds Sequence::timeOfTicks(const int ticks) const
{
return std::chrono::microseconds(static_cast<long>(ticks * m_ticks2micros));
}

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

void Sequence::setTimePosition(long time) {
long lastTime = 0;
for(int i=0; i<m_list.count(); ++i) {
MIDIEvent* ev = m_list[i];
long deltaTicks = ev->delta();
long deltaMillis = std::lround(m_ticks2millis * deltaTicks);
long eventMillis = lastTime + deltaMillis;
if (eventMillis > time) {
m_pos = i > 0 ? i -1 : 0;
return;
}
lastTime = eventMillis;
}
m_pos = m_list.count() -1 ;
}

qreal Sequence::currentTempo() const
{
return m_tempo / m_tempoFactor;
Expand Down
10 changes: 5 additions & 5 deletions sequence.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,20 @@ class Sequence : public QObject
void setTempoFactor(const qreal factor);
MIDIEvent *nextEvent();
int eventTime(MIDIEvent* ev) const;
std::chrono::milliseconds deltaTimeOfEvent(MIDIEvent* ev) const;
std::chrono::milliseconds timeOfTicks(const int ticks) const;
std::chrono::microseconds timeOfEvent(MIDIEvent *ev) const;
std::chrono::microseconds deltaTimeOfEvent(MIDIEvent *ev) const;
std::chrono::microseconds timeOfTicks(const int ticks) const;
bool hasMoreEvents();
int getFormat() const { return m_format; }
int getDivision() const { return m_division; }
bool isEmpty();
void resetPosition();
void setTickPosition(long tick);
void setTimePosition(long time);
qreal currentTempo() const;
QString getName() const { return m_lblName; }
int songLengthTicks() const;
void updateTempo(qreal newTempo);
qreal ticks2millis() const { return m_ticks2millis; }
qreal ticks2micros() const { return m_ticks2micros; }

bool channelUsed(int channel);
QString channelLabel(int channel);
Expand Down Expand Up @@ -220,7 +220,7 @@ public slots:
int m_highestMidiNote;
qreal m_tempo;
qreal m_tempoFactor;
qreal m_ticks2millis;
qreal m_ticks2micros;
qreal m_duration;
qint64 m_lastBeat;
qint64 m_beatLength;
Expand Down

0 comments on commit 14d9a0d

Please sign in to comment.