在开发带有VoIP功能的Android Unity应用程序时,我们发现在某些设备(特别是Galaxy S6)上,麦克风会拾取扬声器输出的声音,导致大量的静态,失真和回声。正因为如此,我开始着手开发一个原生解决方案,以利用Android的一些AEC和NS功能。
我编写了以下测试平台,它在Note 4等一些设备上运行良好。当同样的应用程序在Galaxy S6上运行时,就好像ARC和NS没有得到很好的应用,或者根本没有得到应用。
public class MainActivity extends Activity
{
private AudioRecord mAudioRecorder = null;
private AudioTrack mStreamingPlayer = null;
private Thread playbackThread = null;
private boolean useVoip = true;
int bufferSize;
int chunkSize;
private AutomaticGainControl agc;
private NoiseSuppressor suppressor;
private AcousticEchoCanceler aec;
private boolean manuallyAttachEffects = false;
private void StartRecordingAudio()
{
int sampleRate = 44100;
int channelConfig = AudioFormat.CHANNEL_IN_MONO;
int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
chunkSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
bufferSize = chunkSize * 10;
int audioSource = MediaRecorder.AudioSource.MIC;
if ( useVoip && !manuallyAttachEffects )
{
audioSource = MediaRecorder.AudioSource.VOICE_COMMUNICATION;
}
mAudioRecorder = new AudioRecord( audioSource,
sampleRate,
channelConfig,
audioFormat,
bufferSize );
if ( useVoip && manuallyAttachEffects )
{
SetupVoipEffects( mAudioRecorder.getAudioSessionId() );
}
mAudioRecorder.startRecording();
mStreamingPlayer = new AudioTrack( AudioManager.STREAM_MUSIC,
sampleRate,
AudioFormat.CHANNEL_OUT_MONO,
audioFormat,
bufferSize,
AudioTrack.MODE_STREAM );
mStreamingPlayer.play();
playbackThread = new Thread(new Runnable()
{
@Override
public void run() {
readAudioData();
}
});
playbackThread.start();
}
private void readAudioData()
{
Thread thisThread = Thread.currentThread();
byte[] buffer = new byte[chunkSize];
while ( !thisThread.isInterrupted() )
{
if ( mAudioRecorder == null )
break;
int bytesRead = mAudioRecorder.read(buffer, 0, chunkSize);
if ( bytesRead > 0 )
{
if ( mStreamingPlayer == null )
break;
mStreamingPlayer.write(buffer, 0, chunkSize);
}
}
}
private void SetupVoipEffects( int sessionId )
{
if(AutomaticGainControl.isAvailable())
{
agc = AutomaticGainControl.create( sessionId );
Log.d(LOG_TAG, "AGC is " + (agc.getEnabled()?"enabled":"disabled"));
if ( !agc.getEnabled() )
{
agc.setEnabled(true);
Log.d(LOG_TAG, "AGC is " + (agc.getEnabled()?"enabled":"disabled" +" after trying to enable"));
}
}
if(NoiseSuppressor.isAvailable())
{
suppressor = NoiseSuppressor.create( sessionId );
Log.d(LOG_TAG, "NS is " + (suppressor.getEnabled()?"enabled":"disabled"));
if ( !suppressor.getEnabled() )
{
suppressor.setEnabled(true);
Log.d(LOG_TAG, "NS is " + (suppressor.getEnabled()?"enabled":"disabled" +" after trying to disable"));
}
}
if(AcousticEchoCanceler.isAvailable())
{
aec = AcousticEchoCanceler.create( sessionId );
Log.d(LOG_TAG, "AEC is " + (aec.getEnabled()?"enabled":"disabled"));
if ( !aec.getEnabled() )
{
aec.setEnabled(true);
Log.d(LOG_TAG, "AEC is " + (aec.getEnabled()?"enabled":"disabled" +" after trying to disable"));
}
}
}
}
我已经尝试了很多事情,如使用VOICE_COMMUNICATION的AudioRecord,使用MIC和手动创建NoiseSuppressor和AcousticEchoCanceller,禁用/启用NuPlayer/AwesomePlayer,等等。设备显示支持并启用了AEC,但它似乎并没有发生。当使用耳机或使用手机而不是扬声器时,这个问题就完全消失了(所以麦克风不能接收扬声器的声音)。是我忽略了什么重要的东西,还是有可能有一个bug要报告?
如果AEC和NS在其他设备上不能完美运行,我不会怀疑。
这是Android的一个已知问题。硬件AEC是不可靠的,并且没有被许多硬件实现/支持,即使它们另有报告。我建议在部署硬件AEC的同时也部署软件AEC,以使该特性在软件中更加稳定。唯一的问题是软件AEC不是那么容易正确地完成,所以要准备好大量的测试。