2

I am developing an app which have a speech recognition feature using android built in SpeechRecognizer and RecognizerIntent. Is there any noise reduction or noise cancellation library for android that can integrate to my app to improve the accuracy of speech recognition?. I saw a NoiseSuppressor Class in android but i dint know how to integrate it in SpeechRecognizer. I am newbie in field of android programming. Thanks in advance

public class MainActivity extends AppCompatActivity implements RecognitionListener {

private AudioRecord audioRecord;
private TextView returnedText;
private Button editButton;
private Button clearButton;
private ToggleButton toggleButton;
private ProgressBar progressBar;
private SpeechRecognizer speech ;
private Intent recognizerIntent, editIntent;
private String LOG_TAG = "MainActivity";

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

    //for noise suppressor checking
    int N = AudioRecord.getMinBufferSize(48000,AudioFormat.CHANNEL_IN_MONO,AudioFormat.ENCODING_PCM_16BIT);
    audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, N*10);
    int sessionId = audioRecord.getAudioSessionId();
    NoiseSuppressor noiseSuppresor = NoiseSuppressor.create(sessionId);

    if(noiseSuppresor == null){
        Toast.makeText(this, "No Suppersor", Toast.LENGTH_LONG).show();
    }else{
        Toast.makeText(this, "Have Suppersor", Toast.LENGTH_LONG).show();
    }

    returnedText = (TextView) findViewById(R.id.textView1);
    progressBar = (ProgressBar) findViewById(R.id.progressBar1);
    toggleButton = (ToggleButton) findViewById(R.id.toggleButton1);
    editButton = (Button)findViewById(R.id.button1);
    clearButton = (Button)findViewById(R.id.button2);

    progressBar.setVisibility(View.INVISIBLE);

    toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            initSpeech();
            if (isChecked) {
                progressBar.setVisibility(View.VISIBLE);
                progressBar.setIndeterminate(true);
                speech.startListening(recognizerIntent);
            } else {
                progressBar.setIndeterminate(false);
                progressBar.setVisibility(View.INVISIBLE);
                speech.stopListening();
            }
        }
    });

    clearButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            returnedText.setText("");
        }
    });

    editButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            editIntent = new Intent(MainActivity.this, EditorActivity.class);
            String forEditText = returnedText.getText().toString();
            editIntent.putExtra("forEdit", forEditText);
            startActivity(editIntent);
        }
    });


}

private void initSpeech(){

    speech = SpeechRecognizer.createSpeechRecognizer(this);
    speech.setRecognitionListener(this);
    recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,"en");
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE, true);
    recognizerIntent.putExtra("android.speech.extra.DICTATION_MODE", true);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS, 5000);
}

@Override
public void onResume() {
    super.onResume();
}

@Override
protected void onPause() {
    super.onPause();
    if (speech != null) {
        speech.stopListening();
        speech.cancel();
        Log.i(LOG_TAG, "destroy");
    }


}

@Override
public void onBeginningOfSpeech() {
    Log.i(LOG_TAG, "onBeginningOfSpeech");
    progressBar.setIndeterminate(false);
    progressBar.setMax(10);
}

@Override
public void onBufferReceived(byte[] buffer) {
    Log.i(LOG_TAG, "onBufferReceived: " + buffer);
}

@Override
public void onEndOfSpeech() {
    Log.i(LOG_TAG, "onEndOfSpeech");
    progressBar.setIndeterminate(true);
    toggleButton.setChecked(false);
    speech.destroy();
}

@Override
public void onError(int errorCode) {
    String errorMessage = getErrorText(errorCode);
    Log.d(LOG_TAG, "FAILED " + errorMessage);
    returnedText.setText(errorMessage);
    toggleButton.setChecked(false);
    speech.destroy();
}

@Override
public void onEvent(int arg0, Bundle arg1) {
    Log.i(LOG_TAG, "onEvent");
}

@Override
public void onPartialResults(Bundle partialResults) {
    ArrayList<String> matches = partialResults
            .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
    String text = "";
    for (String result : matches)
        text += result + "\n";

    returnedText.setText(text);
}

@Override
public void onReadyForSpeech(Bundle arg0) {
    Log.i(LOG_TAG, "onReadyForSpeech");
}

@Override
public void onResults(Bundle results) {
    Log.i(LOG_TAG, "onResults");
    ArrayList<String> matches = results
            .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
    String text = "";
    for (String result : matches)
        text += result + "\n";

    returnedText.append(text);
    speech.destroy();
}

@Override
public void onRmsChanged(float rmsdB) {
    Log.i(LOG_TAG, "onRmsChanged: " + rmsdB);
    progressBar.setProgress((int) rmsdB);
}

public static String getErrorText(int errorCode) {
    String message;
    switch (errorCode) {
        case SpeechRecognizer.ERROR_AUDIO:
            message = "Audio recording error";
            break;
        case SpeechRecognizer.ERROR_CLIENT:
            message = "Client side error";
            break;
        case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
            message = "Insufficient permissions";
            break;
        case SpeechRecognizer.ERROR_NETWORK:
            message = "Network error";
            break;
        case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
            message = "Network timeout";
            break;
        case SpeechRecognizer.ERROR_NO_MATCH:
            message = "No match";
            break;
        case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
            message = "RecognitionService busy";
            break;
        case SpeechRecognizer.ERROR_SERVER:
            message = "error from server";
            break;
        case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
            message = "No speech input";
            break;
        default:
            message = "Didn't understand, please try again.";
            break;
    }
    return message;
}

}

1 Answers1

1

Just add "noise_suppression=on" in your AudioManager this way:

yourAudioManager.setParameters("noise_suppression=on");
kike
  • 4,255
  • 5
  • 23
  • 41
  • 1
    Thanks for your answer but how do i implement that? i have no variables that handle AudioManager. My app use offline speech recognition using SpeechRecognizer. – intac system solutions Oct 16 '17 at 10:58
  • 1
    There is no audio manager in the code, neither is it obvious what you mean by 'your AudioManager'? So please clearly state what code change you are suggesting, ideally by referencing and editing related to above code. – Sushovan Mandal Feb 06 '18 at 12:01
  • Use this: ((AudioManager)getSystemService(Context.AUDIO_SERVICE)).setParameters("noise_suppression=on"); – Alexander.Berg Oct 08 '19 at 09:11