Android音视频学习第8章:使用fmod对音频进行变调处理
2019-07-13 19:43发布
生成海报
使用fmod模仿QQ变声处理
用到的变量
System *system;
Sound *sound;
Channel *channel;
DSP *dsp;
bool playing = true;
float frequency = 0;
初始化
System_Create(&system);
system->init(32, FMOD_INIT_NORMAL, NULL);
创建声音(来自内存的wav音频)
const char* path_cstr = env->GetStringUTFChars(path_jstr,NULL);
system->createSound(path_cstr, FMOD_DEFAULT, NULL, &sound);
正常声音
system->playSound(sound, 0, false, &channel);
萝莉
system->createDSPByType(FMOD_DSP_TYPE_PITCHSHIFT,&dsp);
dsp->setParameterFloat(FMOD_DSP_PITCHSHIFT_PITCH,2.5);
system->playSound(sound, 0, false, &channel);
channel->addDSP(0,dsp);
惊悚
system->createDSPByType(FMOD_DSP_TYPE_TREMOLO,&dsp);
dsp->setParameterFloat(FMOD_DSP_TREMOLO_SKEW, 0.5);
system->playSound(sound, 0, false, &channel);
channel->addDSP(0,dsp);
大叔
system->createDSPByType(FMOD_DSP_TYPE_PITCHSHIFT,&dsp);
dsp->setParameterFloat(FMOD_DSP_PITCHSHIFT_PITCH,0.8);
system->playSound(sound, 0, false, &channel);
channel->addDSP(0,dsp);
搞怪(提高说话的速度)
system->playSound(sound, 0, false, &channel);
channel->getFrequency(&frequency);
frequency = frequency * 1.6;
channel->setFrequency(frequency);
空灵
system->createDSPByType(FMOD_DSP_TYPE_ECHO,&dsp);
dsp->setParameterFloat(FMOD_DSP_ECHO_DELAY,300);
dsp->setParameterFloat(FMOD_DSP_ECHO_FEEDBACK,20);
system->playSound(sound, 0, false, &channel);
channel->addDSP(0,dsp);
播放
system->update();
while(playing){
channel->isPlaying(&playing);
usleep(1000 * 1000);
}
释放资源
env->ReleaseStringUTFChars(path_jstr,path_cstr);
sound->release();
system->close();
system->release();
完整代码
#include "inc/fmod.hpp"
#include
#include
#include "org_fmod_example_EffectUtils.h"
#include
#define LOGI(FORMAT,...) __android_log_print(ANDROID_LOG_INFO,"jason",FORMAT,##__VA_ARGS__);
#define LOGE(FORMAT,...) __android_log_print(ANDROID_LOG_ERROR,"jason",FORMAT,##__VA_ARGS__);
#define MODE_NORMAL 0
#define MODE_LUOLI 1
#define MODE_DASHU 2
#define MODE_JINGSONG 3
#define MODE_GAOGUAI 4
#define MODE_KONGLING 5
using namespace FMOD;
JNIEXPORT void JNICALL Java_org_fmod_example_EffectUtils_fix
(JNIEnv *env, jclass jcls, jstring path_jstr, jint type){
System *system;
Sound *sound;
Channel *channel;
DSP *dsp;
bool playing = true;
float frequency = 0;
const char* path_cstr = env->GetStringUTFChars(path_jstr,NULL);
LOGI("%s",path_cstr);
try {
System_Create(&system);
system->init(32, FMOD_INIT_NORMAL, NULL);
system->createSound(path_cstr, FMOD_DEFAULT, NULL, &sound);
switch (type) {
case MODE_NORMAL:
system->playSound(sound, 0, false, &channel);
LOGI("%s","fix normal");
break;
case MODE_LUOLI:
system->createDSPByType(FMOD_DSP_TYPE_PITCHSHIFT,&dsp);
dsp->setParameterFloat(FMOD_DSP_PITCHSHIFT_PITCH,2.5);
system->playSound(sound, 0, false, &channel);
channel->addDSP(0,dsp);
LOGI("%s","fix luoli");
break;
case MODE_JINGSONG:
system->createDSPByType(FMOD_DSP_TYPE_TREMOLO,&dsp);
dsp->setParameterFloat(FMOD_DSP_TREMOLO_SKEW, 0.5);
system->playSound(sound, 0, false, &channel);
channel->addDSP(0,dsp);
break;
case MODE_DASHU:
system->createDSPByType(FMOD_DSP_TYPE_PITCHSHIFT,&dsp);
dsp->setParameterFloat(FMOD_DSP_PITCHSHIFT_PITCH,0.8);
system->playSound(sound, 0, false, &channel);
channel->addDSP(0,dsp);
LOGI("%s","fix dashu");
break;
case MODE_GAOGUAI:
system->playSound(sound, 0, false, &channel);
channel->getFrequency(&frequency);
frequency = frequency * 1.6;
channel->setFrequency(frequency);
LOGI("%s","fix gaoguai");
break;
case MODE_KONGLING:
system->createDSPByType(FMOD_DSP_TYPE_ECHO,&dsp);
dsp->setParameterFloat(FMOD_DSP_ECHO_DELAY,300);
dsp->setParameterFloat(FMOD_DSP_ECHO_FEEDBACK,20);
system->playSound(sound, 0, false, &channel);
channel->addDSP(0,dsp);
LOGI("%s","fix kongling");
break;
default:
break;
}
} catch (...) {
LOGE("%s","发生异常");
goto end;
}
system->update();
while(playing){
channel->isPlaying(&playing);
usleep(1000 * 1000);
}
goto end;
end:
env->ReleaseStringUTFChars(path_jstr,path_cstr);
sound->release();
system->close();
system->release();
}
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮