gpt4 book ai didi

android - android 的语音增强/降噪/降噪库

转载 作者:行者123 更新时间:2023-11-29 01:06:24 29 4
gpt4 key购买 nike

我正在开发一个具有语音识别功能的应用程序,使用内置于 SpeechRecognizer 和 RecognizerIntent 的 android。是否有任何适用于 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;
}

最佳答案

只需以这种方式在您的 AudioManager 中添加“noise_suppression=on”:

yourAudioManager.setParameters("noise_suppression=on");

关于android - android 的语音增强/降噪/降噪库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46762235/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com