Skip to content

Commit

Permalink
feat: add linked gain knob to metronome effect
Browse files Browse the repository at this point in the history
  • Loading branch information
Swiftb0y committed Oct 10, 2024
1 parent 128c440 commit b10d4b1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/effects/backends/builtin/metronomeeffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@

#include "audio/types.h"
#include "effects/backends/effectmanifest.h"
#include "effects/backends/effectmanifestparameter.h"
#include "engine/effects/engineeffectparameter.h"
#include "engine/engine.h"
#include "metronomeclick.h"
#include "util/math.h"
#include "util/sample.h"
#include "util/types.h"

namespace {

std::size_t playMonoSamples(std::span<const CSAMPLE> monoSource, std::span<CSAMPLE> output) {
std::size_t playMonoSamplesWithGain(std::span<const CSAMPLE> monoSource,
std::span<CSAMPLE> output,
CSAMPLE_GAIN gain) {
const std::size_t outputBufferFrames = output.size() / mixxx::kEngineChannelOutputCount;
std::size_t framesPlayed = std::min(monoSource.size(), outputBufferFrames);
SampleUtil::addMonoToStereo(output.data(), monoSource.data(), framesPlayed);
SampleUtil::addMonoToStereoWithGain(gain, output.data(), monoSource.data(), framesPlayed);
return framesPlayed;
}

Expand Down Expand Up @@ -93,13 +97,27 @@ EffectManifestPointer MetronomeEffect::getManifest() {
periodUnit->setUnitsHint(EffectManifestParameter::UnitsHint::Unknown);
periodUnit->setRange(0, 1, 1);

EffectManifestParameterPointer gain = pManifest->addParameter();
gain->setId(QStringLiteral("gain"));
gain->setName(QObject::tr("Gain"));
gain->setDescription(QObject::tr(
"Set the gain of metronome click sound"));
gain->setValueScaler(EffectManifestParameter::ValueScaler::Linear);
gain->setUnitsHint(EffectManifestParameter::UnitsHint::Decibel);
gain->setDefaultLinkType(EffectManifestParameter::LinkType::Linked);
gain->setRange(-24.0, 0.0, 3.0); // decibel
// 0db on the range above, assumes scale is linear default=(max-min)x+min (solve for x)
// TODO: move this generally to ControlPotmeterBehavior?
gain->setNeutralPointOnScale(24.0 / 27.0);

return pManifest;
}

void MetronomeEffect::loadEngineEffectParameters(
const QMap<QString, EngineEffectParameterPointer>& parameters) {
m_pBpmParameter = parameters.value(QStringLiteral("bpm"));
m_pSyncParameter = parameters.value(QStringLiteral("sync"));
m_pGainParameter = parameters.value(QStringLiteral("gain"));
}

void MetronomeEffect::processChannel(
Expand Down Expand Up @@ -137,7 +155,9 @@ void MetronomeEffect::processChannel(
}
}

playMonoSamples(subspan_clamped(click, gs->framesSinceLastClick), output);
const CSAMPLE_GAIN gain = db2ratio(static_cast<float>(m_pGainParameter->value()));

playMonoSamplesWithGain(subspan_clamped(click, gs->framesSinceLastClick), output, gain);
gs->framesSinceLastClick += engineParameters.framesPerBuffer();

std::span<CSAMPLE> outputBufferOffset = [&] {
Expand All @@ -156,6 +176,6 @@ void MetronomeEffect::processChannel(
}();

if (!outputBufferOffset.empty()) {
gs->framesSinceLastClick = playMonoSamples(click, outputBufferOffset);
gs->framesSinceLastClick = playMonoSamplesWithGain(click, outputBufferOffset, gain);
}
}
1 change: 1 addition & 0 deletions src/effects/backends/builtin/metronomeeffect.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class MetronomeEffect : public EffectProcessorImpl<MetronomeGroupState> {
private:
EngineEffectParameterPointer m_pBpmParameter;
EngineEffectParameterPointer m_pSyncParameter;
EngineEffectParameterPointer m_pGainParameter;

DISALLOW_COPY_AND_ASSIGN(MetronomeEffect);
};

0 comments on commit b10d4b1

Please sign in to comment.