[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] How do you do the sound?



O yes, let's see (there are 2:21AM here and i'm going to sleep)

I've thought, James, about the start silence but there wasn't..... :(

The code is not to easy to explain now and not to clean to show it now :( but I will try to explain what Im doing:

I would like to be able to play the game in internet -no idea of how to do for now- so I have done a 'engine'.

That 'engine' have one list that are all level .bmp's loaded an converted and another one that are all the sounds of level preloaded too... So instead have multiple copies of images and sounds I have all here and if some object needs one, it can get a reference.... also I'll manage it easy if someday I have memory problems.

So, each object is renderer, it's position calculated, its frame calculated... and after do that instead draw it, I serialize to engine's container in this way: 9 integers that represents:

[index of desired bmp in bmp's list, xposition, yposition,   x,y,w,h -like a rect that define the sprite top draw-, rotated angle in degrees, sound that this objects needs to play] this way I have almost create a protocol to play in internet, i only have to do the server and the clients... but well... you know, you can do a server in python in 2 lines :P. And also I've bought a heavy book of openGL, so I'll be able to change pygame by pyopengl in the future because the only part of my code that uses pygame is one that gets a list with integers and draw images in a display...

Then, when all objects are serializated and in the container, the engine class's draw function draws all objects in container... like this (CAUTION! dirty code)

###-----------------------------------------------------------------------
    ### Draw all objects on input container-----------------------------------
    def draw(self):
        cont = self.container
        while cont != []:
            temp = cont.pop()
            # imageSprList is the list with the bmps, temp 1-4 x,y,w,h, hre the frame is drawed
            display_surface.blit(imageSprList[temp[0]][0], (temp[1], temp[2]), ((temp[3], temp[4]),(temp[5], temp[6])))
            # NASTY: the sound engine have a container of boolean variables that represent if
            # the correspondent sound in sound list must be played.
            for sound in temp[8:]:
                if sound != -1:
                    self.soundEngine.container[sound] = 1
        #After drawing all objects, the list with the sounds that must be played is formed, so play it!
        self.soundEngine.playSoundsAndReset()
        pygame.display.flip()


In the sound's engine I do:

# Plays seleted sounds
def playSoundsAndReset(self):
        # I work with java, sorry...
        k = 0
        chan = None
        # for all sounds loaded in the sounds array
        for sound in soundsList:
            # if it must to be played, play it
            if self.container[k] != -1:
                chan = pygame.mixer.find_channel()
                if chan != None:
                    chan.play(sound)
            k += 1
        self.resetSoundList()
        
    
    def resetSoundList(self):
        self.container = [-1]*len(soundsList)

This way each sound only can be played once in a loop.

I've check the code's behaviour (via t= time.time(), t = time.time()-t  at begin and at the end of playSoundsAndReset) and it seems to be quasi-instantaneous so I think that pygame sends the sound to another threath and between SDL and pulse audio are bad communications... because with esound works fine, 0 lag.

Well, 2:37, i can't go ahead now.... im going to bed, thanks all, all code corrections are wellcome and sorry about my senseless english, but i'm too tired....

Sorry, tomorrow I'll be more clear...
--
Nota: Tildes omitidas para evitar incompatibilidades.

:wq