Thank you for making your Java Source Resourses
available. The Web page
http://www.jsresources.org/examples/
OscillatorPlayer.html
states that this program has a bug; namely:
Full-scale waves can lead to clipping. It is
currently not known which component is responsible
for this.
The problem is caused by line 82 of the file
Oscillator.java:
fAmplitude = (float) (fAmplitude *
Math.pow(2, getFormat().getSampleSizeInBits()
- 1));
For a "full-scale wave"--i.e., fAmplitude originally
1--the new value of fAmplitude that results from this
line is
Math.pow(2, getFormat().getSampleSizeInBits() - 1))
--viz., 2 raised to the power
getFormat().getSampleSizeInBits() - 1.
Roughly put, the subtraction of 1 here has the desired
effect allotting half of the quantization levels to
the positive values of the signal, and the other half
to the negative values; however, it does not account
for a quantization level also being allotted to a
signal level of zero. Accordingly, either the
positive or negative half of the quantizer actually
has one fewer level allotted to it. A simple fix is
to scale the signal into the quantizer as if both
positive and negative signal values are allotted one
fewer level; that is, replace line 82 with
fAmplitude = (float) (fAmplitude *
(Math.pow(2, getFormat().getSampleSizeInBits()
- 1) - 1));
I have tested this modification with fAmplitude = 1,
and no distortion results.
By the way, the headers of both of the files
OscillatorPlayer.java and Oscillator.java state "this
code is formatted to fit into 80 columns"; but, it is
easily seen that this is not the case.
Jerome Breitenbach
jbreiten@calpoly.edu