Reduce FFT memory slightly

This commit is contained in:
Geraint 2025-01-07 19:40:15 +00:00
parent bc8a7fd65e
commit 241ba8d97d
5 changed files with 24 additions and 8 deletions

View File

@ -1,8 +1,11 @@
all: out/stretch all: out/stretch
out/stretch: ../signalsmith-stretch.h main.cpp util/*.h out/stretch: ../signalsmith-stretch.h main.cpp util/*.h util/*.hxx ../dsp/*.h
mkdir -p out mkdir -p out
g++ main.cpp -o out/stretch -std=c++11 -Ofast -g g++ main.cpp -o out/stretch -std=c++11 -O3 -g
examples: out/stretch
inputs/run-all.sh out/d1- out/stretch --semitones=-1
clean: clean:
rm -rf out rm -rf out

View File

@ -1,5 +1,11 @@
#include "util/memory-tracker.hxx"
#include <iostream> #include <iostream>
#define LOG_EXPR(expr) std::cout << #expr << " = " << (expr) << "\n"; #define LOG_EXPR(expr) std::cout << #expr << " = " << (expr) << "\n";
std::ostream& operator<<(std::ostream& output, const signalsmith::MemoryTracker& memory) {
output << "{.allocBytes=" << memory.allocBytes << ", .freeBytes=" << memory.freeBytes << ", .current=" << memory.currentBytes << "}";
return output;
}
#include <ctime> #include <ctime>
@ -7,8 +13,6 @@
#include "util/simple-args.h" #include "util/simple-args.h"
#include "util/wav.h" #include "util/wav.h"
#include "util/memory-tracker.hxx"
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
SimpleArgs args(argc, argv); SimpleArgs args(argc, argv);
@ -24,7 +28,7 @@ int main(int argc, char* argv[]) {
Wav inWav; Wav inWav;
if (!inWav.read(inputWav).warn()) args.errorExit("failed to read WAV"); if (!inWav.read(inputWav).warn()) args.errorExit("failed to read WAV");
size_t inputLength = inWav.samples.size()/inWav.channels; size_t inputLength = inWav.samples.size()/inWav.channels;
Wav prevWav; Wav prevWav;
prevWav.read(outputWav); // to verify it's the same output prevWav.read(outputWav); // to verify it's the same output
@ -62,7 +66,7 @@ int main(int argc, char* argv[]) {
stretch.process(inWav, inputLength, outWav, outputLength); stretch.process(inWav, inputLength, outWav, outputLength);
processMemory = processMemory.diff(); processMemory = processMemory.diff();
if (processMemory.allocBytes + processMemory.freeBytes > 0) { if (processMemory) {
std::cout << "Processing (alloc/free/current):\t" << processMemory.allocBytes << "/" << processMemory.freeBytes << "/" << processMemory.currentBytes << "\n"; std::cout << "Processing (alloc/free/current):\t" << processMemory.allocBytes << "/" << processMemory.freeBytes << "/" << processMemory.currentBytes << "\n";
args.errorExit("allocated during process()"); args.errorExit("allocated during process()");
} }

View File

@ -14,6 +14,11 @@ struct MemoryTracker {
MemoryTracker now; MemoryTracker now;
return {now.allocBytes - allocBytes, now.freeBytes - freeBytes}; return {now.allocBytes - allocBytes, now.freeBytes - freeBytes};
} }
// Is a `.diff()` result non-zero
operator bool() const {
return allocBytes > 0 || freeBytes > 0;
}
private: private:
MemoryTracker(size_t allocBytes, size_t freeBytes) : allocBytes(allocBytes), freeBytes(freeBytes), currentBytes(allocBytes - freeBytes) {} MemoryTracker(size_t allocBytes, size_t freeBytes) : allocBytes(allocBytes), freeBytes(freeBytes), currentBytes(allocBytes - freeBytes) {}
}; };

View File

@ -164,8 +164,10 @@ namespace signalsmith { namespace fft {
plan.resize(0); plan.resize(0);
twiddleVector.resize(0); twiddleVector.resize(0);
addPlanSteps(0, 0, _size, 1); addPlanSteps(0, 0, _size, 1);
twiddleVector.shrink_to_fit();
permutation.resize(0); permutation.resize(0);
permutation.reserve(_size);
permutation.push_back(PermutationPair{0, 0}); permutation.push_back(PermutationPair{0, 0});
size_t indexLow = 0, indexHigh = factors.size(); size_t indexLow = 0, indexHigh = factors.size();
size_t inputStepLow = _size, outputStepLow = 1; size_t inputStepLow = _size, outputStepLow = 1;
@ -524,7 +526,8 @@ namespace signalsmith { namespace fft {
} }
} }
return complexFft.setSize(size/2); size_t result = complexFft.setSize(size/2);
return result*2;
} }
size_t setFastSizeAbove(size_t size) { size_t setFastSizeAbove(size_t size) {
return setSize(fastSizeAbove(size)); return setSize(fastSizeAbove(size));

View File

@ -170,6 +170,7 @@ namespace spectral {
*/ */
template<typename Sample> template<typename Sample>
class STFT : public signalsmith::delay::MultiBuffer<Sample> { class STFT : public signalsmith::delay::MultiBuffer<Sample> {
using Super = signalsmith::delay::MultiBuffer<Sample>; using Super = signalsmith::delay::MultiBuffer<Sample>;
using Complex = std::complex<Sample>; using Complex = std::complex<Sample>;
@ -215,7 +216,7 @@ namespace spectral {
+ historyLength); + historyLength);
int fftSize = fft.fastSizeAbove(windowSize + zeroPadding); int fftSize = fft.fastSizeAbove(windowSize + zeroPadding);
this->channels = newChannels; this->channels = newChannels;
_windowSize = windowSize; _windowSize = windowSize;
this->_fftSize = fftSize; this->_fftSize = fftSize;