Create and play audio announcements automatically — Shell script

How to use a shell script to make automatic announcements in audio format. This can be used, for example, to automatically output error messages (from services, applications), messages or the weather via an audio interface (e.g.: Internet radio station in the intranet or Internet, RSS podcast, etc.).

A use case is here shown where this shell script automatically creates and outputs announcements over a radio stream. This use case is similar to the announcements you hear at the airport or train station. Something similar is implemented here via an Icecast2 streaming server, which can also be used in the local network to output messages in audio format over loudspeakers at home or in the company.

It can also use this shell script, if it edits something, for other use cases.

Prerequisites

The following must be set up before creating the shell script:

1. If you want to access information or messages via RSS: The RSStail program
Can be installed via package manager apt: sudo apt-get install rsstail
More about rsstail: http://manpages.ubuntu.com/manpages/xenial/man1/rsstail.1.html

2. Internet radio streaming server (set up with icecast2 and ices) with functioning streaming
This is used here as medium for the output of the created announcements. The Internet radio here is silent and plays nothing except when messages need to be output.
There is also a tutorial on how to set up the Internet radio streaming server on my website.

Attention! In this case, the playlist must only contain an audio file that does not output sound. You can download and use this audio file here: https://ard-site.net/downloads/mute_state.mp3

The playlist playlist_live.pls is used here, which contains only the file mute_state.mp3.

3. The program: jq
A program to output values and contents of JSON files. Used in this case to output the weather.
Can be installed via apt package manager: sudo apt-get install jq
More about jq: http://manpages.ubuntu.com/manpages/bionic/man1/jq.1.html

4. The text-to-speech software: gtts
Gtts can be downloaded via the package manager pip or from the website pypi (https://pypi.org/project/gTTS/)
There is a tutorial here on the website on how to install the text-to-speech software.

5. If you want to add intro music for the announcements: ffmpeg
Can be installed via apt package manager: sudo apt-get install ffmpeg

6. The audio file announcement_intro.mp3
This audio file is the intro music that is played before the announcement. It may only be in mp3 format.

7. A playlist with only the audio file for the announcement
A playlist announcement_playlist.pls is created here. It only contains the file /home/ubuntu/announcement.mp3

The shell script

  • The text to be output as audio can be found here in the file: /home/ubuntu/announcement_text.txt
  • The messages/information from an RSS feed are stored in this file: /home/ubuntu/announcement_information.txt

The user account ubuntu is used here. You may need to make some changes to the script. The shell script runs as root here.

Creating a shell script

The shell script has the following content:

The greetings and phrases can be edited as required.
The information/messages are retrieved from an RSS feed via the rsstail program. Please do not forget to change the address of the RSS feed. Only one entry (the most current) will be retrieved. Here you can also use wget instead to retrieve information from a website instead of an RSS feed. However, the information may then only be available as unformatted text.

Now add the required file permissions to the shell script.

The shell script can run as cronjob e.g.: every hour:

Or only be called if a web page contains any text (e.g.: if an error message is displayed instead of an empty page). Here as an example in a shell script:

Now we reached the end of this tutorial. You can also extend this shell script as you wish.

Further use cases

Announcements can also be made available via an RSS podcast feed.
But first you have to create a template for the RSS-Podcastfeed. And the entries of the RSS podcast feed are automatically added via a shell script. The shell script can be extended by the shell script for the RSS podcast feed.

The automatic addition of podcast feed entries can be done as follows:

However, empty templates for the RSS podcast feed must be created. Where there is a comment with the text “PodcastEntries” in front of the podcast feed entries.

Tutorials about Linux, Java and information about IT — https://ard-site.net

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store