voice_list.gd 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. extends Control
  2. var id = 0 #utterance id
  3. var ut_map = {}
  4. var vs
  5. func _ready():
  6. # get voice data
  7. vs = DisplayServer.tts_get_voices()
  8. var root = $Tree.create_item()
  9. $Tree.set_hide_root(true)
  10. $Tree.set_column_title(0, "Name")
  11. $Tree.set_column_title(1, "Language")
  12. $Tree.set_column_titles_visible(true)
  13. for v in vs:
  14. var child = $Tree.create_item(root)
  15. child.set_text(0, v["name"])
  16. child.set_metadata(0, v["id"])
  17. child.set_text(1, v["language"])
  18. $Log.text += "%d voices available\n" % [vs.size()]
  19. $Log.text += "=======\n"
  20. # add callbacks
  21. DisplayServer.tts_set_utterance_callback(DisplayServer.TTS_UTTERANCE_STARTED, Callable(self, "_on_utterance_start"))
  22. DisplayServer.tts_set_utterance_callback(DisplayServer.TTS_UTTERANCE_ENDED, Callable(self, "_on_utterance_end"))
  23. DisplayServer.tts_set_utterance_callback(DisplayServer.TTS_UTTERANCE_CANCELED, Callable(self, "_on_utterance_error"))
  24. DisplayServer.tts_set_utterance_callback(DisplayServer.TTS_UTTERANCE_BOUNDARY, Callable(self, "_on_utterance_boundary"))
  25. set_process(true)
  26. func _process(_delta):
  27. $ButtonPause.button_pressed = DisplayServer.tts_is_paused()
  28. if DisplayServer.tts_is_speaking():
  29. $ColorRect.color = Color(1, 0, 0)
  30. else:
  31. $ColorRect.color = Color(1, 1, 1)
  32. func _on_utterance_boundary(pos, ut_id):
  33. $RichTextLabel.text = "[bgcolor=yellow][color=black]" + ut_map[ut_id].substr(0, pos) + "[/color][/bgcolor]" + ut_map[ut_id].substr(pos, -1)
  34. func _on_utterance_start(ut_id):
  35. $Log.text += "utterance %d started\n" % [ut_id]
  36. func _on_utterance_end(ut_id):
  37. $RichTextLabel.text = "[bgcolor=yellow][color=black]" + ut_map[ut_id] + "[/color][/bgcolor]"
  38. $Log.text += "utterance %d ended\n" % [ut_id]
  39. ut_map.erase(ut_id)
  40. func _on_utterance_error(ut_id):
  41. $RichTextLabel.text = ""
  42. $Log.text += "utterance %d canceled/failed\n" % [ut_id]
  43. ut_map.erase(ut_id)
  44. func _on_ButtonStop_pressed():
  45. DisplayServer.tts_stop()
  46. func _on_ButtonPause_pressed():
  47. if $ButtonPause.pressed:
  48. DisplayServer.tts_pause()
  49. else:
  50. DisplayServer.tts_resume()
  51. func _on_ButtonSpeak_pressed():
  52. if $Tree.get_selected():
  53. $Log.text += "utterance %d queried\n" % [id]
  54. ut_map[id] = $Utterance.text
  55. DisplayServer.tts_speak($Utterance.text, $Tree.get_selected().get_metadata(0), $HSliderVolume.value, $HSliderPitch.value, $HSliderRate.value, id, false)
  56. id += 1
  57. else:
  58. OS.alert("No voice selected.\nSelect a voice in the list, then try using Speak again.")
  59. func _on_ButtonIntSpeak_pressed():
  60. if $Tree.get_selected():
  61. $Log.text += "utterance %d interrupt\n" % [id]
  62. ut_map[id] = $Utterance.text
  63. DisplayServer.tts_speak($Utterance.text, $Tree.get_selected().get_metadata(0), $HSliderVolume.value, $HSliderPitch.value, $HSliderRate.value, id, true)
  64. id += 1
  65. else:
  66. OS.alert("No voice selected.\nSelect a voice in the list, then try using Interrupt again.")
  67. func _on_ButtonClearLog_pressed():
  68. $Log.text = ""
  69. func _on_HSliderRate_value_changed(value):
  70. $HSliderRate/Value.text = "%.2fx" % [value]
  71. func _on_HSliderPitch_value_changed(value):
  72. $HSliderPitch/Value.text = "%.2fx" % [value]
  73. func _on_HSliderVolume_value_changed(value):
  74. $HSliderVolume/Value.text = "%d%%" % [value]
  75. func _on_Button_pressed():
  76. var vc
  77. #demo - en
  78. vc = DisplayServer.tts_get_voices_for_language("en")
  79. if !vc.is_empty():
  80. ut_map[id] = "Beware the Jabberwock, my son!"
  81. ut_map[id + 1] = "The jaws that bite, the claws that catch!"
  82. DisplayServer.tts_speak("Beware the Jabberwock, my son!", vc[0], 50, 1, 1, id)
  83. DisplayServer.tts_speak("The jaws that bite, the claws that catch!", vc[0], 50, 1, 1, id + 1)
  84. id += 2
  85. #demo - es
  86. vc = DisplayServer.tts_get_voices_for_language("es")
  87. if !vc.is_empty():
  88. ut_map[id] = "¡Cuidado, hijo, con el Fablistanón!"
  89. ut_map[id + 1] = "¡Con sus dientes y garras, muerde, apresa!"
  90. DisplayServer.tts_speak("¡Cuidado, hijo, con el Fablistanón!", vc[0], 50, 1, 1, id)
  91. DisplayServer.tts_speak("¡Con sus dientes y garras, muerde, apresa!", vc[0], 50, 1, 1, id + 1)
  92. id += 2
  93. #demo - ru
  94. vc = DisplayServer.tts_get_voices_for_language("ru")
  95. if !vc.is_empty():
  96. ut_map[id] = "О, бойся Бармаглота, сын!"
  97. ut_map[id + 1] = "Он так свирлеп и дик!"
  98. DisplayServer.tts_speak("О, бойся Бармаглота, сын!", vc[0], 50, 1, 1, id)
  99. DisplayServer.tts_speak("Он так свирлеп и дик!", vc[0], 50, 1, 1, id + 1)
  100. id += 2
  101. func _on_LineEditFilterName_text_changed(_new_text):
  102. $Tree.clear()
  103. var root = $Tree.create_item()
  104. for v in vs:
  105. if ($LineEditFilterName.text.is_empty() || $LineEditFilterName.text.to_lower() in v["name"].to_lower()) && ($LineEditFilterLang.text.is_empty() || $LineEditFilterLang.text.to_lower() in v["language"].to_lower()):
  106. var child = $Tree.create_item(root)
  107. child.set_text(0, v["name"])
  108. child.set_metadata(0, v["id"])
  109. child.set_text(1, v["language"])