Skip to content

Commit

Permalink
Provide zero padding #42
Browse files Browse the repository at this point in the history
  • Loading branch information
jurihock committed Dec 2, 2023
1 parent 77f442d commit ab5b261
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 33 deletions.
20 changes: 11 additions & 9 deletions cpp/StftPitchShift/STFT.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,26 @@ namespace stftpitchshift

public:

STFT(const size_t framesize, const size_t hopsize, const bool chronometry = false) :
STFT(std::make_shared<RFFT>(), std::make_tuple(framesize, framesize), hopsize, chronometry)
STFT(const size_t framesize, const size_t hopsize, const size_t padsize, const bool chronometry = false) :
STFT(std::make_shared<RFFT>(), std::make_tuple(framesize, framesize), hopsize, padsize, chronometry)
{
}

STFT(const std::shared_ptr<FFT> fft, const size_t framesize, const size_t hopsize, const bool chronometry = false) :
STFT(fft, std::make_tuple(framesize, framesize), hopsize, chronometry)
STFT(const std::shared_ptr<FFT> fft, const size_t framesize, const size_t hopsize, const size_t padsize, const bool chronometry = false) :
STFT(fft, std::make_tuple(framesize, framesize), hopsize, padsize, chronometry)
{
}

STFT(const std::tuple<size_t, size_t> framesize, const size_t hopsize, const bool chronometry = false) :
STFT(std::make_shared<RFFT>(), framesize, hopsize, chronometry)
STFT(const std::tuple<size_t, size_t> framesize, const size_t hopsize, const size_t padsize, const bool chronometry = false) :
STFT(std::make_shared<RFFT>(), framesize, hopsize, padsize, chronometry)
{
}

STFT(const std::shared_ptr<FFT> fft, const std::tuple<size_t, size_t> framesize, const size_t hopsize, const bool chronometry = false) :
STFT(const std::shared_ptr<FFT> fft, const std::tuple<size_t, size_t> framesize, const size_t hopsize, const size_t padsize, const bool chronometry = false) :
fft(fft),
framesize(framesize),
hopsize(hopsize),
padsize(padsize),
chronometry(chronometry)
{
const auto analysis_window_size = std::get<0>(framesize);
Expand Down Expand Up @@ -74,8 +75,8 @@ namespace stftpitchshift
[unitygain](T value) { return value * unitygain; });

buffer.size = analysis_window_size;
buffer.time.resize(buffer.size);
buffer.freq.resize(buffer.size / 2 + 1);
buffer.time.resize(buffer.size * padsize);
buffer.freq.resize(buffer.size * padsize / 2 + 1);
}

void operator()(const std::span<const T> input, const std::span<T> output, const std::function<void(std::span<std::complex<T>> dft)> callback)
Expand Down Expand Up @@ -143,6 +144,7 @@ namespace stftpitchshift
const std::shared_ptr<FFT> fft;
const std::tuple<size_t, size_t> framesize;
const size_t hopsize;
const size_t padsize;
const bool chronometry;

struct
Expand Down
16 changes: 12 additions & 4 deletions cpp/StftPitchShift/StftPitchShift.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ StftPitchShift::StftPitchShift(
const double samplerate,
const size_t framesize,
const size_t hopsize,
const size_t padsize,
const bool normalization,
const bool chronometry) :
StftPitchShift(
std::make_shared<RFFT>(),
samplerate,
std::make_tuple(framesize, framesize),
hopsize,
padsize,
normalization,
chronometry)
{
Expand All @@ -28,13 +30,15 @@ StftPitchShift::StftPitchShift(
const double samplerate,
const std::tuple<size_t, size_t> framesize,
const size_t hopsize,
const size_t padsize,
const bool normalization,
const bool chronometry) :
StftPitchShift(
std::make_shared<RFFT>(),
samplerate,
framesize,
hopsize,
padsize,
normalization,
chronometry)
{
Expand All @@ -45,13 +49,15 @@ StftPitchShift::StftPitchShift(
const double samplerate,
const size_t framesize,
const size_t hopsize,
const size_t padsize,
const bool normalization,
const bool chronometry) :
StftPitchShift(
fft,
samplerate,
std::make_tuple(framesize, framesize),
hopsize,
padsize,
normalization,
chronometry)
{
Expand All @@ -62,12 +68,14 @@ StftPitchShift::StftPitchShift(
const double samplerate,
const std::tuple<size_t, size_t> framesize,
const size_t hopsize,
const size_t padsize,
const bool normalization,
const bool chronometry) :
fft(fft),
samplerate(samplerate),
framesize(framesize),
hopsize(hopsize),
padsize(padsize),
normalization(normalization),
chronometry(chronometry)
{
Expand Down Expand Up @@ -117,14 +125,14 @@ void StftPitchShift::shiftpitch(
// preemptively clear output #30
std::fill(output.begin(), output.end(), float(0));

StftPitchShiftCore<float> core(fft, samplerate, framesize, hopsize);
StftPitchShiftCore<float> core(fft, samplerate, framesize, hopsize, padsize);

core.factors(factors);
core.quefrency(quefrency);
core.distortion(distortion);
core.normalization(normalization);

STFT<float> stft(fft, framesize, hopsize, chronometry);
STFT<float> stft(fft, framesize, hopsize, padsize, chronometry);

stft(input, output, [&](std::span<std::complex<float>> dft)
{
Expand All @@ -142,14 +150,14 @@ void StftPitchShift::shiftpitch(
// preemptively clear output #30
std::fill(output.begin(), output.end(), double(0));

StftPitchShiftCore<double> core(fft, samplerate, framesize, hopsize);
StftPitchShiftCore<double> core(fft, samplerate, framesize, hopsize, padsize);

core.factors(factors);
core.quefrency(quefrency);
core.distortion(distortion);
core.normalization(normalization);

STFT<double> stft(fft, framesize, hopsize, chronometry);
STFT<double> stft(fft, framesize, hopsize, padsize, chronometry);

stft(input, output, [&](std::span<std::complex<double>> dft)
{
Expand Down
9 changes: 9 additions & 0 deletions cpp/StftPitchShift/StftPitchShift.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,31 @@ namespace stftpitchshift
* @param samplerate The sample rate of the signal in hertz.
* @param framesize The STFT frame size in samples (analysis = synthesis).
* @param hopsize The STFT hop size in samples.
* @param padsize The FFT zero padding factor.
* @param normalization Optionally enable spectral rms normalization.
* @param chronometry Optionally enable runtime measurements.
*/
StftPitchShift(
const double samplerate,
const size_t framesize,
const size_t hopsize,
const size_t padsize = 1,
const bool normalization = false,
const bool chronometry = false);

/**
* @param samplerate The sample rate of the signal in hertz.
* @param framesize The STFT frame size in samples (analysis >= synthesis).
* @param hopsize The STFT hop size in samples.
* @param padsize The FFT zero padding factor.
* @param normalization Optionally enable spectral rms normalization.
* @param chronometry Optionally enable runtime measurements.
*/
StftPitchShift(
const double samplerate,
const std::tuple<size_t, size_t> framesize,
const size_t hopsize,
const size_t padsize = 1,
const bool normalization = false,
const bool chronometry = false);

Expand All @@ -50,6 +54,7 @@ namespace stftpitchshift
* @param samplerate The sample rate of the signal in hertz.
* @param framesize The STFT frame size in samples (analysis = synthesis).
* @param hopsize The STFT hop size in samples.
* @param padsize The FFT zero padding factor.
* @param normalization Optionally enable spectral rms normalization.
* @param chronometry Optionally enable runtime measurements.
*/
Expand All @@ -58,6 +63,7 @@ namespace stftpitchshift
const double samplerate,
const size_t framesize,
const size_t hopsize,
const size_t padsize = 1,
const bool normalization = false,
const bool chronometry = false);

Expand All @@ -66,6 +72,7 @@ namespace stftpitchshift
* @param samplerate The sample rate of the signal in hertz.
* @param framesize The STFT frame size in samples (analysis >= synthesis).
* @param hopsize The STFT hop size in samples.
* @param padsize The FFT zero padding factor.
* @param normalization Optionally enable spectral rms normalization.
* @param chronometry Optionally enable runtime measurements.
*/
Expand All @@ -74,6 +81,7 @@ namespace stftpitchshift
const double samplerate,
const std::tuple<size_t, size_t> framesize,
const size_t hopsize,
const size_t padsize = 1,
const bool normalization = false,
const bool chronometry = false);

Expand Down Expand Up @@ -139,6 +147,7 @@ namespace stftpitchshift
const double samplerate;
const std::tuple<size_t, size_t> framesize;
const size_t hopsize;
const size_t padsize;
const bool normalization;
const bool chronometry;

Expand Down
31 changes: 20 additions & 11 deletions cpp/StftPitchShift/StftPitchShiftCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,53 +20,61 @@ namespace stftpitchshift
StftPitchShiftCore(
const double samplerate,
const size_t framesize,
const size_t hopsize) :
const size_t hopsize,
const size_t padsize) :
StftPitchShiftCore(
std::make_shared<RFFT>(),
samplerate,
std::make_tuple(framesize, framesize),
hopsize)
hopsize,
padsize)
{
}

StftPitchShiftCore(
const double samplerate,
const std::tuple<size_t, size_t> framesize,
const size_t hopsize) :
const size_t hopsize,
const size_t padsize) :
StftPitchShiftCore(
std::make_shared<RFFT>(),
samplerate,
framesize,
hopsize)
hopsize,
padsize)
{
}

StftPitchShiftCore(
const std::shared_ptr<FFT> fft,
const double samplerate,
const size_t framesize,
const size_t hopsize) :
const size_t hopsize,
const size_t padsize) :
StftPitchShiftCore(
fft,
samplerate,
std::make_tuple(framesize, framesize),
hopsize)
hopsize,
padsize)
{
}

StftPitchShiftCore(
const std::shared_ptr<FFT> fft,
const double samplerate,
const std::tuple<size_t, size_t> framesize,
const size_t hopsize) :
const size_t hopsize,
const size_t padsize) :
fft(fft),
samplerate(samplerate),
framesize(framesize),
hopsize(hopsize),
vocoder(samplerate, framesize, hopsize),
pitcher(samplerate, std::get<0>(framesize)),
cepster(fft, samplerate, std::get<0>(framesize)),
envelope(std::get<0>(framesize) / 2 + 1)
padsize(padsize),
vocoder(samplerate, framesize, hopsize, padsize),
pitcher(samplerate, std::get<0>(framesize) * padsize),
cepster(fft, samplerate, std::get<0>(framesize) * padsize),
envelope(std::get<0>(framesize) * padsize / 2 + 1)
{
}

Expand Down Expand Up @@ -175,6 +183,7 @@ namespace stftpitchshift
const double samplerate;
const std::tuple<size_t, size_t> framesize;
const size_t hopsize;
const size_t padsize;

Vocoder<T> vocoder;
Pitcher<T> pitcher;
Expand Down
12 changes: 6 additions & 6 deletions cpp/StftPitchShift/Vocoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ namespace stftpitchshift

public:

Vocoder(const double samplerate, const size_t framesize, const size_t hopsize) :
Vocoder(samplerate, std::make_tuple(framesize, framesize), hopsize)
Vocoder(const double samplerate, const size_t framesize, const size_t hopsize, const size_t padsize) :
Vocoder(samplerate, std::make_tuple(framesize, framesize), hopsize, padsize)
{
}

Vocoder(const double samplerate, const std::tuple<size_t, size_t> framesize, const size_t hopsize)
Vocoder(const double samplerate, const std::tuple<size_t, size_t> framesize, const size_t hopsize, const size_t padsize)
{
const double pi = 2.0 * std::acos(-1.0);

const size_t dftsize = std::get<0>(framesize) / 2 + 1;
const size_t dftsize = std::get<0>(framesize) * padsize / 2 + 1;

stft_freq_inc = samplerate / std::get<0>(framesize);
stft_phase_inc = pi * hopsize / std::get<0>(framesize);
stft_freq_inc = samplerate / (std::get<0>(framesize) * padsize);
stft_phase_inc = pi * hopsize / (std::get<0>(framesize) * padsize);

encode_phase_buffer.resize(dftsize);
decode_phase_buffer.resize(dftsize);
Expand Down
1 change: 1 addition & 0 deletions cpp/StftPitchShift/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ int main(int argc, char** argv)
samplerate,
cli.framesize,
std::get<1>(cli.framesize) / cli.hoprate,
1, // TODO
cli.normalization,
cli.chronometry);

Expand Down
8 changes: 5 additions & 3 deletions examples/realtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ using namespace stftpitchshift;

// basic parameters
// - samplerate as required
// - overlap at least 4
// - STFT overlap factor at least 4
// - FFT zero padding factor at least 1
const double samplerate = 44100;
const size_t overlap = 4;
const size_t padding = 1;

// analysis and synthesis window sizes
// power of two each of them
Expand Down Expand Up @@ -48,8 +50,8 @@ int main()
buffer.input.resize(total_buffer_size);
buffer.output.resize(total_buffer_size);

stft = std::make_shared<STFT<double>>(framesize, hopsize);
core = std::make_shared<StftPitchShiftCore<double>>(samplerate, framesize, hopsize);
stft = std::make_shared<STFT<double>>(framesize, hopsize, padding);
core = std::make_shared<StftPitchShiftCore<double>>(samplerate, framesize, hopsize, padding);

// set pitch shifting parameters as required

Expand Down

0 comments on commit ab5b261

Please sign in to comment.