B4A Library AudioTrack

Djembefola

Active Member
Licensed User
Longtime User
dj, can you post a simple working example project, thanks

I can't upload a test project with wav files because of the forum's upload file size limit.

But it is pretty easy to loop a wave file with AudioTrack in static mode:

B4X:
Dim AT As AudioTrack
Dim Ra As RandomAccessFile
'...
Dim BufferSize As Int
Dim SampleRate As Int
Dim ChannelConfig As Int
Dim AudioFormat As Int
Dim Buffer() As Byte

Ra.Initialize2(File.DirDefaultExternal, "myLoop.wav", True, True)
SampleRate=44100
ChannelConfig=AT.Ch_Conf_Stereo
AudioFormat=AT.Af_PCM_16
BufferSize=Ra.Size - 44              ' RIFF header size is 44 bytes
Dim Buffer(BufferSize) As Byte


Play Loop:

B4X:
AT.Initialize(AT.Stream_Music,SampleRate,ChannelConfig,AudioFormat,BufferSize,AT.MODE_STATIC)
Ra.ReadBytes(Buffer, 0, BufferSize, 44)
AT.WriteByte(Buffer, 0, BufferSize)
AT.Play
AT.SetLoopPoints(105840, 211680, -1)'must be called AFTER calling the .Play Method

To exit from the Loop, just call:

B4X:
AT.SetLoopPoints(0,0,0)
 

Highwinder

Active Member
Licensed User
Longtime User
Can the AudioTrack Library Play MP3 files?

Can anyone shed any light on being able to play an MP3 file with AudioTrack? I've been asking all over the forum and haven't seen any answers come up.

Mucho Thanko!
 

stevel05

Expert
Licensed User
Longtime User
See my answer here
 

Highwinder

Active Member
Licensed User
Longtime User
Can AudioRecorder 2.0 and AudioTrack Play Nice?

Steve,

I am recording a .3GP file with XverhelstX's AudioRecorder 2.0 library using the following code:

'ToastMessageShow("Preparing the AudioRecorder.",False)
AR.AudioSource = AR.AS_MIC
'AR.AudioChannels=1
AR.OutputFormat = AR.OF_THREE_GPP
AR.AudioEncoder = AR.AE_AMR_NB
AR.setOutputFile(File.DirRootExternal,"myRecording.3gp")
AR.prepare()
AR.start


This works fine and creates the file.

Now how do I play this file with AudioTrack? I am not sure where to start as to match the two libraries up so AudioRecorder can record the file and AudioTack can play it.

Oh thou most gracious of B4A gurus, developer of libraries of such divine magificence, canst thou helpest this poor wretched soul playeth his humble and oddly formatted .3gp file with thy most bitchin' library?

:sign0027:
 

Highwinder

Active Member
Licensed User
Longtime User
This is how you do it.

VB1992, thanks, I appreciate it, this is great clarification of how to record with AudioRecorder. However, playback of the file in your example is handled with MediaPlayer - I actually must use AudioTrack due to the sound processing capabilities of the library.

So can a 3GP file created by Tomas's AudioRecorder 2.0 Library be played using Steve's AudioTrack library? I'm assuming the answer is "Yes", but I thought I'd consult with you gurus before attemtping to spend a weekend flailing/failing. The AudioTrack library appears considerably more complex to use than MP, all the way down to file headers and other things that must be handled individually/directly in code. I found nothing simple whatsoever in the example code that comes with the AudioTrack library, so I'm just assuming I need some knowledgeable help on this one.

So I guess the real question is: Would AudioTrack see any difference in a .3GP file recorded by Steve's AudioRecord and Tomas's AudioRecorder libraries in terms of how to play the file? Or even better yet (ok, this is going to be shameless), what would your example code look like if AudioTrack was used instead of MediaPlayer?


Again, I really appreciate the help.
 
Last edited:

vb1992

Well-Known Member
Licensed User
Longtime User
Last edited:

Highwinder

Active Member
Licensed User
Longtime User

I appreciate the suggestions, and under normal circumstances, they would have been great solutions.

AudioRecord (not AudioRecorder, which is Tomas's lib) and AudioTrack are Steve's.

Problem is, I'm already using AudioRecorder (Tomas's lib) for both MIC level monitoring and recording to file. I really don't want to jump ship from using that lib at this point, it's pretty entrenched in my code and I have it working extremely well. And since it's already producing the file I need, I just need to play it now with Steve's AudioTrack lib and its extended processing abilities. I figure since both libraries work with native Android 3GP, this should not only be possible, but fairly close to "of course".

For all practical reasons, this should work, it's just that I'm curious if someone has already blazed this trail (was hoping it wasn't going to have to be me, I'm certainly no sound guru).

So again: Is it possible to use Steve's AudioTrack lib to play a file produced with Tomas's AudioRecorder 2.0 lib? Anybody ever done it?
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
Hi Guys, Sorry I haven't been about this weekend to contribute to this discussion.

Both the audiorecord and audiotrack libraries are thin wrappers for their respective Android java classes.

Neither of these libraries contain any codec's or file handling and work with raw audio data.

You can save/play Wav files by prepending/ignoring the wav header as in the example, the audio data in these wav files is uncompressed.

So to play a 3gp/mp3 or any other compressed or processed file with audiotrack, you will first need a suitable codec/subroutine to decode the contents of the file to raw audio data.
 
Last edited:

Highwinder

Active Member
Licensed User
Longtime User

Thanks, Steve.

So if I was to record a WAV file using Tomas's AudioRecorder 2.0 library (is it recording an actual wav file? How do I know?), how would I play it using AudioTrack? I literally know nothing about WAV file headers, etc. The reason I need to know this is to avoid a nightmare of replacement of existing code in a rather large project.

Can this be done?
 

stevel05

Expert
Licensed User
Longtime User
The example program attest (see the first post) demonstrates how to play a wav file including ignoring the wav header. The header in the files contains useful information that you can use to set up the audio track. The header file format is well documented just google "wav file header".

As to the question of the format of wav files created by audiorecorder, I believe that Thomas's Audiorecorder uses the Android Java MediaRecorder Class. There is no reason it shouldn't work as expected unless the data is compressed in some manner. A simple test using the above program should prove it.

Hope this helps.
 
Last edited:

Highwinder

Active Member
Licensed User
Longtime User
Steve, VB1992:

After recording a WAV file using AudioRecord, can AudioTrack play that WAV file backwards?
 

stevel05

Expert
Licensed User
Longtime User
Using the random access file library you can send the data part of the file through in reverse order.

I think that's all that is needed and it shouldn't be difficult.

Sent from my HTC Sensation XE with Beats Audio Z715e using Tapatalk 2
 

Highwinder

Active Member
Licensed User
Longtime User
Using the random access file library you can send the data part of the file through in reverse order.

I think that's all that is needed and it shouldn't be difficult.

Sent from my HTC Sensation XE with Beats Audio Z715e using Tapatalk 2

This sounds pretty cool. Is there any example of anyone doing this so far? Also, would sending data through in reverse order involving small chunks of data being played "forward" but in reverse order from the end of file? In other words, would this produce a playback sound of stuttering playback of forward-played chunks in reverse order? Hoep that came across in an understandable way...

Thanks
 

stevel05

Expert
Licensed User
Longtime User
No, you'd need to load the data into the buffers so it was totally reversed. It should then play backwards smoothly.
 

Highwinder

Active Member
Licensed User
Longtime User
No, you'd need to load the data into the buffers so it was totally reversed. It should then play backwards smoothly.

I admit, I have no idea how to do this. Is anybody willing to provide an example of how to accomplish loading file data data into the buffers in a totally reversed manner so AudioTrack can play a WAV file in reverse?

(insert groveling here)
 

stevel05

Expert
Licensed User
Longtime User
The attached shows what is needed, unfortunately stuttering is caused by the need to reverse the data in each buffer. I don't have the time to search for a solution at the moment but there must be a more efficient way of doing this.

Steve
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…