Skip to content
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
541 changes: 541 additions & 0 deletions examples/audiograph/source/nodes/DistortionNodes.h

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions examples/audiograph/source/nodes/NodeRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <memory>
#include <vector>

#include "DistortionNodes.h"
#include "GainNode.h"
#include "LatencyNode.h"
#include "LowPassFilterNode.h"
Expand Down Expand Up @@ -59,6 +60,12 @@ class NodeRegistry
/** Stable factory key for the built-in low-pass filter node. */
static constexpr const char* lpfIdentifier = "internal.lpf";

/** Stable factory key for the built-in tanh distortion node. */
static constexpr const char* tanhDistortionIdentifier = "internal.tanhDistortion";

/** Stable factory key for the built-in Blunter soft clipper node. */
static constexpr const char* blunterSoftClipperIdentifier = "internal.blunterSoftClipper";

/** Stable factory key for the built-in looping sample player node. */
static constexpr const char* samplePlayerIdentifier = "internal.samplePlayer";

Expand Down Expand Up @@ -160,6 +167,36 @@ class NodeRegistry
}
};

entries[tanhDistortionIdentifier] = {
[] (const yup::AudioGraphNodeProperties&) -> yup::ResultValue<std::unique_ptr<yup::AudioProcessor>>
{
return yup::makeResultValueOk (std::make_unique<TanhDistortionProcessor>());
},
[] (yup::AudioGraphNodeID nodeID, yup::AudioProcessor* proc, yup::AudioGraphProcessor*) -> std::unique_ptr<yup::AudioGraphNodeView>
{
auto* distortion = dynamic_cast<TanhDistortionProcessor*> (proc);
if (distortion == nullptr)
return nullptr;

return std::make_unique<TanhDistortionNodeView> (nodeID, *distortion);
}
};

entries[blunterSoftClipperIdentifier] = {
[] (const yup::AudioGraphNodeProperties&) -> yup::ResultValue<std::unique_ptr<yup::AudioProcessor>>
{
return yup::makeResultValueOk (std::make_unique<BlunterSoftClipperProcessor>());
},
[] (yup::AudioGraphNodeID nodeID, yup::AudioProcessor* proc, yup::AudioGraphProcessor*) -> std::unique_ptr<yup::AudioGraphNodeView>
{
auto* clipper = dynamic_cast<BlunterSoftClipperProcessor*> (proc);
if (clipper == nullptr)
return nullptr;

return std::make_unique<BlunterSoftClipperNodeView> (nodeID, *clipper);
}
};

entries[samplePlayerIdentifier] = {
[] (const yup::AudioGraphNodeProperties&) -> yup::ResultValue<std::unique_ptr<yup::AudioProcessor>>
{
Expand Down Expand Up @@ -392,6 +429,8 @@ class NodeRegistry
oscillatorIdentifier,
gainIdentifier,
lpfIdentifier,
tanhDistortionIdentifier,
blunterSoftClipperIdentifier,
latencyIdentifier,
samplePlayerIdentifier,
subgraphIdentifier
Expand Down Expand Up @@ -420,6 +459,12 @@ class NodeRegistry
if (id == lpfIdentifier)
return "Low Pass Filter";

if (id == tanhDistortionIdentifier)
return "Tanh Distortion";

if (id == blunterSoftClipperIdentifier)
return "Blunter Soft Clip";

if (id == samplePlayerIdentifier)
return "Sample Player";
if (id == subgraphIdentifier)
Expand Down
9 changes: 7 additions & 2 deletions examples/audiograph/source/nodes/NodeViewHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,19 @@ inline void configureParameterSlider (yup::Slider& slider, yup::Color accent)
slider.setColor (yup::Slider::Style::thumbDownColorId, accent.darker (0.15f));
}

inline yup::Rectangle<float> getInlineSliderBounds (const yup::Component& component, int preferredWidth)
inline yup::Rectangle<float> getInlineSliderBounds (const yup::Component& component, int preferredWidth, int rowIndex)
{
const auto bounds = component.getLocalBounds();
const auto scale = bounds.getWidth() / static_cast<float> (preferredWidth);
return { 62.0f * scale,
49.0f * scale,
(49.0f + 25.0f * static_cast<float> (rowIndex)) * scale,
yup::jmax (42.0f * scale, bounds.getWidth() - (150.0f * scale)),
20.0f * scale };
}

inline yup::Rectangle<float> getInlineSliderBounds (const yup::Component& component, int preferredWidth)
{
return getInlineSliderBounds (component, preferredWidth, 0);
}

} // namespace NodeViewHelpers
20 changes: 20 additions & 0 deletions modules/yup_audio_basics/sources/yup_BufferingAudioSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,26 @@ int64 BufferingAudioSource::getNextReadPosition() const
: pos;
}

void BufferingAudioSource::setLooping (bool shouldLoop)
{
{
const ScopedLock sl (bufferRangeLock);

source->setLooping (shouldLoop);

const auto isSourceLooping = source->isLooping();

if (wasSourceLooping != isSourceLooping)
{
wasSourceLooping = isSourceLooping;
bufferValidStart = 0;
bufferValidEnd = 0;
}
}

backgroundThread.moveToFrontOfQueue (this);
}

void BufferingAudioSource::setNextReadPosition (int64 newPosition)
{
const ScopedLock sl (bufferRangeLock);
Expand Down
3 changes: 3 additions & 0 deletions modules/yup_audio_basics/sources/yup_BufferingAudioSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ class YUP_API BufferingAudioSource : public PositionableAudioSource
/** Implements the PositionableAudioSource method. */
bool isLooping() const override { return source->isLooping(); }

/** Implements the PositionableAudioSource method. */
void setLooping (bool shouldLoop) override;

/** A useful function to block until the next the buffer info can be filled.

This is useful for offline rendering.
Expand Down
214 changes: 214 additions & 0 deletions modules/yup_dsp/dynamics/yup_BlunterClipper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
/*
==============================================================================

This file is part of the YUP library.
Copyright (c) 2026 - kunitoki@gmail.com

YUP is an open source library subject to open-source licensing.

The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
to use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.

YUP IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.

==============================================================================
*/

#pragma once

namespace yup
{

//==============================================================================
/**
Blunter soft clipper based on a quadratic bounded clipping curve.

Based on the research by Lorenzo Fiestas ("Quantifying Clipping Softness"),
the Blunter was experimentally found to minimize peak second derivative
(maximize softness) among the generated symmetric bounded clippers for the
normalization range studied in the paper.

The canonical Blunter formula on the unit domain:

@code
B(x) = x * (2 - |x|) for |x| <= 1
B(x) = sign(x) for |x| > 1
@endcode

This gives a constant |B″(x)| = 2 across the entire knee region, spreading
the transition evenly compared to tanh or hyperbolic clippers that
concentrate distortion near the clip point.

The processSample function computes: B(x * inputGain) * outputGain

- inputGain (A_in) controls the amount of distortion (overdrive). Values
above 1.0 push the signal further into the non-linear region.
- outputGain (A_out) compensates the volume after clipping.

Note: B′(0) = 2, so signals well below the clip point are amplified by 2×.
Set outputGain = 0.5 for unity gain at the origin if needed.

Gain setters are not synchronized. Do not call them concurrently with
processSample() or processBlock(); update parameters between processing
passes, or wrap this class in a thread-safe/smoothed parameter layer.

@tparam SampleType The type of audio samples (float or double)
@tparam CoeffType The type used for internal calculations (defaults to double)
*/
template <typename SampleType, typename CoeffType = double>
class BlunterClipper
{
public:
//==============================================================================
/** Constructs a BlunterClipper with the given input and output gains.

@param newInputGain Scales the input before clipping (default: 1.0).
Higher values push more signal into the non-linear
region and produce more harmonic distortion.
@param newOutputGain Scales the output after clipping (default: 1.0).
Use 0.5 to restore unity gain at the origin
(since B′(0) = 2).
*/
BlunterClipper (CoeffType newInputGain = static_cast<CoeffType> (1),
CoeffType newOutputGain = static_cast<CoeffType> (1)) noexcept
: inputGain (sanitizeInputGain (newInputGain))
, outputGain (sanitizeOutputGain (newOutputGain))
{
}

//==============================================================================
/** Sets the input gain (A_in), which controls distortion amount.

@param newInputGain The new input gain. Values less than or equal to
zero are clamped to the smallest positive value
representable by CoeffType.
*/
void setInputGain (CoeffType newInputGain) noexcept
{
inputGain = sanitizeInputGain (newInputGain);
}

/** Returns the current input gain. */
CoeffType getInputGain() const noexcept
{
return inputGain;
}

/** Sets the output gain (A_out), which compensates the volume after clipping.

@param newOutputGain The new output gain. Negative values are clamped
to zero to preserve monotonicity.
*/
void setOutputGain (CoeffType newOutputGain) noexcept
{
outputGain = sanitizeOutputGain (newOutputGain);
}

/** Returns the current output gain. */
CoeffType getOutputGain() const noexcept
{
return outputGain;
}

/** Sets both gains at once.

@param newInputGain The new input gain.
@param newOutputGain The new output gain.
*/
void setParameters (CoeffType newInputGain, CoeffType newOutputGain) noexcept
{
inputGain = sanitizeInputGain (newInputGain);
outputGain = sanitizeOutputGain (newOutputGain);
}

//==============================================================================
/** No-op: this processor is stateless. */
void reset() noexcept {}

/** No-op: this processor is stateless.

@param sampleRate Unused.
@param maximumBlockSize Unused.
*/
void prepare (double /*sampleRate*/, int /*maximumBlockSize*/) noexcept {}

//==============================================================================
/** Processes a single sample through the Blunter clipper.

Computes: B(inputSample * inputGain) * outputGain

@param inputSample The input sample to process.
@returns The soft-clipped output sample.
*/
SampleType processSample (SampleType inputSample) noexcept
{
const auto x = static_cast<CoeffType> (inputSample) * inputGain;
return static_cast<SampleType> (blunter (x) * outputGain);
}

/** Processes a block of samples.

@param inputBuffer Pointer to the input samples.
@param outputBuffer Pointer to the output buffer.
@param numSamples Number of samples to process.
*/
void processBlock (const SampleType* inputBuffer, SampleType* outputBuffer, int numSamples) noexcept
{
for (int i = 0; i < numSamples; ++i)
outputBuffer[i] = processSample (inputBuffer[i]);
}

/** Processes a block of samples in-place.

@param buffer Pointer to the buffer to process in-place.
@param numSamples Number of samples to process.
*/
void processInPlace (SampleType* buffer, int numSamples) noexcept
{
processBlock (buffer, buffer, numSamples);
}

private:
//==============================================================================
/** Applies the canonical Blunter function: B(x) = x*(2-|x|), clamped to [-1, 1]. */
static CoeffType blunter (CoeffType x) noexcept
{
if (x >= static_cast<CoeffType> (1))
return static_cast<CoeffType> (1);

if (x <= static_cast<CoeffType> (-1))
return static_cast<CoeffType> (-1);

return x * (static_cast<CoeffType> (2) - std::abs (x));
}

static CoeffType sanitizeInputGain (CoeffType value) noexcept
{
const auto minimumGain = std::numeric_limits<CoeffType>::epsilon();
return value > minimumGain ? value : minimumGain;
}

static CoeffType sanitizeOutputGain (CoeffType value) noexcept
{
return value > static_cast<CoeffType> (0) ? value : static_cast<CoeffType> (0);
}

//==============================================================================
CoeffType inputGain = static_cast<CoeffType> (1);
CoeffType outputGain = static_cast<CoeffType> (1);

//==============================================================================
YUP_LEAK_DETECTOR (BlunterClipper)
};

//==============================================================================
/** Type aliases for convenience. */
using BlunterClipperFloat = BlunterClipper<float>;
using BlunterClipperDouble = BlunterClipper<double>;

} // namespace yup
Loading
Loading