Advertisement
banovski

TTS interface

May 27th, 2022 (edited)
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.50 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. function exiting()
  4. {
  5.     espeak -v whisper "$1"
  6.     exit "$2"
  7. }
  8.  
  9. function text_processing_and_storing()
  10. {
  11.     xclip -o -selection primary | sed -re "$primary_replacement" | tr '\n' ' ' | sed -re "$secondary_replacement" >> "$current_text_fragment_file"
  12.     echo >> "$current_text_fragment_file"
  13.     last_words="…"$(tail -1 "$current_text_fragment_file" | grep -Eo ".{,16}$")
  14. }
  15.  
  16. function total_duration_evaluation()
  17. {
  18.     number_of_cyrillic_syllables=$(grep -ro "[аеёиоуыэюя]" "$text_directory"/*.txt | wc -l)
  19.     number_of_latin_syllables=$(grep -ro "[aeiouy]" "$text_directory"/*.txt | wc -l)
  20.     number_of_cyrillic_seconds=$(echo "scale=0; $number_of_cyrillic_syllables / 8.3" | bc -l)
  21.     number_of_latin_seconds=$(echo "scale=0; $number_of_latin_syllables / 5" | bc -l)
  22.     number_of_seconds=$(echo "$number_of_cyrillic_seconds + $number_of_latin_seconds" | bc -l)
  23.     number_of_minutes=$(printf "%02d" $((number_of_seconds / 60)))
  24.     number_of_remaining_seconds=$(printf "%02d" $((number_of_seconds % 60)))
  25.     total_duration="$number_of_minutes:$number_of_remaining_seconds"
  26. }
  27.  
  28. initial_dialog_display_flag="True"
  29. texts_processing_interruption_flag="False"
  30. last_words="… …"
  31.  
  32. # sed replacement patterns. There's a COMBINING ACUTE ACCENT in the
  33. # second expression.
  34. primary_replacement='s/\[[^]]*\]//g; s/[́)(}{"|«»]//g; s/#/ решетка /g; s/\*/ астериск /g; s/\+/ плюс /g; s/\// слэш /g; s/</ меньше /g; s/=/ равно /g; s/>/ больше /g; s/@/ собачка /g; s/\\/ бэкслэш /g; s/\^/ циркумфлекс /g; s/_/ подчеркивание /g; s/`/ бэктик /g; s/~/ тильда /g; s/#/ решетка /g; s/\$/ доллар /g; s/&/ амперсанд /g; s/([05-9])[  ]*%/\1 процентов /g; s/1[  ]*%/1 процент /g; s/([2-4])[  ]*%/\1 процента /g; s/%//g; s/([05-9])[  ]*°/\1 градусов /g; s/1[  ]*°/1 градус /g; s/([2-4])[  ]*°/\1 градуса /g; s/°//g; s/-$//g'
  35. secondary_replacement='s/[[:space:]]+/ /g; s/([A-Za-z][^A-Za-zА-Яа-я]*)([А-Яа-я])/\1\n\2/g; s/([А-Яа-я][^A-Za-zА-Яа-я]*)([A-Za-z])/\1\n\2/g'
  36.  
  37. base_directory="/tmp/WTA_$(date +%y-%m-%d_%H-%M-%S)"
  38. text_directory="$base_directory/text"
  39. audio_directory="$base_directory/audio"
  40. mkdir "$base_directory" "$text_directory" "$audio_directory"
  41. cd "$base_directory" || exiting "Couldn't enter the base directory" 1
  42.  
  43. # GTK bookmarks: deletion and creation
  44. sed -i '/^.*Audible$/d' ~/.config/gtk-3.0/bookmarks
  45. echo "file://$base_directory Audible" >> ~/.config/gtk-3.0/bookmarks
  46.  
  47. while [ "$texts_processing_interruption_flag" == "False" ]; do
  48.     while true; do
  49.         if [ "$initial_dialog_display_flag" == "True" ]; then
  50.             current_text_fragment_file="$text_directory"/$(date +%s%N).txt
  51.             touch "$current_text_fragment_file"
  52.             espeak -v whisper "Primary!" &
  53.             total_duration_evaluation
  54.             yad --title="Written to audible" \
  55.                 --text="Total audio duration: $total_duration\nLast stored words: $last_words\nSelect text and / or choose an action: " \
  56.                 --button="Append:2" \
  57.                 --button="Abort:1" \
  58.                 --borders=12 \
  59.                 --on-top \
  60.                 --geometry=-1-1
  61.             if [ $? -eq 1 ] || [ $? -eq 252 ]; then
  62.                 exiting "Aborted!" 1
  63.             else
  64.                 text_processing_and_storing
  65.                 initial_dialog_display_flag="False"
  66.             fi
  67.         else
  68.             total_duration_evaluation
  69.             yad --title="Written to audible" \
  70.                 --text="Total audio duration: $total_duration\nLast stored words: $last_words\nSelect text and / or choose an action: " \
  71.                 --button="Append:2" \
  72.                 --button="Next article:3" \
  73.                 --button="Convert:4" \
  74.                 --button="Abort:1" \
  75.                 --borders=12 \
  76.                 --on-top \
  77.                 --geometry=-1-1
  78.            
  79.             case "$?" in
  80.                 2)
  81.                     text_processing_and_storing
  82.                     ;;
  83.                 3)
  84.                     initial_dialog_display_flag="True"
  85.                     break
  86.                     ;;
  87.                 4)
  88.                     texts_processing_interruption_flag="True"
  89.                     break
  90.                     ;;
  91.                 1|252)
  92.                     exiting "Aborted!" 1
  93.                     ;;
  94.                 *)
  95.                     true
  96.                     ;;
  97.             esac
  98.         fi
  99.     done
  100.  
  101.     if [ "$texts_processing_interruption_flag" == "True" ]; then
  102.         break
  103.     fi
  104. done
  105.  
  106. for file in $text_directory/*.txt; do
  107.     sox -r 44100 -c 1 -n "$audio_directory/$(date +%s%N)".wav synth 3 sine 220 vol -12dB
  108.     while read -r line; do
  109.         audio_chunk_file_path="$audio_directory/$(date +%s%N).wav"
  110.         if [[ "$line" =~ ^.*[А-Яа-я].*$ ]]; then
  111.             echo "$line" | RHVoice-client -s Anna -r 0.5 -v 1 | sox - --norm=-1 -r 44100 -c 1 "$audio_chunk_file_path"
  112.         else
  113.             espeak -s 160 -v english-us "$line" -w /dev/stdout | sox - --norm=-1 -r 44100 -c 1 "$audio_chunk_file_path"
  114.         fi
  115.     done < "$file"
  116.  
  117.     audio_files_number=$(ls "$audio_directory" | wc -l)
  118.     if [ "$audio_files_number" -ge 1024 ]; then
  119.         ulimit -n $((audio_files_number))
  120.     fi
  121.    
  122.     sox "$audio_directory/*.wav" ~/Data/shared/"$(date +%d_%H_%M_%S).ogg"
  123.     rm -f "$audio_directory"/*.wav
  124. done
  125.  
  126. exiting "Done!" 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement