Advertisement
Rich4rd

TextToSpeechVoicesMenu

May 18th, 2019
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;TextToSpeechVoicesMenu.
  2. ; Class TTS by evilC
  3. ; Based on code by Learning one. For AHK_L. Thanks: jballi, Sean, Frankie.
  4. ; AHK forum location:   www.autohotkey.com/forum/topic57773.html
  5. ; Read more:            msdn.microsoft.com/en-us/library/ms723602(v=VS.85).aspx, www.autohotkey.com/forum/topic45471.html, www.autohotkey.com/forum/topic83162.html
  6. ;I have made just only the menu and added in this menu to include many common languages. The languages not used can be replaced or removed.
  7. F12:: ; i.e. press F12 hotkey to show the menu. ;press Gaming Mouse XButton1 or ScrollLock to read selected text aloud
  8. s := new TTS()
  9. ;Speech Engines Menu
  10. ;Menu, MenuName, Add,  Voice Name, FunctionToBeExecuted(WithoutSpaces)
  11. Menu, TTS, Add,  ; Add a separator line.
  12. Menu, Arabic, Add, Laila, Laila
  13. Menu, Armenian, Add, Armenian, Armenian
  14. Menu, English, Add, Steven, Steven
  15. Menu, English, Add, Eric, Eric
  16. Menu, French, Add, Juliette, Juliette
  17. Menu, French, Add, Hortense, Hortense
  18. Menu, Georgian, Add, Georgian, Georgian
  19. Menu, German, Add, Stefan, Stefan
  20. Menu, German, Add, Hedda, Hedda
  21. Menu, Greek, Add, Greek, Greek
  22. Menu, Greek, Add, Melina, Melina
  23. Menu, Hebrew, Add, Carmit, Carmit
  24. Menu, Hindi, Add, Hindi, Hindi
  25. Menu, Hindi, Add, Lekha, Lekha
  26. Menu, Italian, Add, Silvia, Silvia
  27. Menu, Italian, Add, Lucia, Lucia
  28. Menu, Latin, Add, Latin, Latin
  29. Menu, Russian, Add, Katya, Katya
  30. Menu, Russian, Add, Olga, Olga
  31. Menu, Persian, Add, Persian, Persian
  32. Menu, Spanish, Add, Isabel, Isabel
  33. Menu, Spanish, Add, Helena, Helena
  34. Menu, Chinese, Add, Li-li, Li-li
  35. Menu, Chinese, Add, HuiHui, HuiHui
  36. Menu, Japanese, Add, Haruka, Haruka
  37. Menu, Japanese, Add, Kyoko, Kyoko
  38. Menu, Korean, Add, Yuna, Yuna
  39. Menu, Korean, Add, Heami, Heami
  40.  
  41. ; Create a submenu in the first menu (a right-arrow indicator). When the user selects it, the second menu is displayed.
  42. Menu, TTS, Add, Arabic, :Arabic
  43. Menu, TTS, Add, Armenian, :Armenian
  44. Menu, TTS, Add, English, :English
  45. Menu, TTS, Add, French, :French
  46. Menu, TTS, Add, Georgian, :Georgian
  47. Menu, TTS, Add, German, :German
  48. Menu, TTS, Add, Greek, :Greek
  49. Menu, TTS, Add, Hebrew, :Hebrew
  50. Menu, TTS, Add, Hindi, :Hindi
  51. Menu, TTS, Add, Italian, :Italian
  52. Menu, TTS, Add, Latin, :Latin
  53. Menu, TTS, Add, Russian, :Russian
  54. Menu, TTS, Add, Persian, :Persian
  55. Menu, TTS, Add, Spanish, :Spanish
  56. Menu, TTS, Add, Chinese, :Chinese
  57. Menu, TTS, Add, Japanese, :Japanese
  58. Menu, TTS, Add, Korean, :Korean
  59. Menu, TTS, Add,  ; Add a separator line.
  60. Menu, TTS, Show
  61.  
  62.  
  63. s.Speak(gst())  ; select some text and press Ctrl+ScrollLock to test Speak()
  64. XButton1::s.Speak(gst())
  65. ScrollLock::s.Speak(gst())
  66. s.SetVoice("")
  67.  
  68. ;Applications
  69. English:
  70. Steven:
  71. s.SetVoice("Steven") ;PUT HERE YOUR OWN TTS VOICE NAME
  72. return
  73. Eric:
  74. s.SetVoice("IVONA 2 Eric OEM") ;PUT HERE YOUR OWN TTS VOICE NAME
  75. return
  76. Russian:
  77. Katya:
  78. s.SetVoice("Vocalizer Expressive Katya Harpo 22kHz") ;PUT HERE YOUR OWN TTS VOICE NAME
  79. return
  80. Olga:
  81. s.SetVoice("Olga") ;Russian
  82. return
  83. Latin:
  84. s.SetVoice("eSpeak-la+f2") ;Latin
  85. return
  86. Armenian:
  87. s.SetVoice("eSpeak-hy+f2") ;Armenian
  88. return
  89. Carmit:
  90. s.SetVoice("Vocalizer Expressive Carmit Harpo 22kHz") ;Hebrew
  91. return
  92. Greek:
  93. s.SetVoice("eSpeak-grc+f2") ;Ancient Greek
  94. return
  95. Melina:
  96. s.SetVoice("Vocalizer Expressive Melina Harpo 22kHz") ;New Greek
  97. return
  98. Georgian:
  99. s.SetVoice("eSpeak-ka+f2") ;Georgian
  100. return
  101. French:
  102. Juliette:
  103. s.SetVoice("Juliette") ;French
  104. return
  105. Hortense:
  106. s.SetVoice("Hortense, fr-FR MS") ;French
  107. return
  108. German:
  109. Stefan:
  110. s.SetVoice("Stefan") ;German
  111. return
  112. Hedda:
  113. s.SetVoice("Hedda, de-DE MS") ;German
  114. return
  115. Italian:
  116. Silvia:
  117. s.SetVoice("ScanSoft Silvia_Full_22kHz") ;Italian
  118. return
  119. Lucia:
  120. s.SetVoice("Lucia, it-IT MS") ;Italian
  121. return
  122. Spanish:
  123. Isabel:
  124. s.SetVoice("ScanSoft Isabel_Full_22kHz") ;Spanish
  125. return
  126. Helena:
  127. s.SetVoice("Helena, es-ES MS") ;Spanish
  128. return
  129.  
  130. Laila:
  131. s.SetVoice("Vocalizer Expressive Laila Harpo 22kHz") ;Arabic
  132. return
  133. Persian:
  134. s.SetVoice("eSpeak-fa+f2") ;Persian
  135. return
  136. Japanese:
  137. Kyoko:
  138. s.SetVoice("Vocalizer Expressive Kyoko Harpo 22kHz") ;Japanese
  139. return
  140. Haruka:
  141. s.SetVoice("Haruka, ja-JP MS")
  142. return
  143. Chinese:
  144. Li-li:
  145. s.SetVoice("Vocalizer Expressive Li-li Harpo 22kHz") ;Chinese
  146. return
  147. HuiHui:
  148. s.SetVoice("HuiHui, zh-CN MS") ;Chinese
  149. return
  150. Korean:
  151. Yuna:
  152. s.SetVoice("Vocalizer Expressive Yuna Harpo 22kHz") ;Korean
  153. return
  154. Heami:
  155. s.SetVoice("Heami, ko-KR MS") ;Korean
  156. return
  157. Hindi:
  158. s.SetVoice("eSpeak-hi+f2") ;Hindi
  159. return
  160. Lekha:
  161. s.SetVoice("Vocalizer Expressive Lekha Harpo 22kHz") ;Hindi
  162. return
  163.  
  164.  
  165. s.ToggleSpeak(gst())    ; select some text and press Shift+Alt+F11 to test ToggleSpeak()
  166. s.NextVoice()           ; select some text and press Shift+Alt+F12 to switch to the next voice
  167.  
  168. gst() {   ; GetSelectedText by Learning one
  169.     IsClipEmpty := (Clipboard = "") ? 1 : 0
  170.     if !IsClipEmpty {
  171.         ClipboardBackup := ClipboardAll
  172.         While !(Clipboard = "") {
  173.             Clipboard =
  174.             Sleep, 10
  175.         }
  176.     }
  177.     Send, ^c
  178.     ClipWait, 0.1
  179.     ToReturn := Clipboard, Clipboard := ClipboardBackup
  180.     if !IsClipEmpty
  181.     ClipWait, 0.5, 1
  182.     Return ToReturn
  183. }
  184.  
  185. ; Class TTS by evilC
  186. ; Based on code by Learning one. For AHK_L. Thanks: jballi, Sean, Frankie.
  187. ; AHK forum location:   www.autohotkey.com/forum/topic57773.html
  188. ; Read more:            msdn.microsoft.com/en-us/library/ms723602(v=VS.85).aspx, www.autohotkey.com/forum/topic45471.html, www.autohotkey.com/forum/topic83162.html
  189. Class TTS {
  190.     VoiceList := []     ; An indexed array of the available voice names
  191.     VoiceAssoc := {}    ; An Associative array of voice names, key = voice name, value = voice index (VoiceList lookup)
  192.     VoiceCount := 0     ; The number of voices available
  193.     VoiceNumber := 0    ; The number of the current voice
  194.     VoiceName := ""     ; The name of the current voice
  195.    
  196.     __New(){
  197.         this.oVoice := ComObjCreate("SAPI.SpVoice")
  198.         this._GetVoices()
  199.         this.SetVoice(this.VoiceList.1)
  200.     }
  201.  
  202.     ; speak or stop speaking
  203.     ToggleSpeak(text){
  204.         Status := this.oVoice.Status.RunningState
  205.         if Status = 1   ; finished
  206.         this.oVoice.Speak(text,0x1) ; speak asynchronously
  207.         Else if Status = 0  ; paused
  208.         {
  209.             this.oVoice.Resume
  210.             this.oVoice.Speak("",0x1|0x2)   ; stop
  211.             this.oVoice.Speak(text,0x1) ; speak asynchronously
  212.         }
  213.         Else if Status = 2  ; reading
  214.         this.oVoice.Speak("",0x1|0x2)   ; stop
  215.     }
  216.  
  217.     ; speak asynchronously
  218.     Speak(text){
  219.         Status := this.oVoice.Status.RunningState
  220.         if Status = 0   ; paused
  221.         this.oVoice.Resume
  222.         this.oVoice.Speak("",0x1|0x2)   ; stop
  223.         this.oVoice.Speak(text,0x1) ; speak asynchronously
  224.     }
  225.    
  226.     ; speak synchronously
  227.     SpeakWait(text){
  228.         Status := this.oVoice.Status.RunningState
  229.         if Status = 0   ; paused
  230.         this.oVoice.Resume
  231.         this.oVoice.Speak("",0x1|0x2)   ; stop
  232.         this.oVoice.Speak(text,0x0) ; speak synchronously
  233.     }
  234.    
  235.     ; Pause toggle
  236.     Pause(){
  237.         Status := this.oVoice.Status.RunningState
  238.         if Status = 0   ; paused
  239.         this.oVoice.Resume
  240.         else if Status = 2  ; reading
  241.         this.oVoice.Pause
  242.     }
  243.    
  244.     Stop(){
  245.         Status := this.oVoice.Status.RunningState
  246.         if Status = 0   ; paused
  247.         this.oVoice.Resume
  248.         this.oVoice.Speak("",0x1|0x2)   ; stop
  249.     }
  250.    
  251.     ; rate (reading speed): rate from -10 to 10. 0 is default.
  252.     SetRate(rate){
  253.         this.oVoice.Rate := rate
  254.     }
  255.    
  256.     ; volume (reading loudness): vol from 0 to 100. 100 is default
  257.     SetVolume(vol){
  258.         this.oVoice.Volume := vol
  259.     }
  260.    
  261.     ; pitch : From -10 to 10. 0 is default.
  262.     ; http://msdn.microsoft.com/en-us/library/ms717077(v=vs.85).aspx
  263.     SetPitch(pitch){
  264.         this.oVoice.Speak("<pitch absmiddle = '" pitch "'/>",0x20)
  265.     }
  266.  
  267.     ; Set voice by name
  268.     SetVoice(VoiceName){
  269.         if (!ObjHasKey(this.VoiceAssoc, VoiceName))
  270.             return 0
  271.         While !(this.oVoice.Status.RunningState = 1)
  272.         Sleep, 20
  273.         this.oVoice.Voice := this.oVoice.GetVoices("Name=" VoiceName).Item(0) ; set voice to param1
  274.         this.VoiceName := VoiceName
  275.         this.VoiceNumber := this.VoiceAssoc[VoiceName]
  276.         return 1
  277.     }
  278.  
  279.     ; Set voice by index
  280.     SetVoiceByIndex(VoiceIndex){
  281.         return this.SetVoice(this.VoiceList[VoiceIndex])
  282.     }
  283.  
  284.     ; Use the next voice. Loops around at end
  285.     NextVoice(){
  286.         v := this.VoiceNumber + 1
  287.         if (v > this.VoiceCount)
  288.             v := 1
  289.         return this.SetVoiceByIndex(v)
  290.     }
  291.    
  292.     ; Returns an array of voice names
  293.     GetVoices(){
  294.         return this.VoiceList
  295.     }
  296.  
  297.     GetStatus(){
  298.         Status := this.oVoice.Status.RunningState
  299.         if Status = 0 ; paused
  300.         Return "paused"
  301.         Else if Status = 1 ; finished
  302.         Return "finished"
  303.         Else if Status = 2 ; reading
  304.         Return "reading"
  305.     }
  306.    
  307.     GetCount(){
  308.         return this.VoiceCount
  309.     }
  310.    
  311.     SpeakToFile(param1, param2){
  312.         oldAOS := this.oVoice.AudioOutputStream
  313.         oldAAOFCONS := this.oVoice.AllowAudioOutputFormatChangesOnNextSet
  314.         this.oVoice.AllowAudioOutputFormatChangesOnNextSet := 1
  315.        
  316.         SpStream := ComObjCreate("SAPI.SpFileStream")
  317.         FileDelete, % param2    ; OutputFilePath
  318.         SpStream.Open(param2, 3)
  319.         this.oVoice.AudioOutputStream := SpStream
  320.         this.SpeakWait(param1)
  321.         SpStream.Close()
  322.         this.oVoice.AudioOutputStream := oldAOS
  323.         this.oVoice.AllowAudioOutputFormatChangesOnNextSet := oldAAOFCONS
  324.     }
  325.  
  326.     ; ====== Private funcs, not intended to be called by user =======
  327.     _GetVoices(){
  328.         this.VoiceList := []
  329.         this.VoiceAssoc := {}
  330.         this.VoiceCount := this.oVoice.GetVoices.Count
  331.         Loop, % this.VoiceCount
  332.         {
  333.             Name := this.oVoice.GetVoices.Item(A_Index-1).GetAttribute("Name")  ; 0 based
  334.             this.VoiceList.push(Name)
  335.             this.VoiceAssoc[Name] := A_Index
  336.         }
  337.     }
  338. }
  339. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement