Compute next block in smaller steps

This commit is contained in:
Geraint 2025-02-11 21:04:39 +00:00
parent 3e5dc06697
commit 2c671f01aa

View File

@ -44,15 +44,20 @@ struct SignalsmithStretch {
return stft.blockSamples() - stft.analysisOffset();
}
int outputLatency() const {
return stft.synthesisOffset();
return stft.synthesisOffset() + stft.defaultInterval();
}
void reset() {
stft.reset(0.1);
stashedInput = stft.input;
stashedOutput = stft.output;
prevInputOffset = -1;
channelBands.assign(channelBands.size(), Band());
silenceCounter = 0;
didSeek = false;
blockProcess = {};
}
// Configures using a default preset
@ -69,6 +74,8 @@ struct SignalsmithStretch {
stft.configure(channels, channels, blockSamples, intervalSamples + 1);
stft.setInterval(intervalSamples, stft.kaiser);
stft.reset(0.1);
stashedInput = stft.input;
stashedOutput = stft.output;
tmpBuffer.resize(blockSamples + intervalSamples);
bands = stft.bands();
@ -79,6 +86,8 @@ struct SignalsmithStretch {
smoothedEnergy.resize(bands);
outputMap.resize(bands);
channelPredictions.resize(channels*bands);
blockProcess = {};
}
/// Frequency multiplier, and optional tonality limit (as multiple of sample-rate)
@ -160,6 +169,7 @@ struct SignalsmithStretch {
if (silenceFirst) { // first block of silence processing
silenceFirst = false;
//stft.reset();
blockProcess = {};
for (auto &b : channelBands) {
b.input = b.prevInput = b.output = 0;
b.inputEnergy = 0;
@ -195,18 +205,22 @@ struct SignalsmithStretch {
}
for (int outputIndex = 0; outputIndex < outputSamples; ++outputIndex) {
if (stft.samplesSinceSynthesis() >= stft.defaultInterval()) {
// Time to process a spectrum! Where should it come from in the input?
int inputOffset = std::round(outputIndex*Sample(inputSamples)/outputSamples);
int inputInterval = inputOffset - prevInputOffset;
prevInputOffset = inputOffset;
Sample processRatio = Sample(blockProcess.samplesSinceLast)/stft.defaultInterval();
size_t processToStep = std::min<size_t>(blockProcess.steps, blockProcess.steps*processRatio);
while (blockProcess.step < processToStep) {
size_t step = blockProcess.step++;
copyInput(inputOffset);
bool newSpectrum = didSeek || (inputInterval > 0);
if (newSpectrum) {
if (didSeek || inputInterval != int(stft.defaultInterval())) { // make sure the previous input is the correct distance in the past
stft.analyse(stft.defaultInterval());
if (blockProcess.newSpectrum) {
if (blockProcess.reanalysePrev) {
// analyse past input
if (step < stft.analyseSteps()) {
stashedInput.swap(stft.input);
stft.analyseStep(step, stft.defaultInterval());
stashedInput.swap(stft.input);
continue;
}
step -= stft.analyseSteps();
if (step < 1) {
// Copy previous analysis to our band objects
for (int c = 0; c < channels; ++c) {
auto channelBands = bandsForChannel(c);
@ -215,9 +229,20 @@ struct SignalsmithStretch {
channelBands[b].prevInput = spectrumBands[b];
}
}
continue;
}
step -= 1;
}
// Analyse latest (stashed) input
if (step < stft.analyseSteps()) {
stashedInput.swap(stft.input);
stft.analyse();
stashedInput.swap(stft.input);
continue;
}
step -= stft.analyseSteps();
if (step < 1) {
// Copy analysed spectrum into our band objects
for (int c = 0; c < channels; ++c) {
auto channelBands = bandsForChannel(c);
@ -226,12 +251,19 @@ struct SignalsmithStretch {
channelBands[b].input = spectrumBands[b];
}
}
continue;
}
step -= 1;
}
Sample timeFactor = didSeek ? seekTimeFactor : stft.defaultInterval()/std::max<Sample>(1, inputInterval);
processSpectrum(newSpectrum, timeFactor);
didSeek = false;
if (step < processSpectrumSteps) {
processSpectrum(step, blockProcess.newSpectrum, blockProcess.timeFactor);
continue;
}
step -= processSpectrumSteps;
if (step < 1) {
// Copy band objects into spectrum
for (int c = 0; c < channels; ++c) {
auto channelBands = bandsForChannel(c);
auto *spectrumBands = stft.spectrum(c);
@ -239,9 +271,55 @@ struct SignalsmithStretch {
spectrumBands[b] = channelBands[b].output;
}
}
stft.synthesise();
};
continue;
}
step -= 1;
if (step < stft.synthesiseSteps()) {
stft.synthesiseStep(step);
continue;
}
LOG_EXPR("uh oh");
LOG_EXPR(processToStep);
LOG_EXPR(blockProcess.steps);
LOG_EXPR(blockProcess.step);
abort();
}
if (processRatio >= 1) { // we *should* have just written a block, and are now ready to start a new one
blockProcess.step = 0;
blockProcess.steps = 0; // how many steps
blockProcess.samplesSinceLast = 0;
// Time to process a spectrum! Where should it come from in the input?
int inputOffset = std::round(outputIndex*Sample(inputSamples)/outputSamples);
int inputInterval = inputOffset - prevInputOffset;
prevInputOffset = inputOffset;
copyInput(inputOffset);
stashedInput = stft.input; // save the input state, since that's what we'll analyse later
stashedOutput = stft.output; // save the current output, and read from it
stft.moveOutput(stft.defaultInterval()); // the actual input jumps forward in time by one interval, ready for the synthesis
blockProcess.newSpectrum = didSeek || (inputInterval > 0);
if (blockProcess.newSpectrum) {
// make sure the previous input is the correct distance in the past
blockProcess.reanalysePrev = didSeek || inputInterval != int(stft.defaultInterval());
if (blockProcess.reanalysePrev) blockProcess.steps += stft.analyseSteps() + 1;
// analyse a new input
blockProcess.steps += stft.analyseSteps() + 1;
}
blockProcess.timeFactor = didSeek ? seekTimeFactor : stft.defaultInterval()/std::max<Sample>(1, inputInterval);
didSeek = false;
blockProcess.steps += processSpectrumSteps;
blockProcess.steps += stft.synthesiseSteps() + 1;
}
++blockProcess.samplesSinceLast;
stashedOutput.swap(stft.output);
for (int c = 0; c < channels; ++c) {
auto &&outputChannel = outputs[c];
Sample v = 0;
@ -249,6 +327,7 @@ struct SignalsmithStretch {
outputChannel[outputIndex] = v;
}
stft.moveOutput(1);
stashedOutput.swap(stft.output);
}
copyInput(inputSamples);
@ -286,6 +365,16 @@ struct SignalsmithStretch {
}
}
private:
struct {
size_t samplesSinceLast = -1;
size_t steps = 0;
size_t step = 0;
bool newSpectrum = false;
bool reanalysePrev = false;
Sample timeFactor;
} blockProcess;
using Complex = std::complex<Sample>;
static constexpr Sample noiseFloor{1e-15};
static constexpr Sample maxCleanStretch{2}; // time-stretch ratio before we start randomising phases
@ -295,7 +384,11 @@ private:
Sample freqMultiplier = 1, freqTonalityLimit = 0.5;
std::function<Sample(Sample)> customFreqMap = nullptr;
signalsmith::linear::DynamicSTFT<Sample, false, true> stft;
using STFT = signalsmith::linear::DynamicSTFT<Sample, false, true>;
STFT stft;
typename STFT::Input stashedInput;
typename STFT::Output stashedOutput;
std::vector<Sample> tmpBuffer;
int channels = 0, bands = 0;
@ -384,11 +477,16 @@ private:
RandomEngine randomEngine;
void processSpectrum(bool newSpectrum, Sample timeFactor) {
static constexpr size_t processSpectrumSteps = 6;
void processSpectrum(size_t step, bool newSpectrum, Sample timeFactor) {
Sample smoothingBins = Sample(stft.fftSamples())/stft.defaultInterval();
int longVerticalStep = std::round(smoothingBins);
timeFactor = std::max<Sample>(timeFactor, 1/maxCleanStretch);
bool randomTimeFactor = (timeFactor > maxCleanStretch);
std::uniform_real_distribution<Sample> timeFactorDist(maxCleanStretch*2*randomTimeFactor - timeFactor, timeFactor);
switch(step) {
case 1: {
if (newSpectrum) {
for (int c = 0; c < channels; ++c) {
auto bins = bandsForChannel(c);
@ -405,11 +503,16 @@ private:
}
}
}
Sample smoothingBins = Sample(stft.fftSamples())/stft.defaultInterval();
int longVerticalStep = std::round(smoothingBins);
return;
}
case 2: {
if (customFreqMap || freqMultiplier != 1) {
findPeaks(smoothingBins);
}
return;
}
case 3: {
if (customFreqMap || freqMultiplier != 1) {
updateOutputMap();
} else { // we're not pitch-shifting, so no need to find peaks etc.
for (int c = 0; c < channels; ++c) {
@ -422,7 +525,9 @@ private:
outputMap[b] = {Sample(b), 1};
}
}
return;
}
case 4: {
// Preliminary output prediction from phase-vocoder
for (int c = 0; c < channels; ++c) {
Band *bins = bandsForChannel(c);
@ -445,7 +550,9 @@ private:
outputBin.output = phase/(std::max(prevEnergy, prediction.energy) + noiseFloor);
}
}
return;
}
case 5: {
// Re-predict using phase differences between frequencies
for (int b = 0; b < bands; ++b) {
// Find maximum-energy channel and calculate that
@ -528,6 +635,9 @@ private:
bin.prevInput = bin.input;
}
}
return;
}
} // switch
}
// Produces smoothed energy across all channels