I am creating tones in my Java-program. So there is a number of oscillator modules which produce the samples I need so that I can play more than one tone at a time. These samples are added and played. If I select a low amplitude, e.g. 100, everything works fine (normalized it in Audacity):
But if I select a higher amplitude, in this case Short.MAX_VALUE, the created tone looks and sounds strange:
The samples are created here (source: http://www.wolinlabs.com/blog/java.sine.wave.html):
short value;
if (type == TYPE_SINE)
{
cycleIncrease = frequency / parent.getSamplingRate();
value = (short) (amplitude * Math.sin(2 * Math.PI * cyclePosition));
cyclePosition += cycleIncrease;
if (cyclePosition > 1)
cyclePosition -= 1;
return value;
}
With sawtooth waves I have the same problem:
else if (type == TYPE_SAW)
{
cycleIncrease = frequency / parent.getSamplingRate();
double doubleValue = cyclePosition - Math.floor(cyclePosition);
cyclePosition += cycleIncrease;
value = (short) (amplitude * doubleValue);
if (cyclePosition > 1)
cyclePosition -= 1;
return value;
}
amplitude
has the value I mentioned above, so in these two cases 100
and Short.MAX_VALUE
.
The samples are summed up in this way, where sum
is the output sample and value
is the calculated sample (0 if no tone is played):
int n = 0;
int sum = 0;
for (Wire inputWire:inputWires)
{
short value = inputWire.getNextSample();
if (value != 0)
{
n += 1;
}
sum += value;
x++;
}
if (n != 0)
sum = sum / n;
I also made sure that there is just the one sample from one oscillator if just one tone is played, so sum = value
.
But I don't understand why different tones are created for different amplitudes with the same code, so why is this happening?
I also have a second question: If I end playing a sine tone with low amplitude, I can hear a short noise, which looks like this:
Where does this come from and how can I fix it?
Thank you for your help!