How to calculate the audio amplitude in real time (android)

瘦欲@ 提交于 2019-12-06 03:29:54

问题


I am working on an Android app where I need to calculate the audio amplitude in real time. As of now I am using MediaPlayer to play the track. Is there a way to calculate its amplitude in real time while playing it?

Here is my code:

int counterPlayer = 0;
static double[] drawingBufferForPlayer = new double[100];
private byte[] mBytes;
private byte[] mFFTBytes;
private Visualizer mVisualizer;

public void link(final MediaPlayer player)
{
    if(player == null)
    {
        throw new NullPointerException("Cannot link to null MediaPlayer");
    }

    // Create the Visualizer object and attach it to our media player.
    mVisualizer = new Visualizer(player.getAudioSessionId());
    mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
    //mVisualizer.setMeasurementMode(Visualizer.MEASUREMENT_MODE_PEAK_RMS);

    // 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);
    // Enabled Visualizer and disable when we're done with the stream
    mVisualizer.setEnabled(true);
    player.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
    {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer)
        {
            mVisualizer.setEnabled(false);
        }
    });
}
public void updateVisualizer(byte[] bytes) {

    int t = calculateRMSLevel(bytes);
    Visualizer.MeasurementPeakRms measurementPeakRms = new Visualizer.MeasurementPeakRms();
    int x = mVisualizer.getMeasurementPeakRms(measurementPeakRms);
    mBytes = bytes;
}

/**
 * Pass FFT data to the visualizer. Typically this will be obtained from the
 * Android Visualizer.OnDataCaptureListener call back. See
 * {@link android.media.audiofx.Visualizer.OnDataCaptureListener#onFftDataCapture }
 * @param bytes
 */
public void updateVisualizerFFT(byte[] bytes) {
    int t = calculateRMSLevel(bytes);
    mFFTBytes = bytes;
}
public int calculateRMSLevel(byte[] audioData) {
    //System.out.println("::::: audioData :::::"+audioData);
    double amplitude = 0;
    for (int i = 0; i < audioData.length; i++) {
        amplitude += Math.abs((double) (audioData[i] / 32768.0));
    }
    amplitude = amplitude / audioData.length;
    //Add this data to buffer for display
    if (counterPlayer < 100) {
        drawingBufferForPlayer[counterPlayer++] = amplitude;
    } else {
        for (int k = 0; k < 99; k++) {
            drawingBufferForPlayer[k] = drawingBufferForPlayer[k + 1];
        }
        drawingBufferForPlayer[99] = amplitude;
    }

    updateBufferDataPlayer(drawingBufferForPlayer);
    setDataForPlayer(100,100);

    return (int)amplitude;
}

回答1:


Your problem lies in the improper conversion of 16-bit samples to double precision. First you need to convert two adjacent bytes to an int and then do the conversion to double. For example

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;

Please note that your code and my answer are both assuming one channel of data. If you have more than one channel you'll need to be careful to separate the amplitudes as the data will be interleaved L,R,L,R (after the conversion to double).




回答2:


Did you try with that solution?

// Calc amplitude for this waveform
float accumulator = 0;
for (int i = 0; i < data.bytes.length - 1; i++) {
  accumulator += Math.abs(data.bytes[i]);
}

float amp = accumulator/(128 * data.bytes.length);



回答3:


Use this example:

http://androidexample.com/Detect_Noise_Or_Blow_Sound_-_Set_Sound_Frequency_Thersold/index.php?view=article_discription&aid=108&aaid=130

Hope this solves your problem....cheerz



来源:https://stackoverflow.com/questions/28215289/how-to-calculate-the-audio-amplitude-in-real-time-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!