-
-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added initial impl of speech recognition API
- Loading branch information
Showing
9 changed files
with
194 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
fxgl-intelligence/src/main/java/com/almasb/fxgl/intelligence/SpeechRecognitionService.java
This file was deleted.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
fxgl-intelligence/src/main/java/com/almasb/fxgl/intelligence/WebAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* FXGL - JavaFX Game Library. The MIT License (MIT). | ||
* Copyright (c) AlmasB (almaslvl@gmail.com). | ||
* See LICENSE for details. | ||
*/ | ||
|
||
package com.almasb.fxgl.intelligence; | ||
|
||
/** | ||
* Stores constants related to web-api projects. | ||
* Changes to these values must be synchronized with the web-api project (https://github.com/AlmasB/web-api). | ||
* | ||
* @author Almas Baim (https://github.com/AlmasB) | ||
*/ | ||
public final class WebAPI { | ||
|
||
public static final String SPEECH_RECOGNITION_API = "https://almasb.github.io/web-api/speech-recog/"; | ||
public static final String GESTURE_RECOGNITION_API = "https://almasb.github.io/web-api/gesture-recog-v1/"; | ||
|
||
public static final int SPEECH_RECOGNITION_PORT = 55555; | ||
public static final int GESTURE_RECOGNITION_PORT = 55560; | ||
} |
96 changes: 96 additions & 0 deletions
96
fxgl-intelligence/src/main/java/com/almasb/fxgl/speechrecog/SpeechRecognitionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* FXGL - JavaFX Game Library. The MIT License (MIT). | ||
* Copyright (c) AlmasB (almaslvl@gmail.com). | ||
* See LICENSE for details. | ||
*/ | ||
|
||
package com.almasb.fxgl.speechrecog; | ||
|
||
import com.almasb.fxgl.core.EngineService; | ||
import com.almasb.fxgl.core.concurrent.Async; | ||
import com.almasb.fxgl.logging.Logger; | ||
import com.almasb.fxgl.ws.LocalWebSocketServer; | ||
import org.openqa.selenium.WebDriver; | ||
import org.openqa.selenium.chrome.ChromeDriver; | ||
import org.openqa.selenium.chrome.ChromeOptions; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import static com.almasb.fxgl.intelligence.WebAPI.SPEECH_RECOGNITION_API; | ||
import static com.almasb.fxgl.intelligence.WebAPI.SPEECH_RECOGNITION_PORT; | ||
|
||
public final class SpeechRecognitionService extends EngineService { | ||
|
||
private static final Logger log = Logger.get(SpeechRecognitionService.class); | ||
private LocalWebSocketServer server = new LocalWebSocketServer("SpeechRecogServer", SPEECH_RECOGNITION_PORT); | ||
|
||
private WebDriver webDriver = null; | ||
|
||
@Override | ||
public void onInit() { | ||
server.start(); | ||
} | ||
|
||
/** | ||
* Starts speech recognition in a background thread. | ||
* Can be called after stop() to restart speech recognition. | ||
* If the service has already started, then calls stop() and restarts it. | ||
*/ | ||
public void start() { | ||
Async.INSTANCE.startAsync(() -> { | ||
try { | ||
if (webDriver != null) { | ||
stop(); | ||
} | ||
|
||
ChromeOptions options = new ChromeOptions(); | ||
options.addArguments("--headless=new"); | ||
options.addArguments("--use-fake-ui-for-media-stream"); | ||
|
||
webDriver = new ChromeDriver(options); | ||
webDriver.get(SPEECH_RECOGNITION_API); | ||
|
||
// we are ready to use the web api service | ||
|
||
} catch (Exception e) { | ||
log.warning("Failed to start Chrome web driver. Ensure Chrome is installed in default location"); | ||
log.warning("Error data", e); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Stops speech recognition. | ||
* No-op if it has not started via start() before. | ||
*/ | ||
public void stop() { | ||
try { | ||
if (webDriver != null) { | ||
webDriver.quit(); | ||
webDriver = null; | ||
} | ||
} catch (Exception e) { | ||
log.warning("Failed to quit web driver", e); | ||
} | ||
} | ||
|
||
/** | ||
* Add a [handler] for incoming speech input. | ||
*/ | ||
public void addInputHandler(Consumer<String> handler) { | ||
server.addMessageHandler(handler); | ||
} | ||
|
||
/** | ||
* Remove an existing input handler. | ||
*/ | ||
public void removeInputHandler(Consumer<String> handler) { | ||
server.removeMessageHandler(handler); | ||
} | ||
|
||
@Override | ||
public void onExit() { | ||
stop(); | ||
server.stop(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
fxgl-samples/src/main/java/sandbox/net/SpeechRecogSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* FXGL - JavaFX Game Library. The MIT License (MIT). | ||
* Copyright (c) AlmasB (almaslvl@gmail.com). | ||
* See LICENSE for details. | ||
*/ | ||
|
||
package sandbox.net; | ||
|
||
import com.almasb.fxgl.app.GameApplication; | ||
import com.almasb.fxgl.app.GameSettings; | ||
import com.almasb.fxgl.speechrecog.SpeechRecognitionService; | ||
import com.almasb.fxgl.ui.FontType; | ||
import javafx.scene.control.TextArea; | ||
|
||
import static com.almasb.fxgl.dsl.FXGL.*; | ||
|
||
/** | ||
* Shows how to use the speech recognition service. | ||
* | ||
* @author Almas Baim (https://github.com/AlmasB) | ||
*/ | ||
public class SpeechRecogSample extends GameApplication { | ||
|
||
private TextArea output; | ||
|
||
@Override | ||
protected void initSettings(GameSettings settings) { | ||
settings.addEngineService(SpeechRecognitionService.class); | ||
} | ||
|
||
@Override | ||
protected void initGame() { | ||
getService(SpeechRecognitionService.class).addInputHandler(input -> { | ||
getExecutor().startAsyncFX(() -> { | ||
output.appendText(input + "\n"); | ||
}); | ||
}); | ||
|
||
getService(SpeechRecognitionService.class).start(); | ||
} | ||
|
||
@Override | ||
protected void initUI() { | ||
output = new TextArea(); | ||
output.setWrapText(true); | ||
output.setPrefSize(getAppWidth(), getAppHeight()); | ||
output.setFont(getUIFactoryService().newFont(FontType.MONO, 18)); | ||
|
||
addUINode(output); | ||
} | ||
|
||
public static void main(String[] args) { | ||
launch(args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters