Trouble synchronizing audio with SourceDataLines

My last topic about the pitching car-engine-sound-thing is solved as far as for the pitching problem.

However - When I stream a sample via a SourceDataLine a lose control of how much of the sample that has “streamed away”. I need to know this since I want to loop the engine-sample with a different pitch when the first sample is finished. And thereafter repeating again ang again… Wait until first sample is written before repitching and writing again…

It’s just the only way I know of to get that control is to use the drain() method and that gives the sample a small pause between the loops (while drainging). If I don’t “wait” for the samples to end before looping them, my engine will keep a certain rpm for a few secs eventhough I hit the brakes…

Soo - concluding - How should a use the SourceDataLine to have control over it and to seamlessly loop and pitch my engine-sample?

Thanx! /Markus

EDIT:

Tried this and it’s better but not good:


while ((line.getBufferSize() - line.available()) > 0)
  Thread.yield();

Instead of


line.drain();

… Still feel that I would like to read a paper from someone who “had the answers” of how to control SourceDataLines without the latency implied with the drain() method…