0

I am trying to define a one-liner alias like the following:

alias speak='curl -G --data-urlencode "text=$(cat /dev/stdin)" "https://speech.botiumbox.com/api/tts/en?&tts=marytts" -H "accept: audio/wav" | mpv -'

such that I can use it like this

echo Hello World! | speak
speak Hello World!
speak<RET> # continuously do TTS per line until ^D
Hello World!
Another line!
<Ctrl-D>

The API I am using works if I use

curl -G --data-urlencode "text=Hello World!" "https://speech.botiumbox.com/api/tts/en?&tts=marytts" -H "accept: audio/wav" | mpv -

As demonstrated above, simply taking the standard input by cat /dev/stdin didn't seem to create a interactive CLI program. Any ideas to wrap this API into an interactive CLI program? Ideally, be POSIX compliant so it can run in a bash shell in Unixen.

hyiltiz
  • 1,158
  • 14
  • 25
  • You may take advantage from [Make a Bash alias that takes a parameter?](https://stackoverflow.com/questions/7131670/), [Alias with variable in bash?](https://stackoverflow.com/questions/4438147/) or [How to properly use $1 in an alias?](https://superuser.com/questions/295150/). – U880D Jun 11 '20 at 06:48

1 Answers1

1

I found a workaround that uses bash functions instead of an alias. The function marySpeak takes a string, sends it online for TTS then plays it back with mpv. Everything is defined as a oneliner, so can be placed in your .bashrc:

marySpeak() { curl -s -G --data-urlencode "text=$1" "https://speech.botiumbox.com/api/tts/en?&tts=marytts" -H "accept: audio/wav" | mpv - 2>&1 > /dev/null; } 

# then simply use it like this
marySpeak "hello world!"

Tested in GNU/Linux and macOS.

hyiltiz
  • 1,158
  • 14
  • 25