title: How to remove pops from concatented sound data in PyAudio
body: <p>How do you remove "popping" and "clicking" sounds in audio constructed by concatenating sound tonal sound clips together?</p>
  
  <p>I have this PyAudio code for generating a series of tones:</p>
  
  <pre><code>import time
  import math
  import pyaudio
  
  class Beeper(object):
  
      def __init__(self, **kwargs):
          self.bitrate = kwargs.pop('bitrate', 16000)
          self.channels = kwargs.pop('channels', 1)
          self._p = pyaudio.PyAudio()
          self.stream = self._p.open(
              format = self._p.get_format_from_width(1), 
              channels = self.channels, 
              rate = self.bitrate, 
              output = True,
          )
          self._queue = []
  
      def __enter__(self):
          return self
  
      def __exit__(self, exc_type, exc_val, exc_tb):
          self.stream.stop_stream()
          self.stream.close()
  
      def tone(self, frequency, length=1000, play=False, **kwargs):
  
          number_of_frames = int(self.bitrate * length/1000.)
  
          ##TODO:fix pops?
          g = get_generator()
          for x in xrange(number_of_frames):
              self._queue.append(chr(int(math.sin(x/((self.bitrate/float(frequency))/math.pi))*127+128)))
  
      def play(self):
          sound = ''.join(self._queue)
          self.stream.write(sound)
          time.sleep(0.1)
  
  with Beeper(bitrate=88000, channels=2) as beeper:
      i = 0
      for f in xrange(1000, 800-1, int(round(-25/2.))):
          i += 1
          length = log(i+1) * 250/2./2.
          beeper.tone(frequency=f, length=length)
      beeper.play()
  </code></pre>
  
  <p>but when the tones changes, there's a distinctive "pop" in the audio, and I'm not sure how to remove it.</p>
  
  <p>At first, I thought the pop was occurring because I was immediately playing each clip, and the time between each playback when I generate the clip was enough of a delay to cause the audio to flatline. However, when I concatenated all the clips into a single string and played that, the pop was still there.</p>
  
  <p>Then, I thought the sine-waves weren't matching at the boundaries for each clip, so I tried to average the first N frames of the current audio clip with the last N frames of the previous clip, but that also had no effect.</p>
  
  <p>What am I doing wrong? How do I fix this?</p>