File size: 9,399 Bytes
ab1b068 | 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | #include "synth.h"
#include <algorithm>
#include <cmath>
#include <complex>
#include <stdexcept>
namespace {
using cd = std::complex<double>;
const double PI = 3.14159265358979323846;
// In-place iterative radix-2 FFT (n a power of two). inverse: no 1/n scaling.
void fft(std::vector<cd>& a, bool inverse) {
const size_t n = a.size();
for (size_t i = 1, j = 0; i < n; ++i) {
size_t bit = n >> 1;
for (; j & bit; bit >>= 1) j ^= bit;
j ^= bit;
if (i < j) std::swap(a[i], a[j]);
}
for (size_t len = 2; len <= n; len <<= 1) {
const double ang = 2 * PI / double(len) * (inverse ? 1 : -1);
const cd wl(std::cos(ang), std::sin(ang));
for (size_t i = 0; i < n; i += len) {
cd w(1);
for (size_t k = 0; k < len / 2; ++k) {
const cd u = a[i + k], v = a[i + k + len / 2] * w;
a[i + k] = u + v;
a[i + k + len / 2] = u - v;
w *= wl;
}
}
}
}
std::vector<double> hann(int n) { // torch.hann_window(n) (periodic)
std::vector<double> w(n);
for (int i = 0; i < n; ++i) w[i] = 0.5 - 0.5 * std::cos(2 * PI * i / n);
return w;
}
// cepstrum_response: exp(rfft(buffer)) with the real part clamped to [-14, 6]; buffer = causal at the start, the
// anticausal coefficients (flipped) at the end. Returns fft/2+1 bins.
void cepstrum_response(const float* causal, int n_causal, const float* anti, int n_anti, int n_fft, std::vector<cd>& work,
cd* out) {
work.assign(n_fft, cd(0));
for (int i = 0; i < n_causal; ++i) work[i] = causal[i];
for (int j = 0; j < n_anti; ++j) work[n_fft - n_anti + j] = anti[n_anti - 1 - j];
fft(work, false);
for (int k = 0; k <= n_fft / 2; ++k) {
const double re = std::clamp(work[k].real(), -14.0, 6.0);
out[k] = std::polar(std::exp(re), work[k].imag());
}
}
// SourceFilter._shape: STFT (centered, zero padded, Hann), multiply by response[frame][bin], inverse STFT (overlap-add,
// divided by the summed squared window), trimmed to the source length. The source is padded with fft/2 trailing zeros
// first, as in the model.
template <typename ResponseFn> // response(frame, cd* bins): fills fft/2+1 bins for one frame, on demand
std::vector<double> shape(const std::vector<double>& source_in, ResponseFn&& response, int frames, int n_fft, int hop) {
const int S = int(source_in.size());
const int L = S + n_fft / 2; // F.pad(source, (0, fft // 2))
const int bins = n_fft / 2 + 1;
const int n_frames = 1 + L / hop;
if (n_frames != frames) throw std::runtime_error("filter frames do not match the source");
std::vector<double> padded(L + n_fft, 0.0); // center=True: n_fft/2 zeros on both sides
std::copy(source_in.begin(), source_in.end(), padded.begin() + n_fft / 2);
const std::vector<double> w = hann(n_fft);
std::vector<double> ola(size_t(n_fft) + size_t(hop) * (n_frames - 1), 0.0), env(ola.size(), 0.0);
std::vector<cd> buf(n_fft), r(bins);
for (int f = 0; f < n_frames; ++f) {
const double* x = padded.data() + size_t(f) * hop;
for (int n = 0; n < n_fft; ++n) buf[n] = cd(x[n] * w[n], 0.0);
fft(buf, false);
response(f, r.data());
for (int k = 0; k < bins; ++k) buf[k] *= r[k];
// one-sided -> full Hermitian spectrum (irfft ignores the imaginary part of DC and Nyquist)
buf[0] = cd(buf[0].real(), 0.0);
buf[n_fft / 2] = cd(buf[n_fft / 2].real(), 0.0);
for (int k = 1; k < n_fft / 2; ++k) buf[n_fft - k] = std::conj(buf[k]);
fft(buf, true);
double* o = ola.data() + size_t(f) * hop;
double* e = env.data() + size_t(f) * hop;
for (int n = 0; n < n_fft; ++n) {
o[n] += buf[n].real() / n_fft * w[n];
e[n] += w[n] * w[n];
}
}
std::vector<double> y(S);
for (int i = 0; i < S; ++i) { // drop the centering pad, keep the first S samples
const size_t j = size_t(i) + n_fft / 2;
y[i] = env[j] > 1e-11 ? ola[j] / env[j] : 0.0;
}
return y;
}
} // namespace
std::vector<float> vocos_istft(const float* head, int frames, int n_fft, int hop) {
const int bins = n_fft / 2 + 1;
const std::vector<double> w = hann(n_fft);
std::vector<double> ola(size_t(n_fft) + size_t(hop) * (frames - 1), 0.0), env(ola.size(), 0.0);
std::vector<cd> buf(n_fft);
for (int f = 0; f < frames; ++f) {
const float* row = head + size_t(f) * 2 * bins; // [log magnitude x bins, phase x bins]
for (int k = 0; k < bins; ++k) buf[k] = std::polar(std::min(std::exp(double(row[k])), 100.0), double(row[bins + k]));
buf[0] = cd(buf[0].real(), 0.0); // irfft ignores the imaginary part of DC and Nyquist
buf[n_fft / 2] = cd(buf[n_fft / 2].real(), 0.0);
for (int k = 1; k < n_fft / 2; ++k) buf[n_fft - k] = std::conj(buf[k]);
fft(buf, true);
double* o = ola.data() + size_t(f) * hop;
double* e = env.data() + size_t(f) * hop;
for (int n = 0; n < n_fft; ++n) {
o[n] += buf[n].real() / n_fft * w[n];
e[n] += w[n] * w[n];
}
}
const size_t S = size_t(hop) * (frames - 1); // center=True: n_fft/2 trimmed at both ends
std::vector<float> y(S);
for (size_t i = 0; i < S; ++i) {
const size_t j = i + n_fft / 2;
y[i] = float(env[j] > 1e-11 ? ola[j] / env[j] : 0.0);
}
return y;
}
std::vector<float> pulse_train(const float* f0, int T, int sample_rate, int hop, double limit) {
std::vector<double> knots(T + 1);
for (int t = 0; t < T; ++t) knots[t] = std::log(std::max<double>(f0[t], 1.0));
knots[T] = knots[T - 1];
const size_t S = size_t(T) * hop;
std::vector<float> out(S);
double cum = 0.0;
for (size_t i = 0; i < S; ++i) {
const double position = double(i) / hop;
const size_t low = size_t(std::floor(position));
const double weight = position - double(low);
const double frequency = std::exp(knots[low] * (1 - weight) + knots[low + 1] * weight);
cum += frequency / sample_rate;
double theta = std::fmod(2 * PI * cum, 2 * PI);
if (theta < 0) theta += 2 * PI;
const double harmonics = std::max(limit * sample_rate / frequency, 1.0);
const double whole = std::floor(harmonics);
const double den = 2 * std::sin(theta / 2);
const double core = std::fabs(den) < 1e-7 ? whole : std::sin((whole + 0.5) * theta) / den - 0.5;
const double wave = core + (harmonics - whole) * std::cos((whole + 1) * theta);
out[i] = float(wave * std::sqrt(2.0 / harmonics));
}
return out;
}
std::vector<float> synthesize_wave(const SynthConfig& c, const float* harmonic, const float* noise_head, const float* f0,
int T, const float* noise) {
const int hsteps = c.hop / c.synth_hop, nsteps = c.hop / c.noise_hop;
const int hw = c.cep_causal + c.cep_anti + hsteps, nw = c.noise_cep + nsteps;
const int last = T - 1;
const size_t S = size_t(T) * c.hop;
std::vector<cd> work;
// harmonic branch: envelope interpolated between frames, one gain per sub-frame
const int hframes = hsteps * T + 3;
std::vector<float> causal(c.cep_causal), anti(c.cep_anti);
auto hresp = [&](int j, cd* out) {
const int sub = std::min(j, hsteps * T - 1);
const int here = sub / hsteps, following = std::min((sub + 1) / hsteps, last);
const float* a = harmonic + size_t(here) * hw;
const float* b = harmonic + size_t(following) * hw;
for (int k = 0; k < c.cep_causal; ++k) causal[k] = 0.5f * (a[k] + b[k]) * c.causal_lifter[k];
for (int k = 0; k < c.cep_anti; ++k)
anti[k] = 0.5f * (a[c.cep_causal + k] + b[c.cep_causal + k]) * c.anti_lifter[k];
const int gi = sub; // gains flattened [T * hsteps]
causal[0] += harmonic[size_t(gi / hsteps) * hw + c.cep_causal + c.cep_anti + gi % hsteps];
cepstrum_response(causal.data(), c.cep_causal, anti.data(), c.cep_anti, c.synth_fft, work, out);
};
// noise branch: one envelope per frame, nsteps gains per frame
const int nframes = nsteps * T + 3;
std::vector<float> shp(c.noise_cep);
auto nresp = [&](int j, cd* out) {
const int sub = std::min(j, nsteps * T - 1);
const float* a = noise_head + size_t(sub / nsteps) * nw;
for (int k = 0; k < c.noise_cep; ++k) shp[k] = a[k] * c.noise_lifter[k];
shp[0] += noise_head[size_t(sub / nsteps) * nw + c.noise_cep + sub % nsteps];
cepstrum_response(shp.data(), c.noise_cep, nullptr, 0, c.noise_fft, work, out);
};
const std::vector<float> pulses = pulse_train(f0, T, c.sample_rate, c.hop, c.harmonic_limit);
std::vector<double> ps(pulses.begin(), pulses.end()), ns(noise, noise + S);
const std::vector<double> yh = shape(ps, hresp, hframes, c.synth_fft, c.synth_hop);
const std::vector<double> yn = shape(ns, nresp, nframes, c.noise_fft, c.noise_hop);
std::vector<float> wave(S);
for (size_t i = 0; i < S; ++i) wave[i] = float(yh[i] + yn[i]);
return wave;
}
|