#!/bin/bash
#
# RJP 16.4.2024 Machine speaker which need packages espeak-ng, yad and sox to be installed
# not so good than Gespeaker but working with every systems
time=$(date +%Y-%m-%d-%S)
echo "hello" > /tmp/nolla0
sed -i s/$/\"\"""/ /tmp/nolla0
sed 's/hello"//' /tmp/nolla0 > /tmp/nolla.txt

1=$({                                   
echo '#!/bin/bash
##time=$(date "+%F-%H-%M-%S")
VOICES=$(espeak-ng --voices > /tmp/voices.txt)
sleep 1
cat /tmp/voices.txt| yad --list --title "Search Results" --width=1000 --height=500 --text "INFORMATION" --column "Available Voices" --button=OK:1
'
} > /tmp/voices) 

2=$({ 
echo '#!/bin/bash
VARIANT=`yad --center --width=10 --height=100 --text-align=center --title=$"Select locale" --text="Voice variant you want to use (eg. fi if finnish)." --entry --entry-label=VARIANT --entry-text=""`
echo "$VARIANT" > /tmp/variant.txt
'
} > /tmp/variant)

3=$({ 
echo '#!/bin/bash
GENDER=`yad --center --width=10 --height=100 --text-align=center --title=$"Select gender" --text="Voice gender you want to use (f5 is female and m5 is male)." --entry --entry-label=GENDER --entry-text=""`
echo "$GENDER" > /tmp/gender.txt
'
} > /tmp/gender)


4=$({ 
echo '#!/bin/bash
SPEAK=`yad --center --entry --width=700 --height=100 --text-align=center --title=$"Write or paste here" --text="WRITE OR PASTE TEXT INTO BOX" --entry --entry-label=BOX --entry-text=""`
echo "$SPEAK" > /tmp/input.txt
'
} > /tmp/speak-text)

chmod +x /tmp/voices
chmod +x /tmp/variant
chmod +x /tmp/speak-text
chmod +x /tmp/gender

#!/bin/bash
yad --form --columns=5 --rows=3 --width=1000 --title=$"MACHINE SPEAKER" --text="MACHINE SPEAKER:" \
\
--field="Available Voices":fbtn "/tmp/./voices " \
--field="Voice variant you want to use":fbtn "/tmp/./variant " \
--field="Gender for speaker (male or female)":fbtn "/tmp/./gender " \
--field="Set text to speak":fbtn "/tmp/./speak-text " \
--field="Load text (reload this script)":fbtn "bash -c 'exec "$BASH" "$0" "$@"; exec bash' & " \
--field="Speak text (caching takes few seconds)":fbtn "bash -c 'cat {/tmp/nolla.txt,/tmp/input.txt,/tmp/nolla.txt} > /tmp/jees && cat /tmp/jees | espeak-ng -v $(cat /tmp/variant.txt)+$(cat /tmp/gender.txt); exec bash' & " \
--field="Stop speaking":fbtn "bash -c 'killall espeak-ng; exec bash' & " \
--field="Speak text and record text":fbtn "bash -c 'cat {/tmp/nolla.txt,/tmp/input.txt,/tmp/nolla.txt} > /tmp/jees && cat /tmp/jees | espeak-ng -v $(cat /tmp/variant.txt)+$(cat /tmp/gender.txt) | sox -t alsa default Recording.wav; exec bash' & " \
--field="Stop recording":fbtn "bash -c 'killall sox; exec bash' & " \
--field="Clear settings and input text":fbtn "bash -c 'cd /tmp && rm variant.txt input.txt variant speak-text speak jees ; exec bash' & " \
--button=Exit:1
