- #1
btb4198
- 572
- 10
How do Mathematically Ramp down a sinewave to 0 or switch frequencies without causing a clicking sound?
I wrote code to play a sinewave but when I stop the sound I can hear a clicking sound. I tried to slowly ramp down the Amplitude, but that seem to only work for some Combination of frequencies and amplitudes.
here is what I have:
I wrote code to play a sinewave but when I stop the sound I can hear a clicking sound. I tried to slowly ramp down the Amplitude, but that seem to only work for some Combination of frequencies and amplitudes.
here is what I have:
Naudio play code:
public override int Read(byte[] buffer, int offset, int sampleCount)
{
if (position == 0 && onetimeflag == false )
{
n2 = 0;
onetimeflag = true;
}
if(stopflag == true && Amplitude > 0 )
{
Amplitude = Math.Round(Amplitude - 0.01,2);
}
if ((n2 >= BufferLength || stopflag == true) && lastvalue == 0 )
{
return -1;
}
for (int i = 0; i < (sampleCount / 4); i++)
{
temp1 = (float)(Amplitude * Math.Sin(2D * Math.PI * RightFrequencyOutPut * n2 / 44100D));
Frequency_switch = true;
n2++;
}
lastvalue = temp1;
byte[] bytes = BitConverter.GetBytes(temp1);
buffer[i * 4 + 0] = bytes[0];
buffer[i * 4 + 1] = bytes[1];
buffer[i * 4 + 2] = bytes[2];
buffer[i * 4 + 3] = bytes[3];
tempSample++;
}
return sampleCount;
}