[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Noticeable lag between key press events and sound events



I too had to use the pygame.mixer.pre_init

I completely forgot about it until I saw the code.

On Sun, 12 Dec 2010 22:31:31 -0500, Brian Gryder <bgryderclock@xxxxxxxxx> wrote:
Thank you for your replies: Dan, Greg, Inigo, and Ian

I changed the sound buffer size and it helped a lot. I verified that
there was no empty space in the loop with Audacity. I changed the
program to queue the next sound on the key press events and play the
queued sound when the previous sound ends. This way there is less
chance for weird uneven drum loops. My long term goal is to replace
the key press events with Midi events.

Here is my updated code ( from the terminal "export
SDL_AUDIODRIVER=pulse && python -u sampler.py")

#----------------------------------------------------------
 import pygame
from pygame.locals import *
 from sys import exit

pygame.mixer.pre_init(44100,-16,2,2048)
 pygame.init()

#pygame.mixer.set_reserved(1)
 reserved_channel_0 = pygame.mixer.Channel(0)
sound1 = pygame.mixer.Sound("beat1.wav")
 sound2 = pygame.mixer.Sound("beat2.wav")
sound3 = pygame.mixer.Sound("beat3.wav")
 sound4 = pygame.mixer.Sound("beat4.wav")

green = (138,226,52)
 blue = (114,159,207)
black = (46,52,54)
 white = (238,238,236)

pygame.display.set_caption("badp3nn1 sampler")

my_font = pygame.font.SysFont("Arial",32)

 SCREEN_SIZE = (400, 300)
screen = pygame.display.set_mode(SCREEN_SIZE, RESIZABLE, 32)

TRACK_END = USEREVENT + 1
reserved_channel_0.set_endevent(TRACK_END)

def updateScreenAndQueueNextSound():
ÂÂÂÂÂÂÂ if reserved_channel_0.get_busy() == False:
 ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ print "Channel is not busy"
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ reserved_channel_0.queue(nextsound)
 ÂÂÂÂÂÂÂ screen.fill(blue)
ÂÂÂÂÂÂÂ screen.blit(my_font.render("Change
Queued",True,white),(0,0) )
 ÂÂÂÂÂÂÂ pygame.display.update()

 while True:
ÂÂÂÂÂÂÂ for event in pygame.event.get():
 ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ if event.type == KEYDOWN:
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ if event.key == K_UP:
 ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ
nextsound = sound1
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ
updateScreenAndQueueNextSound()

ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ elif event.key ==
K_DOWN:
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ
nextsound = sound2
 ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ
updateScreenAndQueueNextSound()

ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ elif event.key ==
K_LEFT:
 ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ
nextsound = sound3
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ
updateScreenAndQueueNextSound()

ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ elif event.key ==
K_RIGHT:
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ
nextsound = sound4
 ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ
updateScreenAndQueueNextSound()

ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ elif event.type == TRACK_END:
 ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ
reserved_channel_0.queue(nextsound)
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ screen.fill(green)
 ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ
screen.blit(my_font.render("Looping",True,black),(0,0) )
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ
pygame.display.update()

ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ elif event.type == QUIT:
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ exit()

#----------------------------------------------------------

Â

On Sun, Dec 12, 2010 at 6:34 PM, Greg Ewing  wrote:

Brian Gryder wrote:

  There is a noticeable lag between the key press events and the
sound changes.

 Have you tried reducing the size of the sound buffer? It seems
 that the pygame mixer can only start a sound on a buffer
 boundary, so for accurate sound timing you need quite a small
 buffer size.

 --
 Greg



Links:
------
[1] mailto:greg.ewing@xxxxxxxxxxxxxxxx