B4J Library jAudioTrack

There is a newer version of this library with more functionality (not a direct plugin replacement) jAudioTrack2

Here is the opposite number to jAudioRecord, again based on javax.sound.sampled it's setup is very similar.

Again, not tested on linux or Mac, feedback would be appreciated.

AudioTrack
Author:
Steve Laming
Version: 1.0

  • Methods:
    • AddLineListener(EventName As String)
      Adds a listener to this line.
      Callback to sub {EventName}_Event will be called when one of the following events has been raised:
      OPEN, CLOSE, START or STOP


      Available As Int
      Obtains the number of bytes of data currently available
      to the application for processing in the data line's internal buffer.


      Buffersize As Int
      Get the buffer size of the SourceDataLine

      Drain
      Drains queued data from the line by continuing data I/O until the data line's internal buffer has been emptied.

      Flush
      Flushes queued data from the line.

      Initialize(SampleRateHZ As Float, SampleSizeInBits As Int, ChannelConfig As Int) As Boolean
      Initialize the object

      IsActive As Boolean
      Indicates whether the line is engaging in active I/O (such as playback or capture). When an inactive line becomes active, it sends a START event to its listeners.
      Similarly, when an active line becomes inactive, it sends a STOP event.


      IsInitialized As Boolean
      Is the Audiotrack initialized?

      IsRunning As Boolean
      Indicates whether the line is running. The default is false. An open line begins running when the first data is presented in response to an invocation of the start method,
      and continues until presentation ceases in response to a call to stop or because playback completes.


      LastException As String
      If an error occurred get the last exception

      Release
      Closes the line

      Start
      Start playing data

      Stop
      Stop playing data

      Write(Data As Byte(), Off As Int, Len As Int) As Int
      Writes data to the SourceDataLine, id more data is sent that it can read it will block the thread
      off = offset into array data
      Returns the actual number of bytes written


The demo app (ATTest.xip) loads a small wav file (you can also load your own with a file selector) to play. It depends on:

jRandomAccessFile
Threading

The threading library is necessary so we can control the processing of data as it arrives within B4j. The data capture is blocking, so if you run it on the GUI thread, the app will lock.

It plays back PCM signed linear data. No Mp3 or other formats, although a byte array is passed to the Write method if you have an decoder library to decode a different format first.

You cannot debug an application that uses the threading library, so if you are just testing and don't need a Gui, run the playing subroutine in the Gui thread.

Download jAudioTrack.zip and unzip the xml and jar files to your additional libraries folder.

Have fun.

ATTest1.zip should fix issues in post #5
 

Attachments

  • jAudioTrack.zip
    4.3 KB · Views: 907
  • ATTest1.zip
    293.8 KB · Views: 954
Last edited:

BPak

Active Member
Licensed User
Longtime User
It works fine on my Windows 7 computer.
Playing shows the LOG
Program started.
Getting line from Audiosystem
Playing
412648
Write 88200
Write 88200
EndedOK true
Playing Finished

If I compile and then close down the prog without playing I get a message in the LOG
Program started.
main._mainform_closed (java line: 116)
java.lang.NullPointerException
at com.stevel05.JAudioTrack.Release(JAudioTrack.java:139)
at b4j.example.main._mainform_closed(main.java:116)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.BA$2.run(BA.java:162)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
at java.lang.Thread.run(Thread.java:744)

On the Mac OS X Lion it plays the sound OK but says no to Load File button click.

I enclosed screen capture of the Console output and marked where the Play worked in a red rectangle.
The message and text in the console under that area comes up when using the Load File button.

I think the error comes from your code:
B4X:
InitialFilePath = "\"

Mac uses "/"
 

Attachments

  • ATTestMac.zip
    132.6 KB · Views: 419

stevel05

Expert
Licensed User
Longtime User
Thanks for the feedback.

If I compile and then close down the prog without playing I get a message in the LOG

Change AT.Release in MainForm_Closed to
B4X:
If AT.IsInitialized Then AT.Release

And yes, you'll need to change
InitialFilePath = "\" to
InitialFilePath = "/" or wherever your music files are saved.

Or better
B4X:
InitialFilePath = GetSystemProperty("user.home","")
 
Last edited:

Stulish

Active Member
Licensed User
Longtime User
Hi is there a way to mute either the left or right channel during stereo playback??

Also is there a way to pause playback?

Also just so you know, i used this and it works on my mac along with windows :)
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
To Pause the playback, you would need to manage the pointer in the random access file so it doesn't start at the beginning of the file when play is restarted, volume, panning etc. is done using a control object which I have not made available with the current library. When I get some time I'll look at adding them.

Thanks for the info re Mac, it's good to know.
 

ciapw

Member
Licensed User
Is it possible to receive a PCM audio stream and play it?
Hi Philip :) Could you tell me how to do it? I have PCM audio Stream from my android phone and I am trying to implement this using a nano pi to receive and give a playback. Looking forward to your replies . Many thanks :)
 
Top