2014-01-14 9 views
5

AudioRecordから振幅を取得するコードがあります。問題は、数学が返すことだけです - 無限。私はいくつかのより多くの目が私と一緒にそれをしてください見てもらうことができます:Amplitude from AudioRecord

private class measureSnoreAudio extends AsyncTask<String, String, String> { 

    @Override 
    protected String doInBackground(String... params) { 


      Log.d(TAG, "Creating the buffer of size " + BUFFER_SIZE); 
      byte[] buffer = new byte[BUFFER_SIZE]; 

      Log.d(TAG, "Creating the AudioRecord"); 
      recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, 
        RECORDING_RATE, CHANNEL, FORMAT, BUFFER_SIZE * 10); 

      Log.d(TAG, "AudioRecord recording..."); 
      recorder.startRecording(); 

      while (isRecordingSnore) { 

       // read the data into the buffer 
       int read = recorder.read(buffer, 0, buffer.length); 
       int amplitude = (buffer[0] & 0xff) << 8 | buffer[1]; 

       // Determine amplitude 
       double amplitudeDb = 20 * Math 
         .log10(Math.abs(amplitude)/32768); 
       String dbString = String.valueOf(amplitudeDb); 
       Log.d("Snore DB", "dB " + dbString); 
       //TextView textAmplitude = (TextView) findViewById(R.id.tvAmplitude); 
       //textAmplitude.setText(dbString); 
      } 

      Log.d(TAG, "AudioRecord finished recording"); 
     return null; 
    } 
} 
+0

に変更することができますが、私はあなたがこの行で何をしているか尋ねることができますバッファ[1]; ' また、FFTを使用せずにdb-amplitudeをどのように報告しますか? –

答えて

7
double amplitudeDb = 20 * Math.log10(Math.abs(amplitude)/32768); 

私は多分問題は、Math.abs(振幅)/ 32768からで、振幅が整数だと思うので、Math.abs(振幅Math.abs(振幅)が32768(おそらく私は正しくない、バイトは最大2^7 - 1、ここでは32768より大きい振幅か?)より小さい整数を返します。だから、Math.abs(振幅)/ 32768は0に等しい。Log10(0)は-Infinityなので、EclipseのJavaプロジェクトでテストした。 | `int型の振幅=([0]&0xffのをバッファリング)<< 8: あなたは

double amplitudeDb = 20 * Math.log10((double)Math.abs(amplitude)/32768); 
+0

いい仕事です。私はついにいくつかの数字を持っていました。彼らはすべてほとんど陰性ですが、少なくとも私は何が起こっているか見ることができます。御時間ありがとうございます。 –

+0

私は私が助けることができてうれしいです。 :] – frogoscar

+0

なぜint振幅=(バッファ[0]と0xff)<< 8 |バッファ[1]。 ?ありがとうございました – user601836

関連する問題