NDK实现QQ语音变声功能
实现语音调节效果
利用fmog (官网:https://www.fmod.com/)
fmog并不是开源库,是收费的,但是有试用版本,一些简单的效果还是可以实现的
流程
1,在cmake中头文件导入
2,源码/预编译库+jar
预编译库+jar:
2.1 jar导入
2.2 预编译库导入jniLibs(如果不是默认jinLibs文件中,否则需要在gradle中配置)
2.3 cmakelists中链接预编译库
1, 播放声音
2,给声音加上特效
3,释放资源。。
#include "com_netease_voicechange_MainActivity.h"
#include "fmod.hpp"
#include
#include
#include
using namespace FMOD;
extern "C"
JNIEXPORT void JNICALL
Java_com_example_qqvoicechange_MainActivity_voiceChangeNative(JNIEnv *env, jobject thiz, jint mode,
jstring voice_path_) {
const char *voice_path = env->GetStringUTFChars(voice_path_, 0);
System *system;//fmod音效引擎系统
Sound *sound;//声音
Channel *channel =0;//通道
DSP *dsp =0;
System_Create(&system);//1.创建系统
system->init(32, FMOD_INIT_NORMAL, 0);//2.初始化系统
system->createSound(voice_path, FMOD_DEFAULT, 0, &sound);//3.创建声音
system->playSound(sound, 0, false, &channel);//4.播放)
switch (mode) {
case com_netease_voicechange_MainActivity_MODE_NORMAL:
system->playSound(sound, 0, false, &channel);//4.播放)
break;
case com_netease_voicechange_MainActivity_MODE_LUOLI:
__android_log_print(ANDROID_LOG_ERROR,"projectname","test");
//萝莉音效 音调高
system->createDSPByType(FMOD_DSP_TYPE_PITCHSHIFT, &dsp);
dsp->setParameterFloat(FMOD_DSP_PITCHSHIFT_PITCH, 2.0);
channel->addDSP(0, dsp);
break;
case com_netease_voicechange_MainActivity_MODE_DASHU:
//大叔音效 音调低
system->createDSPByType(FMOD_DSP_TYPE_PITCHSHIFT, &dsp);
dsp->setParameterFloat(FMOD_DSP_PITCHSHIFT_PITCH, 0.8f);
channel->addDSP(0, dsp);
break;
case com_netease_voicechange_MainActivity_MODE_JINGSONG:
break;
case com_netease_voicechange_MainActivity_MODE_GAOGUAI:
//搞怪音效 频率快
//获取原始声音的频率
float frequency;
channel->getFrequency(&frequency);
channel->setFrequency(frequency *1.5f);
break;
case com_netease_voicechange_MainActivity_MODE_KONGLING:
//空灵音效
system->createDSPByType(FMOD_DSP_TYPE_ECHO, &dsp);
dsp->setParameterFloat(FMOD_DSP_ECHO_DELAY, 200);
dsp->setParameterFloat(FMOD_DSP_ECHO_FEEDBACK, 10);
channel->addDSP(0, dsp);
break;
}
//判断是否播放完
bool isPlaying =true;
while (isPlaying) {
channel->isPlaying(&isPlaying);
usleep(1000 *1000);
}
//5.释放
sound->release();
system->close();
system->release();
env->ReleaseStringUTFChars(voice_path_, voice_path);
}