From 748fa60c55393b6db1ac4df1ec8f9df7f14837c1 Mon Sep 17 00:00:00 2001 From: jatin Date: Sat, 6 Jan 2024 17:27:10 -0800 Subject: [PATCH] Starting on Frequency Shifter module --- modules/chowdsp_utils | 2 +- src/CMakeLists.txt | 1 + src/processors/ProcessorStore.cpp | 2 + src/processors/other/FrequencyShifter.cpp | 48 +++++++++++++++++++++++ src/processors/other/FrequencyShifter.h | 23 +++++++++++ 5 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 src/processors/other/FrequencyShifter.cpp create mode 100644 src/processors/other/FrequencyShifter.h diff --git a/modules/chowdsp_utils b/modules/chowdsp_utils index 67e3f536..8629f740 160000 --- a/modules/chowdsp_utils +++ b/modules/chowdsp_utils @@ -1 +1 @@ -Subproject commit 67e3f536ec856b833694fcee1535538af7df337d +Subproject commit 8629f7406555baf0be8e722e50af42bfe256b1a9 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a6cc59ad..c549114b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -132,6 +132,7 @@ target_sources(BYOD PRIVATE processors/other/Compressor.cpp processors/other/Delay.cpp processors/other/EnvelopeFilter.cpp + processors/other/FrequencyShifter.cpp processors/other/LevelDetective.cpp processors/other/Gate.cpp processors/other/Octaver.cpp diff --git a/src/processors/ProcessorStore.cpp b/src/processors/ProcessorStore.cpp index 0c434616..bbfcdd25 100644 --- a/src/processors/ProcessorStore.cpp +++ b/src/processors/ProcessorStore.cpp @@ -53,6 +53,7 @@ #include "other/Compressor.h" #include "other/Delay.h" #include "other/EnvelopeFilter.h" +#include "other/FrequencyShifter.h" #include "other/Gate.h" #include "other/LevelDetective.h" #include "other/Octaver.h" @@ -148,6 +149,7 @@ ProcessorStore::StoreMap ProcessorStore::store = { { "Crying Child", { &processorFactory, { ProcessorType::Other, CryBaby::numInputs, CryBaby::numOutputs } } }, { "Delay", { &processorFactory, { ProcessorType::Other, 1, 1 } } }, { "Envelope Filter", { &processorFactory, { ProcessorType::Other, EnvelopeFilter::numInputs, EnvelopeFilter::numOutputs } } }, + { "Frequency Shifter", { &processorFactory, { ProcessorType::Other, 1, 1 } } }, { "Level Detective", { &processorFactory, { ProcessorType::Other, 1, 1 } } }, { "Gate", { &processorFactory, { ProcessorType::Other, 1, 1 } } }, { "Octaver", { &processorFactory, { ProcessorType::Other, 1, 1 } } }, diff --git a/src/processors/other/FrequencyShifter.cpp b/src/processors/other/FrequencyShifter.cpp new file mode 100644 index 00000000..18404f47 --- /dev/null +++ b/src/processors/other/FrequencyShifter.cpp @@ -0,0 +1,48 @@ +#include "FrequencyShifter.h" +#include "processors/ParameterHelpers.h" + +FrequencyShifter::FrequencyShifter (UndoManager* um) + : BaseProcessor ("Frequency Shifter", createParameterLayout(), um) +{ + using namespace ParameterHelpers; + loadParameterPointer (shiftParam, vts, "shift"); + + uiOptions.backgroundColour = Colours::palevioletred.brighter (0.1f); + uiOptions.powerColour = Colours::yellow; + uiOptions.info.description = "A frequency shifter effect."; + uiOptions.info.authors = StringArray { "Jatin Chowdhury" }; +} + +ParamLayout FrequencyShifter::createParameterLayout() +{ + using namespace ParameterHelpers; + auto params = createBaseParams(); + + createFreqParameter (params, "shift", "Shift", 0.0f, 200.0f, 20.0f, 20.0f); + + return { params.begin(), params.end() }; +} + +void FrequencyShifter::prepare (double sampleRate, int samplesPerBlock) +{ + hilbertFilter.reset(); + modulator.prepare ({ sampleRate, (uint32_t) samplesPerBlock, 1}); +} + +void FrequencyShifter::processAudio (AudioBuffer& buffer) +{ + modulator.setFrequency (shiftParam->getCurrentValue()); + + for (auto [ch, data] : chowdsp::buffer_iters::channels (buffer)) + { + for (auto& x : data) + { + const auto [x_re, x_im] = hilbertFilter.process (x); + const auto [mod_sin, mod_cos] = modulator.processSampleQuadrature(); + + x = x_re * mod_sin - x_im * mod_cos; + } + } + + // TODO: DC blocker +} diff --git a/src/processors/other/FrequencyShifter.h b/src/processors/other/FrequencyShifter.h new file mode 100644 index 00000000..2b958a47 --- /dev/null +++ b/src/processors/other/FrequencyShifter.h @@ -0,0 +1,23 @@ +#pragma once + +#include "../BaseProcessor.h" + +class FrequencyShifter : public BaseProcessor +{ +public: + explicit FrequencyShifter (UndoManager* um = nullptr); + + ProcessorType getProcessorType() const override { return Other; } + static ParamLayout createParameterLayout(); + + void prepare (double sampleRate, int samplesPerBlock) override; + void processAudio (AudioBuffer& buffer) override; + +private: + chowdsp::FloatParameter* shiftParam = nullptr; + + chowdsp::HilbertFilter hilbertFilter; + chowdsp::SineWave modulator {}; + + JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FrequencyShifter) +};