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:
@@ -0,0 +1,48 @@
|
||||
plugins {
|
||||
id 'com.android.application'
|
||||
}
|
||||
|
||||
android {
|
||||
namespace 'com.voicecraft.xposed'
|
||||
compileSdk 35
|
||||
|
||||
defaultConfig {
|
||||
applicationId 'com.voicecraft.xposed'
|
||||
minSdk 24
|
||||
targetSdk 34
|
||||
versionCode 1
|
||||
versionName '0.1.0-m1'
|
||||
|
||||
ndk {
|
||||
abiFilters 'arm64-v8a'
|
||||
}
|
||||
externalNativeBuild {
|
||||
cmake {
|
||||
cppFlags '-O2'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
}
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_17
|
||||
targetCompatibility JavaVersion.VERSION_17
|
||||
}
|
||||
|
||||
externalNativeBuild {
|
||||
cmake {
|
||||
path file('src/main/cpp/CMakeLists.txt')
|
||||
version '3.22.1'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
// Xposed API 82(经典版,LSPosed 完全兼容)——仅编译期,运行时由框架提供
|
||||
compileOnly 'de.robv.android.xposed:api:82'
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<application
|
||||
android:label="变声注入模块"
|
||||
android:allowBackup="false">
|
||||
|
||||
<!-- Xposed 模块元数据(LSPosed 识别) -->
|
||||
<meta-data
|
||||
android:name="xposedmodule"
|
||||
android:value="true" />
|
||||
<meta-data
|
||||
android:name="xposeddescription"
|
||||
android:value="实时男变女变声注入模块(M1):hook AudioRecord,对微信/QQ 语音实时变声" />
|
||||
<meta-data
|
||||
android:name="xposedminversion"
|
||||
android:value="93" />
|
||||
</application>
|
||||
</manifest>
|
||||
@@ -0,0 +1 @@
|
||||
com.voicecraft.xposed.HookEntry
|
||||
@@ -0,0 +1,17 @@
|
||||
cmake_minimum_required(VERSION 3.22.1)
|
||||
project(vchangemin LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
# 复用 M0 的 DSP 引擎源码(同一份代码,两处部署:测试 App / 注入模块)
|
||||
set(SHARED ${CMAKE_CURRENT_SOURCE_DIR}/../../../../app/src/main/cpp)
|
||||
|
||||
add_library(vchangemin SHARED
|
||||
engine_min.cpp
|
||||
${SHARED}/psola.cpp
|
||||
${SHARED}/dsp_pipeline.cpp)
|
||||
|
||||
target_include_directories(vchangemin PRIVATE ${SHARED})
|
||||
target_compile_options(vchangemin PRIVATE -O2 -Wall)
|
||||
target_link_libraries(vchangemin log)
|
||||
@@ -0,0 +1,103 @@
|
||||
// 注入侧轻量 JNI:只做 buffer 就地变声,不做音频 I/O
|
||||
// DSP 管线复用 M0 的 dsp_pipeline(PSOLA 变调 + 共振峰搬移)
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
#include "dsp_pipeline.h"
|
||||
|
||||
namespace {
|
||||
|
||||
std::mutex g_mutex;
|
||||
DspPipeline g_pipeline;
|
||||
std::vector<float> g_in, g_out;
|
||||
bool g_ready = false;
|
||||
|
||||
void processInternal(const short* in, short* out, int n) {
|
||||
g_in.resize((size_t)n);
|
||||
g_out.resize((size_t)n);
|
||||
for (int i = 0; i < n; ++i) g_in[(size_t)i] = (float)in[i] / 32768.f;
|
||||
g_pipeline.process(g_in.data(), g_out.data(), n);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
float v = g_out[(size_t)i] * 32767.f;
|
||||
if (v > 32767.f) v = 32767.f;
|
||||
if (v < -32768.f) v = -32768.f;
|
||||
out[i] = (short)v;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
extern "C" {
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_voicecraft_xposed_VoiceEngineMin_nativeInit(JNIEnv*, jclass, jint sampleRate) {
|
||||
std::lock_guard<std::mutex> lk(g_mutex);
|
||||
g_pipeline.setup((float)sampleRate);
|
||||
g_ready = true;
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_voicecraft_xposed_VoiceEngineMin_nativeSetParams(JNIEnv*, jclass,
|
||||
jfloat pitchSemis, jfloat formant,
|
||||
jfloat strength, jfloat eqFreqHz,
|
||||
jfloat eqGainDb, jfloat gateThreshold,
|
||||
jfloat f0MinHz, jfloat f0MaxHz,
|
||||
jfloat smoothing, jboolean bypass) {
|
||||
std::lock_guard<std::mutex> lk(g_mutex);
|
||||
g_pipeline.setParams(pitchSemis, formant, strength,
|
||||
eqFreqHz, eqGainDb, gateThreshold,
|
||||
f0MinHz, f0MaxHz, smoothing, bypass == JNI_TRUE);
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_voicecraft_xposed_VoiceEngineMin_nativeSetTargetMode(JNIEnv*, jclass,
|
||||
jboolean enabled,
|
||||
jfloat targetF0Hz) {
|
||||
std::lock_guard<std::mutex> lk(g_mutex);
|
||||
g_pipeline.setTargetMode(enabled == JNI_TRUE, targetF0Hz);
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_voicecraft_xposed_VoiceEngineMin_nativeProcessByte(JNIEnv* env, jclass,
|
||||
jbyteArray buf, jint nBytes) {
|
||||
if (nBytes <= 0) return;
|
||||
std::lock_guard<std::mutex> lk(g_mutex);
|
||||
if (!g_ready) return;
|
||||
|
||||
const int n = nBytes / 2;
|
||||
if (n <= 0) return;
|
||||
std::vector<short> tmp((size_t)n);
|
||||
|
||||
jbyte* raw = env->GetByteArrayElements(buf, nullptr);
|
||||
if (raw == nullptr) return;
|
||||
// 16 位 PCM 小端 → short
|
||||
unsigned char* u = reinterpret_cast<unsigned char*>(raw);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
tmp[(size_t)i] = (short)((unsigned short)u[2 * i] | ((unsigned short)u[2 * i + 1] << 8));
|
||||
}
|
||||
processInternal(tmp.data(), tmp.data(), n);
|
||||
// 写回(就地替换)
|
||||
for (int i = 0; i < n; ++i) {
|
||||
u[2 * i] = (unsigned char)(tmp[(size_t)i] & 0xff);
|
||||
u[2 * i + 1] = (unsigned char)((tmp[(size_t)i] >> 8) & 0xff);
|
||||
}
|
||||
env->ReleaseByteArrayElements(buf, raw, 0);
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_voicecraft_xposed_VoiceEngineMin_nativeProcessShort(JNIEnv* env, jclass,
|
||||
jshortArray buf, jint n) {
|
||||
if (n <= 0) return;
|
||||
std::lock_guard<std::mutex> lk(g_mutex);
|
||||
if (!g_ready) return;
|
||||
|
||||
jshort* raw = env->GetShortArrayElements(buf, nullptr);
|
||||
if (raw == nullptr) return;
|
||||
processInternal(raw, raw, (int)n);
|
||||
env->ReleaseShortArrayElements(buf, raw, 0);
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
@@ -0,0 +1,162 @@
|
||||
package com.voicecraft.xposed;
|
||||
|
||||
import android.media.AudioRecord;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import de.robv.android.xposed.IXposedHookLoadPackage;
|
||||
import de.robv.android.xposed.XC_MethodHook;
|
||||
import de.robv.android.xposed.XposedBridge;
|
||||
import de.robv.android.xposed.XposedHelpers;
|
||||
import de.robv.android.xposed.callbacks.XC_LoadPackage;
|
||||
|
||||
/**
|
||||
* M1 注入入口(L1 Java 层 hook):
|
||||
* 在目标 App(微信/QQ)进程内 hook {@link AudioRecord#read} 全部常用重载,
|
||||
* 读取完成后对 buffer 就地做男变女处理——App 自身无感知。
|
||||
*
|
||||
* 设计要点:
|
||||
* 1. afterHookedMethod 中处理(拿到返回值 = 实际读取的字节/样本数,不越界)
|
||||
* 2. 异常全兜底——hook 出任何岔子都不能带崩宿主 App
|
||||
* 3. 参数热更新:/sdcard/RealtimeVoice/voice_config.json(复用 M0 配置格式,
|
||||
* 每 2 秒检查一次修改时间;宿主 App 有存储权限,可直接读)
|
||||
* 4. 只做 L1(Java AudioRecord);若微信通话走 AAudio/native 采集(L2/L3),
|
||||
* 需在 M1 验证阶段确认并追加 hook
|
||||
*/
|
||||
public class HookEntry implements IXposedHookLoadPackage {
|
||||
|
||||
private static final String TAG = "[变声M1]";
|
||||
|
||||
/** 注入目标(M2 阶段补 赫兹/他趣 的实测包名) */
|
||||
private static final Set<String> TARGET_PKGS = new HashSet<>(Arrays.asList(
|
||||
"com.tencent.mm", // 微信
|
||||
"com.tencent.mobileqq", // QQ
|
||||
"cn.soulapp.android", // Soul
|
||||
"cn.neoclub.uki" // Uki
|
||||
));
|
||||
|
||||
/**
|
||||
* 配置查找顺序(hook 运行于宿主 UID,Android 11+ 分区存储下 /sdcard 直读可能被拒):
|
||||
* 1. /data/local/tmp —— adb push 可写、全应用可读,联调首选
|
||||
* 2. /sdcard/RealtimeVoice/ —— 兼容旧路径(宿主有存储权限时可用)
|
||||
*/
|
||||
private static final String[] CFG_PATHS = {
|
||||
"/data/local/tmp/voice_config.json",
|
||||
"/sdcard/RealtimeVoice/voice_config.json",
|
||||
};
|
||||
private static volatile long cfgMtimeSeen = 0;
|
||||
private static volatile long cfgLastCheckMs = 0;
|
||||
private static volatile String cfgPathSeen = "";
|
||||
|
||||
@Override
|
||||
public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) {
|
||||
if (!TARGET_PKGS.contains(lpparam.packageName)) return;
|
||||
XposedBridge.log(TAG + " 注入目标: " + lpparam.packageName);
|
||||
|
||||
// AudioRecord.read 的常用重载(byte[] / short[],3 参与 4 参版本)
|
||||
hookRead("byte", new Object[]{byte[].class, int.class, int.class});
|
||||
hookRead("byte", new Object[]{byte[].class, int.class, int.class, int.class});
|
||||
hookRead("short", new Object[]{short[].class, int.class, int.class});
|
||||
hookRead("short", new Object[]{short[].class, int.class, int.class, int.class});
|
||||
}
|
||||
|
||||
private void hookRead(final String kind, Object[] paramTypes) {
|
||||
try {
|
||||
Object[] args = Arrays.copyOf(paramTypes, paramTypes.length + 1);
|
||||
args[paramTypes.length] = new XC_MethodHook() {
|
||||
@Override
|
||||
protected void afterHookedMethod(MethodHookParam param) {
|
||||
try {
|
||||
Object result = param.getResult();
|
||||
if (!(result instanceof Integer)) return;
|
||||
int n = (Integer) result;
|
||||
if (n <= 0) return;
|
||||
|
||||
Object buf = param.args[0];
|
||||
AudioRecord rec = (AudioRecord) param.thisObject;
|
||||
|
||||
// 首次/采样率变化时初始化引擎
|
||||
VoiceEngineMin.ensureInit(rec.getSampleRate());
|
||||
maybeReloadConfig();
|
||||
|
||||
if ("byte".equals(kind)) {
|
||||
VoiceEngineMin.nativeProcessByte((byte[]) buf, n);
|
||||
} else {
|
||||
VoiceEngineMin.nativeProcessShort((short[]) buf, n);
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
// 绝不让 hook 异常波及宿主 App
|
||||
}
|
||||
}
|
||||
};
|
||||
XposedHelpers.findAndHookMethod(AudioRecord.class, "read", args);
|
||||
} catch (Throwable t) {
|
||||
XposedBridge.log(TAG + " hook read(" + kind + ") 失败: " + t);
|
||||
}
|
||||
}
|
||||
|
||||
/** 配置热更新:每 2 秒查一次文件修改时间,变了才解析下发 */
|
||||
private static void maybeReloadConfig() {
|
||||
try {
|
||||
long now = System.currentTimeMillis();
|
||||
if (now - cfgLastCheckMs < 2000) return;
|
||||
cfgLastCheckMs = now;
|
||||
|
||||
// 依次尝试配置路径,取第一个存在的
|
||||
File f = null;
|
||||
for (String path : CFG_PATHS) {
|
||||
File cand = new File(path);
|
||||
if (cand.exists() && cand.length() > 0) {
|
||||
f = cand;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (f == null) return; // 两个路径都没有 → 用默认参数(静默即可)
|
||||
long mtime = f.lastModified();
|
||||
if (mtime == cfgMtimeSeen) return;
|
||||
|
||||
byte[] buf = new byte[(int) Math.min(f.length(), 65536)];
|
||||
try (FileInputStream in = new FileInputStream(f)) {
|
||||
int off = 0;
|
||||
while (off < buf.length) {
|
||||
int r = in.read(buf, off, buf.length - off);
|
||||
if (r < 0) break;
|
||||
off += r;
|
||||
}
|
||||
JSONObject p = new JSONObject(new String(buf, 0, off, StandardCharsets.UTF_8))
|
||||
.optJSONObject("params");
|
||||
if (p == null) return;
|
||||
|
||||
VoiceEngineMin.nativeSetParams(
|
||||
(float) p.optDouble("pitchSemitones", 9),
|
||||
(float) p.optDouble("formantRatio", 1.20),
|
||||
(float) p.optDouble("strength", 1.0),
|
||||
(float) p.optDouble("eqFreqHz", 3000),
|
||||
(float) p.optDouble("eqGainDb", 3),
|
||||
(float) p.optDouble("gateThreshold", 0.006),
|
||||
(float) p.optDouble("f0MinHz", 65),
|
||||
(float) p.optDouble("f0MaxHz", 500),
|
||||
(float) p.optDouble("smoothing", 0.05),
|
||||
p.optBoolean("bypass", false));
|
||||
VoiceEngineMin.nativeSetTargetMode(
|
||||
p.optBoolean("targetMode", false),
|
||||
(float) p.optDouble("targetF0Hz", 205));
|
||||
cfgMtimeSeen = mtime;
|
||||
if (!f.getAbsolutePath().equals(cfgPathSeen)) {
|
||||
cfgPathSeen = f.getAbsolutePath();
|
||||
XposedBridge.log(TAG + " 配置来源: " + cfgPathSeen);
|
||||
}
|
||||
XposedBridge.log(TAG + " 配置已更新");
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
XposedBridge.log(TAG + " 配置读取异常(路径=" + cfgPathSeen + "): " + t);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.voicecraft.xposed;
|
||||
|
||||
/**
|
||||
* 注入侧引擎包装(轻量版,无 Oboe——音频 I/O 由宿主 App 自己做,
|
||||
* 我们只在 buffer 上就地变声)。native 实现在 engine_min.cpp。
|
||||
*/
|
||||
public final class VoiceEngineMin {
|
||||
|
||||
private static final Object LOCK = new Object();
|
||||
private static int currentSr = 0;
|
||||
|
||||
static {
|
||||
System.loadLibrary("vchangemin");
|
||||
}
|
||||
|
||||
private VoiceEngineMin() {
|
||||
}
|
||||
|
||||
/** 采样率变化时重建引擎(AudioRecord 采样率由宿主决定,常见 16k/44.1k/48k) */
|
||||
public static void ensureInit(int sampleRate) {
|
||||
if (sampleRate == currentSr) return;
|
||||
synchronized (LOCK) {
|
||||
if (sampleRate == currentSr) return;
|
||||
nativeInit(sampleRate);
|
||||
currentSr = sampleRate;
|
||||
}
|
||||
}
|
||||
|
||||
public static native void nativeInit(int sampleRate);
|
||||
|
||||
/**
|
||||
* 全参数下发(与 voice_config.json 一一对应,语义同 M0)
|
||||
*/
|
||||
public static native void nativeSetParams(float pitchSemis, float formant, float strength,
|
||||
float eqFreqHz, float eqGainDb, float gateThreshold,
|
||||
float f0MinHz, float f0MaxHz, float smoothing,
|
||||
boolean bypass);
|
||||
|
||||
/** 目标音高模式:输出 f0 锚定 targetF0Hz(深沉嗓音自动多补) */
|
||||
public static native void nativeSetTargetMode(boolean enabled, float targetF0Hz);
|
||||
|
||||
/**
|
||||
* 就地变声:16 位 PCM 小端(AudioRecord.read(byte[]) 的格式)
|
||||
*
|
||||
* @param nBytes 实际读取的字节数(来自 read 返回值)
|
||||
*/
|
||||
public static native void nativeProcessByte(byte[] buf, int nBytes);
|
||||
|
||||
/**
|
||||
* 就地变声:16 位 PCM(AudioRecord.read(short[]) 的格式)
|
||||
*
|
||||
* @param n 实际读取的样本数(来自 read 返回值)
|
||||
*/
|
||||
public static native void nativeProcessShort(short[] buf, int n);
|
||||
}
|
||||
Reference in New Issue
Block a user