AudioMaxAmplitude help!

dibb3386

Member
Licensed User
Longtime User
Hi,

Am trying to have the android microphone detect sound input and play a sound file when it gets so loud.

I have followed the audio recorder threads and have used the example that logs the AudioMaxAmplitube, not what i want to do is when the level reaches 5000 or what ever a sound file is played.

Is this going to be possible as i need it to be as close to real time as possible so recording to a file then playing a sound if the AudioMaxAmplitude reaches 5000, i think is going to be to slow.

Any help would be great.

B4X:
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
Dim A As AudioRecorder
Dim timer1 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 Panel1 As Panel
   Dim speak1 As ImageView
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("Layout1")
Activity.LoadLayout("main.bal")
   timer1.Initialize("timer1", 3000)
   timer1.Enabled = True
'Calls sub AmplitudeCheck every 500 milliseconds
   T.Initialize("AmplitudeCheck",500)
   
   'Set up recorder
   A.AudioSource=A.AS_MIC
   A.OutputFormat=A.OF_THREE_GPP
   A.AudioEncoder=A.AE_AMR_NB
   A.setOutputFile("","/dev/null")
   A.prepare
   
   A.start
   T.Enabled=True   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
T.Enabled=False
   A.stop
End Sub
Sub timer1_Tick
speak1.Visible = False
End Sub
Sub AmplitudeCheck_tick
   Level=A.AudioMaxAmplitude
      Log(Level)
End Sub
 

stevel05

Expert
Licensed User
Longtime User
How big a sound file do you want to play? For small files, soundpool may be the way to go. Initialization is done in advance. I haven't tried using it with media recorder though.

via Tapatalk
 
Upvote 0

dibb3386

Member
Licensed User
Longtime User
There will be around 4-5 different files, no more than 3 seconds long each.

How did you have the microphone detect sound input? I know how to use the soundpool, just I dont understand how I can detect microphone input to then play a sound.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You'd have to use the audio recorder library to listen, pretty much as the example, and then trigger the sounds using soundpool.

You'll have experiment to test the sensitivity, I would assume that it can be different on different devices.

via Tapatalk
 
Upvote 0

dibb3386

Member
Licensed User
Longtime User
Hey thanks for the reply

I have added the soundpool and it plays the audio clip only all the time, (maybe because of the microphone sensitivity if so I need to have it only play a sound when it detects an input of a certain loudness.) Is there anyway i can have the soundfile only play if the amplitudeCheck_tick reaches 7000 or more?

Very cool library tho so thank you for that :)
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Check the value of Level before playing the sound.

If you're having a problem, you can post your code which will make it easier to help.

via Tapatalk
 
Upvote 0

dibb3386

Member
Licensed User
Longtime User
Am unsure how to check the level, i think thats the only thing missing now. Check sound level if its X or greater play sound file.

B4X:
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
Dim A As AudioRecorder
Dim timer1 As Timer
Dim SP As SoundPool
Dim LoadId1, PlayId1, LoadId2, PlayId2 As Int
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 Panel1 As Panel
   Dim speak1 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
SP.Initialize(1)
LoadId1 = SP.Load(File.DirAssets, "Pushy.mp3")
LoadId2 = SP.Load(File.DirAssets, "long_fart.ogg")
End If
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
Activity.LoadLayout("main.bal")
   timer1.Initialize("timer1", 3000)
   timer1.Enabled = True
'Calls sub AmplitudeCheck every 500 milliseconds
   T.Initialize("AmplitudeCheck",500)
   
   'Set up recorder
   A.AudioSource=A.AS_MIC
   A.OutputFormat=A.OF_THREE_GPP
   A.AudioEncoder=A.AE_AMR_NB
   A.setOutputFile("","/dev/null")
   A.prepare
   
   A.start
   T.Enabled=True   
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
T.Enabled=False
   A.stop
End Sub
Sub timer1_Tick
speak1.Visible = False
End Sub
Sub AmplitudeCheck_tick
   Level=A.AudioMaxAmplitude   
   'PlayId1 = SP.Play(LoadId1, 1, 1, 1, 2, 1) 'Uncommenting this only has the sound file play all the time over and over
   'Log(Level)
End Sub

Thanks for taking the time to help me out. :)
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
That's it

B4X:
If Level > X Then PlayId1 = SP.Play(LoadId1, 1, 1, 1, 2, 1)

Should do it, set X to whatever level you determine through testing.
 
Upvote 0

dibb3386

Member
Licensed User
Longtime User
yeah thats it lol :D Thanks so much was driving me nuts. Well glad that got sorted. Its gonna be a monkey that when detects your voice farts until you stop speaking. Which comes from the mind of my 6 year old boy lol.

Again thanks a lot :D
 
Upvote 0
Top