Connecting a USB Speaker to a Raspberry Pi to Convert Text to Speech and Have It Talk
Overview
Motivated by the idea of “Let’s have English conversations through a Raspberry Pi and OpenAI!”,
as a first step I made it read out specified text through a USB speaker.
Environment
I’m using the Raspbian OS that came preinstalled with the Marstudy Raspberry Pi 4 Model B Starter Kit.
1 | $ cat /etc/os-release |
Connecting the USB Speaker
I’m using the Sanwa Supply Compact PC Speaker MS-P08UBK.
When I bought it on 2023-05-10, it was ¥857.
Check the card number and device number of the connected USB speaker
1 | aplay -l |
- Card number = 1
- Device number = 0
Test whether sound can be heard from the speaker
1 | // plughw:<card>,<device> |
Playing sound from a Python script
1 | pip3 install pygame |
1 | curl https://www.soundjay.com/buttons/button-3.mp3 -o button.mp3 |
- play_sound.py
1 | import pygame |
Running the following confirms that the mp3 is played.
1 | python3 play_sound.py |
Generating an audio file from text and reading it aloud
- speech.py
1 | pip3 install gTTS |
1 | from gtts import gTTS |
Running the following plays “はい、お元気ですか?” from the speaker.
1 | $ python3 speech.py |
That’s all.
I hope this is helpful.
Next time I’d like to write about the setup for speech recognition from a microphone.
Connecting a USB Speaker to a Raspberry Pi to Convert Text to Speech and Have It Talk
https://kenzo0107.github.io/en/2023/05/18/raspberrypi4b-speaks-via-usb-speaker/
