Skip to content
Merged
Show file tree
Hide file tree
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
54 changes: 52 additions & 2 deletions packages/core/useSpeechSynthesis/demo.vue
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
<script setup lang="ts">
import { useSpeechSynthesis } from '@vueuse/core'
import { ref as deepRef, onMounted, shallowRef } from 'vue'
import { computed, ref as deepRef, onMounted, shallowRef, watch } from 'vue'

const voice = deepRef<SpeechSynthesisVoice>(undefined as unknown as SpeechSynthesisVoice)
const text = shallowRef('Hello, everyone! Good morning!')
const pitch = shallowRef(1)
const rate = shallowRef(1)
const volume = shallowRef(1)

const boundaryStart = shallowRef(0)
const boundaryEnd = shallowRef(0)

const textSegments = computed(() => {
const fullText = text.value || ''
const startIndex = Math.max(0, Math.min(boundaryStart.value, fullText.length))
const endIndex = Math.max(startIndex, Math.min(boundaryEnd.value, fullText.length))
return {
leadingText: fullText.slice(0, startIndex),
highlightedText: fullText.slice(startIndex, endIndex),
trailingText: fullText.slice(endIndex),
}
})

const speech = useSpeechSynthesis(text, {
voice,
pitch,
rate,
volume,
onBoundary,
})

let synth: SpeechSynthesis
Expand All @@ -30,12 +45,34 @@ onMounted(() => {
}
})

function onBoundary(event: SpeechSynthesisEvent) {
const { charIndex, charLength } = event
const startIndex = charIndex
let endIndex = charIndex
if (typeof charLength === 'number' && charLength > 0) {
endIndex = startIndex + charLength
}
else {
const fullText = text.value || ''
const remainingText = fullText.slice(startIndex)
const firstWordMatch = remainingText.match(/^\S+/)
endIndex = startIndex + (firstWordMatch ? firstWordMatch[0].length : 0)
}
boundaryStart.value = startIndex
boundaryEnd.value = endIndex
}

function resetSpeakingText() {
boundaryStart.value = 0
boundaryEnd.value = 0
}

function play() {
if (speech.status.value === 'pause') {
console.log('resume')
window.speechSynthesis.resume()
}
else {
resetSpeakingText()
speech.speak()
}
}
Expand All @@ -46,7 +83,14 @@ function pause() {

function stop() {
speech.stop()
resetSpeakingText()
}

watch(() => speech.status.value, (s) => {
if (s === 'end') {
resetSpeakingText()
}
})
</script>

<template>
Expand All @@ -61,6 +105,12 @@ function stop() {
<div v-else>
<label class="font-bold mr-2">Spoken Text</label>
<input v-model="text" class="!inline-block" type="text">
<div class="mt-2" aria-label="current-boundary-preview">
<label class="font-bold mr-2">Speaking Text</label>
<span>{{ textSegments.leadingText }}</span>
<span class="text-primary">{{ textSegments.highlightedText }}</span>
<span>{{ textSegments.trailingText }}</span>
</div>

<br>
<label class="font-bold mr-2">Language</label>
Expand Down
9 changes: 9 additions & 0 deletions packages/core/useSpeechSynthesis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export interface UseSpeechSynthesisOptions extends ConfigurableWindow {
* @default 1
*/
volume?: MaybeRefOrGetter<SpeechSynthesisUtterance['volume']>
/**
* Callback function that is called when the boundary event is triggered.
*/
onBoundary?: (event: SpeechSynthesisEvent) => void
}

/**
Expand All @@ -53,6 +57,7 @@ export function useSpeechSynthesis(
rate = 1,
volume = 1,
window = defaultWindow,
onBoundary,
} = options

const synth = window && (window as any).speechSynthesis as SpeechSynthesis
Expand Down Expand Up @@ -99,6 +104,10 @@ export function useSpeechSynthesis(
utterance.onerror = (event) => {
error.value = event
}

utterance.onboundary = (event) => {
onBoundary?.(event)
}
}

const utterance = computed(() => {
Expand Down