title: Generating smooth audio from Python on a low-powered computer
body: <p>I am trying to write a simple audio function generator in Python, to be run on a Raspberry Pi (model 2).  The code essentially does this:</p>
  
  <ul>
  <li>Generate 1 second of the audio signal (say, a sine wave, or a square wave, etc)</li>
  <li>Play it repeatedly in a loop</li>
  </ul>
  
  <p>For example:</p>
  
  <pre><code>import pyaudio
  from numpy import linspace,sin,pi,int16
  
  def note(freq, len, amp=1, rate=44100):
   t = linspace(0,len,len*rate)
   data = sin(2*pi*freq*t)*amp
   return data.astype(int16) # two byte integers
  
  RATE = 44100
  FREQ = 261.6
  
  pa = pyaudio.PyAudio()
  s = pa.open(output=True,
              channels=2,
              rate=RATE,
              format=pyaudio.paInt16,
              output_device_index=2)
  
  # generate 1 second of sound
  tone = note(FREQ, 1, amp=10000, rate=RATE)
  
  # play it forever    
  while True:
    s.write(tone)
  </code></pre>
  
  <p>The problem is that every iteration of the loop results in an audible "tick" in the audio, even when using an external USB sound card.  Is there any way to avoid this, rather than trying to rewrite everything in C?</p>
  
  <p>I tried using the pyaudio callback interface, but that actually sounded worse (like maybe my Pi was flatulent).</p>
  
  <p>The generated audio needs to be short because it will ultimately be adjusted dynamically with an external control, and anything more than 1 second latency on control changes just feels awkward.  Is there a better way to produce these signals from within Python code?</p>