Android Question Measure dB on loudspeaker

stp

Active Member
Licensed User
Longtime User
Is it possible to measure dB on my android phone loudspeaker ?
Would like to get a value back. I dont want to record any sound. Only the dB that comes out of the laudspeaker
Thank you
 
Last edited:

zed

Well-Known Member
Licensed User
Smartphone microphones are not calibrated for accurate dB measurements.
The values obtained are often in dBFS (decibels full scale), not dB SPL (actual sound pressure).
dBFS isn't an absolute measurement like dB SPL (actual sound pressure), but it does give you a relative volume value.
Very loud sounds (>90 dB SPL) may be misrecorded or distorted.

We will use the microphone to capture the ambient sound (i.e. what comes out of the speaker) and analyze this signal in real time to extract an approximate value in dB.
We use AudioRecord to capture sound without recording it, calculate the RMS (Root Mean Square) level and convert it to dBFS in real time.

B4A:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private rp As RuntimePermissions
    Private audio As AudioRecord
    Private bufferSize As Int
    Private Timer1 As Timer
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    rp.CheckAndRequest("android.permission.RECORD_AUDIO")
    
    ' Initialisation
    bufferSize = audio.GetMinBufferSize(44100, audio.CH_CONF_MONO, audio.AF_PCM_16)
    audio.Initialize(audio.A_SRC_MIC, 44100, audio.CH_CONF_MONO, audio.AF_PCM_16, bufferSize)
    
    ' Timer to measure every 500ms
    Timer1.Initialize("Timer1", 500)
    Timer1.Enabled = True
    
End Sub

Sub Timer1_Tick
    ' Read audio data
    Dim samples() As Short = audio.ReadShort(0, bufferSize / 2) ' each sample = 2 bytes

    ' Calculation RMS
    Dim sum As Double = 0
    For Each s As Short In samples
        sum = sum + s * s
    Next
    Dim rms As Double = Sqrt(sum / samples.Length)

    ' Conversion to dBFS
    Dim dbfs As Double = 20 * Logarithm(rms / 32768, 10)
    Log("Noise level (dBFS) : " & NumberFormat(dbfs, 1, 2))
End Sub


Private Sub Button1_Click
    audio.Initialize(audio.A_SRC_MIC, 44100, audio.CH_CONF_MONO, audio.AF_PCM_16, bufferSize)
    audio.StartRecording
End Sub

You can manually calibrate by comparing it with a physical sound level meter if you want more precision.
You're not recording anything here, just a direct reading from the microphone.

Have fun
 

Attachments

  • dbLevel_Log.png
    dbLevel_Log.png
    5.8 KB · Views: 22
  • dbLevel.zip
    14.2 KB · Views: 16
Upvote 0

stp

Active Member
Licensed User
Longtime User
Smartphone microphones are not calibrated for accurate dB measurements.
The values obtained are often in dBFS (decibels full scale), not dB SPL (actual sound pressure).
dBFS isn't an absolute measurement like dB SPL (actual sound pressure), but it does give you a relative volume value.
Very loud sounds (>90 dB SPL) may be misrecorded or distorted.

We will use the microphone to capture the ambient sound (i.e. what comes out of the speaker) and analyze this signal in real time to extract an approximate value in dB.
We use AudioRecord to capture sound without recording it, calculate the RMS (Root Mean Square) level and convert it to dBFS in real time.

B4A:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private rp As RuntimePermissions
    Private audio As AudioRecord
    Private bufferSize As Int
    Private Timer1 As Timer
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    rp.CheckAndRequest("android.permission.RECORD_AUDIO")
   
    ' Initialisation
    bufferSize = audio.GetMinBufferSize(44100, audio.CH_CONF_MONO, audio.AF_PCM_16)
    audio.Initialize(audio.A_SRC_MIC, 44100, audio.CH_CONF_MONO, audio.AF_PCM_16, bufferSize)
   
    ' Timer to measure every 500ms
    Timer1.Initialize("Timer1", 500)
    Timer1.Enabled = True
   
End Sub

Sub Timer1_Tick
    ' Read audio data
    Dim samples() As Short = audio.ReadShort(0, bufferSize / 2) ' each sample = 2 bytes

    ' Calculation RMS
    Dim sum As Double = 0
    For Each s As Short In samples
        sum = sum + s * s
    Next
    Dim rms As Double = Sqrt(sum / samples.Length)

    ' Conversion to dBFS
    Dim dbfs As Double = 20 * Logarithm(rms / 32768, 10)
    Log("Noise level (dBFS) : " & NumberFormat(dbfs, 1, 2))
End Sub


Private Sub Button1_Click
    audio.Initialize(audio.A_SRC_MIC, 44100, audio.CH_CONF_MONO, audio.AF_PCM_16, bufferSize)
    audio.StartRecording
End Sub

You can manually calibrate by comparing it with a physical sound level meter if you want more precision.
You're not recording anything here, just a direct reading from the microphone.

Have fun
Thank you, that works and i found also other examples that work that way but user has to accept "android.permission.RECORD_AUDIO" that is not good. None will accept that. Another think is the green micro at the top... There must be another solution. Not with record.
 
Upvote 0

stp

Active Member
Licensed User
Longtime User
There is no recording.
The user must give consent to activate the microphone.
Yes i know that there is no recording but tell it also to the user that will see: android.permission.RECORD_AUDIO !!!!
 
Upvote 0

zed

Well-Known Member
Licensed User
Another think is the green micro at the top... There must be another solution
Every time you activate the microphone. This thing is run by the Android system. You can't do anything about it.

The permission name is not specific enough. There should be android.permission.ACTIVE_MICROPHONE
 
Upvote 0

stp

Active Member
Licensed User
Longtime User
Every time you activate the microphone. This thing is run by the Android system. You can't do anything about it.

The permission name is not specific enough. There should be android.permission.ACTIVE_MICROPHONE
I got it. There in no way. i made also a research. Microphone is the device that measures. (It does not record but gets dB sample).
Thank you
 
  • Like
Reactions: zed
Upvote 0
Top