Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Double bufferring in the pygame example.

  • Loading branch information...
commit 88c0945099a4651aab8dc20b6fcc66451b92ac0c 1 parent ec5c6d3
Chris McCormick authored
Showing with 15 additions and 8 deletions.
  1. +15 −8 pyExampleGarden.py
View
23 pyExampleGarden.py
@@ -9,7 +9,7 @@
from pyZenGarden import pyZenGarden
-BLOCKSIZE = 256
+BLOCKSIZE = 512
CHANNELS = 2
# initialise the pygame mixer
@@ -31,10 +31,14 @@ def myPrintHook(instring):
zg.setPrintHook(myPrintHook)
-#s = pygame.mixer.Sound("dubstep.ogg")
-s = pygame.sndarray.make_sound(zeros((BLOCKSIZE, CHANNELS)))
-a = pygame.sndarray.samples(s)
-#a[0][0] = pow(2, 16) / 2
+s1 = pygame.sndarray.make_sound(zeros((BLOCKSIZE, CHANNELS)))
+s2 = pygame.sndarray.make_sound(zeros((BLOCKSIZE, CHANNELS)))
+o1 = pygame.sndarray.samples(s1)
+o2 = pygame.sndarray.samples(s2)
+
+sounds = [s1, s2]
+arrays = [o1, o2]
+current_buffer = 0
c = pygame.mixer.find_channel()
c.set_endevent(locals.USEREVENT)
@@ -42,15 +46,18 @@ def myPrintHook(instring):
s_ranger = xrange(BLOCKSIZE)
c_ranger = xrange(CHANNELS)
-c.play(s)
+c.queue(sounds[current_buffer])
+c.queue(sounds[current_buffer + 1])
try:
while 1:
if pygame.event.get(locals.USEREVENT):
+ # start playing the next buffer
zg.process()
for x in c_ranger:
for y in s_ranger:
- a[y][x] = zg.outBlock[y + x * BLOCKSIZE] * (pow(2, 16) / 2 - 1)
- c.play(s)
+ arrays[current_buffer][y][x] = zg.outBlock[y + x * BLOCKSIZE] * (pow(2, 16) / 2 - 1)
+ c.queue(sounds[current_buffer])
+ current_buffer = (current_buffer + 1) % 2
except KeyboardInterrupt:
print "done"
Please sign in to comment.
Something went wrong with that request. Please try again.