views:

31

answers:

1

Hi, I'm writing a little software synthesizer and it sounds pretty decent except for a slight noise when the volume changes. e.g. while the volume is changing, a sine wave sounds more like a flute, when the volume is constant it sounds bright and pure. As the volume changes pretty much constantly because of the ADSR, this is very annoying.

How can I get rid of this noise?

Thanks in advance.

Additional info: -The volume is calculated simply by multiplying the samples by the volume (between 0 and 1). -I've added a spectrum analyzer and waveform plotter but the noise isn't visible on either -I've tried changing the volume only between waveforms but that didn't seem to help much -I've tried changing the volume at a slower rate but that just lowers the pitch of the noise

Edit: I made a test class to reproduce the problem with a few lines of code. GetOutput is called 44,000 times a second and returns a value between 0 and 1 which is converted to a 16 bit value and output via wasapi. It produces a slowly dying sine wave at 800Hz. You can clearly hear a ringing noise in addition to the sine wave.

const int waveFormCount = 60; //800Hz
int waveFormIndex = 0;
float volume = 1f;

override public float GetOutput()
{
    float phase = (float)waveFormIndex / (float)waveFormCount;

    waveFormIndex++;
    if (waveFormIndex >= waveFormCount)
    {
        waveFormIndex = 0;
        volume -= 0.0001f;
        if (volume < 0)
            volume = 0;
    }
    float sin = ((float)Math.Sin(phase * 2 * Math.PI) + 1) / 2; //between 0 and 1

    return (sin - 0.5f) * volume + 0.5f;
}
A: 

CLOSED The problem was in another part of the code (a small bug in one of the API calls %255 instead of %256 or &255).

Erwin J.