Android Question Need a sound level (dB) example.

jmeuse2

Member
Licensed User
Longtime User
I am new and have made countless attempts to get a sample program to show the decibel level of a live microphone but I am utterly unsuccessful. Bugs galore. No need for graphics, I just need the changing numeric value. Thanks
 

JohnC

Expert
Licensed User
Longtime User
ChatGPT says...

Here's a simple B4A code snippet to measure the microphone volume level in decibels. This example continuously monitors the audio input level from the microphone and displays the volume level as a numeric value.

B4A Code to Measure Microphone Volume​

B4X:
Sub Process_Globals
    Private Recorder As AudioRecord
    Private Buffer() As Byte
    Private BufferSize As Int
    Private SampleRate As Int = 44100
    Private AudioSource As Int = 1  ' Microphone
    Private ChannelConfig As Int = 16  ' MONO
    Private AudioEncoding As Int = 2  ' PCM 16-bit
    Private Timer1 As Timer
End Sub

Sub Globals
    Private lblVolume As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main")
    
    BufferSize = AudioRecord.GetMinBufferSize(SampleRate, ChannelConfig, AudioEncoding)
    Recorder.Initialize(AudioSource, SampleRate, ChannelConfig, AudioEncoding, BufferSize)
    Buffer = Array As Byte(BufferSize)
    
    Timer1.Initialize("Timer1", 200) ' Check volume every 200 ms
    Timer1.Enabled = True
    
    Recorder.StartRecording
End Sub

Sub Activity_Resume
    Recorder.StartRecording
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    Recorder.Stop
End Sub

Sub Timer1_Tick
    Dim ReadBytes As Int = Recorder.Read(Buffer, 0, BufferSize)
    If ReadBytes > 0 Then
        Dim Sum As Long = 0
        For i = 0 To ReadBytes - 1 Step 2
            Dim Sample As Short = Bit.Or(Bit.And(0xff, Buffer(i)), Bit.ShiftLeft(Bit.And(0xff, Buffer(i + 1)), 8))
            Sum = Sum + Abs(Sample)
        Next
        
        Dim Amplitude As Float = Sum / ReadBytes
        Dim Decibels As Float = 20 * Log10(Amplitude)
        
        lblVolume.Text = "Volume: " & NumberFormat2(Decibels, 1, 2, 2, False) & " dB"
    End If
End Sub

Explanation:​

  1. AudioRecord Initialization: Sets up the microphone for recording with a SampleRate of 44100 Hz, MONO channel, and PCM 16-bit encoding.
  2. Buffer Handling: The microphone audio input is read into a buffer for processing.
  3. Decibel Calculation: The volume level is calculated based on the amplitude of the recorded audio samples.
  4. Timer: Regularly checks the microphone input every 200 ms and displays the decibel value.

Layout (Main.bal)​

You need a simple layout with a label named lblVolume to display the decibel level.

Permissions (AndroidManifest.xml)​

Add these permissions to your AndroidManifest.xml file:

<uses-permission android:name="android.permission.RECORD_AUDIO"/>

Notes:​

  • This code works on most Android devices but may require API level 23+ (Android 6.0+) for proper microphone handling.
  • Make sure to request runtime permissions if targeting Android 6.0 or higher.
Let me know if you want me to help you enhance this code or make it more efficient.
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
Have a look at this, it's not perfect:
 
Upvote 0

jmeuse2

Member
Licensed User
Longtime User
 
Upvote 0

jmeuse2

Member
Licensed User
Longtime User
Thanks, I downloaded that one, it gave me a lot of trouble but I'm going to go at it again. What Java version do I need?
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
Thanks for the ChatGPT tip, I didn't know about that resource.

Grok is a ripper too. Although ChatGPT might well be better, given its inside link to GitHub.

If you start a request with "I am writing an Android app using B4A dialect of BASIC programming language" then it does a pretty good job of producing runnable code.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…