Working with Sound and Music


Sound and music add another sensory dimension to your programs. Loading, playing, looping, and stopping sound and music are easy to do with the games module. And while people might argue about the difference between sound and music, there's no such argument when it comes to the games module, where there's a clear distinction between the two.

Introducing the Sound and Music Program

The Sound and Music program allows the user to play, loop, and stop the sound effect of a missile firing and the theme music from the Astrocrash game. The user can even play both at the same time. Figure 12.7 shows the program running (but unfortunately, doesn't make a sound).

click to expand
Figure 12.7: The program lets the user play a sound and some music.

Working with Sounds

You can create a sound object for use in a program by loading a WAV file. The WAV format is great for sound effects because it can be used to encode whatever you can record with a microphone.

Loading a Sound

First, I set up the program as always by importing games:

 # Sound and Music # Demonstrates playing sound and music files # Michael Dawson 5/18/03 from livewires import games 

Then, I load a WAV file by using the games function load_sound(). The function takes a string for the name of the sound file to be loaded. I load the file missile.wav and assign the resulting sound object to missile.

 # load a sound file missile = games.load_sound("missile.wav") 

TRAP

You can load only WAV files with the load_sound() function.

Next, I load the music file:

 #load the music file games.load_music("theme.mid") 

I'll save the discussion of music until after I finish demonstrating sounds.

Playing a Sound

Next, I write the menu system that you've seen before in Chapter 5:

 choice = None while choice != "0":     print \     """     Sound and Music     0 - Quit     1 - Play missile sound     2 - Loop missile sound     3 - Stop missile sound     4 - Play theme music     5 - Loop theme music     6 - Stop theme music     """     choice = raw_input("Choice: ")     print     # exit     if choice == "0":         print "Good-bye." 

If the user enters 0, the program says good-bye.

The following code handles the case where a user enters 1:

    # play missile sound    elif choice == "1":        missile.play()        print "Playing missile sound." 

To play the sound once, I invoke the sound object's play() method. When a sound plays, it takes up one of the eight available sound channels. To play a sound, you need at least one open sound channel. Once all eight sound channel are in use, invoking a sound object's play() method has no effect.

If a sound is already playing, you can invoke that sound object's play() method again. As a result, the sound will start playing on another sound channel, if one is available.

Looping a Sound

You can loop a sound by passing to the object's play() method the number of additional times you want the sound played. For example, if you pass 3 to play(), the corresponding sound will play four times (its initial playing plus an additional three times). You can loop a sound forever by passing -1 to play().

The following code handles the case when a user enters 2:

    # loop missile sound    elif choice == "2":        loop = int(raw_input("Loop how many extra times? (-1 = forever): "))        missile.play(loop)        print "Looping missile sound." 

In this section of code, I get the number of additional times the user wants to hear the missile sound and then I pass that value to the sound object's play() method.

Stopping a Sound

You stop a sound object from playing by invoking its stop() method. This stops that particular sound on all of the channels on which it's playing. If you invoke the stop() method of a sound object that's not currently playing, Python is forgiving and won't complain with an error.

If the user enters 3, I stop the missile sound (if it's playing):

    # stop missile sound    elif choice == "3":        missile.stop()        print "Stopping missile sound." 

Working with Music

In livewires, music is handled somewhat differently than sound. First, there is only one music channel, so only one file can be designated as the current music file at any given time. However, the music channel is more flexible than the sound channels. The music channel accepts many different types of sound files, including WAV, OGG, and MIDI. Finally, since there is only one music channel, you don't create and work with an object like you do with sounds. Instead, you access the single music channel through a group of functions from the games module.

Loading Music

You saw the code for loading the music file in the section "Loading a Sound." The code I used to load the music file, games.load_music("theme.mid"), sets the current music to the MIDI file theme.mid. You load a music file with the games.load_music() function by passing the file name as a string.

HINT

MIDI files are often used for music (rather than WAV or OGG files) because of their small size and their tendency to place lower system demands on the computer playing the music.

Playing Music

The following code handles the case where the user enters 4:

    # play theme music    elif choice == "4":        games.play_music()        print "Playing theme music." 

As a result, the computer plays the music file that I loaded, theme.mid. You can play the current music file with games.play_music(). If you don't pass any values to the function, the music plays once.

Looping Music

You can loop the music by passing to games.play_music() the number of additional times you want the music played. For example, if you pass 3 to games.play_music(), the music will play four times (its initial playing plus an additional three times). You can loop a music file forever by passing -1 to the function.

The following code handles the case when a user enters 5:

    # loop theme music    elif choice == "5":        loop = int(raw_input("Loop how many extra times? (-1 = forever): "))        games.play_music(loop)        print "Looping theme music." 

In this section of code, I get the number of additional times the user wants to hear the theme music and then I pass that value to the games.play_music() function.

Stopping Music

If the user enters 6, the following code stops the music (if it's playing):

     # stop theme music     elif choice == "6":         games.stop_music()         print "Stopping theme music." 

You can stop the current music from playing by calling the games.stop_music() function, which is what I do here. If you call the games.stop_music() function while there is no music playing, Python is forgiving and won't complain with an error.

Wrapping Up the Program

Finally, I wrap up the program by handling an invalid choice and waiting for the user:

     # some unknown choice     else:         print "\nSorry, but", choice, "isn't a valid choice." raw_input("\n\nPress the enter key to exit.") 




Python Programming for the Absolute Beginner
Python Programming for the Absolute Beginner, 3rd Edition
ISBN: 1435455002
EAN: 2147483647
Year: 2003
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net