Skip to content

Commit

Permalink
clang_formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
tremblap committed Aug 27, 2024
1 parent a2d385e commit 536215d
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 154 deletions.
89 changes: 52 additions & 37 deletions include/algorithms/public/VoiceAllocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ class VoiceAllocator

public:
VoiceAllocator(index nVoices, Allocator& alloc)
: mTracking(alloc), mVoices{ nVoices },
mFreeVoices(alloc), mActiveVoices(alloc),
mActiveVoiceData(0, alloc)
: mTracking(alloc), mVoices{nVoices}, mFreeVoices(alloc),
mActiveVoices(alloc), mActiveVoiceData(0, alloc)
{}

void init(index nVoices, Allocator& alloc)
Expand All @@ -34,12 +33,17 @@ class VoiceAllocator
while (!mFreeVoices.empty()) { mFreeVoices.pop(); }
for (index i = 0; i < nVoices; ++i) { mFreeVoices.push(i); }
mActiveVoiceData.resize(nVoices);
for (VoicePeak each : mActiveVoiceData) { each = { 0, 0, 0 }; }
for (VoicePeak each : mActiveVoiceData) { each = {0, 0, 0}; }
mTracking.init();
mInitialized = true;
}

void processFrame(vector<SinePeak> incomingVoices, vector<VoicePeak>& outgoingVoices, index minTrackLen, double birthLowTreshold, double birthHighTreshold, index trackMethod, double trackMagRange, double trackFreqRange, double trackProb, index sortMethod, Allocator& alloc)
void processFrame(vector<SinePeak> incomingVoices,
vector<VoicePeak>& outgoingVoices, index minTrackLen,
double birthLowTreshold, double birthHighTreshold,
index trackMethod, double trackMagRange,
double trackFreqRange, double trackProb, index sortMethod,
Allocator& alloc)
{
assert(mInitialized);

Expand All @@ -49,100 +53,111 @@ class VoiceAllocator
if (voice.logMag > maxAmp) { maxAmp = voice.logMag; }
}

mTracking.processFrame(incomingVoices, maxAmp, minTrackLen, birthLowTreshold, birthHighTreshold, trackMethod, trackMagRange, trackFreqRange, trackProb, alloc);
mTracking.processFrame(incomingVoices, maxAmp, minTrackLen,
birthLowTreshold, birthHighTreshold, trackMethod,
trackMagRange, trackFreqRange, trackProb, alloc);

outgoingVoices = mTracking.getActiveVoices(alloc);
outgoingVoices = sortVoices(outgoingVoices, sortMethod);
if (outgoingVoices.size() > mVoices)
outgoingVoices.resize(mVoices);
if (outgoingVoices.size() > mVoices) outgoingVoices.resize(mVoices);
outgoingVoices = assignVoices(outgoingVoices, alloc);

mTracking.prune();
}

void reset() {mInitialized = false;}
void reset() { mInitialized = false; }

bool initialized() const { return mInitialized; }

private:

vector<VoicePeak> sortVoices(vector<VoicePeak>& incomingVoices, index sortingMethod)
vector<VoicePeak> sortVoices(vector<VoicePeak>& incomingVoices,
index sortingMethod)
{
switch (sortingMethod)
{
case 0: //lowest
case 0: // lowest
std::sort(incomingVoices.begin(), incomingVoices.end(),
[](const VoicePeak& voice1, const VoicePeak& voice2)
{ return voice1.freq < voice2.freq; });
[](const VoicePeak& voice1, const VoicePeak& voice2) {
return voice1.freq < voice2.freq;
});
break;
case 1: //loudest
case 1: // loudest
std::sort(incomingVoices.begin(), incomingVoices.end(),
[](const VoicePeak& voice1, const VoicePeak& voice2)
{ return voice1.logMag > voice2.logMag; });
[](const VoicePeak& voice1, const VoicePeak& voice2) {
return voice1.logMag > voice2.logMag;
});
break;
}
return incomingVoices;
}

vector<VoicePeak> assignVoices(vector<VoicePeak>& incomingVoices, Allocator& alloc)
vector<VoicePeak> assignVoices(vector<VoicePeak>& incomingVoices,
Allocator& alloc)
{
//move released to free
// move released to free
for (index existing = 0; existing < mActiveVoiceData.size(); ++existing)
{
if (mActiveVoiceData[existing].state == algorithm::VoiceState::kReleaseState)
if (mActiveVoiceData[existing].state ==
algorithm::VoiceState::kReleaseState)
mActiveVoiceData[existing].state = algorithm::VoiceState::kFreeState;
}

//handle existing voices - killing or sustaining
// handle existing voices - killing or sustaining
for (index existing = 0; existing < mActiveVoices.size(); ++existing)
{
bool killVoice = true;
for (index incoming = 0; incoming < incomingVoices.size(); ++incoming)
{
//remove incoming voice events & allows corresponding voice to live if it already exists
if (mActiveVoiceData[mActiveVoices[existing]].voiceID == incomingVoices[incoming].voiceID)
// remove incoming voice events & allows corresponding voice to live if
// it already exists
if (mActiveVoiceData[mActiveVoices[existing]].voiceID ==
incomingVoices[incoming].voiceID)
{
killVoice = false;
mActiveVoiceData[mActiveVoices[existing]] = incomingVoices[incoming]; //update freq/mag
mActiveVoiceData[mActiveVoices[existing]].state = algorithm::VoiceState::kSustainState;
mActiveVoiceData[mActiveVoices[existing]] =
incomingVoices[incoming]; // update freq/mag
mActiveVoiceData[mActiveVoices[existing]].state =
algorithm::VoiceState::kSustainState;
incomingVoices.erase(incomingVoices.begin() + incoming);
break;
}
}
if (killVoice) //voice off
if (killVoice) // voice off
{
mActiveVoiceData[mActiveVoices[existing]].state = algorithm::VoiceState::kReleaseState;
mActiveVoiceData[mActiveVoices[existing]].state =
algorithm::VoiceState::kReleaseState;
mFreeVoices.push(mActiveVoices[existing]);
mActiveVoices.erase(mActiveVoices.begin() + existing);
--existing;
}
}

//handle new voice allocation
// handle new voice allocation
for (index incoming = 0; incoming < incomingVoices.size(); ++incoming)
{
if (!mFreeVoices.empty()) //voice on
if (!mFreeVoices.empty()) // voice on
{
index newVoiceIndex = mFreeVoices.front();
mFreeVoices.pop();
mActiveVoices.push_back(newVoiceIndex);
algorithm::VoiceState prevState = mActiveVoiceData[newVoiceIndex].state;
mActiveVoiceData[newVoiceIndex] = incomingVoices[incoming];
if (prevState == algorithm::VoiceState::kReleaseState) //mark as stolen
mActiveVoiceData[newVoiceIndex].state = algorithm::VoiceState::kStolenState;
if (prevState == algorithm::VoiceState::kReleaseState) // mark as stolen
mActiveVoiceData[newVoiceIndex].state =
algorithm::VoiceState::kStolenState;
}
}

return mActiveVoiceData;
}

PartialTracking mTracking;
index mVoices;
rt::queue<index> mFreeVoices;
rt::deque<index> mActiveVoices;
vector<VoicePeak> mActiveVoiceData;
PartialTracking mTracking;
index mVoices;
rt::queue<index> mFreeVoices;
rt::deque<index> mActiveVoices;
vector<VoicePeak> mActiveVoiceData;

bool mInitialized{ false };
bool mInitialized{false};
};
} // namespace algorithm
} // namespace fluid
59 changes: 30 additions & 29 deletions include/algorithms/util/PartialTracking.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@ Capability through Linear Programming". Proceedings of DAFx-2018.
namespace fluid {
namespace algorithm {

enum class VoiceState
{
kFreeState,
kAttackState,
kSustainState,
kReleaseState,
kStolenState
enum class VoiceState {
kFreeState,
kAttackState,
kSustainState,
kReleaseState,
kStolenState
};

struct SinePeak
Expand All @@ -43,10 +42,10 @@ struct SinePeak

struct VoicePeak
{
double freq;
double logMag;
index voiceID;
VoiceState state;
double freq;
double logMag;
index voiceID;
VoiceState state;
};

struct SineTrack
Expand All @@ -56,8 +55,8 @@ struct SineTrack

SineTrack(rt::vector<SinePeak>&& p, index s, index e, bool a, bool ass,
index t)
: peaks{p}, startFrame{s}, endFrame{e}, active{a}, assigned{ass}, trackId{
t}
: peaks{p}, startFrame{s}, endFrame{e}, active{a}, assigned{ass},
trackId{t}
{}

rt::vector<SinePeak> peaks;
Expand Down Expand Up @@ -154,22 +153,24 @@ class PartialTracking
// todo - refactor this function with the one above
vector<VoicePeak> getActiveVoices(Allocator& alloc)
{
vector<VoicePeak> voicePeaks(0, alloc);
index latencyFrame = mCurrentFrame - mMinTrackLength;
if (latencyFrame < 0) return voicePeaks;
for (auto&& track : mTracks)
{
if (track.startFrame > latencyFrame) continue;
if (track.endFrame >= 0 && track.endFrame <= latencyFrame) continue;
if (track.endFrame >= 0 &&
track.endFrame - track.startFrame < mMinTrackLength)
continue;
voicePeaks.push_back({track.peaks[asUnsigned(latencyFrame - track.startFrame)].freq,
pow(10, track.peaks[asUnsigned(latencyFrame - track.startFrame)].logMag / 20),
track.trackId,
VoiceState::kAttackState});
}
return voicePeaks;
vector<VoicePeak> voicePeaks(0, alloc);
index latencyFrame = mCurrentFrame - mMinTrackLength;
if (latencyFrame < 0) return voicePeaks;
for (auto&& track : mTracks)
{
if (track.startFrame > latencyFrame) continue;
if (track.endFrame >= 0 && track.endFrame <= latencyFrame) continue;
if (track.endFrame >= 0 &&
track.endFrame - track.startFrame < mMinTrackLength)
continue;
voicePeaks.push_back(
{track.peaks[asUnsigned(latencyFrame - track.startFrame)].freq,
pow(10,
track.peaks[asUnsigned(latencyFrame - track.startFrame)].logMag /
20),
track.trackId, VoiceState::kAttackState});
}
return voicePeaks;
}

private:
Expand Down
Loading

0 comments on commit 536215d

Please sign in to comment.