How can I sync with audio framerate?

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.

ags wrote:
I think this breaks down into two related but separate issues:
First, the latency between writing audio (to a SDL) and when that audio is actually heard. If I could find a way to measure that, and it was consistent, I could adjust my processing and delivery of audio data to that SDL. As I tried to explain in my previous post, there will ALWAYS be a variability in the time when a given frame is processed in an SDL and when it sounds. Biggest variability is between the time required for the onset of the first sound (played via bytecode interpretation) and onsets of subsequent sounds (if played via code loaded into memory). But JVM switching also accounts for variability.
Have you seen this article?
http://quod.lib.umich.edu/cgi/p/pod/dod-idx?c=icmc;idno=bbp2372.2007.131
"Real Time Low Latency Audio Processing in Java"
It does a good job of listing various factors that affect real-time audio latencies.
The other problem is that even if I were able to get an accurate measurement of the delay (latency) in the audio lines, if I then set system timers to cause action in sync with the audio frame rate based on audio sample rate (frame rate for uncompressed PCM audio) there is still an error path. Again from observation not direct knowledge, it seems that there is no direct link between the sound card clock (controlling real time frame rate and what should really be the master of all actions) and the system clock (which I would use as a surrogate for the actual sound card clock to synchronize my actions with the audio being heard). Even if I were to establish perfect synchronization at the start of a song, by the time 5 or 10 minutes of audio has been processed, I suspect there will be an accumulation of errors between the sound card clock and the system clock that becomes noticeable.The sound card clock and the system clock are perfectly in synch. The problem is the granularity of the system clock. With Microsoft, the clock info is only updated once every 15 msec or so. As a consequence of this, System.currentTimeMillis() will only report the time at the last update. Also, Thread.sleep(millis) and setting a Timer are subject to this constraint. The error doesn't accumulate, though. It is just an issue of having a reduced "granularity" that creates a predictable error. (System.nanoTime will always be more accurate--it is not subject to letting the OS determine the update frequency.)
As I wrote, there is a hack that allows you to increase the granularity of the OS clock interrupt. It involves setting an independant, background thread to sleep for the maximum amount of time, e.g. Thread.sleep(Long.Max_Value). This somehow forces the OS to use a higher granularity, and with Microsoft the accuracy becomes about 1 msec (comparable to Linux and Mac systems). I learned this at the forum Java-Gaming.org.
>
I'm open to ideas, criticism or examples of how I might acheive my goal. While it might not be practical or the only way, the best I can describe what I'm looking for is to have an audio-event driven system where I can determine a frame position corresponding to the audio being rendered in real time and trigger events to keep other actions synchronized with that audio. If a constant delay (latency) was maintained and measureable, that would be OK.
This is a difficult problem.
Agreed about the difficulty!
Given all this, I think the questions are:
1) how much in advance do you know about given frames that you wish to use as cues?
2) how well can you guestimate an anchor point? (and, is there a way to improve upon the guess with feedback?)
3) how accurately can you trigger the visual cue?
Regardless of when Java decides to process a frame, it DOES manage to keep the playback steady. Thus, if you focus your efforts on finding an ANCHOR (for example, the nanoTime, or "close enough" approximation, when the first frame sounds) that you can use as a reference point for the duration of the sound, then you can use that point to calculate conversions between the frame count of the sound and the time it can be expected to play.
For example, if the frame is 44100 and you estimate frame 0 was at time 1336426670003 (System.currentTimeMillis), then set a Timer, in advance, to trigger the visual cue at 1336426671003 (I'm adding 1000 msec). The level of accuracy will remain the same regardless of the frame number and duration involved as long as you use the same anchor (and the sound playback is not interrupted).

Similar Messages

  • How can I sync with this library without erasing my data?

    The iPod "Sayira's iPod" is synced with another iTunes library. Do you want to erase this iPod and sync with this iTunes library? An iPod can be synced with only one iTunes library at a time. Erasing and syncing replaces the contents of this iPod with the contents of this iTunes library.
    So, okay, here's my story: My laptop caught a virus, so i sent it to this guy who fixes computers, now that my laptop is fixed, everything is deleted from it. I didn't really mind this too much, but anyway I redownloaded iTunes to my new hardrive and then it said the following (posted above). My question is, how can i sync with this new library without erasing any data from my apps (such as games, photos, videos, music)? I REALLY don't want to lose this data!

    - Transffer iTunes purchases to the computer by:
    iTunes Store: Transferring purchases from your iOS device or iPod to a computer
    - Transfer other music by using a third-party program like one of those discussed here.
    Copy music
    - Connect the iPod to the computer and make a backup by right clicking on the iPod under Devices in iTunes and select Back Up
    - Restore the IPod from that backup
    Note that the backup that iTunes makes does no include symec media like apps and music.

  • I've recently purchased a macbook but my iphone 4 is synced with another computer. How can I sync with my new computer and keep my information?

    I've recently purchased a macbook but my iphone 4 is synced with another computer. How can I sync with my new computer and keep my information?

    Sync the iTunes content from the iPhone to the new computer.
    iOS: How to transfer or sync content to your computer
    And... launch iTunes on the new computer. From the menu bar click Store / Authorize This Computer.

  • How can I sync with a new computer without lost all the info?, the old one was stolen.

    How can I sync with a new computer without lost all the info?

    See Syncing to a "New" Computer or replacing a "crashed" Hard Drive

  • TS4036 how can i sync with my icloud account to iPhone?

    how can I sync with my icloud account to my Iphone not from my itunes account?

    Download iCloud for Windows and follow the instructions.

  • Hello how can I sync with my MacBook air apps that I have

    Hi, How can I sync my apps form iphone 4 to my new MacBook Air

    If you don't have Sync Apps selected under the Apps tab for your iPhone sync preferences with iTunes, before doing so with your iPhone connected to iTunes - at the iTunes menu bar go to File and select Transfer Purchases From - the name of your iPhone. After confirming all apps on your iPhone are now in your iTunes library, under the Apps tab for your iPhone sync preferences with iTunes select Sync Apps and make sure all apps on your iPhone are selected below followed by selecting Apply.

  • If i bought a second ipad how can a sync with first ipad

    I bought a second ipad, an iPad mini, how can a sync it with my first ipad

    How to Transfer Everything from an Old iPad to New iPad
    http://osxdaily.com/2012/03/16/transfer-old-ipad-to-new-ipad/
     Cheers, Tom

  • How can I sync  with this uncompatible iTunes 11.x

    How can I sync an iPad Air (iOS7) and iPhone 3G with a MacBook?
    To help answering this awful question:
    - iTunes 11.x does not allow iPhone 3G iOS 6.x to sync.
        (iPhone 3G even 3GS is not subject to iOS7)
    - iPad (iOS7) does not sync iTunes 10.x
    All-in-all, iTunes 11.x is a garbage!

    - Transffer iTunes purchases to the computer by:
    iTunes Store: Transferring purchases from your iOS device or iPod to a computer
    - Transfer other music by using a third-party program like one of those discussed here.
    Copy music
    - Connect the iPod to the computer and make a backup by right clicking on the iPod under Devices in iTunes and select Back Up
    - Restore the IPod from that backup
    Note that the backup that iTunes makes does no include symec media like apps and music.

  • How can i sync with ppc?

    i am using ppc-hp1950,
    i use sync cable to sync with macbook,
    but macbook can't search my ppc.
    since hp1950 has no bluetooth.
    so... how can i sync the data of ical, address book, entourage, etc.. with my macbook?
    thank you.

    Synchronization of iCal and the Address Book to a Hewlett-Packard iPAQ rx1950 is supported by the Missing Sync for Windows Mobile:
    http://www.markspace.com/missingsync_windowsmobile.php

  • How can I sync with Firefox Sync?

    I got a Intex Cloud Fx. How do I sync with Firefox sync feature?

    HI Rajvi,
    Congratulations on your new Intex Cloud Fx with Firefox OS 1.3! Unfortunately accounts was integrated into a later version of Firefox OS . It appeared in test builds of the Flame device in version 2.0. However I do not know the plan for future updates for this device yet.
    Please stay tuned, and if you have any other questions about this device, we are happy to help!

  • I am using Itune at my office laptop and also home laptop. whenever i am Syncing i am losing apps, picture, songs synced from other laptop. how can i sync with both laptop

    Hi, I am using iphone 4s. i am using itune at my office laptop & home laptop, whenever i am using itune all data get lost from phone. is there any way i can use both i tune without loosing data?
    thanks
    VoLTz

    Sync ith only one computer. When you sync with the second anything acquired from the first computer will be erased. Syncing back with the first computer will erase everything synced from the second computer.
    Put all your stuff on one of the computers and sync with only that one.

  • How can icloud sync with multiple outlook calendars?

    I have an outlook calendar syncing to icloud.  But I want to sync another calendar as well and I want to add holidays.  Can this be done with icloud? Or should I go back to yahoo calendar?  Thanks.

    It works for millions of users, you are doing it wrong. Read the directions and follow them exactly, no diversions.
    Please try to understand that there is NO sync. iCloud is a server to client Dav system, not a client to client sync system.
    How does the desktop tell you that "outlook has been set up to sync"
    I would like to know the exact conditions that generate that,
    So post a screenshot here.

  • Formatted hard drive and changed computer, how can I sync with iTunes?

    Yes, I formatted my hard drive and got a new computer, without thinking that it would cause any problems. Now I have installed iTunes and want to sync my iPhone, but it wants to erase my current music files. What do I do?
    P.s. If there are any ppl who have contact with developers, a little proposition... perhaps develop a shared computer version of iTunes (for work-home commuting, internet cafes ect.) where a person can log-in manipulate and download onto their iPod/Phone ect. without this problem of being fixed to one computer (which to be honest is rather unrealistic? no?)

    found life saveing program called media widget well worth the $29 if you have alot of stuff on tour ipod

  • How can I sync with the cable in ios7?

    whatever happened to privacy? We should at least be able to CHOOSE whether we want to share all our personal information with all the hackers and god knows what else has access to icloud. Do I have to revert to a previous OS  in order to sync via cable?

    Thanks for the reply
    The error message that i am receiving is error 58 and error 59.
    Actually, i have installed the Bluetooth drivers that came along with it. however LabVIEW uses the windows Drivers only. so do i need to uninstall the drivers first and let the OS install the bluetooth on its own?
    Somil Gautam
    Think Weird

  • Lost old computer how can I sync with new?

    The computer I used to sync my iPhone has stopped working and I really would like to sync my phone so is there any way to sync my phone to another computer without losing everything?

    Whatever you bought/downloaded from iTunes Store can be re-downloaded from there to your new computer and synced to your iPhone, or iPod. Just sign into your iTunes account.
    As far as music that didn't come from iTunes Store there are non-Apple programs to move music from your iPhone to iTunes on your new computer.  I'm using PodWorks (not cheap,~ $8); another is Senuti. There are many others & some good ones are free.
    I agree that you should be using a backup.  But that's not the question you asked, just something we all need to do. Saves a lot of headaches.

Maybe you are looking for

  • Error in JAX-WS while report generation

    Hi all, I am using JAX-WS as a webservice client to generate the report. I tried generating the same through BI console directly and it got successfully generated. But when i try to generate it through the webservice client call in my code it gives f

  • How to reset OSB domain from EM point of view?

    Hi folks, Since I set up a OSB domain in EM on a client host for the very first time, the administrative server changed (actually moved inside firewall and hence changed its name, consequently I re-installed OSB on it from scratch). Now EM on the cli

  • Where are my order number and new number?!

    So I ordered a sim card kit and go phone plan. And I never receive any email about my order numbers and new phone number. I thought my order didn't went through. So I checked my bank statement saying at&t took out the amount I paid for the stuff I bo

  • Blurring out a piece of video

    I've got a phone number in a piece of video that I want to eliminate. Unfortunately, the number is in a place that can't be cropped out. Is there any mechanism in FCP 7 where I can blur out the phone number without compromising the rest of the video?

  • Downloaded Videos Don't Appear in Video App on iPad

    Please help, I've tried to research this everywhere and can't find an answer. I have purchased and downloaded many shows from the itunes store onto my iPad 2 and have previously successfully watched them. All of a sudden, not sure when, all the video