// 注入侧轻量 JNI:只做 buffer 就地变声,不做音频 I/O // DSP 管线复用 M0 的 dsp_pipeline(PSOLA 变调 + 共振峰搬移) #include #include #include #include "dsp_pipeline.h" namespace { std::mutex g_mutex; DspPipeline g_pipeline; std::vector 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 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 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 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 lk(g_mutex); if (!g_ready) return; const int n = nBytes / 2; if (n <= 0) return; std::vector tmp((size_t)n); jbyte* raw = env->GetByteArrayElements(buf, nullptr); if (raw == nullptr) return; // 16 位 PCM 小端 → short unsigned char* u = reinterpret_cast(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 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"