title: creating an infinitely long sine tone in Python
body: <p>I'm trying to create an infinitely long pure sine tone in Python (so I can later add realtime on/off events) and while I can get a tone playing, it is choppy and clipped.  My assumption is that it's either from the next chunk not starting in the same place in the wave's cycle as the last chunk ended, or that there is a delay in calculating the next chunk, and I have no idea which it is. </p>
  
  <p>Are either of those things occurring, or have I made some other error?  Moreover, is there a better approach to take without sacrificing the ability to alter the incoming data in real time?</p>
  
  <pre><code>import time
  import numpy
  import pyaudio
  import math
  
  CHUNK = 4096
  RATE = 44100
  
  def sine(current_time, frequency=440):
      length = CHUNK
      factor = float(frequency) * (math.pi * 2) / RATE
      this_chunk = numpy.arange(length) + current_time
      return numpy.sin(this_chunk * factor)
  
  def get_chunk():
      data = sine(time.time())
      return data * 0.1
  
  def callback(in_data, frame_count, time_info, status):
      chunk = get_chunk() * 0.25
      data = chunk.astype(numpy.float32).tostring()
      return (data, pyaudio.paContinue)
  
  p = pyaudio.PyAudio()
  stream = p.open(format = pyaudio.paFloat32,
                  channels = 2,
                  rate = RATE,
                  output = True,
                  stream_callback = callback)
  
  stream.start_stream()
  
  while stream.is_active():
      time.sleep(0.1)
  
  stream.stop_stream()
  stream.close()
  </code></pre>