本文介绍了通过Speex库取消Android回声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 //将播放的和录制的短裤发送到speex中,//返回已删除回声的音频数据filterShorts = nativeMethod_speexEchoCancel(recordedShorts,recvShorts); 

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

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

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);
}
解决方案

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);

这篇关于通过Speex库取消Android回声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 22:39