feat: 初始化变声器项目仓库(逆向分析 + 实时变声 + PC 变声麦)

包含三大模块:
- 逆向分析报告与实时变声方案设计文档
- realtime-voice:Android 实时变声(PSOLA 变调 + VTLP 共振峰 DSP 管线、Xposed 注入模块)
- pc-voice:PC 变声麦工具(miniaudio 引擎、可配置 DSP 参数)

已通过 .gitignore 排除构建产物、本机工具链与本机路径配置。

Co-Authored-By: Claude Code <[email protected]>
This commit is contained in:
2026-11-06 16:51:06 +08:00
co-authored by Claude Code
commit c10f271d87
39 changed files with 99673 additions and 0 deletions
+244
View File
@@ -0,0 +1,244 @@
// engine_test.cpp — 引擎离线自测台(不需要声卡,不需要人耳)
//
// 流程:合成已知参数的男声(f0≈105Hz + /a/ 元音共振峰 730/1090/2440Hz)
// → 走完整 DSP 管线(与实时版同一份代码)
// → 客观测量输出(基频/共振峰/高频清晰度/谐波锐度)
// → 输出 test/in.wav 与 test/out.wav 供人耳 A/B
//
// 编译:g++ -O2 -std=c++17 -I../realtime-voice/app/src/main/cpp engine_test.cpp
// ../realtime-voice/app/src/main/cpp/psola.cpp ../realtime-voice/app/src/main/cpp/dsp_pipeline.cpp -o engine_test.exe
#include <cmath>
#include <cstdio>
#include <cstring>
#include <direct.h>
#include <string>
#include <vector>
#include <algorithm>
#include "dsp_pipeline.h"
static const double kSr = 48000.0;
static const double kPi = 3.14159265358979;
// ---------- 合成:男声 /a/ 元音 ----------
// 脉冲串(f0 带轻微颤音)→ 三个全极点共振峰滤波器
static void synthMaleVowel(std::vector<float>& out, double dur) {
const int n = (int)(dur * kSr);
out.assign((size_t)n, 0.f);
// 1) 基频脉冲串(100→108Hz 缓升 + 3Hz 颤音)
double phase = 0.0;
for (int i = 0; i < n; ++i) {
const double t = (double)i / kSr;
const double f0 = 100.0 + 8.0 * t / dur + 3.0 * std::sin(2.0 * kPi * 3.0 * t);
phase += f0 / kSr;
if (phase >= 1.0) {
phase -= 1.0;
out[(size_t)i] = 1.0f;
}
}
// 2) 共振峰(全极点二阶谐振器级联,/a/: F1=730 F2=1090 F3=2440)
struct Formant { double f, bw; };
const Formant fs[3] = {{730, 90}, {1090, 110}, {2440, 160}};
for (const auto& fm : fs) {
const double r = std::exp(-kPi * fm.bw / kSr);
const double th = 2.0 * kPi * fm.f / kSr;
const double a1 = 2.0 * r * std::cos(th);
const double a2 = -r * r;
const double g = 1.0 - r;
double y1 = 0, y2 = 0;
for (int i = 0; i < n; ++i) {
const double y = g * (double)out[(size_t)i] + a1 * y1 + a2 * y2;
y2 = y1;
y1 = y;
out[(size_t)i] = (float)y;
}
}
// 3) 幅度包络(淡入淡出防爆音)+ 电平归一
float peak = 0.f;
for (float v : out) peak = std::max(peak, std::fabs(v));
if (peak > 0.f) {
for (auto& v : out) v *= 0.5f / peak;
}
const int fade = (int)(0.05 * kSr);
for (int i = 0; i < fade; ++i) {
const float w = (float)i / (float)fade;
out[(size_t)i] *= w;
out[(size_t)(n - 1 - i)] *= w;
}
}
// ---------- 测量工具 ----------
// 自相关法测基频(先低通 800Hz 去掉高频谐波对自相关峰值的干扰)
static double measureF0(const float* x, int n, double sr) {
std::vector<float> buf((size_t)n);
double lp = 0.0;
const double a = std::exp(-2.0 * kPi * 800.0 / sr);
for (int i = 0; i < n; ++i) {
lp = (1.0 - a) * x[i] + a * lp;
buf[(size_t)i] = (float)lp;
}
double mean = 0.0;
for (int i = 0; i < n; ++i) mean += buf[(size_t)i];
mean /= n;
for (int i = 0; i < n; ++i) buf[(size_t)i] = (float)(buf[(size_t)i] - mean);
const int lagMin = (int)(sr / 400.0);
const int lagMax = std::min((int)(sr / 60.0), n / 2);
double best = -1.0;
int bestLag = 0;
for (int lag = lagMin; lag <= lagMax; ++lag) {
double s = 0.0;
for (int i = 0; i + lag < n; ++i) s += (double)buf[(size_t)i] * buf[(size_t)(i + lag)];
s /= (n - lag);
if (s > best) {
best = s;
bestLag = lag;
}
}
return bestLag > 0 ? sr / (double)bestLag : 0.0;
}
// Goertzel 单频幅度
static double goertzel(const std::vector<float>& x, double f, double sr) {
const double w = 2.0 * kPi * f / sr;
const double coeff = 2.0 * std::cos(w);
double s1 = 0, s2 = 0;
for (float v : x) {
const double s0 = (double)v + coeff * s1 - s2;
s2 = s1;
s1 = s0;
}
const double power = s1 * s1 + s2 * s2 - coeff * s1 * s2;
return std::sqrt(std::max(0.0, power)) / ((double)x.size() / 2.0);
}
// 谐波噪声比 HNR(dB):谐波处能量 vs 泛音间能量——"清晰/模糊"的正确度量
static double hnrDb(const std::vector<float>& x, double f0, double sr) {
if (f0 < 20.0) return 0.0;
double h = 0.0, n = 0.0;
int ch = 0, cn = 0;
for (int k = 1; k <= 12; ++k) {
const double fh = f0 * k;
if (fh > 5000.0) break;
h += goertzel(x, fh, sr);
++ch;
const double fn = f0 * (k + 0.5);
if (fn <= 5000.0) {
n += goertzel(x, fn, sr);
++cn;
}
}
if (ch == 0 || cn == 0) return 0.0;
const double mh = h / ch, mn = n / cn;
return 20.0 * std::log10(mh / std::max(1e-9, mn));
}
// 谱包络共振峰:粗网格幅度谱 → 平滑 → 找包络峰(避开谐波栅格的干扰)
static double envelopePeak(const std::vector<float>& x, double fLo, double fHi, double sr) {
std::vector<double> mag;
for (double f = fLo; f <= fHi; f += 20.0) mag.push_back(goertzel(x, f, sr));
// 5 点平滑成包络
std::vector<double> env(mag.size(), 0.0);
for (size_t i = 0; i < mag.size(); ++i) {
double s = 0.0;
int c = 0;
for (int d = -2; d <= 2; ++d) {
const int j = (int)i + d;
if (j >= 0 && j < (int)mag.size()) {
s += mag[(size_t)j];
++c;
}
}
env[i] = s / c;
}
size_t best = 0;
for (size_t i = 1; i < env.size(); ++i) {
if (env[i] > env[best]) best = i;
}
return fLo + 20.0 * (double)best;
}
static void writeWav(const std::string& path, const std::vector<float>& x, double sr) {
FILE* f = fopen(path.c_str(), "wb");
if (!f) return;
const int n = (int)x.size();
const int dataBytes = n * 2;
const int srI = (int)sr;
unsigned char hdr[44];
memcpy(hdr, "RIFF", 4);
*(unsigned int*)(hdr + 4) = (unsigned)(36 + dataBytes);
memcpy(hdr + 8, "WAVEfmt ", 8);
*(unsigned int*)(hdr + 16) = 16;
*(unsigned short*)(hdr + 20) = 1;
*(unsigned short*)(hdr + 22) = 1;
*(unsigned int*)(hdr + 24) = (unsigned)srI;
*(unsigned int*)(hdr + 28) = (unsigned)(srI * 2);
*(unsigned short*)(hdr + 32) = 2;
*(unsigned short*)(hdr + 34) = 16;
memcpy(hdr + 36, "data", 4);
*(unsigned int*)(hdr + 40) = (unsigned)dataBytes;
fwrite(hdr, 1, 44, f);
for (float v : x) {
float c = std::max(-1.f, std::min(1.f, v));
short s = (short)(c * 32767.f);
fwrite(&s, 2, 1, f);
}
fclose(f);
}
int main() {
printf("==== 引擎离线自测 ====\n");
_mkdir("test");
// 1) 合成男声(含 0.3s 静音段测门限)
std::vector<float> input;
synthMaleVowel(input, 2.0);
// 2) 走完整管线(与实时版同一份代码,参数=当前御姐配置)
DspPipeline pipe;
pipe.setup((float)kSr);
pipe.setParams(10.f, 1.17f, 1.f, 2800.f, 1.5f, 0.005f, 70.f, 350.f, 0.02f, false);
pipe.setOutputGain(1.f);
pipe.setTargetMode(true, 205.f);
std::vector<float> output(input.size() + 8192, 0.f);
const int chunk = 480;
for (size_t i = 0; i < input.size(); i += chunk) {
pipe.process(input.data() + i, output.data() + i, (int)std::min((size_t)chunk, input.size() - i));
}
output.resize(input.size());
// 3) 测量(跳过开头 0.3s 的管线建立期,在 0.5~1.5s 稳态区测)
const int m0 = (int)(0.5 * kSr);
const int mN = (int)(1.0 * kSr);
std::vector<float> segIn(input.begin() + m0, input.begin() + m0 + mN);
std::vector<float> segOut(output.begin() + m0, output.begin() + m0 + mN);
const double f0Synth = 104.0; // 合成真值(100→108 缓升的中值,±3Hz 颤音)
const double f0Out = measureF0(segOut.data(), mN, kSr);
const double f1In = envelopePeak(segIn, 400, 1200, kSr);
const double f1Out = envelopePeak(segOut, 400, 1400, kSr);
const double f2In = envelopePeak(segIn, 900, 2000, kSr);
const double f2Out = envelopePeak(segOut, 1000, 2400, kSr);
const double hnrIn = hnrDb(segIn, f0Synth, kSr);
const double hnrOut = hnrDb(segOut, f0Out > 20 ? f0Out : 205.0, kSr);
printf("\n%-16s %10s %10s %s\n", "指标", "输入(男声)", "输出", "判定");
printf("%-14s %10.1f %10.1f %s\n", "基频f0(Hz)", f0Synth, f0Out,
(f0Out > 190 && f0Out < 235) ? "[OK 锚定女声区]" : "[!! 偏离目标]");
printf("%-14s %10.1f %10.1f 比值x%.2f (期望x1.17)\n", "F1包络(Hz)", f1In, f1Out, f1Out / f1In);
printf("%-14s %10.1f %10.1f 比值x%.2f (期望x1.17)\n", "F2包络(Hz)", f2In, f2Out, f2Out / f2In);
printf("%-14s %10.1f %10.1f %s\n", "谐波噪声比dB", hnrIn, hnrOut,
hnrOut >= 8.0 ? "[OK 清晰]" : (hnrOut >= 5.0 ? "[一般]" : "[!! 模糊]"));
printf("\n参考基频(合成真值):测量输入请以 %.0fHz 为准;上表 F 值为谱包络峰(±20Hz 栅格)。\n", f0Synth);
printf("\nWAV 已写出: test\\in.wav(原始男声) test\\out.wav(变声后)——戴上耳机对比听\n");
printf("注意:这两条是引擎裸输出,未经微信处理;微信端的降噪会进一步涂抹音质。\n");
writeWav("test/in.wav", input, kSr);
writeWav("test/out.wav", output, kSr);
return 0;
}