The documentation claims that this will open the Line using the format specified in the argument to “open”. In fact it doesn’t — you get the format specified when obtaining the data line (with possibly the number of channels altered). If you obtained the line by using a Line.Info object which merely specifies the desired line class then you will get some default format.
The same problem also applies to TargetDataLine.
I wonder how many developers have been caught out by this?
I found that I could adjust the sample rate using a control on the data line and get output that sounded close to what was expected, but with some extra noise. However getting the line with the intended format produced perfect output (to my ears at least).
So if you are acquiring Line’s using code like
AudioSystem.getLine(new Line.Info(SourceDataLine.class))
change it to
AudioSystem.getLine(new DataLine.Info(SourceDataLine.class, fmt))
Where fmt is an AudioFormat object describing the format you intend to use. The same applies when using the Mixer methods to get a line.
Sorry if everyone is already aware of this problem.
(I have submitted a bug suggesting that they change the documentation).