Skip to content

Commit

Permalink
Merge branch 'main' into lew
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiswolf committed Mar 17, 2023
2 parents 764d103 + 554f9f3 commit 5799b7d
Show file tree
Hide file tree
Showing 9 changed files with 696 additions and 48 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.vscode
build_arm64
build_x86_64
build
dist

Expand Down
8 changes: 7 additions & 1 deletion NeuralResonatorVST/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,13 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
${TORCH_INSTALL_PREFIX}/lib/libc10.dylib
)

# if the architecture is arm64, we need to add the arm64e library
# force set the architecture variable to the current architecture
# this is needed because the CMAKE_OSX_ARCHITECTURES variable is not set
if (NOT CMAKE_OSX_ARCHITECTURES)
set(CMAKE_OSX_ARCHITECTURES ${CMAKE_SYSTEM_PROCESSOR} CACHE STRING "" FORCE)
endif()

# if the architecture is arm64, we need to add the libiomp5 library
if (CMAKE_OSX_ARCHITECTURES STREQUAL "x86_64")
list(APPEND TORCH_LIBS ${TORCH_INSTALL_PREFIX}/lib/libiomp5.dylib)
endif()
Expand Down
42 changes: 38 additions & 4 deletions NeuralResonatorVST/Filterbank.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
#include "Filterbank.h"
#include "HelperFunctions.h"
Filterbank::Filterbank()
{
mNumParallel = 0;
mNumBiquads = 0;
mStride = 0;
}

Filterbank::Filterbank(int numParallel, int numBiquads)
{
setup(numParallel, numBiquads);
}

void Filterbank::setup(int numParallel, int numBiquads)
{
mNumParallel = numParallel;
mNumBiquads = numBiquads;
Expand All @@ -17,17 +29,28 @@ Filterbank::Filterbank(int numParallel, int numBiquads)
{
for (int j = 0; j < mNumBiquads; j++)
{
mIIRFilters[i][j].set_coefficients(0.0, 0.0, 0.0, 0.0, 0.0);
mIIRFilters[i][j].setCoefficientValues(0.0, 0.0, 0.0, 0.0, 0.0);
}
}
}

Filterbank::~Filterbank() {}
Filterbank::~Filterbank()
{
cleanup();
}

void Filterbank::cleanup()
{
JLOG("Filterbank::cleanup()");
for (int i = 0; i < mNumParallel; i++)
{
for (int j = 0; j < mNumBiquads; j++) { mIIRFilters[i][j].cleanup(); }
}
}

void Filterbank::setCoefficients(const std::vector<float>& coeffs)
{
const juce::SpinLock::ScopedLockType lock(mProcessLock);

for (int i = 0; i < mNumParallel; i++)
{
for (int j = 0; j < mNumBiquads; j++)
Expand Down Expand Up @@ -69,4 +92,15 @@ void Filterbank::processBuffer(juce::AudioBuffer<float>& buffer)
buffer.setSample(channel, sampleIdx, static_cast<float>(out));
}
}
}
}
void Filterbank::setInterpolationDelta(unsigned int delta)
{
mInterpolationDelta = delta;
for (int i = 0; i < mNumParallel; i++)
{
for (int j = 0; j < mNumBiquads; j++)
{
mIIRFilters[i][j].setDelta(mInterpolationDelta);
}
}
}
13 changes: 10 additions & 3 deletions NeuralResonatorVST/Filterbank.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
#pragma once

#include "TwoPole.h"
#include "TwoPoleInterpolated.h"
#include <juce_core/juce_core.h>
#include <juce_audio_basics/juce_audio_basics.h>
#include <vector>

class Filterbank
{
public:
Filterbank(int numParallel, int numBiquads);
Filterbank();
~Filterbank();

Filterbank(int numParallel, int numBiquads);
void setup(int numParallel, int numBiquads);

void cleanup();
/**
* @brief Set the coefficients of the filterbank
* @note The coefficients are expected to be in the following order:
Expand All @@ -28,12 +32,15 @@ class Filterbank
*/
void processBuffer(juce::AudioBuffer<float>& buffer);

void setInterpolationDelta(unsigned int delta);

private:
std::vector<std::vector<TwoPole<double>>> mIIRFilters;
std::vector<std::vector<TwoPoleInterpolated>> mIIRFilters;

int mNumParallel;
int mNumBiquads;
int mStride;
unsigned int mInterpolationDelta;

juce::SpinLock mProcessLock;

Expand Down
128 changes: 128 additions & 0 deletions NeuralResonatorVST/Lerp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#pragma once

#include <juce_core/juce_core.h>

template <typename T>
class Lerp
{
public:
Lerp();
~Lerp();

Lerp(unsigned int delta);
void setup(unsigned int delta);

void setTarget(T target);
void setValue(T value);
void setDelta(unsigned int delta);

T process(bool bound=true);

T getTarget() { return m_target; }
T getValue() { return m_val; }
T getDelta() { return m_delta; }

bool isFinished() { return m_counter == 0; }

private:
T m_val;
T m_increment;
int m_counter;
unsigned int m_delta;
T m_target;

void calcIncrement(T target, unsigned int delta);
T boundValue();
};

template <typename T>
inline Lerp<T>::Lerp()
{
setup(0);
}

template <typename T>
inline Lerp<T>::Lerp(unsigned int delta)
{
setup(delta);
}

template <typename T>
inline void Lerp<T>::setup(unsigned int delta)
{
m_val = 0;
m_increment = 0;
m_counter = 0;
m_delta = delta;
m_target = 0;
}

template <typename T>
inline void Lerp<T>::setTarget(T target)
{
m_target = target;
calcIncrement(m_target, m_delta);
}

template <typename T>
inline void Lerp<T>::setValue(T value)
{
m_val = value;
m_counter = 0;
m_increment = 0;
}

template <typename T>
inline void Lerp<T>::setDelta(unsigned int delta)
{
m_delta = delta;
calcIncrement(m_target, m_delta);
}

template <typename T>
inline void Lerp<T>::calcIncrement(T target, unsigned int delta)
{
if(delta == 0)
m_increment = 0.0;
else
m_increment = (target - m_val) / static_cast<T>(delta);

if(m_increment == 0.0)
{
m_counter = 0;
m_val = target;
}
else
{
m_counter = delta;
}
}

template <typename T>
inline T Lerp<T>::process(bool bound)
{
if (m_counter > 0)
{
m_val += m_increment;
m_counter--;
}
if (bound)
m_val = boundValue();
return m_val;
}

template <typename T>
inline T Lerp<T>::boundValue()
{
if (m_increment > 0 && m_val > m_target)
m_val = m_target;
else if (m_increment < 0 && m_val < m_target)
m_val = m_target;
return m_val;
}

template <typename T>
inline Lerp<T>::~Lerp()
{
juce::Logger::writeToLog("Lerp::~Lerp");
}
Loading

0 comments on commit 5799b7d

Please sign in to comment.