Skip to content

Commit

Permalink
Add function to Zone to normalize samples
Browse files Browse the repository at this point in the history
  • Loading branch information
Jengamon committed Aug 23, 2024
1 parent aadb560 commit 2f26c0e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/engine/zone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

#include "sst/basic-blocks/mechanics/block-ops.h"
#include "group_and_zone_impl.h"
#include "dsp/sample_analytics.h"

namespace scxt::engine
{
Expand Down Expand Up @@ -154,6 +155,24 @@ void Zone::removeVoice(voice::Voice *v)
assert(false);
}

void Zone::setNormalizedSampleLevel(const bool usePeak, const int associatedSampleID)
{
const auto startSample = (associatedSampleID < 0) ? 0 : associatedSampleID;
const auto endSample = (associatedSampleID < 0) ? maxSamplesPerZone : associatedSampleID;

for (auto i = startSample; i < endSample; ++i)
{
if (sampleData.samples[i].active && samplePointers[i])
{
auto normVal = usePeak ? dsp::sample_analytics::computePeak(samplePointers[i])
: dsp::sample_analytics::computeRMS(samplePointers[i]);
// convert linear measure into db
// To undo this, std::pow(amp / 10.f, 10.f)
sampleData.samples[i].normalizationAmplitude = 10.f * std::log10f(normVal);
}
}
}

engine::Engine *Zone::getEngine()
{
if (parentGroup && parentGroup->parentPart && parentGroup->parentPart->parentPatch)
Expand Down
5 changes: 3 additions & 2 deletions src/engine/zone.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ struct Zone : MoveableOnly<Zone>, HasGroupZoneProcessors<Zone>, SampleRateSuppor
int loopCountWhenCounted{0};

int64_t loopFade{0};
float normalizationAmplitude{1.f}; // db
float normalizationAmplitude{0.f}; // db
// per-sample pitch and amplitude
float pitchOffset{0.f}; // semitones
float amplitude{1.f}; // db
float amplitude{0.f}; // db

bool operator==(const AssociatedSample &other) const
{
Expand Down Expand Up @@ -199,6 +199,7 @@ struct Zone : MoveableOnly<Zone>, HasGroupZoneProcessors<Zone>, SampleRateSuppor

return attachToSample(manager, variation);
}
void setNormalizedSampleLevel(bool usePeak = false, int associatedSampleID = -1);

struct ZoneMappingData
{
Expand Down

0 comments on commit 2f26c0e

Please sign in to comment.