Audacity 3.2.0
TimeAndPitch.h
Go to the documentation of this file.
1//
2// Arbitrary time and pitch stretching, based on the Phase-locked Vocoder.
3//
4#pragma once
5
6#include <complex>
7#include <functional>
8#include <memory>
9#include <vector>
10
11namespace staffpad {
12
14{
15public:
19 using ShiftTimbreCb = std::function<void(
20 double factor, std::complex<float>* spectrum, const float* magnitude)>;
21
23 int fftSize, bool reduceImaging = true, ShiftTimbreCb shiftTimbreCb = {});
30 void setup(int numChannels, int maxBlockSize);
31
38 void setTimeStretchAndPitchFactor(double timeStretch, double pitchFactor);
39
46 int getSamplesToNextHop() const;
47
53
58 void feedAudio(const float* const* in_smp, int numSamples);
59
64 void retrieveAudio(float* const* out_smp, int numSamples);
65
69 void processPitchShift(float* const* smp, int numSamples, double pitchFactor);
70
74 int getLatencySamples() const;
75
79 int getLatencySamplesForStretchRatio(float timeStretch) const;
80
84 void reset();
85
86private:
87 const int fftSize;
88 static constexpr int overlap = 4;
89 static constexpr bool normalize_window = true; // compensate for ola window overlaps
90 static constexpr bool modulate_synthesis_hop = true;
91
92 void _process_hop(int hop_a, int hop_s);
93 template <int num_channels>
94 void _time_stretch(float hop_a, float hop_s);
96
97 struct impl;
98 std::shared_ptr<impl> d;
99
100 const bool _reduceImaging;
102
104 int _maxBlockSize = 1024;
105 double _resampleReadPos = 0.0;
107 int _numBins = fftSize / 2 + 1;
109
111
113 double _timeStretch = 1.0;
114 double _pitchFactor = 1.0;
115
117};
118
119} // namespace staffpad
int getNumAvailableOutputSamples() const
void setTimeStretchAndPitchFactor(double timeStretch, double pitchFactor)
int getSamplesToNextHop() const
void _time_stretch(float hop_a, float hop_s)
std::function< void(double factor, std::complex< float > *spectrum, const float *magnitude)> ShiftTimbreCb
Definition: TimeAndPitch.h:20
void setup(int numChannels, int maxBlockSize)
void retrieveAudio(float *const *out_smp, int numSamples)
const ShiftTimbreCb _shiftTimbreCb
Definition: TimeAndPitch.h:101
void _process_hop(int hop_a, int hop_s)
process one hop/chunk in _fft_timeSeries and add the result to output circular buffer
int getLatencySamples() const
static constexpr bool normalize_window
Definition: TimeAndPitch.h:89
double _expectedPhaseChangePerBinPerSample
Definition: TimeAndPitch.h:112
void processPitchShift(float *const *smp, int numSamples, double pitchFactor)
int getLatencySamplesForStretchRatio(float timeStretch) const
TimeAndPitch(int fftSize, bool reduceImaging=true, ShiftTimbreCb shiftTimbreCb={})
std::shared_ptr< impl > d
Definition: TimeAndPitch.h:97
static constexpr bool modulate_synthesis_hop
Definition: TimeAndPitch.h:90
void feedAudio(const float *const *in_smp, int numSamples)
static constexpr int overlap
Definition: TimeAndPitch.h:88