Skip to content

Commit

Permalink
fix: compatible with Firefox
Browse files Browse the repository at this point in the history
  • Loading branch information
weaigc committed Jul 31, 2023
1 parent ed60a44 commit c1a400d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
20 changes: 12 additions & 8 deletions src/lib/bots/bing/sr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ const SpeechRecognitionPolyfill: typeof webkitSpeechRecognition = typeof window
type subscriber = (msg: string, command?: string) => void

export class SR {
recognition: SpeechRecognition
recognition?: SpeechRecognition
onchange?: subscriber
transcript: boolean = false
listening: boolean = false
private commandsRe?: RegExp
constructor(commands: string[]) {
this.recognition = new SpeechRecognitionPolyfill()
this.recognition = SpeechRecognitionPolyfill ? new SpeechRecognitionPolyfill() : undefined
if (!this.recognition) {
return
}
this.configuration('zh-CN')
if (commands.length) {
this.commandsRe = new RegExp(`^(${commands.join('|')})。?$`)
Expand All @@ -31,10 +34,9 @@ export class SR {
this.stop()
}
this.recognition.onend = () => {
if (this.listening) {
if (this.recognition && this.listening) {
this.recognition.start()
}
console.log('end')
}
}

Expand All @@ -54,11 +56,13 @@ export class SR {
}
}

configuration = async (lang: string = 'zh-CN') => {
private configuration = async (lang: string = 'zh-CN') => {
return new Promise((resolve) => {
this.recognition.continuous = true
this.recognition.lang = lang
this.recognition.onstart = resolve
if (this.recognition) {
this.recognition.continuous = true
this.recognition.lang = lang
this.recognition.onstart = resolve
}
})
}

Expand Down
11 changes: 8 additions & 3 deletions src/lib/bots/bing/tts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export class TTS {
private controller = new AbortController()
speaking = false
get isSpeaking() {
console.log('get speak', this.speaking)
return this.speaking
}
finished = false
Expand All @@ -29,15 +28,21 @@ export class TTS {
if (!synth || text?.trim()?.length < 2) {
return
}
this.currentText = text.replace(/[^\u4e00-\u9fa5_a-zA-Z0-9,。?,:\.,:]+/g, '')
this.currentText = text.replace(/[^\u4e00-\u9fa5_a-zA-Z0-9,。?,:\.,:]+/g, '')
this.finished = false
this.loop()
}

private async doSpeek() {
return new Promise((resolve) => {
const endIndex = this.finished ? this.currentText.length :
Math.max(this.currentText.lastIndexOf('。'), this.currentText.lastIndexOf('?'), this.currentText.lastIndexOf('\n'))
Math.max(
this.currentText.lastIndexOf('。'),
this.currentText.lastIndexOf(';'),
this.currentText.lastIndexOf('、'),
this.currentText.lastIndexOf('?'),
this.currentText.lastIndexOf('\n')
)
const startIndex = this.speakText.length ? Math.max(0, this.currentText.lastIndexOf(this.speakText) + this.speakText.length) : 0

if (startIndex >= endIndex) {
Expand Down

0 comments on commit c1a400d

Please sign in to comment.