Skip to content

Commit

Permalink
Use symmetric window in delayed core
Browse files Browse the repository at this point in the history
  • Loading branch information
jurihock committed Dec 9, 2023
1 parent 492bc7f commit 91070e9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 32 deletions.
21 changes: 4 additions & 17 deletions src/StftPitchShiftPlugin/Core.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
#include <StftPitchShiftPlugin/Core.h>

Core::Core(const double samplerate, const int blocksize, const int dftsize, const int overlap)
Core::Core(const double samplerate, const int blocksize, const int dftsize, const int overlap) :
samplerate(samplerate), blocksize(blocksize), dftsize(dftsize), overlap(overlap),
analysis_window_size(static_cast<size_t>(dftsize + dftsize)),
synthesis_window_size(static_cast<size_t>(blocksize))
{
const auto analysis_window_size = static_cast<size_t>(dftsize + dftsize);
const auto synthesis_window_size = static_cast<size_t>(blocksize);

const auto winsize = std::make_tuple(analysis_window_size, synthesis_window_size);
const auto hopsize = synthesis_window_size / static_cast<size_t>(overlap);

const auto fft = std::make_shared<FFT>();

config.analysis_window_size = analysis_window_size;
config.synthesis_window_size = synthesis_window_size;

stft = std::make_unique<stftpitchshift::STFT<double>>(fft, winsize, hopsize);
core = std::make_unique<stftpitchshift::StftPitchShiftCore<double>>(fft, winsize, hopsize, samplerate);
}
Expand Down Expand Up @@ -41,16 +38,6 @@ void Core::pitch(std::vector<double> values)
core->factors(values);
}

size_t Core::get_analysis_window_size() const
{
return config.analysis_window_size;
}

size_t Core::get_synthesis_window_size() const
{
return config.synthesis_window_size;
}

void Core::stft_pitch_shift(const std::span<const double> input, const std::span<double> output) const
{
(*stft)(input, output, [&](std::span<std::complex<double>> dft)
Expand Down
10 changes: 6 additions & 4 deletions src/StftPitchShiftPlugin/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ class Core

protected:

size_t get_analysis_window_size() const;
size_t get_synthesis_window_size() const;
const double samplerate;
const int blocksize;
const int dftsize;
const int overlap;
const size_t analysis_window_size;
const size_t synthesis_window_size;

void stft_pitch_shift(const std::span<const double> input, const std::span<double> output) const;

private:

struct { size_t analysis_window_size, synthesis_window_size; } config;

std::unique_ptr<stftpitchshift::STFT<double>> stft;
std::unique_ptr<stftpitchshift::StftPitchShiftCore<double>> core;

Expand Down
10 changes: 5 additions & 5 deletions src/StftPitchShiftPlugin/Core/DelayedCore.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <StftPitchShiftPlugin/Core/DelayedCore.h>

DelayedCore::DelayedCore(const double samplerate, const int blocksize, const int dftsize, const int overlap) :
InstantCore(samplerate, blocksize, dftsize, overlap)
InstantCore(samplerate, dftsize + dftsize, dftsize, overlap), host_block_size(blocksize)
{
const auto total_buffer_size = get_synthesis_window_size() * 2;
const auto total_buffer_size = analysis_window_size + synthesis_window_size;

buffer.input.resize(total_buffer_size);
buffer.output.resize(total_buffer_size);
Expand All @@ -17,18 +17,18 @@ DelayedCore::~DelayedCore()

int DelayedCore::latency() const
{
return static_cast<int>(get_synthesis_window_size() * 2) + InstantCore::latency();
return 6 * dftsize - host_block_size;
}

bool DelayedCore::compatible(const int blocksize) const
{
return static_cast<size_t>(blocksize) <= get_synthesis_window_size();
return static_cast<size_t>(blocksize) <= synthesis_window_size;
}

void DelayedCore::process(const std::span<const float> input, const std::span<float> output)
{
const auto minsamples = input.size();
const auto maxsamples = get_synthesis_window_size();
const auto maxsamples = synthesis_window_size;

// shift input buffer
std::copy(
Expand Down
2 changes: 2 additions & 0 deletions src/StftPitchShiftPlugin/Core/DelayedCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class DelayedCore : public InstantCore

private:

const int host_block_size;

struct { std::vector<float> input, output; } buffer;

size_t samples;
Expand Down
9 changes: 3 additions & 6 deletions src/StftPitchShiftPlugin/Core/InstantCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
InstantCore::InstantCore(const double samplerate, const int blocksize, const int dftsize, const int overlap) :
Core(samplerate, blocksize, dftsize, overlap)
{
const auto total_buffer_size = get_analysis_window_size() + get_synthesis_window_size();
const auto total_buffer_size = analysis_window_size + synthesis_window_size;

buffer.input.resize(total_buffer_size);
buffer.output.resize(total_buffer_size);
Expand All @@ -15,19 +15,16 @@ InstantCore::~InstantCore()

int InstantCore::latency() const
{
return static_cast<int>(get_analysis_window_size() - get_synthesis_window_size());
return static_cast<int>(analysis_window_size - synthesis_window_size);
}

bool InstantCore::compatible(const int blocksize) const
{
return static_cast<size_t>(blocksize) == get_synthesis_window_size();
return static_cast<size_t>(blocksize) == synthesis_window_size;
}

void InstantCore::process(const std::span<const float> input, const std::span<float> output)
{
const auto analysis_window_size = get_analysis_window_size();
const auto synthesis_window_size = get_synthesis_window_size();

// shift input buffer
std::copy(
buffer.input.begin() + synthesis_window_size,
Expand Down

0 comments on commit 91070e9

Please sign in to comment.