Skip to content

Commit

Permalink
update routings right after track engine tick
Browse files Browse the repository at this point in the history
fixes #167
  • Loading branch information
westlicht committed Apr 29, 2020
1 parent 3b2cd9b commit 36865fa
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 13 deletions.
7 changes: 6 additions & 1 deletion src/apps/sequencer/engine/CurveTrackEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void CurveTrackEngine::restart() {
_currentStepFraction = 0.f;
}

void CurveTrackEngine::tick(uint32_t tick) {
TrackEngine::TickResult CurveTrackEngine::tick(uint32_t tick) {
ASSERT(_sequence != nullptr, "invalid sequence");
const auto &sequence = *_sequence;
const auto *linkData = _linkedTrackEngine ? _linkedTrackEngine->linkData() : nullptr;
Expand Down Expand Up @@ -106,13 +106,18 @@ void CurveTrackEngine::tick(uint32_t tick) {
_linkData.sequenceState = &_sequenceState;
}

TickResult result = TickResult::NoUpdate;

while (!_gateQueue.empty() && tick >= _gateQueue.front().tick) {
result |= TickResult::GateUpdate;
_activity = _gateQueue.front().gate;
_gateOutput = (!mute() || fill()) && _activity;
_gateQueue.pop();

_engine.midiOutputEngine().sendGate(_track.trackIndex(), _gateOutput);
}

return result;
}

void CurveTrackEngine::update(float dt) {
Expand Down
2 changes: 1 addition & 1 deletion src/apps/sequencer/engine/CurveTrackEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CurveTrackEngine : public TrackEngine {

virtual void reset() override;
virtual void restart() override;
virtual void tick(uint32_t tick) override;
virtual TickResult tick(uint32_t tick) override;
virtual void update(float dt) override;

virtual void changePattern() override;
Expand Down
13 changes: 11 additions & 2 deletions src/apps/sequencer/engine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,17 @@ void Engine::update() {
// update play state
updatePlayState(true);

for (auto trackEngine : _trackEngines) {
trackEngine->tick(tick);
// tick track engines
for (size_t trackIndex = 0; trackIndex < CONFIG_TRACK_COUNT; ++trackIndex) {
auto &trackEngine = _trackEngines[trackIndex];
uint32_t result = trackEngine->tick(tick);
// update track outputs and routings if tick results in updating the track's CV output
if (result &= TrackEngine::TickResult::CvUpdate && _trackUpdateReducers[trackIndex].update()) {
trackEngine->update(0.f);
updateTrackOutputs();
updateOverrides();
_routingEngine.update();
}
}

// update midi outputs, force sending CC on first tick
Expand Down
3 changes: 3 additions & 0 deletions src/apps/sequencer/engine/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "MidiPort.h"
#include "MidiLearn.h"
#include "CvGateToMidiConverter.h"
#include "UpdateReducer.h"

#include "model/Model.h"

Expand All @@ -35,6 +36,7 @@ class Engine : private Clock::Listener {
typedef Container<NoteTrackEngine, CurveTrackEngine, MidiCvTrackEngine> TrackEngineContainer;
typedef std::array<TrackEngineContainer, CONFIG_TRACK_COUNT> TrackEngineContainerArray;
typedef std::array<TrackEngine *, CONFIG_TRACK_COUNT> TrackEngineArray;
typedef std::array<UpdateReducer<os::time::ms(25)>, CONFIG_TRACK_COUNT> TrackUpdateReducerArray;

typedef std::function<bool(MidiPort port, const MidiMessage &message)> MidiReceiveHandler;

Expand Down Expand Up @@ -193,6 +195,7 @@ class Engine : private Clock::Listener {

TrackEngineContainerArray _trackEngineContainers;
TrackEngineArray _trackEngines;
TrackUpdateReducerArray _trackUpdateReducers;

MidiOutputEngine _midiOutputEngine;

Expand Down
4 changes: 3 additions & 1 deletion src/apps/sequencer/engine/MidiCvTrackEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ void MidiCvTrackEngine::reset() {
void MidiCvTrackEngine::restart() {
}

void MidiCvTrackEngine::tick(uint32_t tick) {
TrackEngine::TickResult MidiCvTrackEngine::tick(uint32_t tick) {
if (_arpeggiatorEnabled) {
tickArpeggiator(tick);
}

return TickResult::NoUpdate;
}

void MidiCvTrackEngine::update(float dt) {
Expand Down
2 changes: 1 addition & 1 deletion src/apps/sequencer/engine/MidiCvTrackEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class MidiCvTrackEngine : public TrackEngine {

virtual void reset() override;
virtual void restart() override;
virtual void tick(uint32_t tick) override;
virtual TickResult tick(uint32_t tick) override;
virtual void update(float dt) override;

virtual bool receiveMidi(MidiPort port, const MidiMessage &message) override;
Expand Down
8 changes: 7 additions & 1 deletion src/apps/sequencer/engine/NoteTrackEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void NoteTrackEngine::restart() {
_currentStep = -1;
}

void NoteTrackEngine::tick(uint32_t tick) {
TrackEngine::TickResult NoteTrackEngine::tick(uint32_t tick) {
ASSERT(_sequence != nullptr, "invalid sequence");
const auto &sequence = *_sequence;
const auto *linkData = _linkedTrackEngine ? _linkedTrackEngine->linkData() : nullptr;
Expand Down Expand Up @@ -156,7 +156,10 @@ void NoteTrackEngine::tick(uint32_t tick) {

auto &midiOutputEngine = _engine.midiOutputEngine();

TickResult result = TickResult::NoUpdate;

while (!_gateQueue.empty() && tick >= _gateQueue.front().tick) {
result |= TickResult::GateUpdate;
_activity = _gateQueue.front().gate;
_gateOutput = (!mute() || fill()) && _activity;
_gateQueue.pop();
Expand All @@ -166,6 +169,7 @@ void NoteTrackEngine::tick(uint32_t tick) {

while (!_cvQueue.empty() && tick >= _cvQueue.front().tick) {
if (!mute() || _noteTrack.cvUpdateMode() == NoteTrack::CvUpdateMode::Always) {
result |= TickResult::CvUpdate;
_cvOutputTarget = _cvQueue.front().cv;
_slideActive = _cvQueue.front().slide;

Expand All @@ -174,6 +178,8 @@ void NoteTrackEngine::tick(uint32_t tick) {
}
_cvQueue.pop();
}

return result;
}

void NoteTrackEngine::update(float dt) {
Expand Down
2 changes: 1 addition & 1 deletion src/apps/sequencer/engine/NoteTrackEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class NoteTrackEngine : public TrackEngine {

virtual void reset() override;
virtual void restart() override;
virtual void tick(uint32_t tick) override;
virtual TickResult tick(uint32_t tick) override;
virtual void update(float dt) override;

virtual void changePattern() override;
Expand Down
12 changes: 11 additions & 1 deletion src/apps/sequencer/engine/TrackEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "model/Model.h"

#include "core/midi/MidiMessage.h"
#include "core/utils/EnumUtils.h"

#include <cstdint>

Expand All @@ -28,6 +29,13 @@ struct TrackLinkData {

class TrackEngine {
public:
// Set of updates resulting from calling tick().
enum TickResult {
NoUpdate = 0,
CvUpdate = (1<<0),
GateUpdate = (1<<1),
};

TrackEngine(Engine &engine, const Model &model, Track &track, const TrackEngine *linkedTrackEngine) :
_engine(engine),
_model(model),
Expand Down Expand Up @@ -61,7 +69,7 @@ class TrackEngine {

virtual void reset() = 0;
virtual void restart() = 0;
virtual void tick(uint32_t tick) = 0;
virtual TickResult tick(uint32_t tick) = 0;
virtual void update(float dt) = 0;

virtual void changePattern() {}
Expand Down Expand Up @@ -98,4 +106,6 @@ class TrackEngine {
const TrackEngine *_linkedTrackEngine;
};

ENUM_CLASS_OPERATORS(TrackEngine::TickResult)

#undef SANITIZE_TRACK_MODE
21 changes: 21 additions & 0 deletions src/apps/sequencer/engine/UpdateReducer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "os/os.h"

#include <cstdint>

template<uint32_t Interval>
class UpdateReducer {
public:
bool update() {
uint32_t currentTick = os::ticks();
if (currentTick >= _lastUpdate + Interval) {
_lastUpdate = currentTick;
return true;
}
return false;
}

private:
uint32_t _lastUpdate = 0;
};
8 changes: 8 additions & 0 deletions src/core/utils/EnumUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#define ENUM_CLASS_OPERATORS(e_) \
inline e_ operator&(e_ a, e_ b) { return static_cast<e_>(static_cast<int>(a) & static_cast<int>(b)); } \
inline e_ operator|(e_ a, e_ b) { return static_cast<e_>(static_cast<int>(a) | static_cast<int>(b)); } \
inline e_ &operator|=(e_& a, e_ b) { a = a | b; return a; }; \
inline e_ &operator&=(e_& a, e_ b) { a = a & b; return a; }; \
inline e_ operator~(e_ a) { return static_cast<e_>(~static_cast<int>(a)); }
4 changes: 2 additions & 2 deletions src/platform/sim/os/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ namespace os {
};

namespace time {
inline uint32_t us(uint32_t us) {
constexpr inline uint32_t us(uint32_t us) {
return us / 1000;
}
inline uint32_t ms(uint32_t ms) {
constexpr inline uint32_t ms(uint32_t ms) {
return ms;
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/platform/stm32/os/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,10 @@ namespace os {
};

namespace time {
inline uint32_t us(uint32_t us) {
constexpr inline uint32_t us(uint32_t us) {
return (us * configTICK_RATE_HZ) / 1000000;
}
inline uint32_t ms(uint32_t ms) {
constexpr inline uint32_t ms(uint32_t ms) {
return (ms * configTICK_RATE_HZ) / 1000;
}
};
Expand Down

0 comments on commit 36865fa

Please sign in to comment.