LinuxMultimediaOpen Source SoftwareTutorials

How to play sound or read text out loud from the command line

Although not very often used, the Linux terminal is able to play sound and even read out texts for you.

Play sound from the Linux command line

Today's Linux distributions are equipped with the right tools. Especially on Ubuntu-based Desktop distributions (such as Linux Mint), the packages are (mostly) already installed or can be installed using APT.

This is an overview of a few possibilities.

Reading text for you

spd-say

spd-say is a command from the speed-dispatcher package to read out a text, either from stdin or from a file.

To install spd-say, install the speed-dispatcher package:

ck@mint ~ $ sudo apt install speech-dispatcher

To let spd-say read out some text, simply use the command followed by the text to be "read":

ck@mint ~ $ spd-say "Hello there"

You could use cat to read a whole file or use echo to pipe a text to spd-say as an alternative:

ck@mint ~ $ echo "Hello there" | spd-say -e
Hello there

But beware of spd-say, this could also lead to annoying audio crackling in Linux Mint.

espeak

The espeak command comes from the package with the same name and can easily be installed:

ck@mint ~ $ sudo apt install espeak

Similar to spd-say, the text to be read out loud is appended after the command:

ck@mint ~ $ espeak "Hello there"

No audio crackling could be noticed here on Linux Mint when using espeak (compared to spd-say).

As an alternative, espeak also lets you pipe text to the command:

ck@mint ~ $ echo "Hello there" | espeak
ck@mint ~ $ cat /tmp/text.txt | espeak

Playing audio files

aplay

The aplay command is part of the alsa-utils package and should already be installed – at least on a Desktop installation. It can be used to play an audio file from the command line, without having to open a GUI:

ck@mint ~ $ aplay /usr/share/sounds/linuxmint-login.wav
Playing WAVE '/usr/share/sounds/linuxmint-login.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo

It cannot read out text though.

paplay

Very similar to aplay is the paplay command. However this command is part of the pulseaudio-utils package. On a Linux Mint, this package is already installed by default.

ck@mint ~ $ paplay /usr/share/sounds/linuxmint-login.wav

ffplay

The powerful ffmpeg package comes with a lot of tools and commands, including ffplay. This is a command to play multimedia files (both audio and video) from the command line.

By default, ffplay opens up some type of GUI:

ck@mint ~ $ ffplay /usr/share/sounds/linuxmint-login.wav
ffplay opens up a GUI while playing a file
ffplay opens up a GUI while playing a file

To avoid getting this (unnecessary) GUI being shown, use the -nodisp parameter. Also add the -autoexit parameter, otherwise the command would not exit.

ck@mint ~ $ ffplay -nodisp -autoexit /usr/share/sounds/linuxmint-login.wav
ffplay version 4.2.7-0ubuntu0.1 Copyright (c) 2003-2022 the FFmpeg developers
  built with gcc 9 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
[...]
Input #0, wav, from '/usr/share/sounds/linuxmint-login.wav':f=0/0   
  Duration: 00:00:04.01, bitrate: 1411 kb/s
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, 2 channels, s16, 1411 kb/s
   3.93 M-A:  0.000 fd=   0 aq=    0KB vq=    0KB sq=    0B f=0/0  

To get rid of the additional text output (showing ffplay build version and a detailed description of the audio file), append -loglevel quiet:

ck@mint ~ $ ffplay -nodisp -autoexit -loglevel quiet /usr/share/sounds/linuxmint-login.wav

ffplay can also be used to play audio (or video) files as a cron job.

Claudio Kuenzler
Claudio has been writing way over 1000 articles on his own blog since 2008 already. He is fascinated by technology, especially Open Source Software. As a Senior Systems Engineer he has seen and solved a lot of problems - and writes about them.

You may also like

Leave a reply

Your email address will not be published. Required fields are marked *

More in:Linux