Files
voiceScript/realtime-voice/xposed/src/main/cpp/engine_min.cpp
T
HenryandClaude Code c10f271d87 feat: 初始化变声器项目仓库(逆向分析 + 实时变声 + PC 变声麦)
包含三大模块:
- 逆向分析报告与实时变声方案设计文档
- realtime-voice:Android 实时变声(PSOLA 变调 + VTLP 共振峰 DSP 管线、Xposed 注入模块)
- pc-voice:PC 变声麦工具(miniaudio 引擎、可配置 DSP 参数)

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

Co-Authored-By: Claude Code <[email protected]>
2026-11-06 16:51:06 +08:00

104 lines
3.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 注入侧轻量 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"