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

2010-12-13 Thread Dan Ross

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 
 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.ew...@canterbury.ac.nz




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

2010-12-12 Thread Brian Gryder
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
>


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

2010-12-12 Thread Greg Ewing

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


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

2010-12-12 Thread inigo.delg...@gmail.com
I had the same problem in ubuntu... and I think it's yours

There appears to be some problem with pygame (SDL) and ALSA

It worked changing ALSA with pulseaudio... but it was a uggly
solution I've found this one too:

http://ubuntuforums.org/showthread.php?t=429258

But I haven't test it

Good look (tell me if it works, please)




2010/12/12 Ian Mallett 

> If possible, make the buffer size even smaller.  Also, check the sound file
> itself to make sure there isn't any blank space in the beginning.
>



-- 
Nota: Tildes omitidas para evitar incompatibilidades.

:wq


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

2010-12-12 Thread Ian Mallett
If possible, make the buffer size even smaller.  Also, check the sound file
itself to make sure there isn't any blank space in the beginning.


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

2010-12-12 Thread Dan Ross
If I recall I had a lag in a game and solved it by loading the sound when the 
game started rather than when the key was pressed.

In my case though, the sound file was small.


On Dec 12, 2010, at 10:09 AM, Brian Gryder wrote:

> """
> 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)
> #---



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

2010-12-12 Thread Brian Gryder
"""
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)
#---