Files
ko-grammar/docs/.vitepress/theme/components/TTS.vue
T
2026-03-25 07:35:30 +07:00

48 lines
980 B
Vue
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<span>
<span ref="main" :class="class" :lang="lang">
<slot />
</span>
<span>
<button type="button" @click="playAudio()">🎤︎︎</button>
</span>
</span>
</template>
<script setup lang="ts">
import { ref, useTemplateRef } from "vue";
import { speak, getVoices } from "tauri-plugin-tts-api";
const { lang = "ko" } = defineProps<{
class?: string;
lang?: string;
}>();
const voiceIds = ref<string[]>([]);
const mainEl = useTemplateRef("main");
async function playAudio() {
if (!mainEl.value) return;
if (!voiceIds.value.length) {
voiceIds.value = (await getVoices(lang)).map((v) => v.id);
if (!voiceIds.value.length) return;
}
const voiceId =
voiceIds.value[Math.floor(Math.random() * voiceIds.value.length)];
await speak({
text: mainEl.value.innerText,
language: null,
rate: null,
volume: null,
pitch: null,
queueMode: null,
voiceId,
});
}
</script>
<style module></style>