Real time sound rendering: frequency changes aren't smooth

Aug 9, 2011 at 4:05 PM
Edited Aug 10, 2011 at 10:24 AM

Hi,

I've used the code which can be found on this blog post: http://mark-dot-net.blogspot.com/2009/10/playback-of-sine-wave-in-naudio.html to produce a continuous sine wave, where the frequency is updated by the position of the mouse (as I move it up and down, so does the pitch).

However, as I move the mouse, when the frequency changes there is a small pop, meaning that the output is not continuous but sounds like there are discrete levels, whereas I would like the tone to be a smooth glissando between frequencies. I have looked through the other discussions and there doesn't appear to be anyone else using this library to do this kind of thing, but I was wondering if anyone is able to help me out? Here is the relevant code:

(I have used the waveprovider classes as shown on the blog post, without editing them)

 

internal class GenerateSound
    {
        private WaveOut _waveOut;
       
        public void StartSineWave(SineWaveProvider sineWaveProvider)
        {
            sineWaveProvider.SetWaveFormat(8000, 1); // 16kHz mono
            sineWaveProvider.Frequency = 1000;
            sineWaveProvider.Amplitude = 0.25f;
            _waveOut = new WaveOut();
            _waveOut.Init(sineWaveProvider);
            _waveOut.DesiredLatency = 100;
            _waveOut.Play();
            
        }

        public void StopSineWave()
        {
            _waveOut.Stop();
            _waveOut.Dispose();
            _waveOut = null;
        }                   
    }

then to play the sound, my main initialises an instance of the sinewaveProvider, then updates the frequency when the mouse is moved by setting sineWaveProvider.Frequency.

I just can't figure out why, when the frequency of the instance of the sinewave is changed, playback stops momentarily before playing the next frequency

 

Cheers,

Matt

Coordinator
Aug 9, 2011 at 4:11 PM

the sine wave provider I wrote doesn't smoothly change frequency, and will likely fill one whole buffer at a time at the same frequency. I'd suggest asking a DSP tagged question on StackOverflow asking if anyone has an algorithm that allows a smooth changing of frequency.

Mark

Oct 1, 2011 at 1:10 AM

Hello Matt,

Could you please post your code to solve this problem? I am trying to solve it now...

Take care,
Dmitry