android AcousticEchoCanceler似乎不适用于大多数设备

时间:2016-02-11 21:24:25

标签: java android audio aec audioeffect

我正在构建一个应用程序,它需要麦克风取消来自扬声器的任何声音。似乎这个问题几乎是一个在线的阴谋,因为其他具有完全相同问题的问题从未得到长时间的回应。

Android的本机硬件加速AcousticEchoCanceler似乎不适用于大多数设备。在许多设备上进行的测试以及看似有效的设备包括Nexus 5和Moto X,而几乎所有测试过的三星设备都无法删除背景声音。注意:所有测试的手机都返回AcousticEchoCanceler.isAvailable()

然而,必须有一个解决方案,因为Skype或WhatsApp等应用程序似乎取消了应用程序环境之外的声音,即通话时扬声器和麦克风取消收到的任何反馈。

这个简化的录音应用程序将声音录制到文件中,并在点击播放时播放。

MainActivity.java

public class MainActivity extends Activity {

Button startRec, stopRec, playBack;
int minBufferSizeIn;
AudioRecord audioRecord;
short[] audioData;
Boolean recording;
int sampleRateInHz = 48000;
private String TAG = "TAG";

/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startRec = (Button) findViewById(R.id.startrec);
    stopRec = (Button) findViewById(R.id.stoprec);
    playBack = (Button) findViewById(R.id.playback);

    startRec.setOnClickListener(startRecOnClickListener);
    stopRec.setOnClickListener(stopRecOnClickListener);
    playBack.setOnClickListener(playBackOnClickListener);
    playBack.setEnabled(false);
    startRec.setEnabled(true);
    stopRec.setEnabled(false);

    minBufferSizeIn = AudioRecord.getMinBufferSize(sampleRateInHz,
            AudioFormat.CHANNEL_IN_MONO,
            AudioFormat.ENCODING_PCM_16BIT);

    audioData = new short[minBufferSizeIn];

    audioRecord = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION,
            sampleRateInHz,
            AudioFormat.CHANNEL_IN_MONO,
            AudioFormat.ENCODING_PCM_16BIT,
            minBufferSizeIn);

}

OnClickListener startRecOnClickListener
        = new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        playBack.setEnabled(false);
        startRec.setEnabled(false);
        stopRec.setEnabled(true);
        Thread recordThread = new Thread(new Runnable() {

            @Override
            public void run() {
                recording = true;
                startRecord();
            }

        });

        recordThread.start();

    }
};


OnClickListener stopRecOnClickListener
        = new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        playBack.setEnabled(true);
        startRec.setEnabled(false);
        stopRec.setEnabled(false);
        recording = false;
    }
};



OnClickListener playBackOnClickListener
        = new OnClickListener() {

    @Override
    public void onClick(View v) {
        playBack.setEnabled(false);
        startRec.setEnabled(true);
        stopRec.setEnabled(false);
        playRecord();
    }

};


@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void startRecord() {
    File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");

    try {
        FileOutputStream outputStream = new FileOutputStream(file);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
        DataOutputStream dataOutputStream = new DataOutputStream(bufferedOutputStream);

        NoiseSuppressor ns;
        AcousticEchoCanceler aec;

        if (NoiseSuppressor.isAvailable()) {
            ns = NoiseSuppressor.create(audioRecord.getAudioSessionId());
            if (ns != null) {
                ns.setEnabled(true);
            } else {
                Log.e(TAG, "AudioInput: NoiseSuppressor is null and not enabled");
            }
        }

        if (AcousticEchoCanceler.isAvailable()) {
            aec = AcousticEchoCanceler.create(audioRecord.getAudioSessionId());
            if (aec != null) {
                aec.setEnabled(true);
            } else {
                Log.e(TAG, "AudioInput: AcousticEchoCanceler is null and not enabled");
            }
        }
        audioRecord.startRecording();

        while (recording) {
            int numberOfShort = audioRecord.read(audioData, 0, minBufferSizeIn);
            for (int i = 0; i < numberOfShort; i++) {
                dataOutputStream.writeShort(audioData[i]);
            }
        }

        audioRecord.stop();
        dataOutputStream.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

void playRecord() {
    File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");

    int shortSizeInBytes = Short.SIZE / Byte.SIZE;

    int bufferSizeInBytes = (int) (file.length() / shortSizeInBytes);
    short[] audioData = new short[bufferSizeInBytes];

    try {
        FileInputStream inputStream = new FileInputStream(file);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        DataInputStream dataInputStream = new DataInputStream(bufferedInputStream);

        int i = 0;
        while (dataInputStream.available() > 0) {
            audioData[i] = dataInputStream.readShort();
            i++;
        }

        dataInputStream.close();

        AudioTrack audioTrack = new AudioTrack(
                AudioManager.STREAM_MUSIC, sampleRateInHz,
                AudioFormat.CHANNEL_OUT_MONO,
                AudioFormat.ENCODING_PCM_16BIT,
                bufferSizeInBytes,
                AudioTrack.MODE_STREAM);
        while(audioTrack.getState() != AudioTrack.STATE_INITIALIZED){

        }
        audioTrack.play();
        audioTrack.write(audioData, 0, bufferSizeInBytes);


    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />
<Button
    android:id="@+id/startrec"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Start Recording Test" />
<Button
    android:id="@+id/stoprec"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Stop Recording" />
<Button
    android:id="@+id/playback"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Play Back" />

</LinearLayout>

AndroidManfist.xml权限

    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

要验证设备是否只能在后台播放内容,然后点击Start Recording录制一个小扇区,然后点击Stop Recording,此时点击Play Back并检查是否听到了背景声音。如果您能听到背景声音,那么AEC无法正常工作。 但是为什么会出现这种不一致,或者我如何实现回声消除(我已经在我的应用中使用WebRTC在我的应用上下文中进行噪声消除)

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:2)

我在S6设备上遇到了同样的问题。我玩了各种设置,发现了一套似乎能够启用AEC的设备。我和您的设置之间的差异似乎是:

  • 16k采样率
  • audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);

对于其他人,我不确定究竟需要什么设置才能让AEC正常工作。我知道我的同一个应用

  • 48k采样率
  • 没有audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
  • 没有android.permission.MODIFY_AUDIO_SETTINGS

未成功AEC。

答案 1 :(得分:0)

问题:

AudioTrack audioTrack = new AudioTrack(
    AudioManager.STREAM_MUSIC, sampleRateInHz,
    AudioFormat.CHANNEL_OUT_MONO,
    AudioFormat.ENCODING_PCM_16BIT,
    bufferSizeInBytes,
    AudioTrack.MODE_STREAM);

应该是:

AudioTrack audioTrack = new AudioTrack(
    AudioManager.STREAM_MUSIC, sampleRateInHz,
    AudioFormat.CHANNEL_OUT_MONO,
    AudioFormat.ENCODING_PCM_16BIT,
    bufferSizeInBytes,
    AudioTrack.MODE_STREAM
    sessionId); // this param is important, which is audioRecord. getAudioSessionId()