2

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?

2
  • Did you try if espeak would be useful for you? (It works for me, but I work a lot with command line tools.) There are also gespeaker and espeak-ng.
    – sudodus
    Aug 6, 2020 at 9:22
  • Thanks, I do work a lot with command line tools too, but I'm looking for something more equivalent to naturalreaders.com which also has a chrome extension. I can convert pdf to text or html and point firefox at it, but this does not always work well. Why reinvent the wheel with lesser skill if there is already a project that could probably be supported and improved. :-)
    – LiveWireBT
    Aug 6, 2020 at 10:23

1 Answer 1

1

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:

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

  2. 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. 😉

1
  • I think "Ask any blind person" is not appropriate. Maybe change it to "Ask any person" if you agree. Thank you.
    – Raffa
    Aug 11, 2020 at 3:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .