Local Text-to-Speech That Sounds Solid: Piper and Bash

Local Text-to-Speech That Sounds Solid: Piper and Bash

Leader ●1 ●4 ●22
calendar_today ago • schedule8 min read

From My ~/bin, Part 1

Every Linux user ends up with a ~/bin folder full of small tools. This series shares some of mine, one tool at a time. Each one is short, readable bash you can copy, edit and make your own.

First up: two scripts that work as a pair, both turning text into speech entirely on my own machine.

  • speak is the terminal command that does the actual work. Give it a text file and it makes a clean, natural-sounding MP3.
  • speak-gui is a small window for the same job. Type or paste text, click Play or Save MP3, and it runs speak for you behind the scenes.

Why local

Good-sounding options were all cloud services, and I didn't want to send every draft to someone else's server for a job my laptop can do.

Piper is the answer. It's a fast, open-source neural text-to-speech engine that runs on an ordinary CPU. The release is a single folder: the piper binary with its own copies of ONNX Runtime and espeak-ng (used to turn words into phonemes). There's no Python, no GPU and no account. Each voice is just two files, an .onnx model and a .json config.

To set expectations: this is not ElevenLabs. The voices are clear and pleasant, emotion and emphasis are limited, and the choice of good English voices is small next to the commercial services. Quality also varies a lot from voice to voice, so try a few. Still for hearing your own drafts, narrating a short video or reading notes aloud, it's more than good enough, and it's free and private.

On my laptop (an Intel i7-1165G7, CPU only), a 59-word paragraph became a finished 20-second MP3 in under 7 seconds, including all the audio processing below.

Script 1: speak (the terminal command)

This is the one that does the work. Typical use:

speak notes.txt                 # -> notes.mp3 next to the text file
speak -p notes.txt              # make it, then play it
speak -v lessac notes.txt       # a different voice
echo "hello" | speak - hi.mp3   # text from a pipe

The code: speak

#!/bin/bash
# speak - turn text into a polished MP3 with Piper (standalone, no Python)
#
#   speak notes.txt                 -> notes.mp3 next to the text file
#   speak notes.txt out.mp3         -> choose the output file
#   echo "hello" | speak - hi.mp3   -> text from a pipe
#   speak -p notes.txt              -> also play it when done
#   speak -v lessac notes.txt       -> other voice: cori (default), lessac, hfc, amy
#   speak -d 5 notes.txt            -> lower the pitch by 5% (speed unchanged); default is 10 for cori, 0 for other voices
#   speak -d 0 notes.txt            -> no pitch drop at all
#
# Piper lives in ~/.local/share/piper, voices in ~/.local/share/piper/voices.
set -euo pipefail

PIPER_DIR="$HOME/.local/share/piper"
voice="cori"
play=0
drop=""

while getopts "v:d:ph" opt; do
    case $opt in
        v) voice=$OPTARG ;;
        d) drop=$OPTARG ;;
        p) play=1 ;;
        *) sed -n '2,12s/^# \{0,1\}//p' "$0"; exit 0 ;;
    esac
done
shift $((OPTIND - 1))
[ $# -ge 1 ] || { sed -n '2,12s/^# \{0,1\}//p' "$0"; exit 1; }

case $voice in
    cori)   model=en_GB-cori-high ;;           # British female (voice #5)
    lessac) model=en_US-lessac-high ;;         # US female
    hfc)    model=en_US-hfc_female-medium ;;   # US female, another style
    amy)    model=en_US-amy-medium ;;          # US female, warm and clear
    *)      model=$voice ;;                    # any other model name in ~/.local/share/piper/voices
esac

# Default pitch drop: 10% for cori, none for the other voices (override with -d)
if [ -z "$drop" ]; then
    [ "$voice" = "cori" ] && drop=10 || drop=0
fi

input=$1
if [ "$input" = "-" ]; then
    out=${2:-speech.mp3}
    text=$(cat)
else
    out=${2:-${input%.*}.mp3}
    text=$(cat "$input")
fi

# Small fixes so casual writing reads naturally:
#   whole text on one line, lowercase "i" -> "I" (otherwise it's stressed),
#   " - " -> a spoken pause, capital letter at the start of each sentence
text=$(printf '%s' "$text" | tr '\n\t' '  ' | sed -E \
    -e 's/ +/ /g; s/^ //; s/ $//' \
    -e 's/\bi\b/I/g' \
    -e 's/ +(-|–|—)+ +/, /g' \
    -e 's/(^|[.!?] +)([a-z])/\1\u\2/g')

tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT

printf '%s\n' "$text" | "$PIPER_DIR/piper" -m "$PIPER_DIR/voices/$model.onnx" \
    --length_scale 1.07 --noise_scale 0.72 --noise_w 0.95 --sentence_silence 0.35 \
    -f "$tmp/raw.wav" >/dev/null 2>&1

# Studio polish: 2s leading silence, warmer EQ, gentle compression, a touch of room, -16 LUFS loudness
polish="adelay=2000:all=1,highpass=f=70,equalizer=f=180:t=q:w=1:g=2.5,\
equalizer=f=7500:t=q:w=2:g=-2,acompressor=threshold=-20dB:ratio=3:attack=8:release=150:makeup=2,\
lowpass=f=10000,aecho=0.85:0.6:28|47:0.10|0.06,loudnorm=I=-16:TP=-1.5:LRA=9,aresample=48000:resampler=soxr:precision=28"
# Optional pitch drop (-d PERCENT), applied first so the lead-in silence is unaffected
if [ "$drop" != "0" ]; then
    polish="rubberband=pitch=$(awk -v d="$drop" 'BEGIN{printf "%.4f", 1-d/100}'),$polish"
fi

case $out in
    *.wav) ffmpeg -y -loglevel error -i "$tmp/raw.wav" -af "$polish" -c:a pcm_s24le "$out" ;;
    *)     ffmpeg -y -loglevel error -i "$tmp/raw.wav" -af "$polish" -c:a libmp3lame -b:a 320k "$out" ;;
esac
echo "$out"

[ $play -eq 1 ] && mpv --really-quiet --no-video "$out"
exit 0

How speak works

Fix the text before the model sees it. Casual writing trips up text-to-speech in predictable ways, so one sed pass cleans it first:

  • Line breaks become spaces, so a wrapped paragraph reads as one paragraph.
  • A lowercase i becomes I. Left alone, the voice tends to stress it oddly.
  • A spaced dash ( - , –, —) becomes a comma, which gives a natural spoken pause.
  • The first letter of each sentence is capitalised, which helps the model hear where sentences begin.

Piper's own knobs. Every voice ships with default settings in its .json file. The script nudges them: --length_scale 1.07 slows speech by about 7%, which sounds less rushed. Raising --noise_scale and --noise_w a little adds variation, so it sounds less flat. --sentence_silence 0.35 adds a short breath between sentences.

The polish chain is plain ffmpeg. Raw TTS output sounds thin and dry. The polish filter chain does what a simple voice-over preset would do, one filter at a time:

Filter What it does
adelay=2000 2 seconds of silence at the start, so players don't clip the first word
highpass=f=70 removes low rumble
equalizer=f=180 ... g=2.5 a little warmth in the low voice range
equalizer=f=7500 ... g=-2 takes the edge off harsh "s" sounds
acompressor evens out loud and quiet words
lowpass=f=10000 trims digital fizz at the very top
aecho a tiny touch of room, so it doesn't sound like it's inside your head
loudnorm=I=-16 a steady loudness of -16 LUFS, a common target for podcasts and spoken audio
aresample=48000 high-quality resample to 48 kHz

Lower the pitch without changing the speed. My favourite voice sounds slightly better a touch deeper, so -d 10 runs ffmpeg's rubberband filter to drop the pitch by 10% while keeping the timing exactly the same.

Small bash habits that pay off. getopts for the options, - as the filename for standard input, a mktemp -d folder that a trap deletes on exit, and set -euo pipefail so a failed step stops the script instead of producing a broken MP3. The help text is the comment block at the top, printed by sed, so it can't fall out of date.

Script 2: speak-gui (the window)

Some days I don't want a terminal. speak-gui opens a window with a text box and four buttons. It never makes audio itself: every button that needs sound hands your text to speak, the script above, and waits for the MP3.

  • Play makes a temporary MP3 and plays it straight away.
  • Voice... shows a list of the four voices to choose from.
  • Save MP3... asks for a file name and saves the MP3 there.
  • Close quits.

The window comes back after every action with your text still in it, so you can tweak a sentence and play it again.

The code: speak-gui

#!/bin/bash
# speak-gui - a small window for the `speak` command
#
# Type or paste text, pick a voice, then:
#   Play       hear it right away
#   Voice...   choose a different voice
#   Save MP3   choose where to save it
#   Close      quit
# The window comes back after each action with your text still in it.

text=""
voice="cori"
player=""
# Larger text in the yad windows only (not in mpv/ffmpeg etc.)
yad() { GDK_DPI_SCALE=1.3 command yad "$@"; }

tmp=$(mktemp -d)
trap '[ -n "$player" ] && kill "$player" 2>/dev/null; rm -rf "$tmp"' EXIT

make_audio() {    # $1 = output file; shows a "working" bar while speak runs
    printf '%s' "$text" | speak -v "$voice" - "$1" >/dev/null 2>"$tmp/err" &
    local job=$!
    ( while kill -0 $job 2>/dev/null; do echo "#Creating speech..."; sleep 0.3; done ) |
        yad --progress --pulsate --auto-close --no-buttons --center --width=300 \
            --title="Speak" --text="Working..." 2>/dev/null
    wait $job || { yad --error --center --title="Speak" --text="Something went wrong:\n$(cat "$tmp/err")"; return 1; }
}

pick_voice() {
    local v
    v=$(yad --list --center --title="Speak - choose a voice" --width=360 --height=260 \
        --column="Voice" --column="Description" --print-column=1 --separator="" \
        cori "British female (default)" lessac "US female" hfc "US female, another style" amy "US female, warm and clear" \
        --button="OK:0" --button="Cancel:1") && [ -n "$v" ] && voice=${v%%|*}
}

while true; do
    # The text box is edited in place; whatever is in it when a button is pressed is printed back exactly as typed.
    printf '%s' "$text" > "$tmp/text.txt"
    result=$(yad --text-info --editable --wrap --center --title="Speak - text to speech (voice: $voice)" \
        --width=720 --height=480 --filename="$tmp/text.txt" \
        --button="Play:2" --button="Save MP3...:0" --button="Voice...:4" --button="Close:1")
    action=$?
    case $action in 0|2|4) ;; *) break ;; esac
    text=$result

    if [ $action -eq 4 ]; then pick_voice; continue; fi

    if [ -z "${text//[[:space:]]/}" ]; then
        yad --info --center --title="Speak" --text="Type or paste some text first." --button=OK:0
        continue
    fi

    [ -n "$player" ] && kill "$player" 2>/dev/null

    if [ $action -eq 2 ]; then
        make_audio "$tmp/preview.mp3" && { mpv --really-quiet --no-video "$tmp/preview.mp3" & player=$!; }
    else
        dest=$(yad --entry --center --width=560 --title="Save speech as" \
            --text="Where to save it? (type a name; a plain name goes in your home folder)" \
            --entry-text="$HOME/speech.mp3" --button="Save:0" --button="Cancel:1") || continue
        dest=${dest//|/}
        [ -n "${dest// /}" ] || continue
        case $dest in /*) ;; "~/"*) dest="$HOME/${dest#\~/}" ;; *) dest="$HOME/$dest" ;; esac
        case $dest in *.mp3|*.wav) ;; *) dest="$dest.mp3" ;; esac
        make_audio "$dest" && yad --info --center --title="Speak" --text="Saved:\n$dest" --button=OK:0 --timeout=6
    fi
done

How speak-gui works

yad does the window, bash does the logic. yad turns a command line into a GTK dialog: a text box, a list, a progress bar. Each button returns a number (Play is 2, Save is 0, Voice is 4), and a case on $? decides what to do next.

The text box keeps your text. Whatever is in the box is written to a temporary file, and yad opens that file in an editable text box. When you press a button, yad prints the text back, and the loop puts it in the box again next time round.

A "working" bar while speak runs. make_audio starts speak in the background, then feeds a pulsing yad progress bar until it finishes. If speak fails, its error message is shown in a dialog instead of disappearing.

Bigger text in the windows only. The yad() function sets GDK_DPI_SCALE=1.3 for yad alone, so the dialogs are easier to read without changing anything else on the system.

Clean exit. A trap stops any audio still playing and deletes the temporary folder when the window closes.


AI doesn't have to mean a data center. A good voice model, some bash scripting and ffmpeg are enough to turn a laptop into a narration studio.

Ben Santora - December 2026

Want more #linux & #bash deep dives?

Join 6,138+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

More Posts

The cleanup failed on the night there was nothing to clean

BashSnippets - Dec 2

Local-First: The Browser as the Vault

Pocket Portfolio - Apr 20

A deploy guard that passes with no token

BashSnippets - Nov 24

My hung process had no file open. It was stuck inside open().

BashSnippets - Dec 1

Hermes Agent Telegram Setup on Windows — Control Your Local AI From Phone

TheCodeStreet - Nov 19
chevron_left
1.3k Points • 27 Badges
Montserrat MA • xiralinux.pages.dev
7Posts
21Comments
10Connections
Linux and AI Advocate - Creator of xiralinux - a lightweight Debian 12 + Openbox live distro built t... Show more

Related Jobs

View all jobs →

Commenters (This Week)

4 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!