Pregunta

My app. is calculating noise level and peak of frequency of input sound. I used FFT to get array of shorts[] buffer , and this is the code : bufferSize = 1024, sampleRate = 44100

 int bufferSize = AudioRecord.getMinBufferSize(sapleRate,
                channelConfiguration, audioEncoding);
        AudioRecord audioRecord = new AudioRecord(
                MediaRecorder.AudioSource.DEFAULT, sapleRate,
                channelConfiguration, audioEncoding, bufferSize);

and this is converting code :

short[] buffer = new short[blockSize];
        try {
            audioRecord.startRecording();
        } catch (IllegalStateException e) {
            Log.e("Recording failed", e.toString());
        }
        while (started) {
            int bufferReadResult = audioRecord.read(buffer, 0, blockSize);

            /*
             * Noise level meter begins here
             */
            // Compute the RMS value. (Note that this does not remove DC).
            double rms = 0;
            for (int i = 0; i < buffer.length; i++) {
                rms += buffer[i] * buffer[i];
            }
            rms = Math.sqrt(rms / buffer.length);
            mAlpha = 0.9;   mGain = 0.0044;
            /*Compute a smoothed version for less flickering of the
            // display.*/
            mRmsSmoothed = mRmsSmoothed * mAlpha + (1 - mAlpha) * rms;
            double rmsdB = 20.0 * Math.log10(mGain * mRmsSmoothed);

Now I want to know if this algorithm works correctly or i'm missing something ? And I want to know if it was correct and i have sound in dB displayed on mobile , how to test it ? I need any help please , Thanks in advance :)

¿Fue útil?

Solución

The code looks correct but you should probably handle the case where the buffer initially contains zeroes, which could cause Math.log10 to fail, e.g. change:

        double rmsdB = 20.0 * Math.log10(mGain * mRmsSmoothed);

to:

        double rmsdB = mGain * mRmsSmoothed >.0 0 ?
                           20.0 * Math.log10(mGain * mRmsSmoothed) :
                           -999.99;  // choose some appropriate large negative value here for case where you have no input signal
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top