yaw-audio/src/yaw-totune/dsp.cpp

87 lines
2.1 KiB
C++

#include "DistrhoPlugin.hpp"
#include "Resampler.hpp"
START_NAMESPACE_DISTRHO
class PitchCorrector : public Plugin
{
public:
PitchCorrector()
: Plugin(0, 0, 0),
resamplers{},
detectors{}
{
}
protected:
const char *getLabel() const override { return "yaw-totune"; }
const char *getDescription() const override { return "Pitch corrector"; }
const char *getMaker() const override { return "yaw-audio"; }
const char *getHomePage() const override { return "https://yaw.man/plugins/yaw-totune"; }
const char *getLicense() const override { return "Fuck you pay me"; }
uint32_t getVersion() const override { return d_version(1, 0, 0); }
int64_t getUniqueId() const override { return d_cconst('y', 't', 't', 'n'); }
void initParameter(uint32_t index, Parameter &parameter) override
{
parameter.hints = kParameterIsAutomable;
parameter.ranges.def = 0.0f;
parameter.ranges.min = 0.0f;
parameter.ranges.max = 1.0f;
parameter.name = "param";
parameter.symbol = "param";
}
void sampleRateChanged(double newRate) override
{
hzNyq = newRate / 2;
}
float getParameterValue(uint32_t index) const override
{
return 0;
}
void setParameterValue(uint32_t idx, float val) override
{
}
void run(const float **inputs, float **outputs, uint32_t frames) override
{
for (int chn = 0; chn < 2; ++chn)
{
resamplers[chn].write(inputs[chn], frames);
detectors[chn].downsample(inputs[chn], frames);
uint dpitch = detectors[chn].detectPitch(frames);
double taux;
double period;
if (dpitch)
{
period = resamplers[chn].detectPeriodNear(dpitch);
double correctPeriod = scale.getNearestPeriod(period);
taux = period / correctPeriod;
}
else
{
taux = 1.0;
period = 1.0;
}
resamplers[chn].resample(outputs[chn], frames, taux, period);
}
}
private:
double hzNyq;
std::array<Resampler<float>, 2> resamplers;
std::array<Detector<float>, 2> detectors;
Scale scale{440.0};
DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(PitchCorrector)
};
Plugin *createPlugin()
{
return new PitchCorrector();
}
END_NAMESPACE_DISTRHO