<Sound Dev> Long delays from srcLine.write()
mlzarathustra at gmail.com
Mon Jun 8 07:46:01 UTC 2020
I'm working on a software synthesizer based on JavaSound in Windows 10, and
I have been encountering inexplicably long delays in srcLine.write()
Most of the time, it works just fine, but I've traced the problem to a
particular voice I've created that is somehow different. It involves more
computation, is all I can figure. When I play that sound I get gaps in the
playback. (I know about the latency and jitter of MIDI messages. This is
different) But if the problem is the surfeit of computation, why does the
delay show up in srcLine.write(), and not in the time my own code takes?
I'm using a 2k buffer, (16 bit stereo 44.1khz) so it has about 46 ms for
each buffer-full. I'm using System.nanoTime() to measure timing.
My code completes its part in about 10ms, as a rough average. But never
even close to 46. And the srcLine.write() portion of the cycle fills in
(blocks) for the rest of the 46 ms, approximately, most of the time.
However, sometimes when I'm playing this sound, srcLine.write() will
occasionally, sometimes for no apparent reason, block for over 200 ms,
which puts an audible (ugly) gap in the sound. As far as I can tell, there
is 100% correspondence between these gaps and the long srcLine.write()
blocking. It seems to happen when I'm playing lots of notes, hence more
computation, I guess, although that doesn't show up as a significant
increase in my code's work in the log. The only increase is in the gap
introduced by srcLine.write() blocking.
I've split everything out onto separate threads, and set the priority
highest for the "dispatcher" and lowest for my own threads. Doesn't help.
I've made sure all the resource allocation happens beforehand, so it is
pooling the voices rather than creating from "new." Doesn't help.
And I don't see anything significant in terms of CPU usage in jconsole or
Here is what I am running:
$ java -version
openjdk version "11.0.7" 2020-04-14 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.7+10-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.7+10-LTS, mixed mode)
Source code is on github
Any insight would be appreciated.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the sound-dev