Skip to content

Commit

Permalink
Change tts types on the fly instead of needing a restart
Browse files Browse the repository at this point in the history
  • Loading branch information
krypciak committed Oct 31, 2023
1 parent bf33dc8 commit 61c5c3c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const ttsHeader = 'crossedeyes-tts'
const ttsToogleId: string = `${ttsHeader}`
const ttsCharToogleId: string = `${ttsHeader}-chartts`
const ttsMenuToogleId: string = `${ttsHeader}-menutts`
const ttsTypeId: string = `${ttsHeader}-type`
export const ttsTypeId: string = `${ttsHeader}-type`
const ttsSpeedId: string = `${ttsHeader}-speed`
const ttsVoulmeId: string = `${ttsHeader}-volume`
const ttsPitchId: string = `${ttsHeader}-pitch`
Expand Down
36 changes: 25 additions & 11 deletions src/tts/tts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MenuOptions } from '../options'
import { MenuOptions, ttsTypeId } from '../options'
import { injectTextGathering } from './gather-text'
import { TTSScreenReader } from './tts-screen-reader'
import { TTSSpeechSynthesisAPI } from './tts-speech-synthesis'
Expand All @@ -20,6 +20,7 @@ export class TTS {
speakQueueId: number = 0

ttsInstance!: TTSInterface
lastOption: number = -1

clearQueue() {
this.speakQueue = {}
Expand All @@ -32,18 +33,31 @@ export class TTS {
(text: string) => this.ttsInstance && this.ttsInstance.speak(text),
() => { this.clearQueue() }
)
const self = this
sc.OptionModel.inject({
set(option: string, value: any) {
this.parent(option, value)
option == ttsTypeId && self.setup()
},
})
}
setup() {
if (this.lastOption != MenuOptions.ttsType) {
this.lastOption = MenuOptions.ttsType
switch (MenuOptions.ttsType) {
case TTSTypes['Built-in']:
this.ttsInstance = new TTSSpeechSynthesisAPI()
break
case TTSTypes['Screen reader']:
this.ttsInstance = new TTSScreenReader()
break
default: throw new Error()
}
this.ttsInstance.init(this.speakQueue, () => this.speakQueueId++)
}
}

async initPoststart() {
switch (MenuOptions.ttsType) {
case TTSTypes['Built-in']:
this.ttsInstance = new TTSSpeechSynthesisAPI()
break
case TTSTypes['Screen reader']:
this.ttsInstance = new TTSScreenReader()
break
default: throw new Error()
}
this.ttsInstance.init(this.speakQueue, () => this.speakQueueId++)
this.setup()
}
}

0 comments on commit 61c5c3c

Please sign in to comment.