如何使用Android中的内置麦克风获取噪音水平

问题描述 投票:1回答:1

我想要的是能够在点击按钮时以分贝(dB)获得当前的噪音水平。我一直在玩传感器,可以让它们轻松工作但是这......我很难过。我尝试了一些代码但没有工作,或帮助我理解这一点。

怎么能实现这一目标?

编辑:

我使用以下代码:

    private Thread recordingThread;
private int bufferSize = 800;
private short[][] buffers = new short[256][bufferSize];
private int[] averages = new int[256];
private int lastBuffer = 0;

AudioRecord recorder;
boolean recorderStarted = false;

protected void startListenToMicrophone()
{
    if (!recorderStarted)
    {

        recordingThread = new Thread()
        {
            @Override
            public void run()
            {
                int minBufferSize = AudioRecord.getMinBufferSize(8000,
                        AudioFormat.CHANNEL_CONFIGURATION_MONO,
                        AudioFormat.ENCODING_PCM_16BIT);
                recorder = new AudioRecord(AudioSource.MIC, 8000,
                        AudioFormat.CHANNEL_CONFIGURATION_MONO,
                        AudioFormat.ENCODING_PCM_16BIT, minBufferSize * 10);
                recorder.setPositionNotificationPeriod(bufferSize);
                recorder.setRecordPositionUpdateListener(new OnRecordPositionUpdateListener()
                {
                    @Override
                    public void onPeriodicNotification(AudioRecord recorder)
                    {
                        short[] buffer = buffers[++lastBuffer
                                % buffers.length];
                        recorder.read(buffer, 0, bufferSize);
                        long sum = 0;
                        for (int i = 0; i < bufferSize; ++i)
                        {
                            sum += Math.abs(buffer[i]);
                        }
                        averages[lastBuffer % buffers.length] = (int) (sum / bufferSize);
                        lastBuffer = lastBuffer % buffers.length;
                        Log.i("dB", ""+averages);
                        tv4.setText("" + averages[1]);
                    }

                    @Override
                    public void onMarkerReached(AudioRecord recorder)
                    {
                    }
                });

                recorder.startRecording();
                short[] buffer = buffers[lastBuffer % buffers.length];
                recorder.read(buffer, 0, bufferSize);
                while (true)
                {
                    if (isInterrupted())
                    {
                        recorder.stop();
                        recorder.release();
                        break;
                    }
                }
            }
        };
        recordingThread.start();

        recorderStarted = true;
    }
}

private void stopListenToMicrophone()
{
    if (recorderStarted)
    {
        if (recordingThread != null && recordingThread.isAlive()
                && !recordingThread.isInterrupted())
        {
            recordingThread.interrupt();
        }
        recorderStarted = false;
    }
}
    }

我的应用程序中有两个按钮。第一个调用startListenToMicrophone,第二个调用stop。我不明白这是如何工作的。我从这里得到了代码。

textview有一个奇怪的,非常大的价值。我需要的是以分贝为单位的声级。

android noise
1个回答
2
投票

只是一个过去的想法,我可能是非常错误但是,幅度以dB = 20xlog(S1 / S2)。我无法在您的代码中的任何位置找到此计算。您需要做的是获得S1,这将是当前记录的水平并获得S2,这需要是要记录的最大可能值。然后计算dB值。

© www.soinside.com 2019 - 2024. All rights reserved.