Page 258 -
P. 258

building a graphical user interface








            Q:   So pygame is a library created   Q:  What’s the deal with the wait_  Q:  So the wait_finish() function is

             by some programmer other than the   finish() function in the Ready Bake   like an artificial delay after each sound?
             Python folks?                  Code?  I just don’t get why it’s there.
                                                                            A: No, not really a delay, more like a
            A: Yes, it’s what’s called a third-party  A: It’s a function that waits for the sound   pause designed to let the sound effect play
             library. It wasn’t created by you or the   to finish playing before continuing with the   fully before trying to play anything else.
             people that bring you Python. Some other   rest of the program.  The wait_finish() function forces
             programmer(s), a third party, created                          your sound chip to finish with one sound
             pygame.                        Q:   What?! Surely the sound just   before starting another. What happens is
             Q:   And it’s just given away for free?  plays?                that when a sound is played, the play()
                                                                            method passes back the channel (or track)
                                            A: Playing with pygame in this way,   number that the sound is playing on. You
            A: Yes. Which is very nice of them, isn’t   although fun, masks a problem that can   can then use the channel number to ask
             it?                            surface when working with sound (in any   pygame to wait for the channel to finish
                                                                            playing a sound before continuing, which
             Q:   Are there other libraries like   programming language). It turns out that,   is what the code in the Test Drive on the
                                            when asked to play a sound, the main chip
             pygame that can be added into my   inside your computer (the CPU) doesn’t   previous page does.
             Python environment?            even bother trying. Instead, there’s another,   Q:
                                            smaller chip in your computer that is    And if I don’t use wait_finish(),
                                            specifically designed to play sounds and   what happens then?
            A: Yes, there are lots. To see the current   it is to this chip that your main chip hands
             list, follow the Package Index link from   the sound file to and says: “play this for
             the main Python website. Python refers to   me.” The main chip then goes back to   A: All the sounds attempt to play at the
             third-party libraries as “packages” and, as   running your code, sees another request   same time and it sounds like a jumble of
            you’ll see, there are packages available for   to play a sound, doesn’t bother, hands   sounds as opposed to one sound playing,
            every conceivable purpose, not just playing   the new sound file off to the sound chip,   then another, then another, and so on.
            sounds or developing games (as is the   and repeats until your program ends. The
            case with pygame).              sound chip—and this is the important
             Q:   The sounds I downloaded are   part—is designed to operate in parallel

             WAV files. I know that WAV is one of the   with your main chip. While your main chip
                                            is doing something else, the sound chip is
            standards for encoding sound, but is it   busy playing any sounds it has been asked
             the best format to use?        to play. And—here’s the rub—if the sound
                                            chip has been asked to play more than one
            A: That depends on who you ask! We   sound, it attempts to play each sound at
            are using WAV files because they are used   the same time.
             in lots of places and are well-supported on
             most operating systems. There are lots of
             file formats for sound and many of them
            claim to be “better” than WAV, but for what
             we are doing here, WAV is perfect.





                                                                                       you are here 4    223
   253   254   255   256   257   258   259   260   261   262   263