# How Robots Talk

On TV, the movies, and even the real world, you'll often hear robots speak in a metallic, not-very-fluid voice — we often even refer to it as a "robot voice." The reason robots and machines tend to sound this way is that, while recorded voices can provide a very accurate reproduction of the original sound, computer generated sound isn't as accurate.

In order for machines to use real recorded sounds, you need to know the exact speech that the machine might want to say, which is impossible if you want to have a computer or robot respond with arbitrary sentences. Since you can't know what might need to be said beforehand, and because you can't record everything, it's often better to let the computer generate its own sounds, even if they don't sound completely human.

For years, programmers have been working on making computers be able to say arbitrary sentence while sounding the same as a human. This process is called "text-to-speech" (or TTS). And even though we have come a long way, most people can still detect the robotic voices that you often hear coming from your computer, phone or car.

# Using TTS

As we saw earlier, the Ready Set STEM API provides some simple functions to play recorded sounds that are on the SD card. But, the API also provides a function to take a text string and convert it to sound:

    Speech(text_to_speak)

where `text_to_speak` is a string of text that you want the computer to speak.

For example, you can try this:

from rstem.sound import Speech madbro = Speech('Y U MAD BRO?') madbro.play() madbro.wait()
Volume Control

The base CREATOR Kit speaker is unamplified, and has a volume similar to that of a speaker in a greeting card. As such, the volume, which is by default at its maximum, can still sometimes be hard to hear.

To make really loud sounds, you can use external speakers or headphones. However, even with the unamplified speaker, you can "overdrive" the speaker by setting the volume to louder than the maximum. The drawback is that the sound will be distorted — and the more you increase the volume past the maximum, the more distorted it will be.

All `Sound`s, `Note`s, and `Speech` can have their volume changed by running:

    my_sound.volume = new_volume

where `new_volume` is an integer that is the volume percentage. By default, the volume is set at 100 (that is 100%, which is the maximum).

So, for example, to say "hello" at louder than max, you might do:

    from rstem.sound import Speech
    hello = Speech('Hello.')
    hello.volume = 400
    hello.play()
    hello.wait()

# Sound Chains

Oftentimes, you may want to perform a common sequence of operations in a row. For example, we've been doing this with sounds, where we first create the sound (or note or speech), then we play it, then we wait for it to finish. We've been doing this using three lines of code, as you can see in our example above.

But, when we have sequences of commands that are commonly run in order, many times the API will provide a simpler way to do it, called chaining. The Ready Set STEM API supports chaining for sounds, making it much easier and more concise.

As an example, we can take our four-line piece of code from above:

from rstem.sound import Speech madbro = Speech('Y U MAD BRO?') madbro.play() madbro.wait()

And we can simplify it to the following two lines:

from rstem.sound import Speech Speech('Y U MAD BRO?').play().wait()

The above creates some speech, plays it, and waits for it exactly as before, but does it in a much more efficient way.