B4A Library AudioRecord Library

This is an Audio Recording Library based on the Android AudioRecord object which allows capturing of sound input to a data file for further processing (maybe realtime if we're lucky).

Included in the demo program is a WAV file header routine that allows saving captured sound as an uncompressed wav file.

Please read the documentation of the AudioRecord object. It's a little more complicated that the media recorder version, but gives additional flexibility.

Please test it and hopefully we can get it to work well for us.

Added libs 1.01 - Additional constants and Capitalized

12/6/2012 Artest updated to better manage thread on closedown.

For use with V2 of B4a (more specifically the latest threading library) you'll need to add a parameter to the start thread call in artest. Line 89, Record.Start("Recording",Null) becomes Record.Start(Null,"Recording",Null)

artest 1155
 

Attachments

  • AudioRecord1.01.zip
    4.9 KB · Views: 3,047
  • artest1.02.zip
    8.2 KB · Views: 1,243
Last edited:

JohnC

Expert
Licensed User
Longtime User

Highwinder

Active Member
Licensed User
Longtime User
Steve,

I have read elsewhere that both your AudioRecord library and Tomas's AudioRecorder library can be used to monitor MIC input levels.

I am doing this successfully with Tomas's library by way of GetMaxAmplitude, it is quite stable and reliable. However, in case I end up needing to use your library, I am trying to find this functionality with AudioRecord. How is MIC input level detected in AudioRecord?

Thanks!
 

stevel05

Expert
Licensed User
Longtime User
The AudioRecord class within Android see here does not support getting the mic level, you would need to use AudioRecorder for that.
 

Highwinder

Active Member
Licensed User
Longtime User
The AudioRecord class within Android see here does not support getting the mic level, you would need to use AudioRecorder for that.

Good to know, thanks for the clarification. Tomas's library it is then (thankfully I have it working well). I'm sure others will be interested in knowing this, as it is reported elsewhere that AudioRecord can monitor MIC level (might have eve been Erel that posted that).

Next question: In the example app that comes with AudioTrack, there is a whole handful of libraries, like RandomAccessFile, Threading, etc. Are all of these truly necessary just to play a sound file?
 

stevel05

Expert
Licensed User
Longtime User
With the audiorecord and audiotrack classes, you are dealing with the interface at a fairly low level (in respect to java classes) so it expects to be told everything and assumes nothing.

You need some way to split the files onto buffer sized blocks to send through for audiotrack to play, the random access file seemed the most logical way to do this. And threading is used to try to minimize the impact of user interface events while the file is playing.

You can do this any way you wish, if it works OK for you without threading, then that's fine. I was trying to provide a method that should work in most cases.
 

urikupfer

Member
Licensed User
Longtime User
highwinder ,
you can calculate the mic laval with the byte array buffer (RecData):
' convert to double and calc volume
sum=0
sum2=0
Dim st=10 As Int
For i = 0 To RecData.Length-1
y(i)=RecData(i)
sum=sum+y(i)
sum2=sum2+y(i)*y(i)
Next
level=(sum2-sum*sum/y.Length)/y.Length
level=20*Logarithm(level/128,10)

uri
 

Highwinder

Active Member
Licensed User
Longtime User
With the audiorecord and audiotrack classes, you are dealing with the interface at a fairly low level (in respect to java classes) so it expects to be told everything and assumes nothing.

You need some way to split the files onto buffer sized blocks to send through for audiotrack to play, the random access file seemed the most logical way to do this. And threading is used to try to minimize the impact of user interface events while the file is playing.

You can do this any way you wish, if it works OK for you without threading, then that's fine. I was trying to provide a method that should work in most cases.

Steve,

I trust you on the threading issue, makes sense. As the UI in my app has some other things going on during playback of the file, the threading capability might be required. I'm going to play it safe and implement it.
 

Highwinder

Active Member
Licensed User
Longtime User
highwinder ,
you can calculate the mic leval with the byte array buffer (RecData):
' convert to double and calc volume
sum=0
sum2=0
Dim st=10 As Int
For i = 0 To RecData.Length-1
y(i)=RecData(i)
sum=sum+y(i)
sum2=sum2+y(i)*y(i)
Next
level=(sum2-sum*sum/y.Length)/y.Length
level=20*Logarithm(level/128,10)

uri

Uri, this is incredibly helpful! It is a bit beyond my programming skills to truly know how to make use of this code, as I am not very well versed in writing any code for sound processing of any kind.

Can you tell me what the "level" value's max and min values might be so I know what range I'm working with the value? For example, if I were to show a level meter in the form of a bar graph, what would I use for the min and max values of the bar graph?

Incredibly helpful, thank you!

HW
 

urikupfer

Member
Licensed User
Longtime User
Highwinder hi
you will have to calibrate the results. For accurate db.
I am also not a big expert, and I found this method in the internet.
I wrote a little program just to learn how to analyze in "real time" fft ,on the mic data ,
The program doesn’t have any help ' and it's just in the start phase, but it demonstrate the use
Of this library.
It's a about 1M , then if you want I can send it to your mail.
uri
 

Highwinder

Active Member
Licensed User
Longtime User
Highwinder hi
you will have to calibrate the results. For accurate db.
I am also not a big expert, and I found this method in the internet.
I wrote a little program just to learn how to analyze in "real time" fft ,on the mic data ,
The program doesn’t have any help ' and it's just in the start phase, but it demonstrate the use
Of this library.
It's a about 1M , then if you want I can send it to your mail.
uri

Yes, I would love to see your example, please send to my email. I'm pretty desperate to get this project completed by deadline, so I'll take all the help I can get! :sign0162:
 

vb1992

Well-Known Member
Licensed User
Longtime User
DEMO PROJECT CODE

Uses Steve's AudioRecord and AudioTrack (Playback) Libraries together.

To record a 44100 / Af_PCM_16 audio from the phone's microphone.
 

Attachments

  • SteveRecPlayWav.zip
    9.8 KB · Views: 449

susu

Well-Known Member
Licensed User
Longtime User
Hi guys, I want to get microphone data as an array of bytes in real-time. Could you please show me something to do it? Thank you.
 

susu

Well-Known Member
Licensed User
Longtime User
Hi vb1992, I read almost of your link but there's only about AudioMaxAmplitude or Microphone Volume Level. I can't find anything about microphone data.
 

stevel05

Expert
Licensed User
Longtime User
In the program VB1992 has posted above, the audio data from the microphone is read temporarily into the RecData array. You can adapt the code to achieve what you want.

Steve
 

vb1992

Well-Known Member
Licensed User
Longtime User
Is this what you are looking for:

B4X:
      Dim AR As AudioRecord
           'Determine the required min buffer size
         BufferSize=AR.GetMinBufferSize(SampleRate,ChannelConfig,AudioFormat)


Dim RecData() As Byte

RecData=AR.ReadBytes(0,BufferSize)
 
Top