131 lines
4.8 KiB
C++
131 lines
4.8 KiB
C++
#pragma once
|
||||
|
|
|
|||
|
|
// voice_config.json 极简解析器(与 Android 版配置格式完全一致)
|
|||
|
|
// 只解析 "params" 段的扁平键值;键按首次出现匹配(params 段在文件中先于 presets)
|
|||
|
|
|
|||
|
|
#include <cstdio>
|
|||
|
|
#include <cstring>
|
|||
|
|
#include <string>
|
|||
|
|
|
|||
|
|
struct VoiceCfg {
|
|||
|
|
float pitchSemitones = 9.f;
|
|||
|
|
float formantRatio = 1.20f;
|
|||
|
|
float strength = 1.f;
|
|||
|
|
float eqFreqHz = 3000.f;
|
|||
|
|
float eqGainDb = 3.f;
|
|||
|
|
float gateThreshold = 0.006f;
|
|||
|
|
float f0MinHz = 65.f;
|
|||
|
|
float f0MaxHz = 500.f;
|
|||
|
|
float smoothing = 0.05f;
|
|||
|
|
float outputGain = 1.f;
|
|||
|
|
bool bypass = false;
|
|||
|
|
// 目标音高模式:输出 f0 锚定 targetF0Hz(深沉嗓音自动多补)
|
|||
|
|
bool targetMode = false;
|
|||
|
|
float targetF0Hz = 205.f;
|
|||
|
|
// 预设 {音调半音, 共振峰比},顺序:少女 / 御姐 / 萝莉 / 中性女
|
|||
|
|
float presets[4][2] = {
|
|||
|
|
{9.f, 1.20f},
|
|||
|
|
{7.f, 1.15f},
|
|||
|
|
{12.f, 1.25f},
|
|||
|
|
{5.f, 1.12f},
|
|||
|
|
};
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
namespace cfgdetail {
|
|||
|
|
|
|||
|
|
inline bool findValue(const std::string& text, const char* key, std::string& out) {
|
|||
|
|
std::string pat = std::string("\"") + key + "\"";
|
|||
|
|
size_t pos = text.find(pat);
|
|||
|
|
if (pos == std::string::npos) return false;
|
|||
|
|
pos = text.find(':', pos + pat.size());
|
|||
|
|
if (pos == std::string::npos) return false;
|
|||
|
|
++pos;
|
|||
|
|
while (pos < text.size() && (text[pos] == ' ' || text[pos] == '\t')) ++pos;
|
|||
|
|
size_t end = pos;
|
|||
|
|
while (end < text.size() && text[end] != ',' && text[end] != '}' &&
|
|||
|
|
text[end] != '\n' && text[end] != '\r') {
|
|||
|
|
++end;
|
|||
|
|
}
|
|||
|
|
out = text.substr(pos, end - pos);
|
|||
|
|
// 去掉首尾空白
|
|||
|
|
while (!out.empty() && (out.back() == ' ' || out.back() == '\t')) out.pop_back();
|
|||
|
|
return !out.empty();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
inline float parseFloat(const std::string& text, const char* key, float def) {
|
|||
|
|
std::string v;
|
|||
|
|
if (!findValue(text, key, v)) return def;
|
|||
|
|
return (float)atof(v.c_str());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
inline bool parseBool(const std::string& text, const char* key, bool def) {
|
|||
|
|
std::string v;
|
|||
|
|
if (!findValue(text, key, v)) return def;
|
|||
|
|
return v.find("true") != std::string::npos;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 取第 n 次出现的键值(用于 presets 数组里的成对数值,n 从 0 起)
|
|||
|
|
inline float findNthFloat(const std::string& text, const char* key, int n, float def) {
|
|||
|
|
std::string pat = std::string("\"") + key + "\"";
|
|||
|
|
size_t pos = std::string::npos;
|
|||
|
|
for (int i = 0; i <= n; ++i) {
|
|||
|
|
pos = text.find(pat, pos + 1);
|
|||
|
|
if (pos == std::string::npos) return def;
|
|||
|
|
}
|
|||
|
|
pos = text.find(':', pos + pat.size());
|
|||
|
|
if (pos == std::string::npos) return def;
|
|||
|
|
++pos;
|
|||
|
|
while (pos < text.size() && (text[pos] == ' ' || text[pos] == '\t')) ++pos;
|
|||
|
|
return (float)atof(text.c_str() + pos);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
} // namespace cfgdetail
|
|||
|
|
|
|||
|
|
// 读取配置;失败时保留默认值并返回 false
|
|||
|
|
inline bool loadConfig(const std::string& path, VoiceCfg& c) {
|
|||
|
|
FILE* f = fopen(path.c_str(), "rb");
|
|||
|
|
if (!f) return false;
|
|||
|
|
fseek(f, 0, SEEK_END);
|
|||
|
|
long len = ftell(f);
|
|||
|
|
fseek(f, 0, SEEK_SET);
|
|||
|
|
if (len <= 0 || len > 1024 * 1024) {
|
|||
|
|
fclose(f);
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
std::string text((size_t)len, '\0');
|
|||
|
|
size_t rd = fread(&text[0], 1, (size_t)len, f);
|
|||
|
|
fclose(f);
|
|||
|
|
text.resize(rd);
|
|||
|
|
|
|||
|
|
// params 段与 presets 段分开解析(键按段内出现顺序匹配)
|
|||
|
|
const size_t cut = text.find("\"presets\"");
|
|||
|
|
std::string paramsPart = (cut == std::string::npos) ? text : text.substr(0, cut);
|
|||
|
|
std::string presetsPart = (cut == std::string::npos) ? "" : text.substr(cut);
|
|||
|
|
|
|||
|
|
{
|
|||
|
|
using namespace cfgdetail;
|
|||
|
|
c.pitchSemitones = parseFloat(paramsPart, "pitchSemitones", c.pitchSemitones);
|
|||
|
|
c.formantRatio = parseFloat(paramsPart, "formantRatio", c.formantRatio);
|
|||
|
|
c.strength = parseFloat(paramsPart, "strength", c.strength);
|
|||
|
|
c.eqFreqHz = parseFloat(paramsPart, "eqFreqHz", c.eqFreqHz);
|
|||
|
|
c.eqGainDb = parseFloat(paramsPart, "eqGainDb", c.eqGainDb);
|
|||
|
|
c.gateThreshold = parseFloat(paramsPart, "gateThreshold", c.gateThreshold);
|
|||
|
|
c.f0MinHz = parseFloat(paramsPart, "f0MinHz", c.f0MinHz);
|
|||
|
|
c.f0MaxHz = parseFloat(paramsPart, "f0MaxHz", c.f0MaxHz);
|
|||
|
|
c.smoothing = parseFloat(paramsPart, "smoothing", c.smoothing);
|
|||
|
|
c.outputGain = parseFloat(paramsPart, "outputGain", c.outputGain);
|
|||
|
|
c.bypass = parseBool(paramsPart, "bypass", c.bypass);
|
|||
|
|
c.targetMode = parseBool(paramsPart, "targetMode", c.targetMode);
|
|||
|
|
c.targetF0Hz = parseFloat(paramsPart, "targetF0Hz", c.targetF0Hz);
|
|||
|
|
|
|||
|
|
// presets:按出现顺序成对读取(少女/御姐/萝莉/中性女)
|
|||
|
|
if (!presetsPart.empty()) {
|
|||
|
|
for (int i = 0; i < 4; ++i) {
|
|||
|
|
c.presets[i][0] = findNthFloat(presetsPart, "pitchSemitones", i, c.presets[i][0]);
|
|||
|
|
c.presets[i][1] = findNthFloat(presetsPart, "formantRatio", i, c.presets[i][1]);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return true;
|
|||
|
|
}
|