Re: [pygame] Pygame + Jack?
oh, nice one :) On Tue, Feb 22, 2011 at 4:48 AM, Brian Gryder wrote: > 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 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 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
Re: [pygame] Pygame + Jack?
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 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 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
Re: [pygame] Pygame + Jack?
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 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 > 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 >> > >
Re: [pygame] Pygame + Jack?
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 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 >
Re: [pygame] Pygame + Jack?
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
Re: [pygame] Pygame + Jack?
As well as Ians suggestion... may I suggest playing the sound on key down? On Wed, Feb 16, 2011 at 4:13 AM, 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. Can I use JACK with pygame so > there will be less latency (in the same way that SooperLooper+Jack in Ubuntu > is super fast)? Any code snippets or links to articles would be greatly > appreciated. > > ## start simple.py # > > import pygame > from pygame.locals import * > from sys import exit > > pygame.mixer.pre_init(48000,-16,2,1024) > screen = pygame.display.set_mode((400, 300), RESIZABLE, 32) > pygame.display.set_caption("Simple!!1") > > pygame.init() > pygame.mixer.set_reserved(1) > reserved_channel_0 = pygame.mixer.Channel(0) > sound1 = pygame.mixer.Sound("beat1.wav") > > while True: > for event in pygame.event.get(): > if event.type == QUIT: > exit() > if event.type == KEYDOWN: > if event.key == K_UP: > sound1.play() > #print "UP" > if event.key == K_DOWN: > sound1.stop() > #print "DOWN" > > ## end simple.py # > >
Re: [pygame] Pygame + Jack?
Hi, you can go through alsa to jack, or through oss to jack: http://renesd.blogspot.com/2009/09/alsa-midi-timidity-fl uidsynth-and-jack.html Also there is a jack backend for SDL floating around... I can't remember if it's integrated into the latest SDL though. On Wed, Feb 16, 2011 at 4:13 AM, 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. Can I use JACK with pygame so > there will be less latency (in the same way that SooperLooper+Jack in Ubuntu > is super fast)? Any code snippets or links to articles would be greatly > appreciated. > > ## start simple.py # > > import pygame > from pygame.locals import * > from sys import exit > > pygame.mixer.pre_init(48000,-16,2,1024) > screen = pygame.display.set_mode((400, 300), RESIZABLE, 32) > pygame.display.set_caption("Simple!!1") > > pygame.init() > pygame.mixer.set_reserved(1) > reserved_channel_0 = pygame.mixer.Channel(0) > sound1 = pygame.mixer.Sound("beat1.wav") > > while True: > for event in pygame.event.get(): > if event.type == QUIT: > exit() > if event.type == KEYDOWN: > if event.key == K_UP: > sound1.play() > #print "UP" > if event.key == K_DOWN: > sound1.stop() > #print "DOWN" > > ## end simple.py # > >
Re: [pygame] Pygame + Jack?
Hi, I doesn't appear that SDL, and therefore Pygame, supports JACK. Lenard Lindstrom On 15/02/11 08:13 PM, 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. Can I use JACK with pygame so there will be less latency (in the same way that SooperLooper+Jack in Ubuntu is super fast)? Any code snippets or links to articles would be greatly appreciated.
Re: [pygame] Pygame + Jack?
Hi, I see you found the pre_init argument. You can try making the buffer size even smaller, at the risk of some static. Most importantly, I'd say, get a sound editor and look at the beginning of your sound files to see if there is any "silent" section that "plays" before your actual sound starts. Ian
[pygame] Pygame + Jack?
I wrote this simple drum pad program with pygame and the lag between key presses and the start of the sound bugs me. Can I use JACK with pygame so there will be less latency (in the same way that SooperLooper+Jack in Ubuntu is super fast)? Any code snippets or links to articles would be greatly appreciated. ## start simple.py # import pygame from pygame.locals import * from sys import exit pygame.mixer.pre_init(48000,-16,2,1024) screen = pygame.display.set_mode((400, 300), RESIZABLE, 32) pygame.display.set_caption("Simple!!1") pygame.init() pygame.mixer.set_reserved(1) reserved_channel_0 = pygame.mixer.Channel(0) sound1 = pygame.mixer.Sound("beat1.wav") while True: for event in pygame.event.get(): if event.type == QUIT: exit() if event.type == KEYDOWN: if event.key == K_UP: sound1.play() #print "UP" if event.key == K_DOWN: sound1.stop() #print "DOWN" ## end simple.py #