Sei sulla pagina 1di 14

09/04/2018 Text-to-Speech Audio Broadcast with Raspberry Pi | PubNub

(/)

Text-to-Speech Audio Broadcast


with Raspberry Pi
By Shyam Purkayastha (/blog/author/shyam/)
Follow @shyampurk (http://twitter.com/shyampurk) October 14, 2015

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.

Raspberry Pi Audio Setup


https://www.pubnub.com/blog/2015-10-14-text-to-speech-audio-broadcast-with-raspberry-pi/ 1/14
09/04/2018 Text-to-Speech Audio Broadcast with Raspberry Pi | PubNub

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.

Step 0: Software Installation and Driver Loading


Raspbian OS follows the Advanced Linux Sound Architecture (ALSA) for managing
audio devices. We have to install a few packages to test the sound device through
ALSA. Using the apt-get utility, install the following packages ( you will need admin
privileges to install these packages)

apt-get install alsa-utils

apt-get install mpg321

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

apt-get install lame

(/) Load the sound driver as follows:

modprobe snd-bcm2835

Step 1: Connect & Detect the Audio Hardware


Make sure that the Raspberry Pi is powered off. Connect the USB sound card to one of
the USB ports of the Pi and power on the Pi. Once the Raspbian OS has booted, make
sure that the audio hardware has been detected. To check this, log on to the terminal
(either through SSH or LXTerminal app) and issue the 'lsusb' command.

(/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.

Step 2: Enable USB Audio Device in ALSA


By default, the Raspberry Pi sound driver is configured to use a built in PCM audio
device. As we are using the external USB audio card, we have to make a configuration
setting to let the ALSA know about our external USB sound card.

The configuration file is located in the path:

/etc/modprobe.d/alsa-base.conf

Open this file and change the value of snd-usb-audio index to 0.

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.

Step 3: Test Audio Configuration


With the Raspberry Pi restarted, run the following command from the terminal prompt
to see the current audio configuration:

$ 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.

Step 4: Audio Output Selection


https://www.pubnub.com/blog/2015-10-14-text-to-speech-audio-broadcast-with-raspberry-pi/ 4/14
09/04/2018 Text-to-Speech Audio Broadcast with Raspberry Pi | PubNub

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.

Step 5: Testing Audio


With the sound device setup, it's time to test the application. Connect the speakers to
the speaker jack on the sound card and play one of the test sound files provided by
Raspbian OS.

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

speaker-test -t sine -f 440 -c 2 -s 1

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.

Project Resources (Tools and SDKs)


We are going to use the following tools and SDKs for this application:

Raspberry Pi Audio Broadcast GitHub Repo (https://github.com/shyampurk/PubNub-


RaspberryPi-Audio-Broadcast)
Espeak (http://espeak.sourceforge.net/): An open source text-to-speech synthesis
program
PubNub Python SDK (/docs/python/pubnub-python-sdk): PubNub Python SDK for
realtime messaging between the requester and broadcaster

Setting Up the Hardware and Software


Hardware and Driver Setup
Setup the Raspberry Pi with the WiFi Dongle and USB sound card and attach the
speakers to the sound card. Then, setup the sound driver and audio configuration as
mentioned above. Make sure that you can hear the sample sound from the speaker.

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

apt-get install espeak

(/) To test espeak, invoke the espeak command with some text.

espeak "Hello World"

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.

Audio Broadcast Application


First, check out the project GitHub repository for full code
(https://github.com/shyampurk/PubNub-RaspberryPi-Audio-Broadcast). The application
consists of two python scripts, Requester.py and Broadcaster.py. Requester.py can run
on any PC or computer as long as it has Python 2.7 installed with the PubNub Python
SDK. Broadcaster.py runs on the Pi, under Raspbian OS.

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.

Testing the Audio Broadcast


Now, let's test our application! Run the broadcaster script on Pi's terminal console:

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

Raspberry Pi Audio Broadcast message Share

(/)

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.

Sending Broadcast Requests


As the requester side, the contents of the message.txt are split into sentences so as to
provide even spacing of the speech output while broadcasting the message. Each
sentence is sent as a broadcast request and a completion signal is sent at the end to
indicate end of broadcast.

1def process_request():

2 sentence = importData().split(".")

4 for data in sentence :

5 print "sending message: ",data

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

6 request={"type" : "request" , "play" : data}

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()

7 elif message["status"]== "negative":

8 print "error in response"

10 elif message["status"]== "done":

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" :

4 print" Received message = ", message["play"]

5 status=subprocess.call(["espeak","-s 120 -v en ",message["play"]], stdout=FNULL, stder

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!

Read More: Broadcast (https://www.pubnub.com/blog/tag/broadcast/), Internet of


Things (https://www.pubnub.com/blog/tag/internet-of-things-2/), Embedded Devices
(https://www.pubnub.com/blog/tag/iot-consumer-electronics-connected-dev/),
Raspberry Pi (https://www.pubnub.com/blog/tag/raspberry-pi/), Python
(https://www.pubnub.com/blog/tag/python/)

Categories: Tutorials and Demos (/blog/category/demos-tutorials-and-how-tos/)

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

JavaScript (216) (/blog/tag/javascript/) Android (49) (/blog/tag/android/)

iOS (37) (/blog/tag/ios/) Raspberry Pi (30) (/blog/tag/raspberry-pi/)

Python (15) (/blog/tag/python/) Arduino (32) (/blog/tag/arduino/)

Use Cases

Chat (89) (/blog/tag/chat/) Visualizations (39) (/blog/tag/visualizations/)

Maps and Geolocation (29) (/blog/tag/maps-and-geolocation/)

Multiplayer Gaming (39) (/blog/tag/multiplayer-gaming/)

Subscribe to Our Newsletter

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/)

PubNub Engineering » (/blog/category/engineering/)

News and Announcements » (/blog/category/news-announcements-and-around-pubnub/)

Insights » (/blog/category/realtime-technology/)

Talks » (/blog/category/talks/)

Tutorials and Demos » (/blog/category/demos-tutorials-and-how-tos/)

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/)

Subscribe to RSS Feed:


(/) 

Try PubNub Today


Connect up to 100 devices for Free

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

Potrebbero piacerti anche