[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] queued sounds and music



Pete Shinners wrote:
> I've just checked in some code to cvs that allows pygame to queue music and
> sound objects.

i've been testing this out more the last couple nights, good news. with the
queue you can play two sound objects back to back with no pause in between.
if you used the queue with a 'sound done' event, you could effectively
stream your own sound data into pygame.mixer.

this is as good as i'd hoped it would work. i've attached a small sample,
just in case anyone needs to see the code a little better by example. this
takes a normal wave file and breaks into into small sections, and streams
these sections to the mixer.



#!/usr/bin/env python

import os.path, pygame
from Numeric import *

#choose a desired audio format
pygame.mixer.pre_init(11025, 16, 1)
pygame.init()
print pygame.mixer.get_init()

win = pygame.display.set_mode((100,100))


sound = pygame.mixer.Sound('data/house_lo.wav')
origarray = pygame.sndarray.array(sound)
length = origarray.shape[0]

#play first section
start = 0
end = step = 11000
section = pygame.sndarray.make_sound(origarray[start:end])
channel = section.play()
start = end + 1
end = min(end + step, length)

#queue next section
section = pygame.sndarray.make_sound(origarray[start:end])
channel.queue(section)
start = end + 1
end = min(end + step, length)

#setup finished event
channel.set_endevent(pygame.USEREVENT)
while channel.get_busy():
    for event in pygame.event.get():
    	if event.type == pygame.USEREVENT:
	    if end != length:
	    	print 'QUEUEING NEXT SECTION', start, end
		section = pygame.sndarray.make_sound(origarray[start:end])
		channel.queue(section)
		start = end + 1
		end = min(end + step, length)
    	    else:
	    	print 'SOUND FINISHED, PLAYING OUT'
    	pygame.time.wait(50)