Skip to content

Commit

Permalink
optimization: lazy update of main stream events
Browse files Browse the repository at this point in the history
Significantly reduces CPU load during note input
  • Loading branch information
RomanPudashkin committed May 20, 2024
1 parent 262808d commit 3c5074a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
27 changes: 26 additions & 1 deletion src/framework/audio/internal/abstracteventsequencer.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,22 @@ class AbstractEventSequencer : public async::Asyncable

m_mainStreamChanges = data.mainStream;
m_offStreamChanges = data.offStream;
m_playbackEventMap = data.originEvents;
m_dynamicLevelMap = data.dynamicLevelMap;
m_playbackParamMap = data.paramMap;

m_mainStreamChanges.onReceive(this,
[this](const mpe::PlaybackEventsMap& events, const mpe::DynamicLevelMap& dynamics,
const mpe::PlaybackParamMap& params) {
updateMainStreamEvents(events, dynamics, params);
m_playbackEventMap = events;
m_dynamicLevelMap = dynamics;
m_playbackParamMap = params;
m_shouldUpdateMainStreamEvents = true;

if (m_isActive) {
updateMainStreamEvents(events, dynamics, params);
m_shouldUpdateMainStreamEvents = false;
}
});

m_offStreamChanges.onReceive(this, [this](const mpe::PlaybackEventsMap& events, const mpe::PlaybackParamMap& params) {
Expand All @@ -76,7 +87,16 @@ class AbstractEventSequencer : public async::Asyncable

void setActive(const bool active)
{
if (m_isActive == active) {
return;
}

m_isActive = active;

if (m_isActive && m_shouldUpdateMainStreamEvents) {
updateMainStreamEvents(m_playbackEventMap, m_dynamicLevelMap, m_playbackParamMap);
m_shouldUpdateMainStreamEvents = false;
}
}

bool isActive() const
Expand Down Expand Up @@ -230,7 +250,9 @@ class AbstractEventSequencer : public async::Asyncable
EventSequenceMap m_offStreamEvents;
EventSequenceMap m_dynamicEvents;

mpe::PlaybackEventsMap m_playbackEventMap;
mpe::DynamicLevelMap m_dynamicLevelMap;
mpe::PlaybackParamMap m_playbackParamMap;

bool m_isActive = false;

Expand All @@ -239,6 +261,9 @@ class AbstractEventSequencer : public async::Asyncable

OnFlushedCallback m_onOffStreamFlushed;
OnFlushedCallback m_onMainStreamFlushed;

private:
bool m_shouldUpdateMainStreamEvents = false;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ void FluidSequencer::updateOffStreamEvents(const mpe::PlaybackEventsMap& events,
void FluidSequencer::updateMainStreamEvents(const mpe::PlaybackEventsMap& events, const mpe::DynamicLevelMap& dynamics,
const mpe::PlaybackParamMap&)
{
m_dynamicLevelMap = dynamics;

m_mainStreamEvents.clear();
m_dynamicEvents.clear();

Expand Down
7 changes: 1 addition & 6 deletions src/framework/vst/internal/synth/vstsequencer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ void VstSequencer::init(ParamsMapping&& mapping)
m_mapping = std::move(mapping);
m_inited = true;

updateMainStreamEvents(m_playbackEventsMap, m_dynamicLevelMap, {});

m_playbackEventsMap.clear();
updateMainStreamEvents(m_playbackEventMap, m_dynamicLevelMap, m_playbackParamMap);
}

void VstSequencer::updateOffStreamEvents(const mpe::PlaybackEventsMap& events, const mpe::PlaybackParamMap&)
Expand All @@ -68,10 +66,7 @@ void VstSequencer::updateOffStreamEvents(const mpe::PlaybackEventsMap& events, c
void VstSequencer::updateMainStreamEvents(const mpe::PlaybackEventsMap& events, const mpe::DynamicLevelMap& dynamics,
const mpe::PlaybackParamMap&)
{
m_dynamicLevelMap = dynamics;

if (!m_inited) {
m_playbackEventsMap = events;
return;
}

Expand Down
1 change: 0 additions & 1 deletion src/framework/vst/internal/synth/vstsequencer.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ class VstSequencer : public muse::audio::AbstractEventSequencer<VstEvent, Plugin

bool m_inited = false;
ParamsMapping m_mapping;
mpe::PlaybackEventsMap m_playbackEventsMap;
};
}

Expand Down

0 comments on commit 3c5074a

Please sign in to comment.