I just noticed that there is this: The "Speak Text" feature in Okular is not working. How do I get it to work?
Do we have a solution for Gnome without installing the KDE platform through the repos, snap or flatpak?
I just noticed that there is this: The "Speak Text" feature in Okular is not working. How do I get it to work?
Do we have a solution for Gnome without installing the KDE platform through the repos, snap or flatpak?
I have speech-dispatcher set up (not ideally yet—the documentation in a few places is a little thin), but spd-say works for me. If the command works for you, then you might try my solution. I've created two small shell scripts in ~/.local/bin named speak-selection and shutup. No matter what desktop environment or window manager I am using, I can bind these to custom keystrokes (alt-super-z and alt-super-x respectively in my case) and I should get the desired effect. I've done these both as scripts in case I ever have reason to change how they work, but also so that I could implement alternatives on other platforms and not have to change my thinking.
Here is the contents of ~/.local/bin/speak-selection:
#! /bin/sh
xclip -o | sed -e 's/^!-!/ !-!/g' -e '$a\' | spd-say -e -w
The manpage for xclip tells us that by default it reads the XA_PRIMARY selection buffer. The classic X11 behavior in xterms of being able to select text "to copy" and middle-click "to paste" is based around the XA_PRIMARY selection buffer. Most modern DEs actually want you to use the actual clipboard (XA_CLIPBOARD) instead because this notion that you have three things that can all work like a clipboard is weird, but we're taking advantage of it.
The sed command cleans up the selection buffer contents a little. There's two expressions (-e) for sed, and they:
If a line begins with !-!, what follows is interpreted as a SSIP command (change pitch, rate, volume, whatever) and I basically never want this with selection contents, so if it appears, I defang it by prepending a space.
If the last line of input does not have a newline, one is appended. Is this necessary? I'm not 100% sure. I think I added that while trying to sort out why speech was getting cut off, and I'm not sure if I added it before or after I realized I basically always wanted -w, but it doesn't seem to hurt.
Finally the result is piped to spd-say -e -w which speaks stdin and waits until speech-dispatcher is done doing that before exiting, which avoids speech getting cut off.
The contents of ~/.local/bin/shutup are trivial:
#! /bin/sh
spd-say -C
You never want to implement a "start talking" feature without a "shutup" feature. Ask any blind person who's used speech synthesis and they'll tell you it's an essential feature. 😉
espeak
would be useful for you? (It works for me, but I work a lot with command line tools.) There are alsogespeaker
andespeak-ng
.