自学体验使用fmod 以及 仿QQ变声 fmod
来源:互联网 发布:数据分析专业 大学 编辑:程序博客网 时间:2024/04/25 10:23
模块一 体验使用fmod
到https://www.fmod.com/download 上下载fmod android版本的
这里我们用的是别人生成的so库
libfmod.so库
libfmodL.so库
操作步骤
1.在assets中存三个音频
由之前的fmod使用,我们可以知道音频会从assets中去取 file:///android_asset/
const char *Common_MediaPath(const char *fileName){ char *filePath = (char *)calloc(256, sizeof(char)); strcat(filePath, "file:///android_asset/"); strcat(filePath, fileName); gPathList.push_back(filePath); return filePath;}
2.在main目录下新建jni文件夹
将 D:\fmodstudioapi11001android\fmodstudioapi11001android\api\lowlevel 下的inc考入
3.playsound.cpp是主要文件
我们从D:\fmodstudioapi11001android\fmodstudioapi11001android\api\lowlevel\examples下获取
4.导入其他文件
common.h
common.cpp
common_platform.h
common_platform.cpp
===================上面都是最基本的
effects.cpp 控制音效的
(其中记得把导包的路径修改)
5.从java文件中的MainActivity替换新建项目的MainActivity以及包名
6.将lib下的东西考入并导包 fmod.jar以及不同cpu的so库
7.Android.mk文件
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := fmodLOCAL_SRC_FILES := libfmod.soinclude $(PREBUILT_SHARED_LIBRARY)include $(CLEAR_VARS)LOCAL_MODULE := fmodLLOCAL_SRC_FILES := libfmodL.soinclude $(PREBUILT_SHARED_LIBRARY)include $(CLEAR_VARS)LOCAL_MODULE := qq_voicerLOCAL_SRC_FILES := play_sound.cpp common.cpp common_platform.cppLOCAL_SHARED_LIBRARIES := fmod fmodLLOCAL_LDLIBS := -lloginclude $(BUILD_SHARED_LIBRARY)
上面两个是我们依赖的so库
最后面一个 LOCAL_SRC_FILES 是我们需要依赖的cpp文件
LOCAL_LDIBS是允许日志输出
8.Application.mk
APP_STL := gnustl_staticAPP_ABI := all
运行效果
模块二 仿QQ变声
就是在原有基础上多加些东西channel给原生音频加上特效
1)新建EffectUtils.java
package org.fmod.example;/** * Created by Boom on 2017/11/27. */public class EffectUtils { //音效类型 public static final int MODE_NORMAL = 0; public static final int MODE_LUOLI = 1; public static final int MODE_DASHU = 2; public static final int MODE_JINGSONG = 3; public static final int MODE_GAOGUAI = 4; public static final int MODE_KONGLING = 5; /** * 音效处理 * @param path * @param type */ public native static void fix(String path,int type); static { System.loadLibrary("qq_voicer"); }}
2)javah生成头文件
org_fmod_example_EffectUtils.h
/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class org_fmod_example_EffectUtils */#ifndef _Included_org_fmod_example_EffectUtils#define _Included_org_fmod_example_EffectUtils#ifdef __cplusplusextern "C" {#endif#undef org_fmod_example_EffectUtils_MODE_NORMAL#define org_fmod_example_EffectUtils_MODE_NORMAL 0L#undef org_fmod_example_EffectUtils_MODE_LUOLI#define org_fmod_example_EffectUtils_MODE_LUOLI 1L#undef org_fmod_example_EffectUtils_MODE_DASHU#define org_fmod_example_EffectUtils_MODE_DASHU 2L#undef org_fmod_example_EffectUtils_MODE_JINGSONG#define org_fmod_example_EffectUtils_MODE_JINGSONG 3L#undef org_fmod_example_EffectUtils_MODE_GAOGUAI#define org_fmod_example_EffectUtils_MODE_GAOGUAI 4L#undef org_fmod_example_EffectUtils_MODE_KONGLING#define org_fmod_example_EffectUtils_MODE_KONGLING 5L/* * Class: org_fmod_example_EffectUtils * Method: fix * Signature: (Ljava/lang/String;I)V */JNIEXPORT void JNICALL Java_org_fmod_example_EffectUtils_fix (JNIEnv *, jclass, jstring, jint);#ifdef __cplusplus}#endif#endif
#include "inc/fmod.hpp"#include <stdlib.h>#include <unistd.h>#include "org_fmod_example_EffectUtils.h"#include <android/log.h>#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 5using 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://萝莉//DSP digital signal process//dsp -> 音效 创建fmod中预定义好的音效//FMOD_DSP_TYPE_PITCHSHIFT dsp,提升或者降低音调用的一种音效system->createDSPByType(FMOD_DSP_TYPE_PITCHSHIFT,&dsp);//设置音调的参数dsp->setParameterFloat(FMOD_DSP_PITCHSHIFT_PITCH,2.5);system->playSound(sound, 0, false, &channel);//添加到channelchannel->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);//添加到channelchannel->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();}
基本步骤
//初始化System_Create(&system);system->init(32, FMOD_INIT_NORMAL, NULL);//创建声音system->createSound(path_cstr, FMOD_DEFAULT, NULL, &sound);//DSP digital signal process//dsp -> 音效 创建fmod中预定义好的音效//FMOD_DSP_TYPE_PITCHSHIFT dsp,提升或者降低音调用的一种音效system->createDSPByType(FMOD_DSP_TYPE_PITCHSHIFT,&dsp);//设置音调的参数dsp->setParameterFloat(FMOD_DSP_PITCHSHIFT_PITCH,2.5);//原生播放system->playSound(sound, 0, false, &channel);system->update();//释放资源//单位是微秒//每秒钟判断下是否在播放while(playing){ channel->isPlaying(&playing); usleep(1000 * 1000);}
system->close();system->release();
4)Android.mk修改为
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := fmodLOCAL_SRC_FILES := libfmod.soinclude $(PREBUILT_SHARED_LIBRARY)include $(CLEAR_VARS)LOCAL_MODULE := fmodLLOCAL_SRC_FILES := libfmodL.soinclude $(PREBUILT_SHARED_LIBRARY)include $(CLEAR_VARS)LOCAL_MODULE := qq_voicerLOCAL_SRC_FILES := effectfix.cppLOCAL_SHARED_LIBRARIES := fmod fmodLLOCAL_LDLIBS := -llogLOCAL_CPP_FEATURES := exceptionsinclude $(BUILD_SHARED_LIBRARY)
5)MainActivity就是更改布局
package org.fmod.example;import android.app.Activity;import android.graphics.Typeface;import android.os.Bundle;import android.util.TypedValue;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.Button;import android.widget.LinearLayout;import android.widget.LinearLayout.LayoutParams;import android.widget.TextView;public class MainActivity extends Activity implements OnTouchListener, Runnable{private TextView mTxtScreen;private Thread mThread; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Create the text area mTxtScreen = new TextView(this); mTxtScreen.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10.0f); mTxtScreen.setTypeface(Typeface.MONOSPACE); // Create the buttons Button[] buttons = new Button[9]; for (int i = 0; i < buttons.length; i++) { buttons[i] = new Button(this); buttons[i].setText(getButtonLabel(i)); buttons[i].setOnTouchListener(this); buttons[i].setId(i); } // Create the button row layouts LinearLayout llTopRowButtons = new LinearLayout(this); llTopRowButtons.setOrientation(LinearLayout.HORIZONTAL); LinearLayout llMiddleRowButtons = new LinearLayout(this); llMiddleRowButtons.setOrientation(LinearLayout.HORIZONTAL); LinearLayout llBottomRowButtons = new LinearLayout(this); llBottomRowButtons.setOrientation(LinearLayout.HORIZONTAL); // Create the main view layout LinearLayout llView = new LinearLayout(this); llView.setOrientation(LinearLayout.VERTICAL); // Create layout parameters LayoutParams lpLayout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f); // Set up the view hierarchy llTopRowButtons.addView(buttons[0], lpLayout); llTopRowButtons.addView(buttons[6], lpLayout); llTopRowButtons.addView(buttons[1], lpLayout); llMiddleRowButtons.addView(buttons[4], lpLayout); llMiddleRowButtons.addView(buttons[8], lpLayout); llMiddleRowButtons.addView(buttons[5], lpLayout); llBottomRowButtons.addView(buttons[2], lpLayout); llBottomRowButtons.addView(buttons[7], lpLayout); llBottomRowButtons.addView(buttons[3], lpLayout); llView.addView(mTxtScreen, lpLayout); llView.addView(llTopRowButtons); llView.addView(llMiddleRowButtons); llView.addView(llBottomRowButtons); setContentView(llView); // Request necessary permissions /* if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { String[] perms = { "android.permission.RECORD_AUDIO", "android.permission.WRITE_EXTERNAL_STORAGE" }; if (checkSelfPermission(perms[0]) == PackageManager.PERMISSION_DENIED || checkSelfPermission(perms[1]) == PackageManager.PERMISSION_DENIED) { requestPermissions(perms, 200); } }*/ org.fmod.FMOD.init(this); mThread = new Thread(this, "Example Main"); mThread.start(); setStateCreate(); } @Override protected void onStart() { super.onStart(); setStateStart(); } @Override protected void onStop() { setStateStop(); super.onStop(); } @Override protected void onDestroy() { setStateDestroy(); try { mThread.join(); } catch (InterruptedException e) { } org.fmod.FMOD.close(); super.onDestroy(); } @Overridepublic boolean onTouch(View view, MotionEvent motionEvent){if (motionEvent.getAction() == MotionEvent.ACTION_DOWN){buttonDown(view.getId());}else if (motionEvent.getAction() == MotionEvent.ACTION_UP){buttonUp(view.getId());} return true;}@Overridepublic void run(){ main();}public void updateScreen(final String text){runOnUiThread(new Runnable(){ @Override public void run() { mTxtScreen.setText(text); }});}private native String getButtonLabel(int index);private native void buttonDown(int index);private native void buttonUp(int index);private native void setStateCreate();private native void setStateStart();private native void setStateStop();private native void setStateDestroy();private native void main(); static { /* * To simplify our examples we try to load all possible FMOD * libraries, the Android.mk will copy in the correct ones * for each example. For real products you would just load * 'fmod' and if you use the FMOD Studio tool you would also * load 'fmodstudio'. */ //System.loadLibrary("stlport_shared"); System.loadLibrary("qq_voicer"); }}
效果(这个是照着别人后面写的)
阅读全文