Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
(/)
In this tutorial, we'll demonstrate how to use a Raspberry Pi's multimedia capabilities to
host an text-to-speech audio broadcast service. For example, our demo can be
installed as a public address system or even an accompanying audio announcement
device with digital signage.
Project Overview
The Raspberry Pi audio broadcasting service runs as a peer-to-peer application
powered by PubNub Data Streams (/products/publish-subscribe/). On one end, we have
the requester peer which sends a request for audio broadcast. And on the other end,
there is a broadcaster application running on the Raspberry Pi. The requester sends a
text sentence within a PubNub payload and the broadcaster converts it to speech and
sends it to the Raspberry Pi's audio output.
We need to first setup the audio driver for Raspberry Pi. For this application, we are
using a standard USB sound card which attaches to one of the USB ports of the
Raspberry Pi. We'll be using a standard desktop speak from Lenovo, but feel free to
(/)
use any speak system you want.
(/wp-content/uploads/2015/10/raspberry-pi-audio-broadcast-components-and-
setup.png)
In our next step, we'll set up the sound device on the Raspberry Pi and direct the sound
output to the USB sound card.
https://www.pubnub.com/blog/2015-10-14-text-to-speech-audio-broadcast-with-raspberry-pi/ 2/14
09/04/2018 Text-to-Speech Audio Broadcast with Raspberry Pi | PubNub
modprobe snd-bcm2835
(/wp-content/uploads/2015/06/step-1-1.png)
As you can see, the last device listed in the USB port list is the audio sound card.
/etc/modprobe.d/alsa-base.conf
https://www.pubnub.com/blog/2015-10-14-text-to-speech-audio-broadcast-with-raspberry-pi/ 3/14
09/04/2018 Text-to-Speech Audio Broadcast with Raspberry Pi | PubNub
(/)
(/wp-content/uploads/2015/06/step-2-1.png)
Save the file and reboot Raspberry Pi for the new settings to take effect.
$ amixer
(/wp-content/uploads/2015/06/step-3-1.png)
You will now notice a detailed information about the sound card capabilities including
sound playback and sound capture.
Next, we need to select the audio output to be used by Raspberry Pi. Open up the
raspi-config configuration tool from the command line and follow the below procedure:
(/) Select the menu sequence, "Advanced Options" > "Audio" and choose "Auto"
(/wp-content/uploads/2015/06/step-4-11.png)
(/wp-content/uploads/2015/06/step-4-21.png)
https://www.pubnub.com/blog/2015-10-14-text-to-speech-audio-broadcast-with-raspberry-pi/ 5/14
09/04/2018 Text-to-Speech Audio Broadcast with Raspberry Pi | PubNub
(/)
(/wp-content/uploads/2015/06/step-4-31.png)
After selecting "Auto" mode, press the right arrow key to select <Ok> button and exit
the options list, then select "Finish" to exit the configuration tool.
This will enable the Raspberry Pi to autodetect the sound output and use either HDMI
or USB audio jack depending upon which one is connected.
aplay /usr/share/scratch/Media/Sounds/Vocals/Singer1.wav
https://www.pubnub.com/blog/2015-10-14-text-to-speech-audio-broadcast-with-raspberry-pi/ 6/14
09/04/2018 Text-to-Speech Audio Broadcast with Raspberry Pi | PubNub
(/)
(/wp-content/uploads/2015/06/step-5-1-1.png)
If everything went well, you should have heard a quick clip of an opera singer's voice. If
the audio volume is too high or low, then the volume can be adjusted by using
"alsamixer" utility.
(/wp-content/uploads/2015/06/step-5-2.png)
Additionally, you can use do the following to test and verify that the audio is working.
1. Use the speaker-test utility to play a sine wave through the USB output
https://www.pubnub.com/blog/2015-10-14-text-to-speech-audio-broadcast-with-raspberry-pi/ 7/14
09/04/2018 Text-to-Speech Audio Broadcast with Raspberry Pi | PubNub
(/)
(/wp-content/uploads/2015/06/step-5-3-1.png)
2. Alternatively, you can also run the mpg321 utility to test and play a mp3 file.
Software Setup
Text-to-Speech Software
To convert the text to speech, install 'ESpeak' utility.
https://www.pubnub.com/blog/2015-10-14-text-to-speech-audio-broadcast-with-raspberry-pi/ 8/14
09/04/2018 Text-to-Speech Audio Broadcast with Raspberry Pi | PubNub
(/) To test espeak, invoke the espeak command with some text.
Assuming your sound device and audio configuration is setup correctly, you should hear
the speech as per the text you passed to espeak command. ESpeak has a lot of
options to select voice types, accents and playout speeds. But for now the default
configuration is fine to demonstrate this application.
To test this application, we have a sample message stored as a text file named
message.txt (also under GitHub). The message words contained in this file are the ones
which are eventually played out on Pi's audio output by the broadcaster.
python Broadcaster.py
Run the requester script on your PC. Before doing so, make sure that the prerequisites
for Python and PubNub Python SDK are met and the file message.txt is in the same path
as Requester.py.
python Requester.py
Here is how the audio speech will play from the Pi's USB audio output.
https://www.pubnub.com/blog/2015-10-14-text-to-speech-audio-broadcast-with-raspberry-pi/ 9/14
09/04/2018 Text-to-Speech Audio Broadcast with Raspberry Pi | PubNub
Shyam Purkayastha
(/)
Cookie policy
Although the audio speech output generated from espeak is quite 'robotic', we can get
a better performance and more natural feel by trying out one of the commercial text-to-
speech synthesis programs.
Code Walkthrough
PubNub Setup
Both the python scripts use PubNub Pub/Sub Messaging (/products/publish-
subscribe/) for exchanging information. In this application, we used two PubNub
channels, one for sending the broadcast request and the second for receiving the
response and indication for broadcast completion.
1def process_request():
2 sentence = importData().split(".")
https://www.pubnub.com/blog/2015-10-14-text-to-speech-audio-broadcast-with-raspberry-pi/ 10/14
09/04/2018 Text-to-Speech Audio Broadcast with Raspberry Pi | PubNub
7 pubnub.publish(pubnub_requestchannel, request)
8 lock.acquire()
(/)
9
10 pubnub.publish(pubnub_requestchannel,complete_sentence)
view raw
(https://gist.github.com/shyampurk/51874e459aa0c75a7b03/raw/4cde0ce1b147a890f2cddd343ccc5afcb2e23e68/rpi-
rt-audio-rq1)
rpi-rt-audio-rq1 (https://gist.github.com/shyampurk/51874e459aa0c75a7b03#file-rpi-rt-audio-rq1) hosted with ❤
by GitHub (https://github.com)
After sending every request, the requester blocks until a positive response is received
by the broadcaster indicating completion of broadcast. Upon receiving positive
indication, the requester sends the next sentence.
1def _callback(message,channel):
4 if message["status"]== "positive":
5 lock.release()
11 sys.exit()
view raw
(https://gist.github.com/shyampurk/51874e459aa0c75a7b03/raw/4cde0ce1b147a890f2cddd343ccc5afcb2e23e68/rpi-
rt-audio-rq2)
rpi-rt-audio-rq2 (https://gist.github.com/shyampurk/51874e459aa0c75a7b03#file-rpi-rt-audio-rq2) hosted with ❤
by GitHub (https://github.com)
Audio Playback
At the broadcaster end, all action happens inside the callback. Upon receiving the
broadcast request from the requester, the broadcaster invokes the espeak TTS utility.
1def _callback(message,channel):
3 if message["type"]=="request" :
7 if status==0 :
https://www.pubnub.com/blog/2015-10-14-text-to-speech-audio-broadcast-with-raspberry-pi/ 11/14
09/04/2018 Text-to-Speech Audio Broadcast with Raspberry Pi | PubNub
8 pubnub.publish(pubnub_responsechannel, postive_response)
9 elif status!=0 :
10 pubnub.publish(pubnub_responsechannel,negative_response)
(/)
11
12 if message["type"]=="completed" :
13 pubnub.publish(pubnub_responsechannel, done)
14 sys.exit()
view raw
(https://gist.github.com/shyampurk/51874e459aa0c75a7b03/raw/4cde0ce1b147a890f2cddd343ccc5afcb2e23e68/rpi-
rt-audio-bdcast)
rpi-rt-audio-bdcast (https://gist.github.com/shyampurk/51874e459aa0c75a7b03#file-rpi-rt-audio-bdcast) hosted
with ❤ by GitHub (https://github.com)
When espeak completes broadcasting of text sentence, the broadcaster script sends a
positive acknowledgement message back to requester. In case of an error, a negative
acknowledgement is sent.
Wrapping Up
If you want to try out the application on your own, fork the GitHub repository and get the
application program files and the broadcast message script, setup the hardware and
software and you are good to go. To add some fun to the experiment, you can modify
the broadcast script or create your own message and listen to it while it is being
broadcasted.
That's it for this tutorial! However, being huge fans of Raspberry Pi, we have dozens of
great tutorials for Raspberry Pi including smart home, motion control, connected car,
and more. (/blog/tag/raspberry-pi/) Check them out!
https://www.pubnub.com/blog/2015-10-14-text-to-speech-audio-broadcast-with-raspberry-pi/ 12/14
09/04/2018 Text-to-Speech Audio Broadcast with Raspberry Pi | PubNub
The PubNub Data Stream Network powers thousands of apps, streaming 1.9 Trillion
messages to over 330 million devices a month.
(/)
Stack
Use Cases
Your Email
SIGN UP
(/developers/webinars/?
utm_source=OnlineAdvertisement&utm_medium=pubnub.com&utm_campaign=OA-CY17-Q2-
Training-Webinars-Sidebar-Ad)
Categories
BLOCKS » (/blog/category/blocks/)
Functions » (/blog/category/pubnub-functions/)
Insights » (/blog/category/realtime-technology/)
Talks » (/blog/category/talks/)
https://www.pubnub.com/blog/2015-10-14-text-to-speech-audio-broadcast-with-raspberry-pi/ 13/14
09/04/2018 Text-to-Speech Audio Broadcast with Raspberry Pi | PubNub
Other » (/blog/category/uncategorized/)
Get Started
Have a question? Talk to us (/company/contact-us/)
USE CASES
Chat (/solutions/chat/)
IoT Device Control (/solutions/iot/)
Realtime Updates (/solutions/realtime-
updates/)
Collaboration (/solutions/collaboration/)
Multiplayer Gaming
(/solutions/multiplayer-games/)
Healthcare (/solutions/healthcare/)
Cryptocurrency
(/solutions/cryptocurrency/)
WebRTC and VoIP Signaling
(/solutions/webrtc-and-voip-signaling/)
Customer Case Studies (/customers/)
PRODUCTS &
FEATURES
ChatEngine™ NEW!
(/products/chatengine/)
https://www.pubnub.com/blog/2015-10-14-text-to-speech-audio-broadcast-with-raspberry-pi/ 14/14