【问题标题】:android - voice enhancement / noise cancellation / noise reduction library for androidandroid - 用于 android 的语音增强/降噪/降噪库
【发布时间】:2017-10-16 02:44:26
【问题描述】:

我正在开发一个具有语音识别功能的应用程序,它使用 Android 内置的 SpeechRecognizer 和 RecognizerIntent。是否有任何适用于 android 的降噪或降噪库可以集成到我的应用程序中以提高语音识别的准确性?我在 android 中看到了一个 NoiseSuppressor 类,但我不知道如何将它集成到 SpeechRecognizer 中。我是android编程领域的新手。提前致谢

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

}

【问题讨论】:

  • 嗨,找到解决办法了吗?

标签: android speech-recognition noise-reduction


【解决方案1】:

只需在您的 AudioManager 中添加“noise_suppression=on”即可:

yourAudioManager.setParameters("noise_suppression=on");

【讨论】:

  • 感谢您的回答,但我该如何实现呢?我没有处理 AudioManager 的变量。我的应用使用 SpeechRecognizer 使用离线语音识别。
  • 代码中没有音频管理器,您所说的“您的 AudioManager”也不是很明显吗?因此,请明确说明您建议的代码更改,最好是通过引用和编辑与上述代码相关的内容。
  • 使用这个:((AudioManager)getSystemService(Context.AUDIO_SERVICE)).setParameters("noise_suppression=on");
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-22
  • 1970-01-01
  • 2019-10-04
  • 1970-01-01
  • 2012-05-12
相关资源
最近更新 更多