#Region Project Attributes
' #ApplicationLabel: B4A Example
' #VersionCode: 1
' #VersionName:
' 'SupportedOrientations possible values: unspecified, landscape or portrait.
' #SupportedOrientations: unspecified
' #CanInstallToExternalStorage: False
#ApplicationLabel: Knock
#VersionCode: 1
#VersionName:
#BridgeLogger: True
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#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.
Private xui As XUI
Private streamer As AudioStreamer
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
Dim SoundLevelLabel As Label
Dim KnockLabel As Label
Dim btnStartRec As Button
Dim btnStopRec As Button
Dim SoundLevelBar As ProgressBar
Dim Tick0 As Long 'tick as at start of recording
Dim StartNoiseTick As Long = 0 'tick as at start of noise
Dim MaxNoiseSoundLevel As Int 'maximum sound level of current noise burst
Dim NoiseSoundLevel As Int = 1200 '*** adjust to suit your situation ***
End Sub
Sub Activity_Create(FirstTime As Boolean)
'''Activity.LoadLayout("Layout")
Screen
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub Screen
SoundLevelLabel.Initialize("None")
SoundLevelLabel.Color=Colors.LightGray
SoundLevelLabel.Gravity=Gravity.FILL
SoundLevelLabel.Text="TIME" '"Время"
Activity.AddView(SoundLevelLabel, 10%x, 31%y, 80%x, 10%y)
KnockLabel.Initialize("None")
KnockLabel.Color=Colors.LightGray
KnockLabel.Gravity=Gravity.FILL
KnockLabel.Text="TIME" '"Время"
Activity.AddView(KnockLabel, 10%x, 51%y, 80%x, 10%y)
btnStartRec.Initialize("StartRec")
btnStartRec.Color=Colors.Gray
btnStartRec.Gravity=Gravity.FILL
btnStartRec.Text="START"
Activity.AddView(btnStartRec, 10%x, 2%y, 30%x, 10%y)
btnStopRec.Initialize("StopRec")
btnStopRec.Color=Colors.Gray
btnStopRec.Gravity=Gravity.FILL
btnStopRec.Text="STOP"
Activity.AddView(btnStopRec, 50%x, 2%y, 30%x, 10%y)
SoundLevelBar.Initialize("SoundLevelBar")
Activity.AddView(SoundLevelBar, 10%x, 81%y, 80%x, 10%y)
End Sub
Private Sub StartRec_Click
Private rp As RuntimePermissions
Log("StartRec_Click")
rp.CheckAndRequest(rp.PERMISSION_RECORD_AUDIO)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
If Result = False Then
MsgboxAsync("No permission", "")
Return
End If
Tick0 = DateTime.Now
streamer.Initialize("streamer", 44100, True, 16, streamer.VOLUME_MUSIC)
Sleep(1)
streamer.StartRecording
Sleep(1)
End Sub
Private Sub StopRec_Click
Log("StopRec_Click")
streamer.StopRecording
Sleep(1)
'let user know what's going on
SoundLevelLabel.Text = "Stopped"
End Sub
Private Sub streamer_RecordBuffer (Buffer() As Byte)
' buffers.Add(Buffer)
' Log(DateTime.Now & TAB & Buffer.Length)
If Buffer.Length = 0 Then
Return 'nothing to do
End If
Dim bc As ByteConverter
bc.LittleEndian = True
Dim Sample16() As Short = bc.ShortsFromBytes(Buffer)
Dim ThisSample As Short = Sample16(0)
Dim MinSample As Short = ThisSample
Dim MaxSample As Short = ThisSample
For I = 1 To Sample16.Length - 1 'note starts from 1 not 0, so that there is always a previous sample
ThisSample = Sample16(I)
If ThisSample < MinSample Then
MinSample = ThisSample
else if ThisSample > MaxSample Then
MaxSample = ThisSample
End If
Next
Dim SoundLevel As Int = MaxSample - MinSample '0..65535 ie potentially larger than Short
If SoundLevel <= 0 Then 'will never be < 0, but is no extra cost to play safe
Dim SoundLevelLog As Float = 0
Else
Dim SoundLevelLog As Float = Logarithm(SoundLevel, 65535)
End If
SoundLevelLabel.Text = NumberFormat2(SoundLevelLog, 1, 3, 3, False) & TAB & SoundLevel
' Dim Seconds As Float = (DateTime.Now - Tick0) / 1000
'
' Log( _
' NumberFormat2(Seconds, 1, 1, 1, False) & TAB & _
' Buffer.Length & TAB & _
' MinSample & TAB & _
' MaxSample & TAB & _
' SoundLevel & TAB & _
' NumberFormat2(SoundLevelLog, 1, 3, 3, False) _
' )
SoundLevelBar.Progress = SoundLevelLog * 95 's/b 100 but looks like microphone is peaking out
If SoundLevel > NoiseSoundLevel Then
If StartNoiseTick = 0 Then
StartNoiseTick = DateTime.Now
MaxNoiseSoundLevel = SoundLevel 'start tracking max sound level
Else
If SoundLevel > MaxNoiseSoundLevel Then
MaxNoiseSoundLevel = SoundLevel
End If
End If
Else
If StartNoiseTick <> 0 Then
Dim EndNoiseTick As Long = DateTime.Now
Dim NoiseDurationMilliseconds As Int = EndNoiseTick - StartNoiseTick
Log( _
NoiseDurationMilliseconds & _
" ms noise at " & _
DateTime.Date(StartNoiseTick) & _
" " & _
DateTime.Time(StartNoiseTick) & _
" (sound level " & _
MaxNoiseSoundLevel & _
")" _
)
KnockLabel.Text = _
"Last noise was at " & _
DateTime.Time(StartNoiseTick) & _
" (sound level " & _
MaxNoiseSoundLevel & _
")"
StartNoiseTick = 0 'reset ready for next noise
End If
End If
End Sub
Sub streamer_Error
Log(LastException)
End Sub