B4A Library AudioTrack

This is a wrapper library for the AudioTrack Object, simlar to the AudioRecord object, it's more complex to use than the MediaPlayer but has some functionality that is useful for audio processing.

I haven't had time to test this one thoroughly, I thought you may like to play with it as well. Let me know if you find anything that doesn't work.

The attachment is quite big as it has an example wav file that is loaded via a random access file and played via Audiotrack, just to give an idea.

Edit: V1.01 fixed a typo and Separate file for libs only
17/1/12 Added swt.zip, quick and dirty sine wave generator as an example.
Added swt1-1.zip a few more refinements.
Added swt1-2.zip better tuning
Updated lib files to 1.2 - needed for swt1-2
13/2/12 Added and reformatted Constants - audiotrack1.3
19/3/12 Updated SWT to reach 19khz and added input fields (touch the display labels)

15/1/21
  • Added attest2
  • Updated example with a Stream Static and looping example.
  • Replaced threading with Wait For and Sleep in the streaming example. For heavy usage, it may still be necessary to use threading, so I'll leave the original example her as well.
 

Attachments

  • attest.zip
    305.8 KB · Views: 1,296
  • AudioTrack1.3.zip
    7.8 KB · Views: 1,392
  • swt1-3.zip
    8.5 KB · Views: 1,072
  • attest2.zip
    298.4 KB · Views: 116
Last edited:

Highwinder

Active Member
Licensed 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

Steve, thanks so much, can't wait to try this out tonight! (sorry for missing this earlier)

Stuttering: Instead of trying to reverse the data during direct playback, how about copying the reversed data to a new file and then just playing the new (reversed data) file normally? I think that might solve the stuttering problem rather nicely.
 
Last edited:

stevel05

Expert
Licensed User
Yes, that would work if you don't need to play it live.

It's important to remember that a stereo file requires 2 bytes per sample that can't be reversed.

It just makes it a little more complicated.

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

Highwinder

Active Member
Licensed User
Yes, that would work if you don't need to play it live.

It's important to remember that a stereo file requires 2 bytes per sample that can't be reversed.

It just makes it a little more complicated.

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


I don't have any need for stereo, just mono, which is how I'm currently recording using AudioRecord. Considering that I have absolutely no clue as to how sound files are actually "built" in the manner you speak of above (I've never worked with sound files before other than simply playing them), how would your example code be modified to just write to a "backwards" WAV file that AudioTrack can play later?

BTW I absolutely LOVE your audio libraries, I just wish I understood all of this better.

Thanks, I really appreciate your time as well as the knowledge and insight.
 
Last edited:

stevel05

Expert
Licensed User
If the files are just mono, then you can simply reverse the order of the bytes in the file (except the header which should remain the same). You can use the random access library to achieve this.

Read the last byte in the file and put it at header +1 in a new file, last -1 to header +2 etc.

It should then play backwards.
 

Kbogle24

Member
Licensed User
Hi, I am wanting to stream mic audio from one device to another. I am trying to use the AudioRecord Library to capture the data and send it over a Astreams connection.

B4X:
RecData=AR.ReadBytes(0,BufferSize)
AstreamsMic.Write (RecData)

Once the data comes in on the receiving device how would I go about playing it back in a almost real time? Can this be done with the AudioTrack library? :sign0163:
 

stevel05

Expert
Licensed User
Unfortunately Android does not have a real time kernal, so playing back near realtime is not possible at the moment. Add the latency for streaming across a network will just make it worse.

As a guess, If you can get it down to .5 second, you would be doing well.

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

Highwinder

Active Member
Licensed User
Hi, I am wanting to stream mic audio from one device to another. I am trying to use the AudioRecord Library to capture the data and send it over a Astreams connection.

B4X:
RecData=AR.ReadBytes(0,BufferSize)
AstreamsMic.Write (RecData)

Once the data comes in on the receiving device how would I go about playing it back in a almost real time? Can this be done with the AudioTrack library? :sign0163:


If you look at the "official" posts for Steve's AudioRecord and AudioTrack libraries, you'll see a project that Steve and VB1992 were working on a while back. It recorded audio from the mic and played it near real-time to the speaker. I'm sure that there is enough in that code example to get you on the right path. I tried it on my Morotola Atrix 4G and it worked great, so I'm sure you could use that as a starting point.
 

davim123

New Member
Licensed User
Hi Steve,

With your sinewave program, is it possible to modify it so that it plays a 1kHz tone on channel 1 and a 2 kHz tone on channel2.

I think what i effectively want to do is to write two arrays of data to the (one for each channel) audio stream. I'm not how to do this and not even sure if that's the correct approach.

Please let me know what you think.

Cheers.
 

stevel05

Expert
Licensed User
Hi,

You will need to look at the structure of a stereo audio file.

From memory, I think the samples are stored alternately as doubles. You will also need to set the config as stereo.

Steve
 

AntonBrz

Member
Licensed User
Stand alone sound demo, no wireless needed?

I'm at a house with the worlds slowest wireless service. I'd like some code to play wav files in a B4A program that doesn't need to sign into the internet. I always get TOOK TOO LONG errors.

My goal is to write games in B4A with sound effects.

Am using the latest version of B4A purchased three days ago and correctly installed.

Can you make a complete app with short sound files already in it, which doesn't need additional downloads from anywhere during the game, if it's installed on an Android device? Any thoughts please, and I'm a newbie, so please forgive my lame questions.
 

stevel05

Expert
Licensed User
Have a look at soundpool, it allows use of MP3 or Ogg files which can be loaded from the apps resources (file folder in this case), I believe there may have been some issues on some older devices with some MP3 files.

In general, it would be much more suited to your requirements.
 

AntonBrz

Member
Licensed User
Soundplay right code, no sound?

I think I added Soundpool correctly, The audio libraries are included. The wav file is a short msg.wav in the apps files folder. No errors show when compiling. This test program compiles correctly but
I don't hear any sound, either in the emulator or when I install the app independently on the Android. I know the button is pressing because it changes color. Here below are the included sound lines from a larger program. Of course some of these are in different parts of the code as the variables are declared higher up.
The button press is a sub almost at the bottom of the code. Newbie by the way, but I really want to know how to play short sounds using SoundPool.
My speakers are on and the volume is up on both the laptop and the Android by the way. :(
----------

Code:

Dim sp As SoundPool
Dim PressId As Int 'Id for keypress sound
sp.Initialize(16)

'load sounds
sp.Load(File.DirAssets,"msg.wav")

(down below in the subs after the layout is showing with the buttons0

Sub Button7_Click
sp.Play(PressId,1,1,1,0,1)

End Sub
-----------
 

stevel05

Expert
Licensed User
Please start a new thread for any more on soundpool, this is an Audiotrack thread.

It is also easier if you provide an example (file save as zip in the IDE) of the code you are using (working or not) as it saves anyone that want's to help building a complete app.

The attached example works on my phone, it is similar to your code, but as you have not supplied it all, may be different where it counts.

Hope it helps
 

Attachments

  • sptest.zip
    136.7 KB · Views: 98
Last edited:

stevel05

Expert
Licensed User
Ahh, I see your problem, you haven't assigned the load id returned from SP.Load to your variable PressId:

PressId=SP.Load(File.DirAssets,"msg.wav")
 

AntonBrz

Member
Licensed User
Thanks

Thank you Steve, between the zip filee and the 'I see the problem' I'm sure I can get wav playing to work now.

:sign0098:

Anton​



 

yo3ggx

Active Member
Licensed User
Select another audio channel in AudioTrack library

Hi,

I have an Android 4.0.4 device and I intend to use Audiotrack library for some basic sound processing and I/O. I need to use a standard USB audio device for this purpose, as the device does not have a speaker/microphone.
I suppose the USB sound device is recognised by the system because a new PCM channel is created when the USB sound device is connected.
ls /dev/snd returns the following without the USB audio device:
controlC1
pcmC1D0p
which I suppose is the internal audio through HDMI

and with the USB device connected I get more:
control C0
pcmC0D0c
pcmC0C0p
so both mic and speaker from the USB device.

There is any way to select the USB audio channel (C0) from the Audiotrack library?

Thank you,
Dan
 

stevel05

Expert
Licensed User
I haven't tried using a USB device for audio, but I believe that you would need to use the USB library to communicate with it, as although the device is recognised, I don't think drivers are supplied.

I'm afraid I have no experience of USB so cannot help much, I'd be interested to know if you get it working.



via Tapatalk
 

yo3ggx

Active Member
Licensed User
I suppose that is not only recognised, but there are some drivers loaded too, because I can use Google Voice Search with the USB audio device.
Tried even with a Plantronics C-620 USB headset and is working.
The problem is that I cannot find a way to select the USB device for playback.
The USB input device is selected by default because there is no other available in the system.

Dan
 

stevel05

Expert
Licensed User
Hmm, interesting some investigation is required I think. I'll see what I can find as well. I did think about doing this and learning about USB a while ago. I'll have to revisit it.

via Tapatalk
 
Top