Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a correlated noise voice effect generator #45

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions include/sst/voice-effects/generator/GenCorrelatedNoise.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* sst-effects - an open source library of audio effects
* built by Surge Synth Team.
*
* Copyright 2018-2023, various authors, as described in the GitHub
* transaction log.
*
* sst-effects is released under the GNU General Public Licence v3
* or later (GPL-3.0-or-later). The license is found in the "LICENSE"
* file in the root of this repository, or at
* https://www.gnu.org/licenses/gpl-3.0.en.html
*
* The majority of these effects at initiation were factored from
* Surge XT, and so git history prior to April 2023 is found in the
* surge repo, https://github.com/surge-synthesizer/surge
*
* All source in sst-effects available at
* https://github.com/surge-synthesizer/sst-effects
*/

#ifndef INCLUDE_SST_VOICE_EFFECTS_GENERATOR_GENCORRELATEDNOISE_H
#define INCLUDE_SST_VOICE_EFFECTS_GENERATOR_GENCORRELATEDNOISE_H

#include "sst/basic-blocks/params/ParamMetadata.h"
#include "sst/basic-blocks/dsp/BlockInterpolators.h"
#include "sst/basic-blocks/dsp/CorrelatedNoise.h"

#include "../VoiceEffectCore.h"

#include <iostream>
#include <random>

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

namespace sst::voice_effects::generator
{
template <typename VFXConfig> struct GenCorrelatedNoise : core::VoiceEffectTemplateBase<VFXConfig>
{
static constexpr const char *effectName{"Generate Sin"};

enum struct GenCorrelatedNoiseFloatParams : uint32_t
{
color,
level,
num_params
};

enum struct GenCorrelatedNoiseIntParams : uint32_t
{
num_params
};

static constexpr int numFloatParams{(int)GenCorrelatedNoiseFloatParams::num_params};
static constexpr int numIntParams{(int)GenCorrelatedNoiseIntParams::num_params};

// provide a function which is uniform bipolar float -1.f .. 1.f random values
GenCorrelatedNoise()
: core::VoiceEffectTemplateBase<VFXConfig>(), mGenerator((size_t)this), mDistro(-1.f, 1.f)
{
// Warm up
for (int i = 0; i < 7; ++i)
{
sst::basic_blocks::dsp::correlated_noise_o2mk2_supplied_value(mPrior[0], mPrior[1], 0,
mDistro(mGenerator));
}
}

~GenCorrelatedNoise() {}

basic_blocks::params::ParamMetaData paramAt(int idx) const
{
assert(idx >= 0 && idx < (int)GenCorrelatedNoiseFloatParams::num_params);
using pmd = basic_blocks::params::ParamMetaData;

switch ((GenCorrelatedNoiseFloatParams)idx)
{
case GenCorrelatedNoiseFloatParams::color:
return pmd().asPercentBipolar().withName("Color");
case GenCorrelatedNoiseFloatParams::level:
return pmd().asCubicDecibelAttenuation().withDefault(1.f).withName("Level");
default:
break;
}

return pmd().withName("Unknown " + std::to_string(idx)).asPercent();
}

void initVoiceEffect() {}
void initVoiceEffectParams() { this->initToParamMetadataDefault(this); }

void processStereo(float *datainL, float *datainR, float *dataoutL, float *dataoutR,
float pitch)
{
auto levT =
std::clamp(this->getFloatParam((int)GenCorrelatedNoiseFloatParams::level), 0.f, 1.f);
levT = levT * levT * levT;
auto col =
std::clamp(this->getFloatParam((int)GenCorrelatedNoiseFloatParams::color), -1.f, 1.f);
mLevelLerp.set_target(levT);
mColorLerp.newValue(col);
for (int k = 0; k < VFXConfig::blockSize; k++)
{
dataoutL[k] = sst::basic_blocks::dsp::correlated_noise_o2mk2_supplied_value(
mPrior[0], mPrior[1], mColorLerp.v, mDistro(mGenerator));
dataoutR[k] = sst::basic_blocks::dsp::correlated_noise_o2mk2_supplied_value(
mPrior[0], mPrior[1], mColorLerp.v, mDistro(mGenerator));
mColorLerp.process();
}
mLevelLerp.multiply_2_blocks(dataoutL, dataoutR);
}

void processMonoToMono(float *datainL, float *dataoutL, float pitch)
{
auto levT =
std::clamp(this->getFloatParam((int)GenCorrelatedNoiseFloatParams::level), 0.f, 1.f);
levT = levT * levT * levT;
mLevelLerp.set_target(levT);
auto col =
std::clamp(this->getFloatParam((int)GenCorrelatedNoiseFloatParams::color), -1.f, 1.f);
mColorLerp.newValue(col);

for (int k = 0; k < VFXConfig::blockSize; k++)
{
dataoutL[k] = sst::basic_blocks::dsp::correlated_noise_o2mk2_supplied_value(
mPrior[0], mPrior[1], mColorLerp.v, mDistro(mGenerator));
mColorLerp.process();
}
mLevelLerp.multiply_block(dataoutL);
}

protected:
float mPrior[2]{0.f, 0.f};

std::minstd_rand mGenerator;
std::uniform_real_distribution<float> mDistro;
sst::basic_blocks::dsp::lipol_sse<VFXConfig::blockSize, true> mLevelLerp;
sst::basic_blocks::dsp::lipol<float, VFXConfig::blockSize, true> mColorLerp;
};
} // namespace sst::voice_effects::generator

#endif // SHORTCIRCUITXT_GenCorrelatedNoise_H
5 changes: 5 additions & 0 deletions tests/create-voice-effect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "sst/voice-effects/generator/GenSin.h"
#include "sst/voice-effects/generator/GenSaw.h"
#include "sst/voice-effects/generator/GenPhaseMod.h"
#include "sst/voice-effects/generator/GenCorrelatedNoise.h"
#include "sst/voice-effects/eq/EqNBandParametric.h"

struct VTestConfig
Expand Down Expand Up @@ -83,6 +84,10 @@ TEST_CASE("Can Create Voice FX")
{
VTester<sst::voice_effects::generator::GenPhaseMod<VTestConfig>>::TestVFX();
}
SECTION("GenCorrelatedNoise")
{
VTester<sst::voice_effects::generator::GenCorrelatedNoise<VTestConfig>>::TestVFX();
}
SECTION("GenPulseSync")
{
sst::basic_blocks::tables::ShortcircuitSincTableProvider s;
Expand Down
Loading