Skip to content

Commit

Permalink
Make findZone non-allocating (#1191)
Browse files Browse the repository at this point in the history
Closes #1146
  • Loading branch information
baconpaul authored Aug 26, 2024
1 parent 0f6cc9b commit c9d31d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
16 changes: 9 additions & 7 deletions src/engine/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,10 @@ struct Engine : MoveableOnly<Engine>, SampleRateSupport
int16_t key{-1};
int32_t noteid{-1};
};
std::vector<pathToZone_t> findZone(int16_t channel, int16_t key, int32_t noteId,
int16_t velocity)
size_t findZone(int16_t channel, int16_t key, int32_t noteId, int16_t velocity,
std::array<pathToZone_t, maxVoices> &res)
{
// TODO: This allocates which is a bummer
std::vector<pathToZone_t> res;
size_t idx{0};
for (const auto &[pidx, part] : sst::cpputils::enumerate(*patch))
{
if (!part->configuration.mute &&
Expand All @@ -147,14 +146,15 @@ struct Engine : MoveableOnly<Engine>, SampleRateSupport
if (zone->mapping.keyboardRange.includes(key) &&
zone->mapping.velocityRange.includes(velocity))
{
res.push_back(
{(size_t)pidx, (size_t)gidx, (size_t)zidx, channel, key, noteId});
res[idx] = {(size_t)pidx, (size_t)gidx, (size_t)zidx,
channel, key, noteId};
idx++;
}
}
}
}
}
return res;
return idx;
}

tuning::MidikeyRetuner midikeyRetuner;
Expand All @@ -168,6 +168,8 @@ struct Engine : MoveableOnly<Engine>, SampleRateSupport
struct VoiceManagerResponder
{
Engine &engine;
std::array<pathToZone_t, maxVoices> findZoneWorkingBuffer;

VoiceManagerResponder(Engine &e) : engine(e) {}

std::function<void(voice::Voice *)> voiceEndCallback{nullptr};
Expand Down
17 changes: 8 additions & 9 deletions src/engine/engine_voice_responder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,23 @@ int32_t Engine::VoiceManagerResponder::voiceCountForInitializationAction(
{
// TODO: We can optimize this so we don't have to find twice in the future
auto useKey = engine.midikeyRetuner.remapKeyTo(channel, key);
auto nts = engine.findZone(channel, useKey, noteId, std::clamp((int)(velocity * 128), 0, 127));
auto nts = engine.findZone(channel, useKey, noteId, std::clamp((int)(velocity * 128), 0, 127),
findZoneWorkingBuffer);

return nts.size();
return nts;
}

int32_t Engine::VoiceManagerResponder::initializeMultipleVoices(
std::array<voice::Voice *, VMConfig::maxVoiceCount> &voiceInitWorkingBuffer, uint16_t port,
uint16_t channel, uint16_t key, int32_t noteId, float velocity, float retune)
{
auto useKey = engine.midikeyRetuner.remapKeyTo(channel, key);
auto nts = engine.findZone(channel, useKey, noteId, std::clamp((int)(velocity * 128), 0, 127));
auto nts = engine.findZone(channel, useKey, noteId, std::clamp((int)(velocity * 128), 0, 127),
findZoneWorkingBuffer);

int idx{0};
for (const auto &path : nts)
for (auto idx = 0; idx < nts; ++idx)
{
const auto &path = findZoneWorkingBuffer[idx];
auto &z = engine.zoneByPath(path);
auto nbSampleLoadedInZone = z->getNumSampleLoaded();

Expand All @@ -64,7 +66,6 @@ int32_t Engine::VoiceManagerResponder::initializeMultipleVoices(
v->attack();
}
voiceInitWorkingBuffer[idx] = v;
idx++;
}
else if (z->sampleData.variantPlaybackMode == Zone::UNISON)
{
Expand All @@ -79,7 +80,6 @@ int32_t Engine::VoiceManagerResponder::initializeMultipleVoices(
v->attack();
}
voiceInitWorkingBuffer[idx] = v;
idx++;
}
}
else
Expand Down Expand Up @@ -152,12 +152,11 @@ int32_t Engine::VoiceManagerResponder::initializeMultipleVoices(
v->attack();
}
voiceInitWorkingBuffer[idx] = v;
idx++;
}
}
}
engine.midiNoteStateCounter++;
return idx;
return nts;
}

void Engine::VoiceManagerResponder::releaseVoice(voice::Voice *v, float velocity) { v->release(); }
Expand Down

0 comments on commit c9d31d6

Please sign in to comment.