Skip to content

Commit

Permalink
Put samplerate argument to the front, to allow more optional arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
jurihock committed Dec 2, 2023
1 parent 8f64741 commit 77f442d
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 66 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ To include this library in your C++ audio project, study the minimal C++ example

using namespace stftpitchshift;

StftPitchShift pitchshifter(1024, 256, 44100);
StftPitchShift pitchshifter(44100, 1024, 256);

std::vector<float> x(44100);
std::vector<float> y(x.size());
Expand All @@ -142,7 +142,7 @@ Also feel free to explore the Python class `StftPitchShift` in your personal aud
```python
from stftpitchshift import StftPitchShift
pitchshifter = StftPitchShift(1024, 256, 44100)
pitchshifter = StftPitchShift(44100, 1024, 256)
x = [0] * 44100
y = pitchshifter.shiftpitch(x, 1)
Expand Down
6 changes: 3 additions & 3 deletions cpp/StftPitchShift/Cepster.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ namespace stftpitchshift

public:

Cepster(const std::shared_ptr<FFT> fft, const size_t framesize, const double samplerate) :
Cepster(const std::shared_ptr<FFT> fft, const double samplerate, const size_t framesize) :
fft(fft),
framesize(framesize),
samplerate(samplerate),
framesize(framesize),
spectrum(framesize / 2 + 1),
cepstrum(framesize)
{
Expand Down Expand Up @@ -63,8 +63,8 @@ namespace stftpitchshift
private:

const std::shared_ptr<FFT> fft;
const size_t framesize;
const double samplerate;
const size_t framesize;

double value;
size_t cutoff;
Expand Down
6 changes: 3 additions & 3 deletions cpp/StftPitchShift/Pitcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ namespace stftpitchshift

public:

Pitcher(const size_t framesize, const double samplerate) :
framesize(framesize),
Pitcher(const double samplerate, const size_t framesize) :
samplerate(samplerate),
framesize(framesize),
nyquist(samplerate / 2)
{
}
Expand Down Expand Up @@ -115,8 +115,8 @@ namespace stftpitchshift

private:

const size_t framesize;
const double samplerate;
const size_t framesize;
const double nyquist;

std::vector<double> values;
Expand Down
20 changes: 10 additions & 10 deletions cpp/StftPitchShift/StftPitchShift.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,65 +9,65 @@
using namespace stftpitchshift;

StftPitchShift::StftPitchShift(
const double samplerate,
const size_t framesize,
const size_t hopsize,
const double samplerate,
const bool normalization,
const bool chronometry) :
StftPitchShift(
std::make_shared<RFFT>(),
samplerate,
std::make_tuple(framesize, framesize),
hopsize,
samplerate,
normalization,
chronometry)
{
}

StftPitchShift::StftPitchShift(
const double samplerate,
const std::tuple<size_t, size_t> framesize,
const size_t hopsize,
const double samplerate,
const bool normalization,
const bool chronometry) :
StftPitchShift(
std::make_shared<RFFT>(),
samplerate,
framesize,
hopsize,
samplerate,
normalization,
chronometry)
{
}

StftPitchShift::StftPitchShift(
const std::shared_ptr<FFT> fft,
const double samplerate,
const size_t framesize,
const size_t hopsize,
const double samplerate,
const bool normalization,
const bool chronometry) :
StftPitchShift(
fft,
samplerate,
std::make_tuple(framesize, framesize),
hopsize,
samplerate,
normalization,
chronometry)
{
}

StftPitchShift::StftPitchShift(
const std::shared_ptr<FFT> fft,
const double samplerate,
const std::tuple<size_t, size_t> framesize,
const size_t hopsize,
const double samplerate,
const bool normalization,
const bool chronometry) :
fft(fft),
samplerate(samplerate),
framesize(framesize),
hopsize(hopsize),
samplerate(samplerate),
normalization(normalization),
chronometry(chronometry)
{
Expand Down Expand Up @@ -117,7 +117,7 @@ void StftPitchShift::shiftpitch(
// preemptively clear output #30
std::fill(output.begin(), output.end(), float(0));

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

core.factors(factors);
core.quefrency(quefrency);
Expand All @@ -142,7 +142,7 @@ void StftPitchShift::shiftpitch(
// preemptively clear output #30
std::fill(output.begin(), output.end(), double(0));

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

core.factors(factors);
core.quefrency(quefrency);
Expand Down
18 changes: 9 additions & 9 deletions cpp/StftPitchShift/StftPitchShift.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,62 +18,62 @@ namespace stftpitchshift
public:

/**
* @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 samplerate The sample rate of the signal in hertz.
* @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 double samplerate,
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 samplerate The sample rate of the signal in hertz.
* @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 double samplerate,
const bool normalization = false,
const bool chronometry = false);

/**
* @param fft The custom FFT calculation instance.
* @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 samplerate The sample rate of the signal in hertz.
* @param normalization Optionally enable spectral rms normalization.
* @param chronometry Optionally enable runtime measurements.
*/
StftPitchShift(
const std::shared_ptr<FFT> fft,
const double samplerate,
const size_t framesize,
const size_t hopsize,
const double samplerate,
const bool normalization = false,
const bool chronometry = false);

/**
* @param fft The custom FFT calculation instance.
* @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 samplerate The sample rate of the signal in hertz.
* @param normalization Optionally enable spectral rms normalization.
* @param chronometry Optionally enable runtime measurements.
*/
StftPitchShift(
const std::shared_ptr<FFT> fft,
const double samplerate,
const std::tuple<size_t, size_t> framesize,
const size_t hopsize,
const double samplerate,
const bool normalization = false,
const bool chronometry = false);

Expand Down Expand Up @@ -136,9 +136,9 @@ namespace stftpitchshift
private:

const std::shared_ptr<FFT> fft;
const double samplerate;
const std::tuple<size_t, size_t> framesize;
const size_t hopsize;
const double samplerate;
const bool normalization;
const bool chronometry;

Expand Down
38 changes: 19 additions & 19 deletions cpp/StftPitchShift/StftPitchShiftCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,54 +18,54 @@ namespace stftpitchshift
public:

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

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

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

StftPitchShiftCore(
const std::shared_ptr<FFT> fft,
const double samplerate,
const std::tuple<size_t, size_t> framesize,
const size_t hopsize,
const double samplerate) :
const size_t hopsize) :
fft(fft),
samplerate(samplerate),
framesize(framesize),
hopsize(hopsize),
samplerate(samplerate),
vocoder(framesize, hopsize, samplerate),
pitcher(std::get<0>(framesize), samplerate),
cepster(fft, std::get<0>(framesize), samplerate),
vocoder(samplerate, framesize, hopsize),
pitcher(samplerate, std::get<0>(framesize)),
cepster(fft, samplerate, std::get<0>(framesize)),
envelope(std::get<0>(framesize) / 2 + 1)
{
}
Expand Down Expand Up @@ -172,9 +172,9 @@ namespace stftpitchshift
private:

const std::shared_ptr<FFT> fft;
const double samplerate;
const std::tuple<size_t, size_t> framesize;
const size_t hopsize;
const double samplerate;

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

public:

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

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

Expand Down
2 changes: 1 addition & 1 deletion cpp/StftPitchShift/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ int main(int argc, char** argv)
const std::span<double> output = { outdata.data() + channel * size, size };

StftPitchShift stft(
samplerate,
cli.framesize,
std::get<1>(cli.framesize) / cli.hoprate,
samplerate,
cli.normalization,
cli.chronometry);

Expand Down
2 changes: 1 addition & 1 deletion examples/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using namespace stftpitchshift;

int main()
{
StftPitchShift pitchshifter(1024, 256, 44100);
StftPitchShift pitchshifter(44100, 1024, 256);

std::vector<float> x(44100);
std::vector<float> y(x.size());
Expand Down
2 changes: 1 addition & 1 deletion examples/example.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from stftpitchshift import StftPitchShift

pitchshifter = StftPitchShift(1024, 256, 44100)
pitchshifter = StftPitchShift(44100, 1024, 256)

x = [0] * 44100
y = pitchshifter.shiftpitch(x, 1)
2 changes: 1 addition & 1 deletion examples/realtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ int main()
buffer.output.resize(total_buffer_size);

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

// set pitch shifting parameters as required

Expand Down
2 changes: 1 addition & 1 deletion python/stftpitchshift/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def number(value): return 1*1024 if value == '1k' else 2*1024 if value == '2k'
framesize = list(number(framesize) for framesize in window.split(','))
hopsize = framesize[-1] // int(overlap)

pitchshifter = StftPitchShift(framesize, hopsize, samplerate)
pitchshifter = StftPitchShift(samplerate, framesize, hopsize)

channels = x.shape[-1] if x.ndim > 1 else 1

Expand Down
Loading

0 comments on commit 77f442d

Please sign in to comment.