mirror of
https://github.com/Megghy/vtsuru.live.git
synced 2025-12-06 18:36:55 +08:00
tts add words filter
This commit is contained in:
@@ -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,27 +158,37 @@ async function speak() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
function speakDirect(text: string) {
|
function speakDirect(text: string) {
|
||||||
const synth = window.speechSynthesis
|
try {
|
||||||
let u = new SpeechSynthesisUtterance()
|
const synth = window.speechSynthesis
|
||||||
u.text = text
|
if (!synth) {
|
||||||
let voices = synth.getVoices()
|
console.error('当前浏览器环境不支持tts')
|
||||||
const voice = voices.find((v) => v.name === settings.value.speechInfo.voice)
|
return
|
||||||
if (voice) {
|
|
||||||
u.voice = voice
|
|
||||||
u.volume = settings.value.speechInfo.volume
|
|
||||||
u.rate = settings.value.speechInfo.rate
|
|
||||||
u.pitch = settings.value.speechInfo.pitch
|
|
||||||
synth.speak(u)
|
|
||||||
u.onend = () => {
|
|
||||||
isSpeaking.value = false
|
|
||||||
}
|
}
|
||||||
u.onerror = (err) => {
|
synth.cancel()
|
||||||
if (err.error == 'interrupted') {
|
let u = new SpeechSynthesisUtterance()
|
||||||
return
|
u.text = text
|
||||||
|
let voices = synth.getVoices()
|
||||||
|
const voice = voices.find((v) => v.name === settings.value.speechInfo.voice)
|
||||||
|
if (voice) {
|
||||||
|
u.voice = voice
|
||||||
|
u.volume = settings.value.speechInfo.volume
|
||||||
|
u.rate = settings.value.speechInfo.rate
|
||||||
|
u.pitch = settings.value.speechInfo.pitch
|
||||||
|
synth.speak(u)
|
||||||
|
u.onend = () => {
|
||||||
|
isSpeaking.value = false
|
||||||
|
}
|
||||||
|
u.onerror = (err) => {
|
||||||
|
if (err.error == 'interrupted') {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
console.log(err)
|
||||||
|
message.error('无法播放语音: ' + err.error)
|
||||||
|
isSpeaking.value = false
|
||||||
}
|
}
|
||||||
console.log(err)
|
|
||||||
message.error('无法播放语音: ' + err.error)
|
|
||||||
}
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function onGetEvent(data: EventModel) {
|
function onGetEvent(data: EventModel) {
|
||||||
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user