title: XAudio2 - Play generated sine, when changing frequency clicking sound
body: <p>I want to develop an app to match your tinnitus frequency : A frequency  is played and the user decrease or increase the freqency by pressing a plus or minus button. (see part of the codes, based on some coding from stackoverflow thx :-))</p>
  
  <pre><code>   public static short[] BufferSamples = new short[44100 * 1 * 2];  
  
      private SourceVoice sourceVoice;
      private AudioBuffer buffer;
      private int Tfreq;
  
      public MatchTinn()
      {
          InitializeComponent();
          Loaded += MatchTinn_Loaded;
          TFreq = 5000;
      }
  
      private void MatchTinn_Loaded(object sender, RoutedEventArgs e)
      {
          var dataStream = DataStream.Create(BufferSamples, true, true);
  
          buffer = new AudioBuffer
          {
              LoopCount = AudioBuffer.LoopInfinite,
              Stream = dataStream,
              AudioBytes = (int)dataStream.Length,
              Flags = BufferFlags.EndOfStream
          };
  
          FillBuffer(BufferSamples, 44100, Tfreq);
  
          var waveFormat = new WaveFormat();
  
          XAudio2 xaudio = new XAudio2();
          MasteringVoice masteringVoice = new MasteringVoice(xaudio);
  
          sourceVoice = new SourceVoice(xaudio, waveFormat, true);
  
          // Submit the buffer
          sourceVoice.SubmitSourceBuffer(buffer, null);
      }
  
      private void FillBuffer(short[] buffer, int sampleRate, int frequency)
      {
          if (sourceVoice != null)
          {
              sourceVoice.FlushSourceBuffers();
          }
  
          double totalTime = 0;
  
          for (int i = 0; i &lt; buffer.Length - 1; i += 2)
          {
              double time = (double)totalTime / (double)sampleRate;
  
              short currentSample = (short)(Math.Sin(2 * Math.PI * frequency * time) * (double)short.MaxValue);
  
              buffer[i] = currentSample;
              buffer[i + 1] = currentSample;
  
              totalTime++;
          }
  
  
      private void m1_OnTap(object sender, GestureEventArgs e)
      {
          Tfreq = Tfreq - 1;
  
          if (Tfreq &lt; 0)
          {
              Tfreq = 0;
          }
  
          FillBuffer(BufferSamples, 44100, Tfreq);
  
      }
  
      private void p1_OnTap(object sender, GestureEventArgs e)
      {
          Tfreq = Tfreq + 1;
  
          if (Tfreq &gt; 16000)
          {
              Tfreq = 16000;
          }
  
          FillBuffer(BufferSamples, 44100, Tfreq);
      }
  </code></pre>
  
  <p>Playing the frequency is fine, but when the user presses a button you here a clicking sound when the frequency is updated. Do you have any idea what makes the sound and how i can get rid of it?
  Thanks. </p>