Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

How can I sync with audio framerate?

845492Apr 28 2012 — edited May 30 2012
I hope that there is an entirely different way of doing what I'm trying to do because I've run up against what seems like an insurmountable obstacle. I have been working on a project for many months and the last piece to figure out is synchronizing application actions with the audio framerate. I have seen other apps doing this successfully (but proabably written in C++, not Java) so it can be done with standard Wintel boxes - but I'm now concerned that it cannot be done using Java Sound.

My project attempts to animate sound with visual images (not video or pictures but more abstract visualizations based on the audio itself). It works fine when simply "listeing" and responding to the audio. The final step is to be able to sync specific abstract visualizations with precise points in the audio (frames or position in microseconds). I'm now wondering if what I need is handled by the soundcard (hardware) and not available through Java. I presumed that I would be able to rely on the soundcard to handle the realtime audio stream (using its clock) and I would simply wait for increments of a certain frame count that correspond to the refresh/trigger times for my visualizations. As it turns out, after a lot of experimenting with my configuration, I can't find a way to trigger a method in my application synchronized wiht a certain period in the audio being played. I am playing audio by writing to a SourceDataLine, and listening (using loopback, I believe) on a TargetDataLine. To give an idea of what I'm looking for, if a LineListener supported notification based on reaching a certain frame position in the audio stream, that would be great. That doesn't exist (that I could find, only START/STOP/OPEN/CLOSE events). I've been trying to set the buffer size on the TargetDataLine, and the number of bytes (frames) that I read from that TDL, to achieve a synchronization with the actual rate the audio is playing through the sound card. What I've seen is that the number of frames made available through TDL is not consistent. The read() method blocks for various times and with differing incremental frame positions as the TDL buffer is replenished in real time.

My goal is to be able to detect when a number of frames corresponding to 20 milliSeconds at 48kHz or 44.1kHz sample rate have been played through the sound card. Is this possible? I've not posted any code because the code I have isn't non-functional, it's just not capable of performing the task I am trying to achieve. Suggestions as to how I can refine my existing approach, or what other direction/implementation I can pursue will be greatly appreciated.

Edit: typo.

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jun 27 2012
Added on Apr 28 2012
5 comments
4,716 views