如何计算从媒体播放器分贝振幅(分贝)? [英] How to compute decibel (dB) of Amplitude from Media Player?

查看:2001
本文介绍了如何计算从媒体播放器分贝振幅(分贝)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个code计算AudioRecord实时dB的幅度。在code可以很好地用于计算dB的幅度。录制结束后,我保存它WAV文件。现在,我要播放该文件,并重新计算分贝幅度。但是,我不能达到之前类似的结果。你能帮我解决它。这是我的code计算分贝幅度录制和播放时。

I have a code to compute real-time dB Amplitude of AudioRecord. The code works well for computing dB Amplitude. After recording, I save that it to wav file. Now, I want to playback that file and recompute the dB Amplitude. However, I cannot achieve similar result before. Could you help me to fix it. This is my code to compute dB Amplitude when recording and playback.

录制时1.Compute分贝振幅

1.Compute dB amplitude when recording

bufferSize = AudioRecord.getMinBufferSize(16000, AudioFormat.CHANNEL_IN_MONO,
            AudioFormat.ENCODING_PCM_16BIT);
record = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION, SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
            AudioFormat.ENCODING_PCM_16BIT, bufferSize);
audioBuffer = new short[bufferSize];
readSize=record.read(audioBuffer, 0, audioBuffer.length);
double amplitude = 0;
double sum=0;
for (int i = 0; i < readSize; i++) {
     sum += audioBuffer[i] * audioBuffer[i];
}
amplitude = sum / readSize;
dbAmp=20.0 *Math.log10(amplitude/32767.0);

2.Assume该文件输出是ouput.wav。我用的MediaPlayer播放和计算振幅

2.Assume that the file output is ouput.wav. I used MediaPlayer to playback and compute Amplitude

String filePath = Environment.getExternalStorageDirectory().getPath() +"/" +"output.wav";
mPlayer = new  MediaPlayer();
mPlayer.setDataSource(filePath);
mPlayer.prepare();
mPlayer.start();
mVisualizerView.link(mPlayer);

在其中,mVisualizerView是展示台类。这个类有链接功能,如

In which, mVisualizerView is Visualizer class. The class has link function such as

 public void link(MediaPlayer player)
  {
    // Create the Visualizer object and attach it to our media player.
    mVisualizer = new Visualizer(player.getAudioSessionId());
    mVisualizer.setScalingMode(Visualizer.SCALING_MODE_NORMALIZED);
    mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
    // Pass through Visualizer data to VisualizerView
    Visualizer.OnDataCaptureListener captureListener = new Visualizer.OnDataCaptureListener()
    {
      @Override
      public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes,
          int samplingRate)
      {       
        updateVisualizer(bytes);
      }
      @Override
      public void onFftDataCapture(Visualizer visualizer, byte[] bytes,
          int samplingRate)
      {     
        updateVisualizerFFT(bytes);
      }
    };
    mVisualizer.setDataCaptureListener(captureListener,
        Visualizer.getMaxCaptureRate() / 2, true, true);
    player.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
    {
      @Override
      public void onCompletion(MediaPlayer mediaPlayer)
      {
        mVisualizer.setEnabled(false);
      }
    });
  } 

由于我的任务,我会重新计算 dbAmp 在功能updateVisualizer或updateVisualizerFFT字节

As my task, I will recompute dbAmp from bytes in functions updateVisualizer or updateVisualizerFFT

   public void updateVisualizer(byte[] bytes) {
    dbAmp = computedbAmp(bytes); 
    mBytes = bytes;
    invalidate();
  }
  public void updateVisualizerFFT(byte[] bytes) {
    dbAmp = computedbAmp(bytes);
    mFFTBytes = bytes;
    invalidate();
  }
  public double computedbAmp(byte[] audioData) {
        //System.out.println("::::: audioData :::::"+audioData);
      double amplitude = 0;
      for (int i = 0; i < audioData.length/2; i++) {
          double y = (audioData[i*2] | audioData[i*2+1] << 8) / 32768.0;
          // depending on your endianness:
          // double y = (audioData[i*2]<<8 | audioData[i*2+1]) / 32768.0
          amplitude += Math.abs(y);
      }
      amplitude = amplitude / audioData.length / 2;
      return amplitude;
    }

目前,我申请一些方法来计算从字节分贝幅度。然而,它们是不正确的。你能不能帮我解决这个问题或建议给我的解决方案来计算呢?谢谢

Currently, I apply some way to compute dB amplitude from bytes. However, they are not correct. Could you help me to fix it or suggest to me the solution to compute it? Thanks

我的预期的解决方案,如传感器盒Android版

My expected solution such as Sensor Box for Android

推荐答案

正如前面提到的在评论中不使用两个相同的计算。另外,我不认为这两种方法是正确的。

As mentioned in the comments you are not using the same computation for both. Also, I don't think either method is correct.

这是第一个例子您的code那么它的看起来的像你试图计算它是的sqrt(sumOfSquares / N)的RMS 然后再转换为分贝。

From your code in the first example it looks like you are trying to compute the RMS which is the sqrt(sumOfSquares/N) and then convert to dB.

第二个例子是 sumOfAbs / N 不会转换为分贝

The second sample is sumOfAbs/N not converted to dB

另外一个很次要的问题是,在一个情况下,你除以32767,其他32768两者应为32768。

Another very minor issue is that in one case you divide by 32767 and the other 32768. Both should be 32768.

有关第一部分做这样的事情:

For part one do something like this:

double sum=0;
for (int i = 0; i < readSize; i++) {
    double y = audioBuffer[i] / 32768.0;
    sum += y * y;
}
double rms = Math.sqrt(sum / readSize);
dbAmp=20.0 *Math.log10(rms);

而对于第2部分:

And for part 2:

double sum=0;
for (int i = 0; i < audioData.length/2; i++) {
    double y = (audioData[i*2] | audioData[i*2+1] << 8) / 32768.0;
    sum += y * y;
}
double rms = Math.sqrt(sum / audioData.length/2);
dbAmp = 20.0*Math.log10(rms);

注意两个与破解打开字节数组之外几乎完全一致。这应该是一个线索,你找到一个方法来分解出这个功能,然后你会不会遇到这种问题在未来。

Notice the two are almost exactly identical with the exception of cracking open the byte array. This should be a clue to you to find a way to factor out this function and then you won't run into this kind of problem in the future.

编辑:

还有一件事我忘了提。有一点对此事公开辩论,但根据您的应用您可能希望dBFS的结果为正弦波校准。我的意思是你要在一个全面的正弦波运行计算,因为我已经写它,你会得到0.7071(1 / SQRT(2)),或-3dBFS的均方根值。如果你想有一个全面的正弦打恰好为零dBFS的需要乘以开方均方根值(2)。

One more thing I forgot to mention. There is a bit of open debate on this matter but depending on your application you might want your dBFS result to be sine calibrated. What I mean that is you were to run the computation on a single full scale sine wave as I've written it you would get a rms value of 0.7071 (1/sqrt(2)), or -3dBFS. If you want a full scale sine to hit exactly zero dBFS you need to multiply the rms value by sqrt(2).

这篇关于如何计算从媒体播放器分贝振幅(分贝)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆