2013-04-13 63 views
0

我能够在android中通过语音识别帮助检测语音,但想要测量语音强度并在测量强度后相应地执行另一个任务。 我做如下:在android中测量噪声

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.voicerecogscreen); 

    speakButton = (Button) findViewById(R.id.speakButton); 

    wordsList = (ListView) findViewById(R.id.list); 
    PackageManager pm = getPackageManager(); 
    List<ResolveInfo> activities = pm.queryIntentActivities(
      new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); 
    if (activities.size() == 0) 
    { 
     speakButton.setEnabled(false); 
     speakButton.setText("Recognizer not present"); 
    }   
} 

/** 
* Handle the action of the button being clicked 
*/ 
public void speakButtonClicked(View v) 
{ 
    startVoiceRecognitionActivity(); 
} 

/** 
* Fire an intent to start the voice recognition activity. 
*/ 
private void startVoiceRecognitionActivity() 
{ 
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
      RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice recognition Demo..."); 
    startActivityForResult(intent, REQUEST_CODE);   
} 

/** 
* Handle the results from the voice recognition activity. 
*/ 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) 
    { 

     ArrayList<String> matches = data.getStringArrayListExtra(
       RecognizerIntent.EXTRA_RESULTS); 
     wordsList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 
       matches)); 
} 

回答

1

通常测量音频功率(强度),你会使用AudioRecord 获得PCM数据,但我怀疑它,你可以做,而使用Speech Recognizer“不知道”这一点。但这种方法如果工作应该给你质量的结果。

,如果它不工作:

SpeechRecognizer Listener API有:onRMSChangedonBufferReceived

其中:

  • onRMSChanged为您提供了音频信号的电流有效值(有多强是信号)
  • onBufferReceived and AudioRecorder应该使用相同的技术从PCM样本中查找RMS值。看看here

注意:不是所有的API SpeechRecognizerListener被调用的所有设备,所以你必须要小心,并使用测试驱动的方式针对此问题。