 #!/bin/sh
#
# RJP 14.7.2026 Espeak Translator
# fork from machine speaker 2
# default words per minute should be about +- 100
# Version where packages espeak, yad, xterm, and ffmpeg are required
# 
if ! nc -zw1 google.com 443; then 
yad --width=400 --text-align=center --center --title=$"No network connection:" --text="
     No network connection
       
    Add network connection and try again  "
fi

time=$(date "+%F-%H-%M-%S")

echo "
50

55

60

65

70

75

80

85

90

95
 
100

105

110

115

120

125

130

135 
 
140

145

150

155

160

165

170

175

180

185

190

195

200   

" > /tmp/options.txt

TEXT="$(yad --center --entry --width=700 --height=100 --text-align=center --button=OK:0 --title="Write or paste here" --
text="WRITE OR PASTE TEXT INTO BOX" --entry --entry-label=BOX --entry-text="")"
echo "$SPEAK" > /tmp/input.txt


choose=$(cat /tmp/options.txt | \
yad --list --title="select words per minute speed" --text="select words per minute speed" --button=OK:0 --center --radiolist \
  --width=500 --height=2000 \
  --height=300 \
  --column=Select \
  --column=words
) 
cat "$choose" 2> /tmp/tone.txt

cat /tmp/tone.txt | awk '{print $2}' > /tmp/tone2.txt
cat /tmp/tone2.txt | sed 's/^.//' > /tmp/tone3.txt
tr -dc '0-9' < /tmp/tone3.txt > /tmp/tone4.txt

locale=$(
yad --list --title="select locale" --text="select locale" --button=OK:0 --center --radiolist \
  --width=250 --height=500 \
  --column=fi \
  --column=en \
true fi false af false an false bg false bn false bs false ca false cs false cy false da false de false el false en false en-gb false en-sc false en-uk false north false en-uk-rp false en-uk-wmids false en-us false en-wi false eo false es false es-la false et false eu false fa false fa false fa-pin false fr-be false fr-fr false ga false grc false gu false hi false hr false hu false hy false hy-west false ia false id false is false it false jbo false ka false kn false ku false la false lfn false lt false lv false mk false ml false ms false ne false nl false no false pa false pl false pt-br false pt-pt false ro false ru false sk false sq false sr false sv false sw false ta false te false tr false vi false vi-hue false vi-sgn false zh false zh-yue
) 

cat "$locale" 2> /tmp/locale.txt
cat /tmp/locale.txt | awk '{print $2}' > /tmp/locale2.txt
cat /tmp/locale2.txt | sed 's/^.//' > /tmp/locale3.txt
sed -i 's/TRUE|//g' /tmp/locale3.txt 
sed -i 's/.$//' /tmp/locale3.txt 
sed -i 's/.$//' /tmp/locale3.txt 
sed -i 's/.$//' /tmp/locale3.txt  

gender=$(
yad --list --title="select gender" --text="select gender" --button=OK:0 --center --radiolist \
  --width=300 --height=200 \
  --column="man is m5" \
  --column="woman is f5"\
  true m5 false f5
) 

cat "$gender" 2> /tmp/gender.txt
cat /tmp/gender.txt | awk '{print $2}' > /tmp/gender2.txt
cat /tmp/gender2.txt | sed 's/^.//' > /tmp/gender3.txt
sed -i 's/TRUE|//g' /tmp/gender3.txt 
sed -i 's/.$//' /tmp/gender3.txt 
sed -i 's/.$//' /tmp/gender3.txt 
sed -i 's/.$//' /tmp/gender3.txt 

313=$({                                   
echo '#!/bin/bash
pw-record -P "'{ stream.capture.sink=true }'" /tmp/es-audio.flac 
'
} > /tmp/record-es-audio) 

314=$({  
echo '#!/bin/bash
time=$(date "+%F-%H-%M-%S")
pkill pw-record && ffmpeg -i /tmp/es-audio.flac -ab 320k espeak-translation-$time.mp3 & 
'
} > /tmp/stop-recording)

chmod +x /tmp/record-es-audio
chmod +x /tmp/stop-recording

sleep 2

TRANSLATE=$(
trans -s auto -t $(locale | grep ^LANG= | cut -d= -f2 | cut -d_ -f1) -p -b "$TEXT" > /tmp/translation.txt
)

yad --form --center --columns=2 --rows=3 --width=500 --title="ESPEAK TRANSLATOR" --text-align=center --text="ESPEAK TRANSLATOR

" \
\
--field="Speak text":fbtn "sh -c 'cat /tmp/translation.txt | espeak -v  $(cat /tmp/locale3.txt)+$(cat /tmp/gender3.txt) -s $(cat /tmp/tone4.txt) ; exec sh' & " \
--field="Stop speaking":fbtn "sh -c 'killall espeak; exec sh' & " \
--field="Save translation as text":fbtn "sh -c 'cp -ax /tmp/translation.txt ~/translation-$time.txt ; exec sh' & " \
--field="Record monitor audio with Pipewire":fbtn "/tmp/./record-es-audio " \
--field="Stop recording audio":fbtn "/tmp/./stop-recording " \
--button=Exit:1

sleep 90

cd /tmp && rm gender3.txt locale3.txt gender.txt locale.txt tone.txt input.txt gender2.txt locale2.txt options.txt tone2.txt tone3.txt tone4.txt es-audio.mp3 translation.txt record-es-audio es-audio.flac stop-recording


