From 2a703325447eb61615d54bfa11d635aa5c943f0d Mon Sep 17 00:00:00 2001 From: rany Date: Thu, 13 May 2021 16:26:17 +0300 Subject: [PATCH] Update easy-playback.sh --- easy-playback.sh | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/easy-playback.sh b/easy-playback.sh index 6ece969..3656c44 100755 --- a/easy-playback.sh +++ b/easy-playback.sh @@ -1,8 +1,37 @@ #!/usr/bin/env bash -## To use this script you need to install -## edge-tts.py to a directory in your $PATH -## as executable and give it the name edge-tts. -trap 'kill -- $(jobs -p) 2>/dev/null' EXIT -[ "$1" == "stdin" ] && { stdin=$(cat); shift 1; set -- "$@" '--file=/dev/stdin'; } || stdin="" -exec {fd}< <(edge-tts "${@}" <<<"$stdin") -mpg123 -C "/dev/fd/$fd" + +## To use this script you need to install edge-tts.py to a directory in your $PATH as executable +## and give it the name edge-tts. Alternatively you could just run the install script. +export LC_ALL=C + +## We use a temporary file now instead of file descriptor because mpg123 doesn't +## let me seek back a file descriptor, only seek forward. +ttsmpeg=$(mktemp) + +## Cleanup function to kill all processes and remove tmp file +quitfunc() { + # shellcheck disable=SC2046 + kill -- $(jobs -p) + rm -f -- "${ttsmpeg:?}" +} +trap 'quitfunc > /dev/null 2>&1' EXIT + +## If stdin is $1 we shift 1 and save the stdin data to an stdin variable. +## We also add --file=/dev/stdin to params that edge-tts will get. +if [ "$1" == "stdin" ] +then + stdin=$(cat) + shift 1 + set -- "$@" '--file=/dev/stdin' +else + stdin="" +fi +edge-tts "${@}" >"$ttsmpeg" <<<"$stdin" & + +## Wait until temporary file has some data so mpg123 doesn't exit immediately +## because it thinks file is empty and won't have any data. +while [ "$(wc -c <"$ttsmpeg")" == 0 ] +do + sleep 0.1 +done +mpg123 --quiet --control "$ttsmpeg"