""" I am experimenting with playing/changing drum loops with pygame. In the program below, beat1.wav is played when the up arrow key is pressed and beat2.wav is played when the down arrow key is pressed. There is a noticeable lag between the key press events and the sound changes. I am running the program in a linux terminal with "-u" and the ALSA sound driver ( "SDL_AUDIODRIVER=alsa && python -u sampler.py" ). What can I do to make the sounds change quicker? Any suggestions, links, or code snippets would be greatly appreciated. """
#--------------------------------------------------------- import pygame from pygame.locals import * from sys import exit pygame.mixer.pre_init(44100, -16, 1, 512) pygame.mixer.init() sound1 = pygame.mixer.Sound("beat1.wav") sound2 = pygame.mixer.Sound("beat2.wav") SCREEN_SIZE = (400, 300) screen = pygame.display.set_mode(SCREEN_SIZE, RESIZABLE, 32) while True: for event in pygame.event.get(): if event.type == QUIT: exit() if event.type == KEYDOWN: if event.key == K_UP: sound2.stop() sound1.play(-1,0,0) if event.key == K_DOWN: sound1.stop() sound2.play(-1,0,0) #-----------------------------------------------------------