Search code examples
pythonyieldpygletmayavi

how to use pyglet to play sound in a mayavi animation?


I want to play sound using pyglet in a mayavi animation loop, but I found that pyglet worked not well with 'yield', which has to use in a mayavi animation. The situation is that, it just can't start a new loop when the sound played and animation done once, here are some of my codes, any ideas?

The pyglet can play sound in a for-loop, but can't use yield.

@mlab.animate(delay=delays)
def animate():
    f = mlab.gcf()
    while True:
        for i in range(frames_num): 
            # update sound
            sound = 'shiping/shiping_%d.wav'%i
            sound_adjust = pyglet.resource.media(sound, streaming=False)
            sound_adjust.play()

            # update scene
            print('Update scene >>', time.time())
            function_to_update_scene()
            # with out 'yield' it works well
            yield
animate()

Any other modules suggest can also be accepted. The matter is that I need to update sound quickly within 20ms.


Solution

  • I at last solved this by using winsound module. Using

    winsound.PlaySound(sound, winsound.SND_FILENAME | winsound.SND_ASYNC)
    

    to replace

    sound_adjust = pyglet.resource.media(sound, streaming=False) 
    sound_adjust.play()
    

    to play defined sound asynchronously. Off course you have to import winsound at the very beginning.