-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add WidthPanner processor class (#483)
* Add WidthPanner processor class * Apply clang-format --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
7287433
commit 422eecb
Showing
6 changed files
with
198 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
modules/dsp/chowdsp_dsp_utils/Processors/chowdsp_WidthPanner.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#pragma once | ||
|
||
#include "chowdsp_Panner.h" | ||
|
||
namespace chowdsp | ||
{ | ||
/** | ||
* A panner-style processor that affects the "width" of the | ||
* processed signal. | ||
*/ | ||
template <typename SampleType> | ||
class WidthPanner | ||
{ | ||
public: | ||
WidthPanner() | ||
{ | ||
setRule (rule); | ||
setPan (1.0f); | ||
} | ||
|
||
/** Sets the panning rule. */ | ||
void setRule (PanningRule newRule) | ||
{ | ||
rule = newRule; | ||
leftPanner.setRule (rule); | ||
rightPanner.setRule (rule); | ||
} | ||
|
||
/** Sets the current panning value, between 1 (stereo), 0 (mono) and -1 (inverted stereo). */ | ||
void setPan (SampleType newPan) | ||
{ | ||
leftPanner.setPan (-newPan); | ||
rightPanner.setPan (newPan); | ||
} | ||
|
||
/** Initialises the processor. */ | ||
void prepare (const juce::dsp::ProcessSpec& spec) | ||
{ | ||
leftPanner.prepare (spec); | ||
rightPanner.prepare (spec); | ||
|
||
leftPanBuffer.setMaxSize (2, static_cast<int> (spec.maximumBlockSize)); | ||
} | ||
|
||
/** Resets the internal state variables of the processor. */ | ||
void reset() | ||
{ | ||
leftPanner.reset(); | ||
rightPanner.reset(); | ||
} | ||
|
||
/** Processes a stereo buffer. */ | ||
void processBlock (const BufferView<SampleType>& buffer) noexcept | ||
{ | ||
jassert (buffer.getNumChannels() == 2); | ||
|
||
const auto numSamples = buffer.getNumSamples(); | ||
leftPanBuffer.setCurrentSize (2, numSamples); | ||
|
||
// copy left signal into both channels of leftPanBuffer | ||
for (int ch = 0; ch < 2; ++ch) | ||
BufferMath::copyBufferChannels (buffer, leftPanBuffer, 0, ch); | ||
|
||
// copy right signal into both channels of buffer | ||
BufferMath::copyBufferChannels (buffer, buffer, 1, 0); | ||
|
||
leftPanner.processBlock (leftPanBuffer); | ||
rightPanner.processBlock (buffer); | ||
|
||
BufferMath::addBufferData (leftPanBuffer, buffer); | ||
|
||
BufferMath::applyGain (buffer, static_cast<SampleType> (1) / Panner<SampleType>::getBoostForRule (rule)); | ||
} | ||
|
||
private: | ||
PanningRule rule = PanningRule::linear; | ||
|
||
Panner<SampleType> leftPanner; | ||
Panner<SampleType> rightPanner; | ||
|
||
Buffer<SampleType> leftPanBuffer {}; | ||
|
||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WidthPanner) | ||
}; | ||
} // namespace chowdsp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
tests/dsp_tests/chowdsp_dsp_utils_test/WidthPannerTest.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include <CatchUtils.h> | ||
#include <chowdsp_dsp_utils/chowdsp_dsp_utils.h> | ||
|
||
TEST_CASE ("Width Panner Test", "[dsp][misc]") | ||
{ | ||
const auto spec = juce::dsp::ProcessSpec { 48000.0, 4, 2 }; | ||
|
||
chowdsp::Buffer<float> buffer { 2, 4 }; | ||
for (auto [ch, data] : chowdsp::buffer_iters::channels (buffer)) | ||
std::fill (data.begin(), data.end(), ch == 0 ? -1.0f : 1.0f); | ||
|
||
const auto testPanner = [spec, &buffer] (chowdsp::PanningRule rule, float pan, auto&& getExpected) | ||
{ | ||
chowdsp::WidthPanner<float> panner; | ||
panner.setRule (rule); | ||
panner.setPan (pan); | ||
panner.prepare (spec); | ||
panner.processBlock (buffer); | ||
for (auto [ch, data] : chowdsp::buffer_iters::channels (std::as_const (buffer))) | ||
{ | ||
const auto expected = getExpected (ch); | ||
for (auto& x : data) | ||
REQUIRE (x == Catch::Approx { expected }.margin (1.0e-3)); | ||
} | ||
}; | ||
|
||
SECTION ("Stereo -> Mono") | ||
{ | ||
testPanner (chowdsp::PanningRule::linear, 0.0f, [] (int) | ||
{ return 0.0f; }); | ||
testPanner (chowdsp::PanningRule::sin3dB, 0.0f, [] (int) | ||
{ return 0.0f; }); | ||
testPanner (chowdsp::PanningRule::sin6dB, 0.0f, [] (int) | ||
{ return 0.0f; }); | ||
} | ||
|
||
SECTION ("Stereo -> Stereo") | ||
{ | ||
testPanner (chowdsp::PanningRule::linear, 1.0f, [] (int ch) | ||
{ return ch == 0 ? -1.0f : 1.0f; }); | ||
testPanner (chowdsp::PanningRule::sin3dB, 1.0f, [] (int ch) | ||
{ return ch == 0 ? -1.0f : 1.0f; }); | ||
testPanner (chowdsp::PanningRule::sin6dB, 1.0f, [] (int ch) | ||
{ return ch == 0 ? -1.0f : 1.0f; }); | ||
} | ||
|
||
SECTION ("Stereo -> Inverse Stereo") | ||
{ | ||
testPanner (chowdsp::PanningRule::sin4p5dB, -1.0f, [] (int ch) | ||
{ return ch == 0 ? 1.0f : -1.0f; }); | ||
testPanner (chowdsp::PanningRule::squareRoot3dB, -1.0f, [] (int ch) | ||
{ return ch == 0 ? -1.0f : 1.0f; }); | ||
testPanner (chowdsp::PanningRule::squareRoot4p5dB, -1.0f, [] (int ch) | ||
{ return ch == 0 ? 1.0f : -1.0f; }); | ||
} | ||
|
||
SECTION ("Stereo -> Half-Mid (Linear)") | ||
{ | ||
testPanner (chowdsp::PanningRule::linear, 0.5f, [] (int ch) | ||
{ return ch == 0 ? -0.5f : 0.5f; }); | ||
} | ||
|
||
SECTION ("Stereo -> Half-Mid (Sin 6dB)") | ||
{ | ||
testPanner (chowdsp::PanningRule::sin6dB, 0.5f, [] (int ch) | ||
{ return ch == 0 ? -0.7071f : 0.7071f; }); | ||
} | ||
} |