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
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
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:
rm -rf out

View File

@ -1,5 +1,11 @@
#include "util/memory-tracker.hxx"
#include <iostream>
#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>
@ -7,8 +13,6 @@
#include "util/simple-args.h"
#include "util/wav.h"
#include "util/memory-tracker.hxx"
int main(int argc, char* argv[]) {
SimpleArgs args(argc, argv);
@ -24,7 +28,7 @@ int main(int argc, char* argv[]) {
Wav inWav;
if (!inWav.read(inputWav).warn()) args.errorExit("failed to read WAV");
size_t inputLength = inWav.samples.size()/inWav.channels;
Wav prevWav;
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);
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";
args.errorExit("allocated during process()");
}

View File

@ -14,6 +14,11 @@ struct MemoryTracker {
MemoryTracker now;
return {now.allocBytes - allocBytes, now.freeBytes - freeBytes};
}
// Is a `.diff()` result non-zero
operator bool() const {
return allocBytes > 0 || freeBytes > 0;
}
private:
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);
twiddleVector.resize(0);
addPlanSteps(0, 0, _size, 1);
twiddleVector.shrink_to_fit();
permutation.resize(0);
permutation.reserve(_size);
permutation.push_back(PermutationPair{0, 0});
size_t indexLow = 0, indexHigh = factors.size();
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) {
return setSize(fastSizeAbove(size));

View File

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