1
+ /*
2
+ * FXGL - JavaFX Game Library. The MIT License (MIT).
3
+ * Copyright (c) AlmasB (almaslvl@gmail.com).
4
+ * See LICENSE for details.
5
+ */
6
+
7
+ package com.almasb.fxgl.intelligence.tts
8
+
9
+ import com.almasb.fxgl.core.EngineService
10
+ import com.almasb.fxgl.core.concurrent.Async
11
+ import com.almasb.fxgl.intelligence.WebAPI
12
+ import com.almasb.fxgl.logging.Logger
13
+ import com.almasb.fxgl.net.ws.LocalWebSocketServer
14
+ import com.almasb.fxgl.speechrecog.SpeechRecognitionService
15
+ import org.openqa.selenium.By
16
+ import org.openqa.selenium.WebDriver
17
+ import org.openqa.selenium.chrome.ChromeDriver
18
+ import org.openqa.selenium.chrome.ChromeOptions
19
+
20
+ /* *
21
+ * TODO: remove duplicate code
22
+ *
23
+ * @author Almas Baim (https://github.com/AlmasB)
24
+ */
25
+ class TextToSpeechService : EngineService () {
26
+
27
+ private val log = Logger .get(TextToSpeechService ::class .java)
28
+ private val server = LocalWebSocketServer (" TTSServer" , WebAPI .TEXT_TO_SPEECH_PORT )
29
+
30
+ private var webDriver: WebDriver ? = null
31
+
32
+ override fun onInit () {
33
+ server.start()
34
+ }
35
+
36
+ /* *
37
+ * Starts this service in a background thread.
38
+ * Can be called after stop() to restart the service.
39
+ * If the service has already started, then calls stop() and restarts it.
40
+ */
41
+ fun start () {
42
+ Async .startAsync {
43
+ try {
44
+ if (webDriver != null ) {
45
+ stop()
46
+ }
47
+
48
+ val options = ChromeOptions ()
49
+ options.addArguments(" --headless=new" )
50
+ options.addArguments(" --use-fake-ui-for-media-stream" )
51
+
52
+ webDriver = ChromeDriver (options)
53
+ webDriver!! .get(WebAPI .TEXT_TO_SPEECH_API )
54
+
55
+ // TODO: update web-api impl
56
+ // force it to play, so the audio output is initialized
57
+ webDriver!! .findElement(By .id(" play" )).click()
58
+
59
+ // we are ready to use the web api service
60
+ } catch (e: Exception ) {
61
+ log.warning(" Failed to start Chrome web driver. Ensure Chrome is installed in default location" )
62
+ log.warning(" Error data" , e)
63
+ }
64
+ }
65
+ }
66
+
67
+ /* *
68
+ * Stops this service.
69
+ * No-op if it has not started via start() before.
70
+ */
71
+ fun stop () {
72
+ try {
73
+ if (webDriver != null ) {
74
+ webDriver!! .quit()
75
+ webDriver = null
76
+ }
77
+ } catch (e: Exception ) {
78
+ log.warning(" Failed to quit web driver" , e)
79
+ }
80
+ }
81
+
82
+ fun speak (text : String ) {
83
+ server.send(text)
84
+ }
85
+
86
+ override fun onExit () {
87
+ stop()
88
+ server.stop()
89
+ }
90
+ }
0 commit comments