Wednesday, 16 January 2013

Sound


Sound

Text to Speech
Let’s start with a smooth introduction to sound. Most of the latest S60
phones have text-to-speech functionality built-in, meaning that the phone
can speak aloud any given string. Hopefully you can come up with some
useful and amusing application for this feature.
The module named audio provides access to the text-to-speech
engine. The function audio.say() takes a string as its parameter and
speaks it aloud (see Example 22).


Example 22: Text to speech
import appuifw, audio
text = appuifw.query(u"Type a word:", "text")
audio.say(text)

The word you type in the input dialog is spoken aloud by the phone.



Playing MP3 Files

The S60 platform supports a wide range of sound formats, such as MP3,
WAV, AMR, AAC, MIDI and Real Audio. Example 23 shows how to play
a MP3 file.
Example 23: MP3 player
import audio
sound = audio.Sound.open("E:\\Sounds\\mysound.mp3")
def playMP3():
sound.play()
print "PlayMP3 returns.."
playMP3()
You must have an MP3 file named mysound.mp3 on your phone, on
the memory card in this example. If the file is missing, the example raises
an exception. In Chapter 6, we explain the directory hierarchy in detail,
so you know where to load and save files – here we just use a standard
location for sounds.
The call to the function audio.Sound.open() opens the specified
file, in this case E:\Sounds\mysound.mp3 and creates an audio.
Sound object (which we have called sound). When we call the function
sound.play(), the MP3 file starts playing and plays from start to finish.
In Section 5.1.5, we see how to stop the sound at any time when it is
being played.
Example 23 prints out "PlayMP3 returns.." when the sound starts
playing, as the play() function does not wait until the sound has reached
the end. This is desirable if you want to leave the sound playing in the
background and continue doing other things.
In some cases, however, you would like the play() function to block
the program until the sound is played till the end. For example, this

behavior might be useful in a game where short sound effects are being
played. Example 24 shows how to do that.

Example 24: Blocking MP3 player
import audio, e32
snd_lock = e32.Ao_lock()
def sound_callback(prev_state, current_state, err):
if current_state == audio.EOpen:
snd_lock.signal()
def playMP3():
sound = audio.Sound.open("E:\\Sounds\\mysound.mp3")
sound.play(callback = sound_callback)
snd_lock.wait()
sound.close()
print "PlayMP3 returns.."
playMP3()




Here we use the e32.Ao lock() object to wait for an event to
occur, in a similar way to the applications in Chapter 4. The play()
function may be given a callback function as a parameter, which
is named sound callback() here. The callback function is called
whenever the sound state changes. The function is given three parameters:
prev state, current state and err. We can check the
current state of the sound with the variable current state. It equals
audio.EOpen, if the sound has finished, or audio.EPlaying if the
sound is playing.
In the above example, we want the play() function to wait until
the sound has finished. We use the e32.Ao lock object to wait until
the callback function receives a notification that the sound has finished,
after which it releases the lock using the signal() function. As a result,
"PlayMP3 returns.." is printed out only after the sound has been
played in full.
You could easily extend the previous examples into a fully fledged
MP3 player. For example, you can add a popup dialog that lets you pause,
stop and fast forward the playing song. The rest of this section introduces
some useful functions to do this. The next step might be to add simple
playlist functionality to the player. For this purpose, you might want to
have a look at Chapter 6, which introduces data handling.















No comments:

Post a Comment