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 获得了一个奇怪且非常大的价值。我需要的是以分贝为单位的声级。

4

1 回答 1

2

只是一个短暂的想法,我可能非常错误,但是,dB=20xlog(S1/S2) 的幅度。我在您的代码中的任何地方都找不到此计算。您需要做的是获取 S1,这将是当前记录的级别,并获取 S2,这需要是要记录的最大可能值。然后计算dB值。

于 2012-11-05T22:51:12.427 回答