gpt4 book ai didi

通过 Speex 库的 Android 回声消除

转载 作者:太空宇宙 更新时间:2023-11-03 11:11:54 28 4
gpt4 key购买 nike

我只是想通过麦克风录制我的声音并从扬声器中听我自己的声音,现在问题是当我听我的声音时,添加了回声,我试图通过使用 Speex 库取消/删除回声,但是没有成功。有人可以帮我吗,下面是代码:

https://github.com/yayanyang/speex-android下载代码Speex-Android| .

我在 jni 中添加了 echoCanceler.c 文件,如下所示

enter image description here

主 Activity .java

 package com.example;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.R;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class MainActivity extends Activity {
private int minBufSize ;
private AudioTrack track;
private AudioManager am;
private AudioRecord recorder;
private boolean status = true;


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

minBufSize = AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
recorder = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION, 8000, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT, minBufSize);

int maxJitter = AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
track = new AudioTrack(AudioManager.MODE_IN_COMMUNICATION, 8000, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, maxJitter, AudioTrack.MODE_STREAM);


am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_IN_COMMUNICATION);
am.setSpeakerphoneOn(true);

if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}

public void startMethod()
{
Thread thread = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
SpeexEchoCanceler speexEchoCanceler = new SpeexEchoCanceler();
short[] buffer = new short[1024];

recorder.startRecording();
track.play();
status = true;
while(status == true)
{
minBufSize = recorder.read(buffer, 0, buffer.length);
speexEchoCanceler.openEcho(8000, minBufSize, minBufSize);
short b[] = speexEchoCanceler.processEcho(buffer, buffer);
track.write(b, 0, b.length);
track.flush();
}
}
catch (Exception e)
{
Log.e(“E”, "Exception"+e);
}
}
});
thread.start();
}


public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}

//button click event
public void startButtonClick(View view)
{
startMethod();
}

public void stopButtonClick(View view)
{
status = false;
}
}

SpeexEchoCanceler.java

 package com.example;

public class SpeexEchoCanceler
{

protected native static void open(int sampleRate, int bufSize, int totalLength);
protected native static short[] process(short[] inputframe, short[] echoframe);
protected native static void close();


public synchronized void openEcho(int sampleRate, int bufSize, int totalLength)
{
open(sampleRate,bufSize,totalLength);
}

public synchronized short[] processEcho(short[] inputframe, short[] echoframe)
{
return process(inputframe, echoframe);
}

public synchronized void closeEcho()
{
close();
}

static {
System.loadLibrary("speex");
}
}

echoCanceler.c

#include <jni.h>
#include "speex/speex_echo.h"
#include "speex/speex_preprocess.h"
#define NULL 0
SpeexEchoState *st;
SpeexPreprocessState *den;

JNIEXPORT void JNICALL Java_com_example_SpeexEchoCanceler_open
(JNIEnv *env, jobject jObj, jint jSampleRate, jint jBufSize, jint jTotalSize)
{
//init
int sampleRate=jSampleRate;
st = speex_echo_state_init(jBufSize, jTotalSize);
den = speex_preprocess_state_init(jBufSize, sampleRate);
speex_echo_ctl(st, SPEEX_ECHO_SET_SAMPLING_RATE, &sampleRate);
speex_preprocess_ctl(den, SPEEX_PREPROCESS_SET_ECHO_STATE, st);
}

JNIEXPORT jshortArray JNICALL Java_com_example_SpeexEchoCanceler_process
(JNIEnv * env, jobject jObj, jshortArray input_frame, jshortArray echo_frame)
{
//create native shorts from java shorts
jshort *native_input_frame = (*env)->GetShortArrayElements(env, input_frame, NULL);
jshort *native_echo_frame = (*env)->GetShortArrayElements(env, echo_frame, NULL);

//allocate memory for output data
jint length = (*env)->GetArrayLength(env, input_frame);
jshortArray temp = (*env)->NewShortArray(env, length);
jshort *native_output_frame = (*env)->GetShortArrayElements(env, temp, 0);

//call echo cancellation
speex_echo_cancellation(st, native_input_frame, native_echo_frame, native_output_frame);
//preprocess output frame
speex_preprocess_run(den, native_output_frame);

//convert native output to java layer output
jshortArray output_shorts = (*env)->NewShortArray(env, length);
(*env)->SetShortArrayRegion(env, output_shorts, 0, length, native_output_frame);

//cleanup and return
(*env)->ReleaseShortArrayElements(env, input_frame, native_input_frame, 0);
(*env)->ReleaseShortArrayElements(env, echo_frame, native_echo_frame, 0);
(*env)->ReleaseShortArrayElements(env, temp, native_output_frame, 0);

return output_shorts;
}

JNIEXPORT void JNICALL Java_com_example_speexjni_SpeexEchoCanceler_close
(JNIEnv *env, jobject jObj)
{
//close
speex_echo_state_destroy(st);
speex_preprocess_state_destroy(den);
}

最佳答案

引用这篇文章:Speex echo cancellation configuration

//send played and recorded shorts into speex, 
//returning audio data with the echo removed
filteredShorts = nativeMethod_speexEchoCancel(recordedShorts, recvShorts);

你应该处理“记录帧”和“接收帧”,即来自麦克风和扬声器的声音,而不是相同的“缓冲区”

short b[] = speexEchoCanceler.processEcho(buffer, buffer);

关于通过 Speex 库的 Android 回声消除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23010325/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com