anonymous1184

tts.ahk

Apr 3rd, 2021 (edited)
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. ; Uses WinHttpRequest:
  3. ; https://git.io/JnMcX
  4.  
  5. F1:
  6.     backup := ClipboardAll
  7.     Clipboard := ""
  8.     Send ^c
  9.     ClipWait 0
  10.     if (ErrorLevel) {
  11.         Clipboard := backup
  12.         MsgBox 0x40010, Error, Couldn't retrieve text.
  13.         return
  14.     }
  15.     txt := Clipboard
  16.     Clipboard := backup
  17.     FileSelectFile filePath, S, % A_Desktop, Save As..., Audio (*.mp3; *.wav)
  18.     if (!filePath)
  19.         return
  20.     SplitPath filePath,, dir, ext, filename
  21.     if ext not in mp3,wav
  22.         ext := "mp3"
  23.     CoordMode ToolTip, Screen
  24.     parts := Ceil(StrLen(txt) / 4000)
  25.     loop % parts {
  26.         part := SubStr(txt, 1 + (A_Index * 4000) - 4000, 4000)
  27.         if (parts > 1) {
  28.             partName := filename " Pt. " A_Index
  29.             ToolTip % "Converting part " A_Index "/" parts, % A_ScreenWidth / 2, 0
  30.         } else {
  31.             partName := filename
  32.             ToolTip Converting..., % A_ScreenWidth / 2, 0
  33.         }
  34.         text2speech(part, dir, partName, ext)
  35.     }
  36.     ToolTip Conversion finished!, % A_ScreenWidth / 2, 0
  37.     Sleep 3000
  38.     ToolTip
  39. return
  40.  
  41. /*
  42.     * Voices:
  43.     slt                            - Female US
  44.     rms                            - Male US
  45.     awb                            - Male US (Scottish Accent)
  46.     kal                            - Male US (small size, low quality)
  47.     file-cmu_indic_axb.flitevox    - Female Hindi
  48.     file-cmu_indic_aup_mr.flitevox - Male Marathi
  49.  
  50.     * Speeds:
  51.     0.7  - Faster
  52.     0.85 - Fast
  53.     1    - Normal
  54.     1.25 - Slow
  55.     1.5  - Slower
  56. */
  57. text2speech(Text, Dir, Filename, Ext := "mp3", VoiceId := "slt", Speed := 1)
  58. {
  59.     ua := "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0.1) Gecko/20100101 Firefox/97.0.1"
  60.     http := WinHttpRequest({"UA": ua})
  61.     url := "https://www.text2speech.org/"
  62.     body := { "text": Text
  63.         , "voice": VoiceId
  64.         , "speed": Speed
  65.         , "outname": Filename
  66.         , "user_screen_width": 980 }
  67.     response := http.POST(url, body, "", {"multipart":1})
  68.     RegExMatch(response, "FW\/[^=]+=(?<Key>[^']+)", fw)
  69.     loop {
  70.         Sleep 3000
  71.         response := http.GET(url fw "&tscachebusttamp=" A_Now)
  72.     } until response != "__wait__123"
  73.     target := Dir "\" Filename "." Ext
  74.     while FileExist(target)
  75.         target := Dir "\" Filename "-" Format("{:02}", A_Index) "." Ext
  76.     Filename := StrReplace(Filename, " ", "_") "." Ext
  77.     UrlDownloadToFile % url "FW/getfile.php?file=" fwKey "%2F" Filename
  78.         , % target
  79. }
  80.  
Add Comment
Please, Sign In to add comment