#include "core.hpp" #define POCKETFFT_NO_MULTITHREADING #include "pocketfft.h" static Floats branch_audio(const Floats& head, int frames, int fft, int offset, const Floats& window) { const int hop = 184, rows = frames * 16, bins = fft/2 + 1; Floats sum((rows-1)*hop + fft), env(sum.size()), block(fft); std::vector> spectrum(bins); // Reverse window blocks to match istft_np's increasing block-offset sum. for (int t=rows-1; t>=0; --t) { auto src = head.data() + t*2948 + offset; for (int i=0; i({size_t(fft)}, {sizeof(std::complex)}, {sizeof(float)}, 0, false, spectrum.data(), block.data(), 1.f); for (int i=0; i 1e-11f ? env[j] : 1.f); } return audio; } Floats decode_audio(const Floats& head, int frames, const fs::path& shared) { auto a = branch_audio(head, frames, 736, 0, read_file(shared/"hann736.f32",736)); auto b = branch_audio(head, frames, 2208, 738, read_file(shared/"hann2208.f32",2208)); auto mixer = read_file(shared/"mixer.f32",254); auto kernel = read_file(shared/"resample.f32",735*770); Floats mix(17 + a.size() + 17 + 736); for (int ch=0; ch<2; ++ch) { Floats padded(a.size()+126); auto& x = ch ? b : a; std::copy(x.begin(), x.end(), padded.begin()+63); for (size_t j=0; jknee) sample=std::copysign(knee+(1-knee)*std::tanh((std::abs(sample)-knee)/(1-knee)),sample); require(std::isfinite(sample), "nonfinite waveform"); wave[i]=sample; } return wave; } void save_audio(const fs::path& dest, const Floats& wave) { write_file(dest/"waveform.f32",wave); std::ofstream f(dest/"waveform.wav",std::ios::binary); auto u16=[&](uint16_t v){f.write(reinterpret_cast(&v),2);}; auto u32=[&](uint32_t v){f.write(reinterpret_cast(&v),4);}; f.write("RIFF",4); u32(36+wave.size()*2); f.write("WAVEfmt ",8); u32(16); u16(1); u16(1); u32(44100); u32(88200); u16(2); u16(16); f.write("data",4); u32(wave.size()*2); for (float x:wave) u16(static_cast(std::nearbyint(std::clamp(x,-1.f,1.f)*32767.f))); f.close(); require(bool(f),"write waveform.wav"); }