Thank you for the reply René,

I think I fixed it. I found some terminal commands for resolving "Alsa with
Pulseaudio" issues ( http://ubuntuforums.org/showthread.php?t=843012 ). If I
add them to a simple Bash script to launch my pygame program like this, the
sound lag disappears:

####### start mystartup.sh #######
killall pulseaudio
alsa force-reload
pulseaudio -D
python -u badpenni2.py
######### end mystartup.sh #######

Here is the program. it reads midi events and plays sound loops, one shot
sound files and/or gated (start/stop) sounds with no noticeable lag. :-)


######## start badpenni2.py ####################

import sys
import os
import pygame
import pygame.midi
from pygame.locals import *
pygame.mixer.pre_init(44100,-16,2,10)
pygame.init()
pygame.midi.init()

sound1 = pygame.mixer.Sound("beat1.wav")
sound2 = pygame.mixer.Sound("beat2.wav")
sound5 = pygame.mixer.Sound("bassroll.wav")
sound6 = pygame.mixer.Sound("snareroll.wav")

nextsound = pygame.mixer.Sound("silence.wav")

nextnotethatwillplay = ""
notethatwasplayed = ""

for i in range( pygame.midi.get_count() ):
    r = pygame.midi.get_device_info(i)
    (interf, name, input, output, opened) = r
    in_out = ""
    if input:
        in_out = "(input)"
    if output:
        in_out = "(output)"

    print ("%2i: interface :%s:, name :%s:, opened :%s:  %s" % (i, interf,
name, opened, in_out))

mydevicenumber=raw_input('See list above. Please enter the number for your
midi input device: ')

print "you chose " + mydevicenumber + ''
reserved_channel_0 = pygame.mixer.Channel(0)
my_font = pygame.font.SysFont("Arial",104)
pygame.fastevent.init()
event_get = pygame.fastevent.get
event_post = pygame.fastevent.post

input_id = int(mydevicenumber)
i = pygame.midi.Input( input_id )

TRACK_END = USEREVENT + 1
reserved_channel_0.set_endevent(TRACK_END)

pygame.display.set_caption("Revenge of BadPenni Loop Sequencer")
screen = pygame.display.set_mode((800, 600), RESIZABLE, 32)

going = True
while going:
    events = event_get()
    for e in events:
        if e.type in [QUIT]:
            going = False
        if e.type == TRACK_END:
            print reserved_channel_0.get_queue()
            if reserved_channel_0.get_queue() == None:
                reserved_channel_0.queue(nextsound)

            reserved_channel_0.queue(nextsound)
            notethatwasplayed = nextnotethatwillplay
            screen.fill(Color("LawnGreen"))
            screen.blit(my_font.render("Looping",True,Color("Black")),(0,0)
)
            pygame.display.update()

    if i.poll():
        midi_events = i.read(10)
        print "full midi_events " + str(midi_events)
        print "my midi note is " + str(midi_events[0][0][1])

        if str(midi_events[0][0][2]) != "0":
            mymidinote = str(midi_events[0][0][1])
            print "on event"

        if str(midi_events[0][0][2]) == "0":
            mymidinote = str(midi_events[0][0][1]) + "off"
            print "off event"

        if mymidinote == "48":
            reserved_channel_0.queue(sound1)
            nextsound = sound1

        if mymidinote == "49":
            reserved_channel_0.queue(sound2)
            nextsound = sound2

        if mymidinote == "50":
            sound6.play(-1)

        if mymidinote == "50off":
            sound6.stop()

        if mymidinote == "51":
            sound5.play(-1)

        if mymidinote == "51off":
            sound5.stop()

        #convert them into pygame events.
        midi_evs = pygame.midi.midis2events(midi_events, i.device_id)

        for m_e in midi_evs:
            event_post( m_e )
del i
exit()

######## end badpenni2.py ####################




On Sun, Feb 20, 2011 at 4:47 AM, René Dudfield <[email protected]> wrote:

> Unfortunately, that won't work.  I asked on the SDL mailing list about the
> jack backend, but unfortunately the patch was never finished.  Here is the
> old email thread where someone has their SDL+jack patch.
> http://forums.libsdl.org/viewtopic.php?t=5682&sid=5f22dd6ed373f5d4ef4d11562553aad7
> I'll ask more about jack with SDL again on the mailing list.
>
>
>
> I think the easiest way for the moment would be to route it through alsa.
>   http://jackaudio.org/routing_alsa
>
>   Here is the jack plugin for alsa...
>     http://alsa.opensrc.org/Jack_%28plugin%29
>
>
>
> Have you tried changing the to use key down event?  Since there is latency
> from when you hit the key, and finally release it (when the key up event
> appears).
>
>
>
>
>
> On Fri, Feb 18, 2011 at 10:51 PM, Brian Gryder <[email protected]>wrote:
>
>> Thank you all for the replies,
>>
>> I verified there was no empty space on the ends of the sound file and
>> reduced the buffer size.  René, that is a great tutorial on midi and jack. I
>> was able to get midi connectivity with the pygame.midi functions.
>>
>> i found this example of C code that uses jack:
>> http://trac.jackaudio.org/browser/trunk/jack/example-clients/metro.c
>>
>> Is it possible to download the source files like  "jack.h" and
>> "transport.h" in the pygame file's folder then somehow import it and call a
>> function to make my program appear the in
>>
>> qjackctl dialog boxes? Thank you.
>>
>>
>>
>>
>>
>> On Wed, Feb 16, 2011 at 2:43 PM, Greg Ewing 
>> <[email protected]>wrote:
>>
>>> Brian Gryder wrote:
>>>
>>>> I wrote this simple drum pad program with pygame and the lag between key
>>>> presses and the start of the sound bugs me.
>>>>
>>>
>>> Have you tried reducing the size of the sound buffer?
>>> Too large a buffer seems to be a known cause of latency
>>> when using the pygame mixer.
>>>
>>> --
>>> Greg
>>>
>>
>>
>

Reply via email to