Files
sionrui/frontend/app/web-gold/src/components/VoiceSelector.vue

316 lines
7.6 KiB
Vue
Raw Normal View History

<template>
<div class="voice-selector">
2026-02-02 23:16:38 +08:00
<div v-if="userVoiceCards.length === 0" class="empty-voices">
2026-02-11 00:41:25 +08:00
<a-empty :image="simpleImage" description="还没有配音">
</a-empty>
</div>
<div v-else class="voice-selector-with-preview">
<a-select
v-model:value="selectedVoiceId"
placeholder="请选择音色"
class="voice-select"
:options="voiceOptions"
@change="handleVoiceChange"
style="width: calc(100% - 80px)"
/>
<a-button
class="preview-button"
size="small"
:disabled="!selectedVoiceId"
:loading="previewLoadingVoiceId === selectedVoiceId"
2026-02-02 23:16:38 +08:00
@click="handleSynthesize"
>
<template #icon>
<SoundOutlined />
</template>
2026-02-02 23:16:38 +08:00
合成
</a-button>
</div>
2026-02-02 22:36:20 +08:00
<!-- APlayer 播放器容器 -->
2026-02-24 01:11:04 +08:00
<div v-show="audioUrl" ref="playerContainer" class="aplayer-container"></div>
2026-02-02 23:16:38 +08:00
<!-- 下载按钮 -->
<a-button
2026-02-24 01:11:04 +08:00
v-show="audioUrl"
2026-02-02 23:16:38 +08:00
type="link"
size="small"
@click="downloadAudio"
class="download-link"
>
下载音频
</a-button>
</div>
</template>
<script setup>
2026-02-02 23:16:38 +08:00
import { ref, computed, onMounted, onBeforeUnmount, nextTick, watch } from 'vue'
2026-02-11 00:41:25 +08:00
import { Empty } from 'ant-design-vue'
2026-02-24 01:11:04 +08:00
import { SoundOutlined } from '@ant-design/icons-vue'
import { useVoiceCopyStore } from '@/stores/voiceCopy'
import { useTTS, TTS_PROVIDERS } from '@/composables/useTTS'
2026-02-02 22:36:20 +08:00
import APlayer from 'aplayer'
2026-02-02 23:16:38 +08:00
const props = defineProps({
synthText: {
type: String,
default: ''
},
speechRate: {
type: Number,
default: 1.0
}
})
2026-02-11 00:41:25 +08:00
const simpleImage = Empty.PRESENTED_IMAGE_SIMPLE
const voiceStore = useVoiceCopyStore()
const emit = defineEmits(['select'])
2026-02-02 22:36:20 +08:00
let player = null
const playerContainer = ref(null)
const audioUrl = ref('')
const currentVoiceName = ref('')
2026-02-02 22:42:15 +08:00
// 默认封面图片(音频波形图标)
const defaultCover = `data:image/svg+xml;base64,${btoa(`
<svg width="100" height="100" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<rect width="100" height="100" fill="#1f2937" rx="8"/>
<g fill="#60a5fa">
<rect x="20" y="35" width="4" height="30" rx="2">
<animate attributeName="height" values="30;20;30" dur="0.8s" repeatCount="indefinite"/>
<animate attributeName="y" values="35;40;35" dur="0.8s" repeatCount="indefinite"/>
</rect>
<rect x="30" y="30" width="4" height="40" rx="2">
<animate attributeName="height" values="40;25;40" dur="0.6s" repeatCount="indefinite"/>
<animate attributeName="y" values="30;37.5;30" dur="0.6s" repeatCount="indefinite"/>
</rect>
<rect x="40" y="25" width="4" height="50" rx="2">
<animate attributeName="height" values="50;30;50" dur="0.7s" repeatCount="indefinite"/>
<animate attributeName="y" values="25;35;25" dur="0.7s" repeatCount="indefinite"/>
</rect>
<rect x="50" y="28" width="4" height="44" rx="2">
<animate attributeName="height" values="44;28;44" dur="0.9s" repeatCount="indefinite"/>
<animate attributeName="y" values="28;36;28" dur="0.9s" repeatCount="indefinite"/>
</rect>
<rect x="60" y="32" width="4" height="36" rx="2">
<animate attributeName="height" values="36;22;36" dur="0.5s" repeatCount="indefinite"/>
<animate attributeName="y" values="32;39;32" dur="0.5s" repeatCount="indefinite"/>
</rect>
<rect x="70" y="38" width="4" height="24" rx="2">
<animate attributeName="height" values="24;15;24" dur="0.7s" repeatCount="indefinite"/>
<animate attributeName="y" values="38;42.5;38" dur="0.7s" repeatCount="indefinite"/>
</rect>
</g>
<circle cx="50" cy="50" r="18" fill="none" stroke="#60a5fa" stroke-width="2" opacity="0.3"/>
<path d="M44 44 L44 56 L56 50 Z" fill="#60a5fa" opacity="0.5"/>
</svg>
`.trim())}`
2026-02-02 22:36:20 +08:00
// 使用TTS Hook
const {
previewLoadingVoiceId,
playVoiceSample,
setText,
2026-02-02 23:16:38 +08:00
setSpeechRate
} = useTTS({
2026-02-01 21:11:29 +08:00
provider: TTS_PROVIDERS.SILICONFLOW
})
const selectedVoiceId = ref('')
const userVoiceCards = computed(() =>
(voiceStore.profiles || []).map(profile => ({
id: `user-${profile.id}`,
rawId: profile.id,
name: profile.name || '未命名',
2026-02-02 23:16:38 +08:00
category: '',
gender: profile.gender || 'female',
description: profile.note || '我的配音',
fileUrl: profile.fileUrl,
transcription: profile.transcription || '',
source: 'user',
voiceId: profile.voiceId
}))
)
const voiceOptions = computed(() =>
2026-02-02 23:16:38 +08:00
userVoiceCards.value.map(voice => ({
value: voice.id,
label: voice.name,
2026-02-02 23:16:38 +08:00
data: voice
}))
)
const handleVoiceChange = (value, option) => {
const voice = option.data
selectedVoiceId.value = value
2026-02-02 22:36:20 +08:00
currentVoiceName.value = voice.name
emit('select', voice)
}
2026-02-02 23:16:38 +08:00
const handleSynthesize = () => {
if (!selectedVoiceId.value) return
2026-02-02 23:16:38 +08:00
const voice = userVoiceCards.value.find(v => v.id === selectedVoiceId.value)
if (!voice) return
2026-02-02 22:36:20 +08:00
currentVoiceName.value = voice.name
handlePlayVoiceSample(voice)
}
2026-02-02 23:16:38 +08:00
// 监听 prop 变化,更新 TTS 参数
watch(() => props.synthText, (newText) => {
setText(newText || '')
}, { immediate: true })
watch(() => props.speechRate, (newRate) => {
setSpeechRate(newRate)
}, { immediate: true })
/**
2026-02-02 23:16:38 +08:00
* 处理音色
*/
const handlePlayVoiceSample = (voice) => {
2026-02-02 22:36:20 +08:00
currentVoiceName.value = voice.name
playVoiceSample(
voice,
2026-02-02 22:36:20 +08:00
(data) => {
const url = data.audioUrl || data.objectUrl
if (!url) {
console.error('无效的音频数据格式', data)
return
}
initPlayer(url)
},
(error) => {
console.error('音频播放失败', error)
2026-02-24 21:41:05 +08:00
},
{ autoPlay: false } // 禁用自动播放,由 APlayer 控制
)
}
2026-02-02 22:36:20 +08:00
/**
* 初始化 APlayer
*/
const initPlayer = (url) => {
destroyPlayer()
audioUrl.value = url
nextTick(() => {
player = new APlayer({
container: playerContainer.value,
autoplay: true,
theme: '#3b82f6',
volume: 0.7,
2026-02-24 01:11:04 +08:00
loop: 'none',
2026-02-02 22:36:20 +08:00
audio: [{
2026-02-02 23:16:38 +08:00
name: currentVoiceName.value || '语音合成',
artist: '合成',
2026-02-02 22:36:20 +08:00
url: url,
cover: defaultCover
2026-02-02 23:16:38 +08:00
}]
2026-02-02 22:36:20 +08:00
})
2026-02-24 01:11:04 +08:00
player.on('ended', () => {
player.seek(0)
})
2026-02-02 22:36:20 +08:00
player.on('error', (e) => {
console.error('APlayer 播放错误:', e)
})
})
}
2026-02-02 23:16:38 +08:00
/**
* 下载音频
*/
const downloadAudio = () => {
if (!audioUrl.value) return
2026-02-24 01:11:04 +08:00
const filename = `${currentVoiceName.value || '语音合成'}.mp3`
2026-02-02 23:16:38 +08:00
const link = document.createElement('a')
link.href = audioUrl.value
2026-02-24 01:11:04 +08:00
link.download = filename
2026-02-02 23:16:38 +08:00
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
}
2026-02-02 22:36:20 +08:00
/**
* 销毁播放器
*/
const destroyPlayer = () => {
if (player) {
try {
player.destroy()
} catch (e) {
console.error('销毁播放器失败:', e)
}
player = null
}
2026-02-24 01:11:04 +08:00
// 只对 blob URL 调用 revokeObjectURL
if (audioUrl.value && audioUrl.value.startsWith('blob:')) {
2026-02-02 22:36:20 +08:00
URL.revokeObjectURL(audioUrl.value)
}
2026-02-24 01:11:04 +08:00
audioUrl.value = ''
2026-02-02 22:36:20 +08:00
}
2026-02-02 23:16:38 +08:00
defineExpose({})
onMounted(async () => {
await voiceStore.refresh()
})
2026-02-02 22:36:20 +08:00
onBeforeUnmount(() => {
destroyPlayer()
})
</script>
<style scoped>
.voice-selector {
width: 100%;
}
.empty-voices {
2026-02-11 00:41:25 +08:00
padding: 16px 0;
background: var(--color-surface);
border: 1px dashed var(--color-border);
border-radius: var(--radius-card);
}
/* 音色选择器和试听按钮的容器 */
.voice-selector-with-preview {
display: flex;
gap: 8px;
align-items: center;
width: 100%;
}
/* 下拉框样式 */
.voice-select {
flex: 1;
}
/* 试听按钮样式 */
.preview-button {
height: 32px;
white-space: nowrap;
}
2026-02-02 22:36:20 +08:00
/* APlayer 容器样式 */
.aplayer-container {
margin-top: 12px;
}
2026-02-02 23:16:38 +08:00
/* 下载链接样式 */
.download-link {
margin-top: 8px;
padding: 0;
font-size: 12px;
}
</style>