title: How to concatenate sine waves without phase jumps
body: <p>I need to make a python script which generates sine waves of a given frequency and plays them using pyaudio (blocking mode), I also need to be able to change this frequency while it runs, modulate it and plot it using pyqtgraph. For now I have a thread generating chunks of data and my approach to 'connect' those sines was to get the fft and then calculate the angle (numpy.angle), store it in a variable and use it as the phase offset to the next chunk, but I'm not getting the results I expected, maybe I'm missing something or mixing them up.</p>
  
  <pre><code>import matplotlib.pyplot as plt
  import numpy as np
  import pyaudio
  
  #-----------------------
  CHUNK = 1024
  RATE =  44100
  CHANNELS = 2
  FORMAT = pyaudio.paFloat32
  #-----------------------
  
  samples = int(CHUNK)
  t = np.arange(samples) / RATE
  con = 0
  
  
  def generate_sine(a: float = 0.5, freq: float = 440.0):
  
      global con
  
      sine = a * np.sin(2.0 * np.pi * freq * t + con)
  
      # get the angle of the wave
  
      phase = np.angle(np.fft.fft(sine))
  
      # update ref var to generate subsequent sines
      # begining where the last ended
  
      con = phase[-1]
  
      return sine
  
  
  def play_sine(data):
  
      pa = pyaudio.PyAudio()
  
      stream = pa.open(format=FORMAT,
                           channels=CHANNELS,
                           rate=RATE,
                           input=False,
                           output=True,
                           frames_per_buffer=CHUNK)
  
      stream.write(np.array(data).astype(np.float32).tostring())
  
      stream.close()
  
  if __name__ == '__main__':
  
      f = 80
  
      chunks = generate_sine(freq=f)
  
      for i in range(0,4):
  
          chunks = np.concatenate((chunks, generate_sine(freq=f)))
  
      #for i in range(0,10):
  
      #play_sine(chunks)
  
      plt.plot(chunks)
  
      plt.show()
  </code></pre>
  
  <p>Demo plot</p>
  
  <p><img src="https://i.stack.imgur.com/0X5PB.png" alt=""></p>
  
  <p>You can see in the imagem linked that there are discontinuities around x=1024, x=2048 and so on.</p>