Advertisement
tabnation

text to speech

Feb 22nd, 2022
198
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None
  1. #SingleInstance, force
  2. #NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
  3.  
  4. f1::
  5. ;send ^a
  6. send ^c
  7. ComObjCreate("SAPI.SpVoice").Speak(Clipboard)
  8. return
  9.  
  10.  
  11. f2:: ;change voice type
  12. if (SUCCEEDED(SpGetCategoryFromId(SPCAT_VOICES := "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices", cpSpObjectTokenCategory)))
  13. {
  14. hr := DllCall(NumGet(NumGet(cpSpObjectTokenCategory+0)+18*A_PtrSize), "Ptr", cpSpObjectTokenCategory, "Ptr", 0, "Ptr", 0, "Ptr*", cpSpEnumTokens)
  15.  
  16. if (SUCCEEDED(hr))
  17. {
  18. hr := DllCall(NumGet(NumGet(cpSpEnumTokens+0)+8*A_PtrSize), "Ptr", cpSpEnumTokens, "UInt*", tokenCount)
  19. if (SUCCEEDED(hr))
  20. {
  21. voices := Object()
  22. Loop %tokenCount% {
  23. hr := DllCall(NumGet(NumGet(cpSpEnumTokens+0)+7*A_PtrSize), "Ptr", cpSpEnumTokens, "UInt", A_Index - 1, "Ptr*", pToken)
  24. if (FAILED(hr)) {
  25. MsgBox Bailing out
  26. ExitApp 1
  27. }
  28. hr := DllCall(NumGet(NumGet(pToken+0)+6*A_PtrSize), "Ptr", pToken, "Ptr", 0, "Ptr*", pszValue)
  29. if (FAILED(hr)) {
  30. MsgBox Bailing out
  31. ExitApp 2
  32. }
  33. hr := DllCall(NumGet(NumGet(pToken+0)+16*A_PtrSize), "Ptr", pToken, "Ptr*", pszCoMemTokenId)
  34. if (FAILED(hr)) {
  35. MsgBox Bailing out
  36. ExitApp 3
  37. }
  38. voices[StrGet(pszCoMemTokenId, "UTF-16")] := StrGet(pszValue, "UTF-16")
  39. DllCall("ole32\CoTaskMemFree", "Ptr", pszValue)
  40. DllCall("ole32\CoTaskMemFree", "Ptr", pszCoMemTokenId)
  41. ObjRelease(pToken)
  42. }
  43. prompt := "Pick a voice by its number:"
  44. for k, v in voices
  45. prompt .= "`r`n" . A_Index . ": " v
  46. InputBox, TheChosenOne,, %prompt%
  47. if (ErrorLevel == 0) {
  48. for k, v in voices {
  49. if (A_Index == TheChosenOne) {
  50. hr := DllCall(NumGet(NumGet(cpSpObjectTokenCategory+0)+19*A_PtrSize), "Ptr", cpSpObjectTokenCategory, "WStr", k)
  51. break
  52. }
  53. }
  54. }
  55. }
  56. ObjRelease(cpSpEnumTokens)
  57. }
  58.  
  59. ObjRelease(cpSpObjectTokenCategory)
  60. }
  61.  
  62. SpGetCategoryFromId(pszCategoryId, ByRef ppCategory, fCreateIfNotExist := False)
  63. {
  64. static CLSID_SpObjectTokenCategory := "{A910187F-0C7A-45AC-92CC-59EDAFB77B53}"
  65. ,ISpObjectTokenCategory := "{2D3D3845-39AF-4850-BBF9-40B49780011D}"
  66.  
  67. hr := 0
  68. try {
  69. cpTokenCategory := ComObjCreate(CLSID_SpObjectTokenCategory, ISpObjectTokenCategory)
  70. } catch e {
  71. ; No, A_LastError or ErrorLevel doesn't contain the error code on its own and I CBA to use CoCreateInstance directly
  72. if (RegExMatch(e.Message, "0[xX][0-9a-fA-F]+", errCode)) { ; https://stackoverflow.com/a/9221391
  73. hr := errCode + 0
  74. } else {
  75. hr := 0x80004005
  76. }
  77. }
  78.  
  79. if (SUCCEEDED(hr))
  80. {
  81. hr := DllCall(NumGet(NumGet(cpTokenCategory+0)+15*A_PtrSize), "Ptr", cpTokenCategory, "WStr", pszCategoryId, "Int", fCreateIfNotExist)
  82. }
  83.  
  84. if (SUCCEEDED(hr))
  85. {
  86. ppCategory := cpTokenCategory
  87. }
  88. else
  89. {
  90. if (cpTokenCategory)
  91. ObjRelease(cpTokenCategory)
  92. }
  93.  
  94. return hr
  95. }
  96.  
  97. SUCCEEDED(hr)
  98. {
  99. return hr != "" && hr >= 0x00
  100. }
  101.  
  102. FAILED(hr)
  103. {
  104. return hr == "" || hr < 0
  105. }
Advertisement
RAW Paste Data Copied
Advertisement