File size: 3,294 Bytes
2e0d354
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#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<std::complex<float>> 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<bins; ++i) {
            float mag = std::exp(std::min(src[i], std::log(100.f)));
            spectrum[i] = {mag*std::cos(src[bins+i]), mag*std::sin(src[bins+i])};
        }
        pocketfft::c2r<float>({size_t(fft)}, {sizeof(std::complex<float>)}, {sizeof(float)}, 0,
                              false, spectrum.data(), block.data(), 1.f);
        for (int i=0; i<fft; ++i) {
            sum[t*hop+i] += (block[i]*(1.f/fft))*window[i];
            env[t*hop+i] += window[i]*window[i];
        }
    }
    Floats audio(rows*hop);
    for (size_t i=0; i<audio.size(); ++i) {
        size_t j = i + fft/2;
        audio[i] = sum[j] / (env[j] > 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<float>(shared/"hann736.f32",736));
    auto b = branch_audio(head, frames, 2208, 738, read_file<float>(shared/"hann2208.f32",2208));
    auto mixer = read_file<float>(shared/"mixer.f32",254);
    auto kernel = read_file<float>(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; j<x.size(); ++j) {
            float v=0;
            for (int k=0; k<127; ++k) v += padded[j+k]*mixer[ch*127+k];
            mix[17+j] += v;
        }
    }
    Floats wave(frames*2940);
    const float gain=std::pow(10., -3./20.), knee=std::pow(10., -1./20.);
    for (size_t i=0; i<wave.size(); ++i) {
        const float* segment=mix.data()+(i/735)*736;
        const float* coeff=kernel.data()+(i%735)*770;
        float sample=0;
        for (int k=0; k<770; ++k) sample += segment[k]*coeff[k];
        sample *= gain;
        if (std::abs(sample)>knee)
            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<const char*>(&v),2);};
    auto u32=[&](uint32_t v){f.write(reinterpret_cast<const char*>(&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<int16_t>(std::nearbyint(std::clamp(x,-1.f,1.f)*32767.f)));
    f.close();
    require(bool(f),"write waveform.wav");
}