package com.voicecraft.realtimevoice; import android.content.Context; import android.util.Log; import org.json.JSONArray; import org.json.JSONObject; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.charset.StandardCharsets; /** * 变声参数配置 —— JSON 文件持久化,全部参数可手工调试。 * * 配置文件位置(可用 adb push / 文件管理器直接编辑,重启 App 生效): * /sdcard/Android/data/com.voicecraft.realtimevoice/files/voice_config.json * * 参数含义(调"像不像女声"就动这几个): * pitchSemitones 音调(半音) 男变女约 +5~+12,越高越"亮" * formantRatio 共振峰搬移比 1.15~1.25 自然区间,越高越"女"(防小黄人的核心) * strength 效果强度 0~1 整体效果按比例缩放 * eqFreqHz 亮度 EQ 频点 500~8000Hz * eqGainDb 亮度 EQ 增益 0~10dB,女声"空气感" * gateThreshold 噪声门阈值 0~0.01,越低越灵敏(防切字) * f0MinHz/f0MaxHz 基频检测范围 限制 YIN 搜索区间,提高检测稳定 * smoothing 参数平滑系数 0.001~0.1,越小越平滑(防抖/防颤音) */ public class VoiceConfig { private static final String TAG = "VoiceConfig"; public float pitchSemitones = 9f; public float formantRatio = 1.20f; public float strength = 1f; public float eqFreqHz = 3000f; public float eqGainDb = 3f; public float gateThreshold = 0.006f; public float f0MinHz = 65f; public float f0MaxHz = 500f; public float smoothing = 0.05f; public boolean bypass = false; /** 输出增益(0.1~2.0):双机线路模式下匹配目标手机麦克风电平 */ public float outputGain = 1f; /** 目标音高模式:输出 f0 锚定 targetF0Hz(深沉嗓音自动多补),false=按半音比例变调 */ public boolean targetMode = false; /** 目标音高(Hz),御姐约 205 */ public float targetF0Hz = 205f; // 预设 {音调半音, 共振峰比},顺序固定对应界面:少女 / 御姐 / 萝莉 / 中性女 public float[][] presets = { {9f, 1.20f}, {7f, 1.15f}, {12f, 1.25f}, {5f, 1.12f}, }; public static File fileOf(Context ctx) { File dir = ctx.getExternalFilesDir(null); if (dir == null) dir = ctx.getFilesDir(); return new File(dir, "voice_config.json"); } /** 读取配置;文件不存在或损坏时返回默认值(并尝试写出默认文件供后续编辑) */ public static VoiceConfig load(Context ctx) { VoiceConfig cfg = new VoiceConfig(); File f = fileOf(ctx); if (!f.exists()) { cfg.save(ctx); return cfg; } try (FileInputStream in = new FileInputStream(f)) { byte[] buf = new byte[(int) f.length()]; int off = 0; while (off < buf.length) { int r = in.read(buf, off, buf.length - off); if (r < 0) break; off += r; } cfg.fromJson(new JSONObject(new String(buf, 0, off, StandardCharsets.UTF_8))); } catch (Exception e) { Log.w(TAG, "配置读取失败,使用默认值: " + e.getMessage()); } return cfg; } public void save(Context ctx) { File f = fileOf(ctx); try (FileOutputStream out = new FileOutputStream(f)) { out.write(toJson().toString(2).getBytes(StandardCharsets.UTF_8)); } catch (Exception e) { Log.w(TAG, "配置保存失败: " + e.getMessage()); } } private JSONObject toJson() throws Exception { JSONObject root = new JSONObject(); JSONObject p = new JSONObject(); p.put("pitchSemitones", pitchSemitones); p.put("formantRatio", formantRatio); p.put("strength", strength); p.put("eqFreqHz", eqFreqHz); p.put("eqGainDb", eqGainDb); p.put("gateThreshold", gateThreshold); p.put("f0MinHz", f0MinHz); p.put("f0MaxHz", f0MaxHz); p.put("smoothing", smoothing); p.put("bypass", bypass); p.put("outputGain", outputGain); p.put("targetMode", targetMode); p.put("targetF0Hz", targetF0Hz); root.put("params", p); String[] names = {"少女", "御姐", "萝莉", "中性女"}; JSONArray arr = new JSONArray(); for (int i = 0; i < presets.length && i < names.length; i++) { JSONObject one = new JSONObject(); one.put("name", names[i]); one.put("pitchSemitones", presets[i][0]); one.put("formantRatio", presets[i][1]); arr.put(one); } root.put("presets", arr); root.put("version", 1); return root; } private void fromJson(JSONObject root) throws Exception { JSONObject p = root.optJSONObject("params"); if (p != null) { pitchSemitones = (float) p.optDouble("pitchSemitones", pitchSemitones); formantRatio = (float) p.optDouble("formantRatio", formantRatio); strength = (float) p.optDouble("strength", strength); eqFreqHz = (float) p.optDouble("eqFreqHz", eqFreqHz); eqGainDb = (float) p.optDouble("eqGainDb", eqGainDb); gateThreshold = (float) p.optDouble("gateThreshold", gateThreshold); f0MinHz = (float) p.optDouble("f0MinHz", f0MinHz); f0MaxHz = (float) p.optDouble("f0MaxHz", f0MaxHz); smoothing = (float) p.optDouble("smoothing", smoothing); bypass = p.optBoolean("bypass", bypass); outputGain = (float) p.optDouble("outputGain", outputGain); targetMode = p.optBoolean("targetMode", targetMode); targetF0Hz = (float) p.optDouble("targetF0Hz", targetF0Hz); } JSONArray arr = root.optJSONArray("presets"); if (arr != null) { for (int i = 0; i < arr.length() && i < presets.length; i++) { JSONObject one = arr.optJSONObject(i); if (one == null) continue; presets[i][0] = (float) one.optDouble("pitchSemitones", presets[i][0]); presets[i][1] = (float) one.optDouble("formantRatio", presets[i][1]); } } } }