title: Pygame - Sound delay
body: <p>I've made a button class that checks if a button is selected (when the mouse is hovering over the button). When the button is selected, unselected or clicked it plays a wav file. The problem is that there is a huge delay between the sound playing and the button's state changing. The program should check every frame to see if the conditions for the sound to play have been met but the fps doesn't seem to be the problem (60 and 600 fps give the same delay). I've tried decreasing the buffer value in <code>pygame.mixer.init()</code> but that also shows no difference.</p>
  
  <p>Sound files:</p>
  
  <pre><code>buttonSoundSelect = pygame.mixer.Sound(os.path.join(soundPath, "button1.wav"))
  buttonSoundUnselect = pygame.mixer.Sound(os.path.join(soundPath, "button2.wav"))
  buttonSoundClick = pygame.mixer.Sound(os.path.join(soundPath, "button3.wav"))
  buttonSounds = [buttonSoundSelect, buttonSoundUnselect, buttonSoundClick]
  </code></pre>
  
  <p>Creating the object:</p>
  
  <pre><code>playButton = button(textInactive = "Play", font = mainFont, sounds = buttonSounds,  command = playAction)
  </code></pre>
  
  <p>Code from the button class that checks if the button is selected (this is inside the method <code>.display</code> which is called every frame):</p>
  
  <pre><code>    if pygame.mouse.get_pos()[0] &gt;= self.x and pygame.mouse.get_pos()[0] &lt;= self.x + self.width and \
         pygame.mouse.get_pos()[1] &gt;= self.y and pygame.mouse.get_pos()[1] &lt;= self.y + self.height:
  
          self.surfaceActive.blit(self.textSurfaceActive, (self.width / 2 - self.font.size(self.textActive)[0] / 2,
                                                     self.height / 2 - self.font.size(self.textActive)[1] / 2))
  
          self.surface.blit(self.surfaceActive, (self.x, self.y))
  
          if self.selected == False:
              if self.sounds != None:
                  self.sounds[0].stop()
                  self.sounds[1].stop()
                  self.sounds[2].stop()
                  self.sounds[0].play()
              self.selected = True
  
      else:
  
          self.surfaceInactive.blit(self.textSurfaceInactive, (self.width / 2 - self.font.size(self.textInactive)[0] / 2,
                                                       self.height / 2 - self.font.size(self.textInactive)[1] / 2))
  
          self.surface.blit(self.surfaceInactive, (self.x, self.y))
  
          if self.selected == True:
              if self.sounds != None:
                  self.sounds[0].stop()
                  self.sounds[1].stop()
                  self.sounds[2].stop()
                  self.sounds[1].play()
              self.selected = False
  </code></pre>
  
  <p>Code from the button class that checks if the button is clicked (this is inside the method <code>.clickEvent</code> which is called when the left mouse button is clicked):</p>
  
  <pre><code>    if self.command != None:
  
          if pygame.mouse.get_pos()[0] &gt;= self.x and pygame.mouse.get_pos()[0] &lt;= self.x + self.width and \
             pygame.mouse.get_pos()[1] &gt;= self.y and pygame.mouse.get_pos()[1] &lt;= self.y + self.height:    
  
              if self.sounds != None:
                  self.sounds[2].play()
              self.command()
  </code></pre>
  
  <p>So my question is:
  Why is there a long delay and can I make it shorter?</p>