tts add words filter

This commit is contained in:
Megghy
2023-12-19 10:33:39 +08:00
parent d6189537c3
commit 89911c7ad2

View File

@@ -12,6 +12,7 @@ import { Mic24Filled } from '@vicons/fluent'
import { copyToClipboard } from '@/Utils' import { copyToClipboard } from '@/Utils'
import { QueryGetAPI, QueryPostAPI } from '@/api/query' import { QueryGetAPI, QueryPostAPI } from '@/api/query'
import { VTSURU_API_URL } from '@/data/constants' import { VTSURU_API_URL } from '@/data/constants'
import { setInterval, clearInterval } from 'worker-timers'
const props = defineProps<{ const props = defineProps<{
client: DanmakuClient client: DanmakuClient
@@ -157,7 +158,13 @@ async function speak() {
} }
} }
function speakDirect(text: string) { function speakDirect(text: string) {
try {
const synth = window.speechSynthesis const synth = window.speechSynthesis
if (!synth) {
console.error('当前浏览器环境不支持tts')
return
}
synth.cancel()
let u = new SpeechSynthesisUtterance() let u = new SpeechSynthesisUtterance()
u.text = text u.text = text
let voices = synth.getVoices() let voices = synth.getVoices()
@@ -177,8 +184,12 @@ function speakDirect(text: string) {
} }
console.log(err) console.log(err)
message.error('无法播放语音: ' + err.error) message.error('无法播放语音: ' + err.error)
isSpeaking.value = false
} }
} }
} catch (err) {
console.log(err)
}
} }
function onGetEvent(data: EventModel) { function onGetEvent(data: EventModel) {
if (!canSpeech.value) { if (!canSpeech.value) {
@@ -237,6 +248,7 @@ function getTextFromDanmaku(data: EventModel | undefined) {
.replace(templateConstants.message.regex, data.msg) .replace(templateConstants.message.regex, data.msg)
.replace(templateConstants.guard_level.regex, data.guard_level == 1 ? '总督' : data.guard_level == 2 ? '提督' : data.guard_level == 3 ? '舰长' : '') .replace(templateConstants.guard_level.regex, data.guard_level == 1 ? '总督' : data.guard_level == 2 ? '提督' : data.guard_level == 3 ? '舰长' : '')
.replace(templateConstants.fans_medal_level.regex, data.fans_medal_level.toString()) .replace(templateConstants.fans_medal_level.regex, data.fans_medal_level.toString())
.trim()
if (data.type === EventDataTypes.Message) { if (data.type === EventDataTypes.Message) {
text = text.replace(/\[.*?\]/g, ' ') //删除表情 text = text.replace(/\[.*?\]/g, ' ') //删除表情
@@ -245,6 +257,7 @@ function getTextFromDanmaku(data: EventModel | undefined) {
} else if (data.type === EventDataTypes.Guard) { } else if (data.type === EventDataTypes.Guard) {
text = text.replace(templateConstants.guard_num.regex, data.num.toString()) text = text.replace(templateConstants.guard_num.regex, data.num.toString())
} }
text = text.replace(/[^0-9a-z\u4E00-\u9FFF\u3400-\u4DBF\uF900-\uFAFF ]/gi, '').normalize('NFKC') //过滤无效字符, 全角转半角
return text return text
} }
function startSpeech() { function startSpeech() {
@@ -366,12 +379,12 @@ function test(type: EventDataTypes) {
} }
} }
let speechQueueTimer: any let speechQueueTimer: number
onMounted(() => { onMounted(() => {
speechSynthesisInfo.value = EasySpeech.detect() speechSynthesisInfo.value = EasySpeech.detect()
speechQueueTimer = setInterval(() => { speechQueueTimer = setInterval(() => {
speak() speak()
}, 100) }, 500)
props.client.onEvent('danmaku', onGetEvent) props.client.onEvent('danmaku', onGetEvent)
props.client.onEvent('sc', onGetEvent) props.client.onEvent('sc', onGetEvent)