Skip to content

Commit ee16cfe

Browse files
committed
Directly mute the transcript on the browser too
1 parent 5fd215f commit ee16cfe

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

common/src/main/kotlin/xyz/bluspring/unitytranslate/client/UnityTranslateClient.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ class UnityTranslateClient {
175175
var connectedServerHasSupport = false
176176

177177
var shouldTranscribe = true
178+
set(value) {
179+
field = value
180+
transcriber.setMuted(!value)
181+
}
182+
178183
var shouldRenderBoxes = true
179184

180185
val languageBoxes: MutableList<TranscriptBox>

common/src/main/kotlin/xyz/bluspring/unitytranslate/client/transcribers/SpeechTranscriber.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ abstract class SpeechTranscriber(var language: Language) {
1313
lateinit var updater: BiConsumer<Int, String>
1414

1515
abstract fun stop()
16+
open fun setMuted(muted: Boolean) {}
1617

1718
open fun changeLanguage(language: Language) {
1819
this.language = language

common/src/main/kotlin/xyz/bluspring/unitytranslate/client/transcribers/browser/BrowserSpeechTranscriber.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ class BrowserSpeechTranscriber(language: Language) : SpeechTranscriber(language)
7575
})
7676
}
7777

78+
override fun setMuted(muted: Boolean) {
79+
this.socket.broadcast("set_muted", JsonObject().apply {
80+
this.addProperty("muted", muted)
81+
})
82+
}
83+
7884
inner class BrowserSocket : WebSocketServer(InetSocketAddress("0.0.0.0", socketPort)) {
7985
var totalConnections = 0
8086

@@ -85,6 +91,7 @@ class BrowserSpeechTranscriber(language: Language) : SpeechTranscriber(language)
8591
totalConnections++
8692

8793
UnityTranslateClient.displayMessage(Component.translatable("unitytranslate.transcriber.connected"))
94+
setMuted(!UnityTranslateClient.shouldTranscribe)
8895
}
8996

9097
override fun onClose(ws: WebSocket, code: Int, reason: String, remote: Boolean) {

common/src/main/kotlin/xyz/bluspring/unitytranslate/commands/UnityTranslateClientCommands.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,19 @@ object UnityTranslateClientCommands {
4141
1
4242
}
4343

44+
val OPEN_BROWSER = ClientCommandRegistrationEvent.literal("openbrowser")
45+
.executes {
46+
if (UnityTranslateClient.transcriber is BrowserSpeechTranscriber) {
47+
(UnityTranslateClient.transcriber as BrowserSpeechTranscriber).openWebsite()
48+
}
49+
50+
1
51+
}
52+
4453
val ROOT = ClientCommandRegistrationEvent.literal("unitytranslateclient")
4554
.then(TRANSCRIBER)
4655
.then(INFO)
56+
.then(OPEN_BROWSER)
4757

4858
fun init() {
4959
ClientCommandRegistrationEvent.EVENT.register { dispatcher, ctx ->

common/src/main/resources/website/speech.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ let lastReset = 0;
1313
let totalResetsBelow50ms = 0;
1414
let isErrored = false;
1515
let wasNoSpeech = false;
16+
let isCurrentlyMuted = false;
1617

1718
ws.onopen = () => {
1819
console.log('Connected');
@@ -45,6 +46,10 @@ function setupTranscriber(lang) {
4546
return;
4647
}
4748

49+
if (isCurrentlyMuted) {
50+
return;
51+
}
52+
4853
console.log('Transcriber resetting...');
4954
ws.send(JSON.stringify({
5055
op: 'reset'
@@ -127,6 +132,20 @@ ws.onmessage = (ev) => {
127132

128133
break;
129134
}
135+
136+
case 'set_muted': {
137+
const isMuted = data.d.muted;
138+
isCurrentlyMuted = isMuted;
139+
140+
if (!isMuted && !!transcriber) {
141+
ws.send(JSON.stringify({
142+
op: 'reset'
143+
}));
144+
transcriber.start();
145+
} else if (!!transcriber) {
146+
transcriber.stop();
147+
}
148+
}
130149
}
131150
}
132151

0 commit comments

Comments
 (0)