.. note::
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
**Why Join?**
- **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team.
- **Learn & Share**: Exchange tips and tutorials to enhance your skills.
- **Exclusive Previews**: Get early access to new product announcements and sneak peeks.
- **Special Discounts**: Enjoy exclusive discounts on our newest products.
- **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today!
.. _3.1.4_py:
3.1.4 Text-to-speech
=======================
Introduction
-----------------
In many places, we can come into contact with TTS (Text-to-speech) technology, which converts text into natural-sounding speech and brings people a good interactive experience.
Let's try to make your project speak.
Required Components
------------------------------
In this project, we need the following components.
.. image:: ../img/audio2.png
:width: 700
:align: center
It's definitely convenient to buy a whole kit, here's the link:
.. list-table::
:widths: 20 20 20
:header-rows: 1
* - Name
- ITEMS IN THIS KIT
- LINK
* - Raphael Kit
- 337
- |link_Raphael_kit|
You can also buy them separately from the links below.
.. list-table::
:widths: 30 20
:header-rows: 1
* - COMPONENT INTRODUCTION
- PURCHASE LINK
* - :ref:`cpn_gpio_board`
- |link_gpio_board_buy|
* - :ref:`cpn_breadboard`
- |link_breadboard_buy|
* - :ref:`cpn_audio_speaker`
- \-
Experimental Procedures
------------------------------
**Step 1:** Build the circuit.
.. image:: ../img/4.1.4fritzing.png
:width: 800
:align: center
After building the circuit according to the above diagram, then plug the audio cable into the Raspberry Pi’s 3.5mm audio jack.
.. image:: ../img/audio4.png
:width: 400
:align: center
**Step 2:** Install ``espeak`` module.
.. raw:: html
.. code-block::
sudo apt-get install espeak -y
**Step 3:** Get into the folder of the code.
.. raw:: html
.. code-block::
cd ~/raphael-kit/python/
**Step 4:** Run.
.. raw:: html
.. code-block::
python3 3.1.4_Text-to-speech.py
Raspberry pi will greet you kindly after the code runs, and it will say goodbye to you when the code stops.
**Code**
.. note::
You can **Modify/Reset/Copy/Run/Stop** the code below. But before that, you need to go to source code path like ``raphael-kit/python``. After modifying the code, you can run it directly to see the effect. After confirming that there are no problems, you can use the Copy button to copy the modified code, then open the source code in Terminal via ``nano`` cammand and paste it.
.. raw:: html
.. code-block:: python
from tts import TTS
tts = TTS(engine="espeak")
tts.lang('en-US')
def main():
tts.say('Hello, nice to meet you!')
def destroy():
tts.say('See you later')
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
destroy()
**Code Explanation**
.. code-block:: python
from tts import TTS
tts = TTS(engine="espeak")
Import the TTS class and instantiate an object.
.. code-block:: python
tts.lang('en-US')
Set the language.
.. note::
Currently the switchable language only supports English.
.. code-block:: python
tts.say("Hello, nice to meet you!")
Fill in the text to be said as a parameter, after executing ``tts.say()``, Raspberry Pi will say the text you wrote.
Phenomenon Picture
------------------------
.. image:: ../img/3.1.3audio.JPG
:align: center