B4A Library SoundMeter - Real Time display of Sound Level (dB)

It is a wrap for this Github project. I have combined the attached B4A project with this line chart. Sure you will figure it out.

Do at least the following:
1. Extract b4aLibraryFiles.zip and copy the library files to your additional libs folder
2. Extract DemoRes.zip and copy the folder and its contents to be on the same folder level as that of the /Files and /Objects folders of the B4A project.

It displays the dB value on the SoundMeter and at the same time draws a live line chart.

Take it for a test drive and enjoy!

1.png



Sample Code:
B4X:
#Region  Project Attributes
    #ApplicationLabel: b4aSoundMeterNew
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#AdditionalRes: ..\DemoRes

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
 
    Dim t As Timer

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim bm As Bitmap
    Dim bmd As BitmapDrawable

    Private Button1 As Button
    Private Button2 As Button
    Private sm As SoundMeter
    Dim bufsize As Int = 300                                  'set the data buffer size here i.e how many values to display in the plot area
    Dim xlab(bufsize) As String
 
    Private lc1 As RealTimeLineChart
    Dim a As Int = 0
    Dim xlabeltrack As Int = 0
 
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
    t.Initialize("t", 50)
    bm.Initialize(File.DirAssets, "noise_disc.png")
    bmd.Initialize(bm)
    sm.BackgroundImage = bmd
 
    lc1.GraphTitleColor = Colors.White
    lc1.GraphTitleSkewX = -0.15
    lc1.GraphTitleBold = True
    lc1.GraphTitleTextSize = 15.0
    lc1.GraphPlotAreaBackgroundColor = Colors.Black          'this will paint the plotting area DrakGray regardless of what GraphBackgroundColor has been set to
    lc1.GraphBackgroundColor = Colors.Transparent               'this will paint everything within the outer frame to be white
    lc1.GraphFrameColor = Colors.Red                            'this adjusts only the outer frame color
    lc1.GraphFrameWidth = 2.0
    lc1.GraphBufferSize = bufsize
    lc1.GraphTitle = "Sound Meter"

    lc1.DomainLabelColor = Colors.Cyan
    lc1.DomainLabelTextSize = 25
    lc1.DomianLabel = "Sound Level"
 
    lc1.YaxisRangeMode = lc1.YaxisMode_FIXED                    'the other option is FIXED
    lc1.YaxisRange(40.0, 100.0)
    lc1.YaxisDivisions = 10
    lc1.YaxisLabelTicks = 1
    lc1.YaxisShowZero = True
    lc1.YaxisTitleTextSize = 20.0
    lc1.YaxisTitleColor = Colors.Green
    lc1.YaxisGridLineColor = Colors.Yellow
    lc1.YaxisLabelTextSize = 20
    lc1.YaxisLabelColor = Colors.Green
    lc1.YaxisLabelOrientation = 0
    lc1.YaxisTitle = "dB"
 
    '************************ If you comment this code then the x-axis labels will be the index value of the buffer
    For i = 0 To bufsize - 1
        xlab(i) = "" & i
    Next
    lc1.XAxisLabels = xlab
    '*************************************************************************************************************
     
    lc1.XaxisLabelTextSize = 20
    lc1.XaxisLabelTextColor = Colors.Cyan
    lc1.XaxisGridLineColor = Colors.Yellow
    lc1.XaxisLabelOrientation = 0
    lc1.XaxisDivisions = 30
    lc1.XaxisLabelTicks = 1

 
    lc1.LegendTextSize = 15.0
    lc1.LegendTextColor = Colors.White
    lc1.LegendBackgroundColor = Colors.Transparent
 
    'setup for Line 1
    lc1.Line_1_LineColor = Colors.Red
    lc1.Line_1_LineWidth = 3.0
    lc1.Line_1_DrawDash = False
    lc1.Line_1_LegendText = ""

    lc1.NumberOfLineCharts = 1
    lc1.DrawTheGraphs
 
 
End Sub

Sub Activity_Resume

    lc1.START

End Sub

Sub Activity_Pause (UserClosed As Boolean)
 

End Sub


Sub Button1_Click
    t.Enabled = True
    sm.StartdB
 
End Sub

Sub Button2_Click
 
    sm.StopdB
    t.Enabled = False
 
End Sub

Sub t_tick


    lc1.addData(sm.dBValue, 0, 0, 0, 0)
    a = a + 1
    If a = 301 Then a = 0
 
    If xlabeltrack = bufsize + 1 Then
        xlabeltrack = 0
        lc1.ClearAllData                        'THIS WILL CLEAR THE DATA
        a = 0
        For i = 0 To bufsize - 1
            xlab(i) = "" & i
        Next
        lc1.XAxisLabels = xlab
        lc1.DrawTheGraphs
        lc1.START
    End If
 
    '************************ If you comment this code then the x-axis labels will be the index of the buffer
'    xlabeltrack = xlabeltrack + 1
'    If xlabeltrack > bufsize Then
'        xlab = shiftarray(xlab)
'        xlab(bufsize - 1) = "" & xlabeltrack
'        lc1.XAxisLabels = xlab
'    End If
'    xlab(bufsize - 1) = "" & xlabeltrack
    '**************************************************************************************************************
 
End Sub

Sub shiftarray (oldarray() As String) As String()
 
    Dim newarray(bufsize)
    For i = 0 To bufsize - 2
        newarray(i) = oldarray(i + 1)
    Next
    Return newarray
 
End Sub
 

Attachments

  • b4aSoundMeterNew.zip
    103.9 KB · Views: 943
  • b4aLibraryFiles.zip
    271.1 KB · Views: 1,003
  • DemoRes.zip
    125.8 KB · Views: 867
  • TheJavaCode.zip
    9 KB · Views: 792
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
SDK 31 shows such an error and does not measure. There is no error logs either.
Does it mean it doesn't support sdk 31?
thanks..
Have you tried with the project in post #19? My android device is fairly old so cannot test it to see if it is working on the latest android version. The project in post #19 adds the required runtime permissions.
 

Almora

Well-Known Member
Licensed User
Longtime User
now i tested it on android 10 device. necessary permissions are granted. it still gives an error as in the picture.
I am using the last example.
I tested it on android 7.1 emulator. it works fine.
 

Attachments

  • image.png
    image.png
    12 KB · Views: 169
Last edited:

Almora

Well-Known Member
Licensed User
Longtime User
on the other hand, I had the chance to test it on the s21 ultra device. but it didn't work again.

alternatives are attached.
 

Attachments

  • MonitorVolume.zip
    7.4 KB · Views: 184
  • vumeter.zip
    48.7 KB · Views: 163
Last edited:

mohammadreza1360

New Member
Hi
If I want to analysis sound into octave band (63,125,250,500,1000,2000,4000,8000,16000 Hz) and get sound level in this frequencies
How can do it?
 
Top