Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail nicely on browsers with no tts support #529

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions src/stores/alert.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useStorage } from '@vueuse/core'
import { defineStore } from 'pinia'
import { computed, reactive, watch } from 'vue'
import { computed, reactive, ref, watch } from 'vue'

import { Alert, AlertLevel } from '../types/alert'

Expand Down Expand Up @@ -49,29 +49,37 @@ export const useAlertStore = defineStore('alert', () => {
}

// Alert speech syntesis routine
const synth = window.speechSynthesis
const synth = ref(window.speechSynthesis)

// We need to cache these otherwise they get garbage collected...
let availableAlertSpeechVoiceNames
const utterance_cache: SpeechSynthesisUtterance[] = []
if (synth.value !== undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just return if undefined ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, that's better

synth.value.onvoiceschanged = () => {
synth.value.getVoices().forEach((voice) => {
availableAlertSpeechVoices.push(voice)
if (selectedAlertSpeechVoiceName.value === undefined && voice.default) {
selectedAlertSpeechVoiceName.value = voice.name
}
})
}

synth.onvoiceschanged = () => {
synth.getVoices().forEach((voice) => {
availableAlertSpeechVoices.push(voice)
if (selectedAlertSpeechVoiceName.value === undefined && voice.default) {
selectedAlertSpeechVoiceName.value = voice.name
}
})
availableAlertSpeechVoiceNames = computed(() =>
availableAlertSpeechVoices.map((v) => ({ value: v.name, name: `${v.name} (${v.lang})` }))
)
} else {
availableAlertSpeechVoiceNames = computed(() => [])
}

const availableAlertSpeechVoiceNames = computed(() =>
availableAlertSpeechVoices.map((v) => ({ value: v.name, name: `${v.name} (${v.lang})` }))
)

/**
* Speaks a text out loud using the browsers TTS engine
* @param {string} text string
*/
function speak(text: string): void {
if (!synth.value) {
console.warn('No speechSynthesis available')
return
}
const utterance = new SpeechSynthesisUtterance(text)
const voice = availableAlertSpeechVoices.find((v) => v.name === selectedAlertSpeechVoiceName.value)
if (voice) {
Expand All @@ -85,7 +93,7 @@ export const useAlertStore = defineStore('alert', () => {
utterance.onerror = function (event) {
console.error(`SpeechSynthesisUtterance error: ${event.error}`)
}
synth.speak(utterance)
synth.value.speak(utterance)
}

watch(alerts, () => {
Expand Down