package com.android.webrtc.audio;
/**
* This class supports the acoustic echo cancellation for mobile edition. Please <b>bug me</b> if you find any bugs in
* this toolkit.<br>
* <br>
* <b>[Notice]</b><br>
* 1. there are 5 more native interface that I'm not trying to provide in this MobileAEC toolkit.<br>
* But I think I should mention it out as a list below, for secondary development if necessary: <br>
* <ul>
* <li>WebRtc_Word32 WebRtcAecm_get_config(void *, AecmConfig *);</li>
* <li>WebRtc_Word32 WebRtcAecm_InitEchoPath(void* , const void* , size_t);</li>
* <li>WebRtc_Word32 WebRtcAecm_GetEchoPath(void* , void* , size_t);</li>
* <li>size_t WebRtcAecm_echo_path_size_bytes();</li>
* <li>WebRtc_Word32 WebRtcAecm_get_error_code(void *);</li>
* </ul>
* 2. if you are working on an android platform, put the shared library "libwebrtc_aecm.so"<br>
* into path "/your project/libs/armeabi/", if the dir does not exist, you should create it, otherwise you<br>
* will get a "unsatisfied link error" at run time.<br>
* 3. you should always call <b>close()</b> method <b>manually</b> when all things are finished.<br>
* <br>
* <b>[Usage]</b> <br>
* <ul>
* 1. You create a MobileAEC object first(set the parameters to constructor or null are both Ok, if null are set, then
* we will use default values instead).<br>
* 2. change the aggressiveness or sampling frequency of the AECM instance if necessary.<br>
* 3. call <b>prepare()</b> method to make the AECM instance prepared. <br>
* 4. then call "farendBuffer" to set far-end signal to AECM instance. <br>
* 5. now you call "echoCancellation()" to deal with the acoustic echo things.<br>
* The order of step 1,2,3,4 and 5 is significant, when all settings are done or you changed previous<br>
* settings, <b>DO NOT</b> forget to call prepare() method, otherwise your new settings will be ignored by AECM
* instance. <br>
* 6. finally you should call <b>close()</b> method <b>manually</b> when all things are done, after that, the AECM
* instance is no longer available until next <b>prepare()</b> is called.<br>
* </ul>
* <b>[Samples]</b><br>
* <ul>
* see doAECM() in {@link combillhoo.android.aec.demo.DemoActivity DEMO}
* </ul>
*
* @version 0.1 2013-3-8
*
* @author billhoo E-mail:
[email protected]
*/
public class MobileAEC {
static {
System.loadLibrary("webrtc_aecm"); // to load the libwebrtc_aecm.so library.
}
// /////////////////////////////////////////////////////////
// PUBLIC CONSTANTS
/**
* constant unable mode for Aecm configuration settings.
*/
public static final short AECM_UNABLE = 0;
/**
* constant enable mode for Aecm configuration settings.
*/
public static final short AECM_ENABLE = 1;
public static short[] mFarEndBuffer= null;
// /////////////////////////////////////////////////////////
// PUBLIC NESTED CLASSES
/**
* For security reason, this class supports constant sampling frequency values in
* {@link SamplingFrequency#FS_8000Hz FS_8000Hz}, {@link SamplingFrequency#FS_16000Hz FS_16000Hz}
*/
public static final class SamplingFrequency {
public long getFS() {
return mSamplingFrequency;
}
/**
* This constant represents sampling frequency in 8000Hz
*/
public static final SamplingFrequency FS_8000Hz = new SamplingFrequency(
8000);
/**
* This constant represents sampling frequency in 16000Hz
*/
public static final SamplingFrequency FS_16000Hz = new SamplingFrequency(
16000);
private final long mSamplingFrequency;
private SamplingFrequency(long fs) {
this.mSamplingFrequency = fs;
}
}
/**
* For security reason, this class supports constant aggressiveness of the AECM instance in
* {@link AggressiveMode#MILD MILD}, {@link AggressiveMode#MEDIUM MEDIUM}, {@link AggressiveMode#HIGH HIGH},
* {@link AggressiveMode#AGGRESSIVE AGGRESSIVE}, {@link AggressiveMode#MOST_AGGRESSIVE MOST_AGGRESSIVE}.
*/
public static final class AggressiveMode {
public int getMode() {
return mMode;
}
/**
* This constant represents the aggressiveness of the AECM instance in MILD_MODE
*/
public static final AggressiveMode MILD = new AggressiveMode(
0);
/**
* This constant represents the aggressiveness of the AECM instance in MEDIUM_MODE
*/
public static final AggressiveMode MEDIUM = new AggressiveMode(
1);
/**
* This constant represents the aggressiveness of the AECM instance in HIGH_MODE
*/
public static final AggressiveMode HIGH = new AggressiveMode(
2);
/**
* This constant represents the aggressiveness of the AECM instance in AGGRESSIVE_MODE
*/
public static final AggressiveMode AGGRESSIVE = new AggressiveMode(
3);
/**
* This constant represents the aggressiveness of the AECM instance in MOST_AGGRESSIVE_MODE
*/
public static final AggressiveMode MOST_AGGRESSIVE = new AggressiveMode(
4);
private final int mMode;
private AggressiveMode(int mode) {
mMode = mode;
}
}
// /////////////////////////////////////////////////////////
// PRIVATE MEMBERS
private int mAecmHandler = -1; // the handler of AECM instance.
private AecmConfig mAecmConfig = null; // the configurations of AECM instance.
private SamplingFrequency mSampFreq = null; // sampling frequency of input speech data.
private boolean mIsInit = false; // whether the AECM instance is initialized or not.
// /////////////////////////////////////////////////////////
// CONSTRUCTOR
/**
* To generate a new AECM instance, whether you set the sampling frequency of each parameter or not are both ok.
*
* @param sampFreqOfData
* - sampling frequency of input audio data. if null, then {@link SamplingFrequency#FS_16000Hz
* FS_16000Hz} is set.
*/
public MobileAEC(SamplingFrequency sampFreqOfData) {
setSampFreq(sampFreqOfData);
mAecmConfig = new AecmConfig();
// create new AECM instance but without initialize. Init things are in prepare() method instead.
mAecmHandler = nativeCreateAecmInstance();
}
// /////////////////////////////////////////////////////////
// PUBLIC METHODS
/**
* set the sampling rate of speech data.
*
* @param fs
* - sampling frequency of speech data, if null then {@link SamplingFrequency#FS_16000Hz FS_16000Hz} is
* set.
*/
public void setSampFreq(SamplingFrequency fs) {
if (fs == null)
mSampFreq = SamplingFrequency.FS_16000Hz;
else
mSampFreq = fs;
}
/**
* set the far-end signal of AECM instance.
*
* @param farendBuf
* @param numOfSamples
* @return the {@link MobileAEC MobileAEC} object itself.
* @throws Exception
* - if farendBuffer() is called on an unprepared AECM instance or you pass an invalid parameter.<br>
*/
public MobileAEC farendBuffer(short[] farendBuf, int numOfSamples)
throws Exception {
// check if AECM instance is not initialized.
评论0