Android AcousticEchoCanceler不会取消回声

时间:2014-06-11 19:36:16

标签: android audio

我开发的视频聊天应用程序遇到了相当烦人的问题,这就是音频回声的问题。 我充其量只是业余爱好者,但我正在进行的项目至少需要全功能的音频通信。视频比我原先预想的要容易得多。 预期的结构最终是在同一部手机上接受输入和另一个播放输出的线程,为了开发它,我做了两个小应用程序,在一部手机上接收麦克风输入,并通过数据报套接字发送到另一个。有问题的手机是运行Android 4.1.2的LG Optimus L7-2和运行Android 4.2.2的Alcatel Idol Mini(我认为它也被宣传为Onetouch或其他一些产品。)。 传输音频的代码运行完美,背景噪音最小(我猜测得益于我选择的输入和后期处理),但是,只要两部手机足够接近,我就会得到相当惊人的echo,如果我敢于同时尝试将输入/输出放在同一个应用程序中,那么这种情况会变得更糟。

在我最初的尝试以某种方式过滤失败后(AcousticEchoCanceler似乎比NoiseSupressor帮助少,而且AutomaticGainControl似乎造成的伤害大于好),我已经做了一些阅读,但没有发现任何可能有用的东西。 我在这一点上相当困惑,因为我似乎无法摆脱我错过了一些明显的东西的感觉,而且设置起来并不复杂。

我另外提供用于录音/播放的基本代码。

记录器部分

package com.example.audiotest;

import java.io.IOException;
import java.io.InputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.media.audiofx.AcousticEchoCanceler;
import android.media.audiofx.AutomaticGainControl;
import android.media.audiofx.NoiseSuppressor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    private Button startButton,stopButton;
    public byte[] buffer;
    public static DatagramSocket socket;
    private int port=50005;
    AudioRecord recorder;
    private int sampleRate = 22050;
    private int channelConfig = AudioFormat.CHANNEL_IN_MONO;    
    private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
    private int minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
    private boolean status = true;




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startButton = (Button) findViewById (R.id.start_button);
        stopButton = (Button) findViewById (R.id.stop_button);

        startButton.setOnClickListener (startListener);
        stopButton.setOnClickListener (stopListener);
        Log.v("AudioPlayerApp","minBufSize: " + minBufSize);
        //minBufSize += 2048;
        minBufSize = 4096;
        System.out.println("minBufSize: " + minBufSize);
    }

    private final OnClickListener stopListener = new OnClickListener() {

        @Override
        public void onClick(View arg0) {
                    status = false;
                    recorder.release();
                    Log.d("VS","Recorder released");
        }

    };

    private final OnClickListener startListener = new OnClickListener() {

        @Override
        public void onClick(View arg0) {
                    status = true;
                    startStreaming();           
        }

    };

    public void startStreaming() {


        Thread streamThread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {

                    DatagramSocket socket = new DatagramSocket();
                    Log.d("AudioPlayerApp"", "Socket Created");
                    minBufSize = 4096;
                    byte[] buffer = new byte[minBufSize];

                    Log.d("AudioPlayerApp","Buffer created of size " + minBufSize);
                    DatagramPacket packet;

                    final InetAddress destination = InetAddress.getByName("192.168.0.13");



                    recorder = new AudioRecord(MediaRecorder.AudioSource.VOICE_RECOGNITION,sampleRate,channelConfig,audioFormat,minBufSize);
                    AcousticEchoCanceler canceler =  AcousticEchoCanceler.create(recorder.getAudioSessionId());
                    NoiseSuppressor ns = NoiseSuppressor.create(recorder.getAudioSessionId());
                    AutomaticGainControl agc = AutomaticGainControl.create(recorder.getAudioSessionId());
            canceler.setEnabled(true);
                    ns.setEnabled(true);
                    //agc.setEnabled(true);
                    recorder.startRecording();

                    while(status == true) {

                        //reading data from MIC into buffer
                        minBufSize = recorder.read(buffer, 0, buffer.length);

                        //putting buffer in the packet
                        packet = new DatagramPacket (buffer,buffer.length,destination,port);

                        socket.send(packet);
                    }



                } catch(UnknownHostException e) {
                    Log.e("AudioPlayerApp", "UnknownHostException");
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.e("AudioPlayerApp", "IOException");
                } 
            }

        });
        streamThread.start();
     }
     }

玩家细分。

package com.test.playsound;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

    static int port = 50005;
    static String address = "";
    static int sampleRate = 22050;
    private boolean running = true;
    private AudioTrack audioTrack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.v("Player", "Init complete");
        openPlaySocket();
    }

    private void openPlaySocket() {
        // TODO Auto-generated method stub
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {

                // TODO Auto-generated method stub
                try {

                    Log.v("AudioPlayerApp", "Opening socket");
                    DatagramSocket sSock = new DatagramSocket(port);
                    byte[] output = new byte[4096];
                    Log.v("AudioPlayerApp", "Generating AudioTrack");

                    int minBufferSize = AudioTrack.getMinBufferSize(sampleRate, 
                            AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);

                    audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                            sampleRate, AudioFormat.CHANNEL_OUT_MONO,
                            AudioFormat.ENCODING_PCM_16BIT, minBufferSize,
                            AudioTrack.MODE_STREAM);
                    DatagramPacket receivePacket = new DatagramPacket(output,
                            output.length);
                    //Log.v("AudioPlayerApp", "Playing AudioTrack");
                    audioTrack.play();
                    while (running) {
                        //Log.v("AudioPlayerApp", "Waiting Packet");
                        sSock.receive(receivePacket);
                        Log.v("AudioPlayerApp","REcieved packet");
                        //Log.v("AudioPlayerApp", "Packet recieved");
                        try {
                            //Log.v("AudioPlayerApp", "writing data to audioTrack");
                            audioTrack.write(receivePacket.getData(), 0,
                                    receivePacket.getData().length);
                        } catch (Exception e) {
                            Log.v("AudioPlayerApp",
                                    "Failed to write audio: " + e.getMessage());

                        }
                    }
                    /*Log.v("AudioPlayerApp","Opening socket");
                    ServerSocket sSock = new ServerSocket(port);
                    Socket sock = sSock.accept();
                    Log.v("AudioPlayerApp","Socket opened "+port);
                    */
                } catch (Exception e) {
                    // TODO: handle exception
                    Log.v("AudioPlayerApp", "Error: " + e.getMessage());
                }
            }
        });
        Log.v("Player", "Starting thread");
        t.start();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);

        return true;
    }

}

我知道它包含不良做法(例如没有检查有问题的设备是否支持某些事情,或者释放资源等)但是,这是为了开始测试并修复回声尽可能快地。我已经确认这两款手机都可以访问AcousticEchoCanceller,NoiseSupression,录制权限,互联网权利,而且由于AudioFormat.VOICECOMMUNICATION导致我的AudioRecord崩溃这一事实,我没有其他问题。

我正在寻找关于这个主题的任何想法或建议,因为我非常坦率地说。在录制和播放语音时可以采取哪些措施来解决回声问题?

2 个答案:

答案 0 :(得分:1)

AcousticEchoCanceler类用于取消或删除扬声器播放的音频,并由同一设备的麦克风捕获,播放和捕捉之间的延迟很小。

AcousticEchoCanceler类无法消除由于回声路径的回声延迟的长且可变性而将两部手机放在彼此附近所引起的回声。

答案 1 :(得分:0)

  1. 您需要在硬件级别利用内置回声消除功能,检查 AcousticEchoCanceler.isAvailable() 是否检查并返回 true
  2. 然后您可以在 SO
  3. 上尝试以下组合