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 <bgrydercl...@gmail.com> 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

Reply via email to