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