我只是想通过麦克风录制我的声音并从扬声器收听我自己的声音,现在的问题是,当我收听我的声音时,添加了回声,我试图使用speex库取消/删除回声,但没有成功。有人能帮我一下吗,下面是密码:
https://github.com/yayanyang/speex-android下载了speex android代码。
我在jni中添加了echocancer.c文件,如下所示
main活动.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;
        }
    }

speexchocanceler.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");
        }
    }

回声抵消器
#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);

08-26 08:00