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 here as well.
 

Attachments

  • attest.zip
    305.8 KB · Views: 1,587
  • AudioTrack1.3.zip
    7.8 KB · Views: 1,738
  • swt1-3.zip
    8.5 KB · Views: 1,374
  • attest2.zip
    298.4 KB · Views: 402
Last edited:

Roger Taylor

Member
Licensed User
Longtime User
This library is 10 years old, I can't immediately find the source code for it (probably have it backed up somewhere). Feel free to inspect it with jd-gui or similar.

I don't think you should be using the sleep command in a thread, it will not do anything useful and If I remember correctly the subsequent restart will be on the main thread.
I don't remember issues with logging from a thread, but you could try the thread RunOnGuiThread method call to delegate to a different method and Log from there if it is an issue for you, although this would probably then be delayed.

If I were to write this library now, I would use JavaObject in a B4xlib, again from memory, there is very little processing in the library and mainly just calls the java AudioTrack methods.

I don't have an issue with logging messages from a thread. The issue is that at.WriteByte isn't writing the samples. At least there is silence when the command is executed from within the thread type I mentioned.

As for sleep() being used in a thread, try to run a thread having a forever loop such as "Do While (True=True), Loop" and put some commands in the loop and the compiler will complain that some or any of them are "unreachable". Sleep(0) allows my thread to stay in the loop. I have another looping thread (the 6809 CPU emulation) that loops without requiring a sleep command, but doesn't crash if I insert a sleep just as a test.

The following method for a loop works, but the sound is highly distorted.... perhaps because the writes are too soon... I'll investigate.
Sub DacThread

Dim i = 0 As Int
Do While (i = (i-i))
at.WriteByte(CycleBy, 0, 1)
Loop

End Sub
 
Last edited:

Roger Taylor

Member
Licensed User
Longtime User

kimstudio

Active Member
Licensed User
Longtime User
It should be stream_music and mode_stream in Initialize. How the samplerate and samplebits set? If the samplerate you want to emulate can't reach the samplerate supported in at.Initialize, upsampling is needed.
 

stevel05

Expert
Licensed User
Longtime User
Try to post a simple example project where it fails, that would help us to help you. And you might find the problem while you do it.
 

Roger Taylor

Member
Licensed User
Longtime User
It should be stream_music and mode_stream in Initialize. How the samplerate and samplebits set? If the samplerate you want to emulate can't reach the samplerate supported in at.Initialize, upsampling is needed.

Try to post a simple example project where it fails, that would help us to help you. And you might find the problem while you do it.

I'm basically trying to do what this C64 emulator is doing. I need to insert live DAC samples into the AudioTrack stream, as my retro CPU is writing them at it's own rate to it's own DAC... whatever the last sample was should keep getting inserted into the AudioTrack stream to satisfy the requires that AudioTrack expects a new byte in an array per the sample rate. So think of what I'm doing as a live DAC output, which I'm shocked that Android doesn't just support easily.

 

Roger Taylor

Member
Licensed User
Longtime User
Top