Android Question Mic sound level

Indy

Active Member
Licensed User
Longtime User
Hi All,

I know this question comes up now and again and has quite a bit of discussion but, for me never really gets a definite answer. So my apologies for asking this again - Is there a proper method of checking the mic sound input level? I have the following code that I've put together from examples in various post.

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

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 tm As Timer
   Dim ar As AudioRecorder
   Dim pb As ProgressBar
   Dim pnl As Panel
   
End Sub

Sub Activity_Create(FirstTime As Boolean)

   'Calls sub AmplitudeCheck every 100 milliseconds
   tm.Initialize("AmplitudeCheck",100)
   ar.Initialize
   
   'Set up recorder
   ar.AudioSource=ar.AS_MIC
   ar.OutputFormat=ar.OF_THREE_GPP
   ar.AudioEncoder=ar.AE_AMR_NB
   ar.setOutputFile("","/dev/null")
   ar.prepare   
   ar.start
   
   tm.Enabled=True
   
   pnl.Initialize("")
   
   Activity.AddView(pnl,0,25%y,100%x,25%y)
   
   pnl.Color=Colors.DarkGray
   
   pb.Initialize("")
   
   pb.Enabled=False
   
   pnl.AddView(pb,0,50dip,90%x,5dip)
       
   pb.Left = (pnl.Width-pb.Width)/2 'place progress bar in the middle on x
   pb.Top = (pnl.Height-pb.Height)/2 'place progress bar in the middle on y
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
   
   tm.Enabled=False
   
   ar.stop
   
   Activity.Finish
   
End Sub

Sub AmplitudeCheck_tick
   
   Dim Level As Int = 0
   
   Level = ar.AudioMaxAmplitude

   pb.Progress = (Level/2700)*100 'convert value to percentage
   
   If pb.Progress>2 Then Log(pb.Progress)
   
End Sub

If this gets the thumbs-up then I'm going to use it in my app.

Thanks
 

Indy

Active Member
Licensed User
Longtime User
I've just hit a snag. The code works fine by itself, however, if I try and record at the same time then I get a Java error. I think the error relates to the conflict of trying to share the mic for two tasks. :(
 
Upvote 0

Indy

Active Member
Licensed User
Longtime User
It's a java.lang.IllegalStateException.

I'm using the Audio Streamer to capture the sound and Audio Recorder library to monitor the mic.

Thanks
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Why not use the audiorecorder to record the audio?
 
Upvote 0

Indy

Active Member
Licensed User
Longtime User
Why not use the audiorecorder to record the audio?
You know I'm sure there was a reason why I elected to use AudioStreamer over AudioRecorder but, can't think what. Does AR record in wav format? I need to convert the file to mp3.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
It says it does Wav , I haven't used it though. You may have to create the header I suppose, check the demo for it.
 
Upvote 0

Indy

Active Member
Licensed User
Longtime User
It says it does Wav , I haven't used it though. You may have to create the header I suppose, check the demo for it.

I've got the recording of the wav file but I'm having trouble working out how to add the header info. In the Audiostreamer this was done at the beginning of the recording followed by the ending data. With Audiorecorder there's no place to create the header first, how do I add the required data once the file has finsihed recording? I did go through the demo but that seems to be setup differently and besides it doesn't compile.

Thanks
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
If you post an example project, I'll try to help tomorrow.
 
Upvote 0

Indy

Active Member
Licensed User
Longtime User
If you post an example project, I'll try to help tomorrow.

Hi

I've attached the example I'm working on. I use the Liblame mp3 library for mp3 encoding. You'll need to creating a folder on your sd card called "Recordings", which is where the recording goes. Once you save the recording the mp3 version should end up in there as well. It's a bit messy because of the testing I keep doing. But I'm sure a person of your calibre can workout what I've coded.

Thanks for your offer to help. Very much appreciated.

Thanks
 

Attachments

  • myrecorder.zip
    7.6 KB · Views: 307
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Hi Indy, I'm away now until Sunday, I'll take a look then if you haven't sorted it.
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
Hi All,

I know this question comes up now and again and has quite a bit of discussion but, for me never really gets a definite answer. So my apologies for asking this again - Is there a proper method of checking the mic sound input level? I have the following code that I've put together from examples in various post.

Thanks

I believe you cannot control the sound level as it is being recorded, you can only control the playback level. When recording, the microphone signal goes through an automatic gain control circuit as shown in this Google patent (note that there are many ways to do that, the patent shows just one way)

https://patents.google.com/patent/US8135148

That said, as far as monitoring the sound level, I have no suggestion.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Despite the claim in the library post, it appears that it may not be possible to save files from the audiorecorder as Wav files. It cannot encode the data to pcm. Although looking at the wav file specification, it may be possible to create a wav file with other types of encoding, it would need modifications to the header file.

Have a look here if you want to check further : http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html

Which leaves the issue of how to get the amplitude from the mic on anything but audiorecorder, Which is a problem that has been discussed, but as far as I am aware not fully resolved. Sorry I couldn't help further.
 
Upvote 0
Top