Refactor (nest some conditions, remove unused variables)

This commit is contained in:
Geraint 2022-12-17 23:22:17 +00:00
parent 3ffe6704ce
commit ec3c6a253f

View File

@ -61,7 +61,6 @@ struct SignalsmithStretch {
smoothedEnergy.resize(stft.bands()); smoothedEnergy.resize(stft.bands());
outputMap.resize(stft.bands()); outputMap.resize(stft.bands());
channelPredictions.resize(channels*stft.bands()); channelPredictions.resize(channels*stft.bands());
maxEnergyChannel.resize(stft.bands());
} }
template<class Inputs, class Outputs> template<class Inputs, class Outputs>
@ -327,7 +326,6 @@ private:
Prediction * predictionsForChannel(int c) { Prediction * predictionsForChannel(int c) {
return channelPredictions.data() + c*stft.bands(); return channelPredictions.data() + c*stft.bands();
} }
std::vector<int> maxEnergyChannel;
void processSpectrum(bool newSpectrum, Sample timeFactor) { void processSpectrum(bool newSpectrum, Sample timeFactor) {
int bands = stft.bands(); int bands = stft.bands();
@ -349,13 +347,11 @@ private:
findPeaks(smoothingBins); findPeaks(smoothingBins);
updateOutputMap(smoothingBins); updateOutputMap(smoothingBins);
int longVerticalStep = std::round(smoothingBins); // Preliminary output prediction from phase-vocoder
for (auto &c : maxEnergyChannel) c = -1;
for (int c = 0; c < channels; ++c) { for (int c = 0; c < channels; ++c) {
Band *bins = bandsForChannel(c); Band *bins = bandsForChannel(c);
auto *predictions = predictionsForChannel(c); auto *predictions = predictionsForChannel(c);
for (int b = 0; b < stft.bands(); ++b) { for (int b = 0; b < stft.bands(); ++b) {
auto &outputBin = bins[b];
auto mapPoint = outputMap[b]; auto mapPoint = outputMap[b];
int lowIndex = std::floor(mapPoint.inputBin); int lowIndex = std::floor(mapPoint.inputBin);
Sample fracIndex = mapPoint.inputBin - std::floor(mapPoint.inputBin); Sample fracIndex = mapPoint.inputBin - std::floor(mapPoint.inputBin);
@ -365,7 +361,7 @@ private:
prediction.energy *= std::max<Sample>(0, mapPoint.freqGrad); // scale the energy according to local stretch factor prediction.energy *= std::max<Sample>(0, mapPoint.freqGrad); // scale the energy according to local stretch factor
prediction.input = getFractional<&Band::input>(c, lowIndex, fracIndex); prediction.input = getFractional<&Band::input>(c, lowIndex, fracIndex);
// Preliminary output prediction from phase-vocoder auto &outputBin = bins[b];
Complex prevInput = getFractional<&Band::prevInput>(c, lowIndex, fracIndex); Complex prevInput = getFractional<&Band::prevInput>(c, lowIndex, fracIndex);
Complex freqTwist = signalsmith::perf::mul<true>(prediction.input, prevInput); Complex freqTwist = signalsmith::perf::mul<true>(prediction.input, prevInput);
Complex phase = signalsmith::perf::mul(outputBin.prevOutput, freqTwist); Complex phase = signalsmith::perf::mul(outputBin.prevOutput, freqTwist);
@ -373,11 +369,12 @@ private:
} }
} }
auto *predictions0 = predictionsForChannel(0); // Re-predict using phase differences between frequencies
int longVerticalStep = std::round(smoothingBins);
for (int b = 0; b < stft.bands(); ++b) { for (int b = 0; b < stft.bands(); ++b) {
// Find maximum-energy channel and calculate that // Find maximum-energy channel and calculate that
int maxChannel = 0; int maxChannel = 0;
Sample maxEnergy = predictions0[b].energy; Sample maxEnergy = predictionsForChannel(0)[b].energy;
for (int c = 1; c < channels; ++c) { for (int c = 1; c < channels; ++c) {
Sample e = predictionsForChannel(c)[b].energy; Sample e = predictionsForChannel(c)[b].energy;
if (e > maxEnergy) { if (e > maxEnergy) {
@ -394,45 +391,47 @@ private:
Complex phase = 0; Complex phase = 0;
// Short vertical step // Upwards vertical steps
if (b > 0) { if (b > 0) {
if (!prediction.hasShortVertical) { if (!prediction.hasShortVertical) {
Complex downInput = getFractional<&Band::input>(maxChannel, mapPoint.inputBin - timeFactor); Complex downInput = getFractional<&Band::input>(maxChannel, mapPoint.inputBin - timeFactor);
prediction.shortVerticalTwist = signalsmith::perf::mul<true>(prediction.input, downInput); prediction.shortVerticalTwist = signalsmith::perf::mul<true>(prediction.input, downInput);
} }
auto &otherBin = bins[b - 1]; auto &downBin = bins[b - 1];
phase += signalsmith::perf::mul(otherBin.output, prediction.shortVerticalTwist); phase += signalsmith::perf::mul(downBin.output, prediction.shortVerticalTwist);
if (b >= longVerticalStep) {
if (!prediction.hasLongVertical) {
Complex longDownInput = getFractional<&Band::input>(maxChannel, mapPoint.inputBin - longVerticalStep*timeFactor);
prediction.longVerticalTwist = signalsmith::perf::mul<true>(prediction.input, longDownInput);
}
auto &longDownBin = bins[b - longVerticalStep];
phase += signalsmith::perf::mul(longDownBin.output, prediction.longVerticalTwist);
}
} }
// Downwards vertical steps
if (b < stft.bands() - 1) { if (b < stft.bands() - 1) {
auto &otherPrediction = predictions[b + 1]; auto &upPrediction = predictions[b + 1];
{ // upwards short vertical twist { // upwards short vertical twist
auto otherMapPoint = outputMap[b + 1]; auto upMapPoint = outputMap[b + 1];
Complex downInput = getFractional<&Band::input>(maxChannel, otherMapPoint.inputBin - timeFactor); Complex upInput = getFractional<&Band::input>(maxChannel, upMapPoint.inputBin - timeFactor);
otherPrediction.shortVerticalTwist = signalsmith::perf::mul<true>(otherPrediction.input, downInput); upPrediction.shortVerticalTwist = signalsmith::perf::mul<true>(upPrediction.input, upInput);
otherPrediction.hasShortVertical = true; upPrediction.hasShortVertical = true;
} }
auto &otherBin = bins[b + 1]; auto &upBin = bins[b + 1];
phase += signalsmith::perf::mul<true>(otherBin.output, otherPrediction.shortVerticalTwist); phase += signalsmith::perf::mul<true>(upBin.output, upPrediction.shortVerticalTwist);
}
// Long vertical steps if (b < stft.bands() - longVerticalStep) {
if (b > longVerticalStep) { auto &longUpPrediction = predictions[b + longVerticalStep];
if (!prediction.hasLongVertical) { { // upwards long vertical twist
Complex downInput = getFractional<&Band::input>(maxChannel, mapPoint.inputBin - longVerticalStep*timeFactor); auto longUpMapPoint = outputMap[b + longVerticalStep];
prediction.longVerticalTwist = signalsmith::perf::mul<true>(prediction.input, downInput); Complex longUpInput = getFractional<&Band::input>(maxChannel, longUpMapPoint.inputBin - longVerticalStep*timeFactor);
longUpPrediction.longVerticalTwist = signalsmith::perf::mul<true>(longUpPrediction.input, longUpInput);
longUpPrediction.hasLongVertical = true;
}
auto &longUpBin = bins[b + longVerticalStep];
phase += signalsmith::perf::mul<true>(longUpBin.output, longUpPrediction.longVerticalTwist);
} }
auto &otherBin = bins[b - longVerticalStep];
phase += signalsmith::perf::mul(otherBin.output, prediction.longVerticalTwist);
}
if (b < stft.bands() - longVerticalStep) {
auto &otherPrediction = predictions[b + longVerticalStep];
{ // upwards long vertical twist
auto otherMapPoint = outputMap[b + longVerticalStep];
Complex downInput = getFractional<&Band::input>(maxChannel, otherMapPoint.inputBin - longVerticalStep*timeFactor);
otherPrediction.longVerticalTwist = signalsmith::perf::mul<true>(otherPrediction.input, downInput);
otherPrediction.hasLongVertical = true;
}
auto &otherBin = bins[b + longVerticalStep];
phase += signalsmith::perf::mul<true>(otherBin.output, otherPrediction.longVerticalTwist);
} }
outputBin.output = prediction.makeOutput(phase); outputBin.output = prediction.makeOutput(phase);