我在两个安卓设备之间有一个基本的 audiorecord-audiotrack、udp 数据包语音聊天。它有效,但我的回声很差。我正在尝试使用由 JNI 移植到 android 的 Speex 来消除回声。我导入的 speex 有效,但回声消除无效。本机 C 代码为:
#include <jni.h>
#include <speex/speex_echo.h>
#define FRAME_SIZE 256
#define FILTER_LENGTH 800
SpeexEchoState *echoState;
// Initialization of echo cancelation
void Java_telefonie_voip_VoIPActivity_InitEcho(JNIEnv * env, jobject jobj)
{
echoState = speex_echo_state_init(FRAME_SIZE, FILTER_LENGTH);
}
// Queue the frame to soundcard for playing (receiving)
void Java_telefonie_voip_VoIPActivity_Playback(JNIEnv * env, jobject jobj, jshortArray inputFrame)
{
speex_echo_playback(echoState, inputFrame);
}
jshortArray Java_telefonie_voip_VoIPActivity_Capture(JNIEnv * env, jobject jobj, jshortArray inputFrame)
{
jshortArray outputFrame;
speex_echo_capture(echoState, inputFrame, *&outputFrame);
return outputFrame;
}
// Destroing echo cancelation
void Java_telefonie_voip_VoIPActivity_DestroyEcho(JNIEnv * env, jobject jobj)
{
speex_echo_state_destroy(echoState);
}
还有一些重要的java代码:
native void InitEcho();
native void DestroyEcho();
native void Playback(short[] inputData); // listener/receiving
native short[] Capture(short[] inputData); // recorder/sender
static {
System.loadLibrary("speex");
}
public void Initialize ()
{
minBufferSize = 4096;
try
{
InitEcho();
}
catch (Exception e){Log.e(TAG, "jni " + e.getMessage());}
}
public void startRecording ()
{
isStreaming = true;
streamingThread = new Thread(new Runnable(){
@Override
public void run ()
{
try
{
audioRecorder = new AudioRecord(
MediaRecorder.AudioSource.MIC,
sampleRate,
channelConfig,
audioFormat,
minBufferSize*10
);
buffer = new short[minBufferSize];
audioRecorder.startRecording();
while(isStreaming)
{
sendPackets++;
minBufferSize = audioRecorder.read(buffer, 0, buffer.length);
// Echo cancelling
buffer = Capture(buffer);
// Send
dataPacket = new DatagramPacket(ShortToByteArray(buffer), buffer.length*2, receiverAddressIA, serverPort1);
dataSocket.send(dataPacket);
}
}
catch(Exception e)
{
Log.e(TAG, "2" + e.getMessage());
}
}
});
streamingThread.start();
}
public void startListen ()
{
isListening = true;
receivingThread = new Thread(new Runnable(){
@Override
public void run ()
{
try
{
audioTrack = new AudioTrack(
AudioManager.STREAM_VOICE_CALL,
sampleRate,
channelConfig,
audioFormat,
minBufferSize*10,
AudioTrack.MODE_STREAM
);
audioTrack.play();
buffer2 = new short[minBufferSize];
while (isListening)
{
receivedPackets++;
dataPacket2 = new DatagramPacket(ShortToByteArray(buffer2), buffer2.length*2);
dataSocket2.receive(dataPacket2);
// Cancel echo
Playback(buffer2);
// Queue to soundcard
audioTrack.write(buffer2, 0, minBufferSize);
}
}
catch(Exception e)
{
Log.e(TAG, "3" + e.getMessage());
}
}
});
receivingThread.start();
}
public short[] ByteToShortArray(byte[] byteArray)
{
short[] shortArray = new short[byteArray.length/2];
ByteBuffer.wrap(byteArray).order(ByteOrder.BIG_ENDIAN).asShortBuffer().get(shortArray);
return shortArray;
}
问题是当我启动记录器/流媒体线程时,它向我显示它发送了一个包,然后应用程序崩溃而没有任何消息。您有什么建议或建议吗?请帮助我,因为我需要尽快完成这个项目,我已经努力工作并记录了自己,但它仍然不想很好地工作。谢谢!
编辑:我刚刚发现
// Echo cancelling
buffer = Capture(buffer);
正在触发崩溃。