Android Echo Cancelation via Speex Library

我们两清 提交于 2019-12-21 02:58:28

问题


I am just trying to record my voice via mic and listening my own voice from speaker, now the problem is when i m listening my voice, echo sound is added, i tried to cancel/remove the echo by using Speex library, but not succedded. Can someone please help me, below is the code:

Downloaded the code Speex-Android from https://github.com/yayanyang/speex-android.

I have added the echoCanceler.c file in jni as shown below

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

回答1:


refer to this post: Speex echo cancellation configuration

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

you should process with "recorded frame" and "received frame", i.e. sound from microphone and speaker, rather than same "buffer"

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


来源:https://stackoverflow.com/questions/23010325/android-echo-cancelation-via-speex-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!