Skip to content

Commit

Permalink
Setup convolver file resampling via r8brain
Browse files Browse the repository at this point in the history
Signed-off-by: falkTX <falktx@falktx.com>
  • Loading branch information
falkTX committed Jan 15, 2023
1 parent 26ca598 commit d3e790f
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
[submodule "3rd-party/pffft"]
path = 3rd-party/pffft
url = https://github.com/marton78/pffft.git
[submodule "3rd-party/r8brain"]
path = 3rd-party/r8brain
url = https://github.com/avaneev/r8brain-free-src.git
1 change: 0 additions & 1 deletion 3rd-party/pffft
Submodule pffft deleted from 08f5ed
1 change: 1 addition & 0 deletions 3rd-party/pffft
1 change: 1 addition & 0 deletions 3rd-party/r8brain
Submodule r8brain added at 8ce873
6 changes: 3 additions & 3 deletions plugins/ConvolutionReverb/3rd-party.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* DISTRHO OneKnob Convolution Reverb
* Copyright (C) 2022 Filipe Coelho <falktx@falktx.com>
* Copyright (C) 2022-2023 Filipe Coelho <falktx@falktx.com>
*
* 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
Expand All @@ -20,5 +20,5 @@
#define DR_WAV_IMPLEMENTATION
#include "dr_wav.h"

#include "pffft/pffft.c"
#include "pffft/pffft_common.c"
#include "r8brain/pffft.cpp"
#include "r8brain/r8bbase.cpp"
6 changes: 5 additions & 1 deletion plugins/ConvolutionReverb/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,21 @@ FILES_UI = \

include ../../dpf/Makefile.plugins.mk

BUILD_CXX_FLAGS += -DAUDIOFFT_PFFFT
BUILD_CXX_FLAGS += -DR8B_FASTTIMING
BUILD_CXX_FLAGS += -DR8B_R8B_PFFFT
BUILD_CXX_FLAGS += -I../common
BUILD_CXX_FLAGS += -I../../3rd-party
BUILD_CXX_FLAGS += -I../../3rd-party/FFTConvolver
BUILD_CXX_FLAGS += -I../../3rd-party/r8brain
BUILD_CXX_FLAGS += -I../../dpf-widgets/opengl
BUILD_CXX_FLAGS += -pthread
LINK_FLAGS += $(SHARED_MEMORY_LIBS)

# BUILD_CXX_FLAGS += -DAUDIOFFT_FFTW3 $(shell $(PKG_CONFIG) --cflags fftw3f)
# LINK_FLAGS += $(shell $(PKG_CONFIG) --libs fftw3f)

BUILD_CXX_FLAGS += -DAUDIOFFT_PFFFT -DPFFFT_ENABLE_NEON -Ipffft
# BUILD_CXX_FLAGS += -DAUDIOFFT_PFFFT -DPFFFT_ENABLE_NEON -Ipffft

# --------------------------------------------------------------
# Enable all possible plugin types
Expand Down
41 changes: 38 additions & 3 deletions plugins/ConvolutionReverb/OneKnobPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
#include "extra/ScopedPointer.hpp"
#include "extra/Thread.hpp"

#include "FFTConvolver/TwoStageFFTConvolver.h"
#include "dr_flac.h"
#include "dr_wav.h"
// -Wunused-variable
#include "r8brain/CDSPResampler.h"
#include "FFTConvolver/TwoStageFFTConvolver.h"

START_NAMESPACE_DISTRHO

Expand Down Expand Up @@ -317,9 +319,20 @@ class OneKnobConvolutionReverbPlugin : public OneKnobPlugin
unsigned int channels;
unsigned int sampleRate;
drwav_uint64 numFrames;
const size_t valuelen = std::strlen(value);

ScopedPointer<TwoStageThreadedConvolver> newConvolverL, newConvolverR;

if (valuelen <= 5)
{
const MutexLocker cml(mutex);
convolverL.swapWith(newConvolverL);
convolverR.swapWith(newConvolverR);
return;
}

float* ir;
if (::strncasecmp(value + (std::max(size_t(0), std::strlen(value) - 5u)), ".flac", 5) == 0)
if (::strncasecmp(value + (std::max(size_t(0), valuelen - 5u)), ".flac", 5) == 0)
ir = drflac_open_file_and_read_pcm_frames_f32(value, &channels, &sampleRate, &numFrames, nullptr);
else
ir = drwav_open_file_and_read_pcm_frames_f32(value, &channels, &sampleRate, &numFrames, nullptr);
Expand Down Expand Up @@ -363,7 +376,29 @@ class OneKnobConvolutionReverbPlugin : public OneKnobPlugin
break;
}

ScopedPointer<TwoStageThreadedConvolver> newConvolverL, newConvolverR;
if (sampleRate != getSampleRate())
{
r8b::CDSPResampler16IR resampler(sampleRate, getSampleRate(), numFrames);
const int numResampledFrames = resampler.getMaxOutLen(0);
DISTRHO_SAFE_ASSERT_RETURN(numResampledFrames > 0,);

// left channel, always present
float* const irBufResampledL = new float[numResampledFrames];
resampler.oneshot(irBufL, numFrames, irBufResampledL, numResampledFrames);
delete[] irBufL;
irBufL = irBufResampledL;

// right channel, optional
if (irBufL != irBufR)
{
float* const irBufResampledR = new float[numResampledFrames];
resampler.oneshot(irBufR, numFrames, irBufResampledR, numResampledFrames);
delete[] irBufR;
irBufR = irBufResampledR;
}

numFrames = numResampledFrames;
}

newConvolverL = new TwoStageThreadedConvolver();
newConvolverL->init(headBlockSize, tailBlockSize, irBufL, numFrames);
Expand Down

0 comments on commit d3e790f

Please sign in to comment.