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

633 lines
15 KiB
Vue
Raw Normal View History

<template>
<div class="voice-selector">
2026-03-08 13:55:10 +08:00
<!-- 空状态 -->
2026-02-02 23:16:38 +08:00
<div v-if="userVoiceCards.length === 0" class="empty-voices">
<div class="empty-icon">
<Icon icon="lucide:mic-off" class="size-10 text-muted-foreground/40" />
</div>
<p class="text-muted-foreground mb-4">还没有配音</p>
<Button @click="$router.push('/voice-copy')">
去创建配音
</Button>
</div>
2026-03-05 21:01:34 +08:00
<div v-else class="voice-selector-wrapper">
2026-03-08 13:55:10 +08:00
<!-- 标题栏 -->
<div class="selector-header">
<div class="header-left">
<span class="header-title">选择音色</span>
<span class="voice-count">{{ userVoiceCards.length }} 个配音</span>
2026-03-05 21:01:34 +08:00
</div>
<Button
2026-03-08 13:55:10 +08:00
v-if="selectedVoiceId"
class="synthesize-btn"
:disabled="isPlayerInitializing"
:loading="previewLoadingVoiceId === selectedVoiceId"
@click="handleSynthesize"
>
<Icon icon="lucide:volume-2" class="size-4" />
2026-03-08 13:55:10 +08:00
合成试听
</Button>
2026-03-08 13:55:10 +08:00
</div>
2026-03-05 21:01:34 +08:00
2026-03-08 13:55:10 +08:00
<!-- 卡片网格 -->
<div class="voice-grid" :class="{ 'has-many': userVoiceCards.length > 4 }">
<button
2026-03-08 13:55:10 +08:00
v-for="voice in userVoiceCards"
:key="voice.id"
class="voice-card"
:class="{ 'selected': selectedVoiceId === voice.id }"
@click="handleVoiceSelect(voice)"
>
<!-- 头像区域 -->
<div class="card-avatar">
<div class="avatar-ring"></div>
<div class="avatar-icon">
<Icon icon="lucide:audio-waveform" class="size-6" />
2026-03-08 13:55:10 +08:00
</div>
<!-- 选中指示器 -->
<div v-if="selectedVoiceId === voice.id" class="selected-indicator">
<Icon icon="lucide:check" class="size-3" />
2026-03-08 13:55:10 +08:00
</div>
2026-03-05 21:01:34 +08:00
</div>
2026-03-08 13:55:10 +08:00
<!-- 信息区域 -->
<div class="card-info">
<div class="voice-name">{{ voice.name }}</div>
<div class="voice-desc">{{ voice.description || '我的配音' }}</div>
</div>
</button>
2026-03-05 21:01:34 +08:00
</div>
<!-- 播放器区域 -->
<transition name="slide-fade">
<div v-if="audioUrl" class="player-section">
2026-03-08 13:55:10 +08:00
<div class="player-header">
<div class="player-info">
<div class="player-icon">
<Icon icon="lucide:play-circle" class="size-8" />
2026-03-08 13:55:10 +08:00
</div>
<div class="player-meta">
<div class="player-title">{{ currentVoiceName }}</div>
<div class="player-label">合成预览</div>
</div>
</div>
<Button variant="ghost" size="sm" @click="downloadAudio" class="download-btn">
<Icon icon="lucide:download" class="size-4" />
2026-03-08 13:55:10 +08:00
下载
</Button>
2026-03-05 21:01:34 +08:00
</div>
2026-03-08 13:55:10 +08:00
<div ref="playerContainer" class="aplayer-container"></div>
2026-03-05 21:01:34 +08:00
</div>
</transition>
</div>
</div>
</template>
<script setup>
2026-02-02 23:16:38 +08:00
import { ref, computed, onMounted, onBeforeUnmount, nextTick, watch } from 'vue'
import { toast } from 'vue-sonner'
import { Icon } from '@iconify/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'
import { Button } from '@/components/ui/button'
2026-02-02 23:16:38 +08:00
const props = defineProps({
synthText: {
type: String,
default: '',
2026-02-02 23:16:38 +08:00
},
speechRate: {
type: Number,
default: 1.0,
},
2026-02-02 23:16:38 +08:00
})
const voiceStore = useVoiceCopyStore()
2026-03-05 22:58:31 +08:00
const emit = defineEmits(['select', 'audioGenerated'])
2026-02-02 22:36:20 +08:00
let player = null
const playerContainer = ref(null)
const audioUrl = ref('')
const currentVoiceName = ref('')
2026-02-26 18:52:09 +08:00
const isPlayerInitializing = ref(false)
2026-02-02 22:36:20 +08:00
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,
setSpeechRate,
} = useTTS({
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,
})),
)
2026-03-08 13:55:10 +08:00
const handleVoiceSelect = (voice) => {
selectedVoiceId.value = voice.id
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 = () => {
2026-03-05 22:58:31 +08:00
if (!selectedVoiceId.value || isPlayerInitializing.value) return
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 },
)
2026-02-02 23:16:38 +08:00
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
2026-02-26 18:52:09 +08:00
if (!url) return
2026-02-02 22:36:20 +08:00
initPlayer(url)
},
() => {
/* 错误已在 useTTS 中处理 */
},
{ autoPlay: false },
)
}
2026-02-02 22:36:20 +08:00
/**
* 初始化 APlayer
*/
const initPlayer = (url) => {
2026-02-26 18:52:09 +08:00
// 防止并发初始化
if (isPlayerInitializing.value) {
return
}
isPlayerInitializing.value = true
2026-02-02 22:36:20 +08:00
destroyPlayer()
audioUrl.value = url
nextTick(() => {
2026-02-26 18:52:09 +08:00
try {
2026-03-02 01:28:46 +08:00
// 检查容器是否存在
if (!playerContainer.value) {
toast.error('播放器容器未就绪')
2026-03-02 01:28:46 +08:00
isPlayerInitializing.value = false
audioUrl.value = ''
return
}
2026-02-26 18:52:09 +08:00
player = new APlayer({
container: playerContainer.value,
autoplay: true,
theme: '#3b82f6',
volume: 0.7,
loop: 'none',
audio: [
{
name: currentVoiceName.value || '语音合成',
artist: '合成',
url: url,
cover: defaultCover,
},
],
2026-02-26 18:52:09 +08:00
})
player.on('ended', () => {
player.seek(0)
})
player.on('error', (e) => {
console.error('APlayer 播放错误:', e)
})
player.on('canplay', () => {
isPlayerInitializing.value = false
2026-03-05 23:21:00 +08:00
// 发送音频时长和 URL 给父组件
2026-03-05 22:58:31 +08:00
const durationMs = Math.floor(player.audio.duration * 1000)
if (durationMs > 0) {
emit('audioGenerated', {
durationMs,
audioUrl: audioUrl.value, // 使用 URL性能优化
2026-03-05 22:58:31 +08:00
})
}
2026-02-26 18:52:09 +08:00
})
} catch (e) {
console.error('APlayer 初始化失败:', e)
toast.error('播放器初始化失败,请重试')
2026-02-26 18:52:09 +08:00
isPlayerInitializing.value = false
2026-03-02 01:28:46 +08:00
audioUrl.value = ''
2026-02-26 18:52:09 +08:00
}
2026-02-02 22:36:20 +08:00
})
}
2026-02-02 23:16:38 +08:00
const downloadAudio = () => {
if (!audioUrl.value) return
const link = document.createElement('a')
link.href = audioUrl.value
2026-03-05 22:58:31 +08:00
link.download = `${currentVoiceName.value || '语音合成'}.mp3`
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 = () => {
2026-02-26 18:52:09 +08:00
isPlayerInitializing.value = false
2026-02-02 22:36:20 +08:00
if (player) {
try {
2026-02-26 18:52:09 +08:00
player.pause()
2026-02-02 22:36:20 +08:00
player.destroy()
} catch (e) {
console.error('销毁播放器失败:', e)
}
player = null
}
2026-03-05 22:58:31 +08:00
if (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
}
onMounted(async () => {
await voiceStore.refresh()
})
2026-02-02 22:36:20 +08:00
onBeforeUnmount(() => {
destroyPlayer()
})
</script>
2026-03-05 21:01:34 +08:00
<style scoped lang="less">
.voice-selector {
width: 100%;
}
2026-03-08 13:55:10 +08:00
/* 空状态 */
.empty-voices {
2026-03-16 23:54:01 +08:00
padding: var(--space-10) var(--space-6);
background: var(--muted);
border: 1px dashed var(--border);
border-radius: var(--radius-lg);
2026-03-08 13:55:10 +08:00
text-align: center;
.empty-icon {
2026-03-16 23:54:01 +08:00
margin-bottom: var(--space-3);
2026-03-08 13:55:10 +08:00
}
2026-03-05 21:01:34 +08:00
}
/* 主容器 */
.voice-selector-wrapper {
display: flex;
flex-direction: column;
2026-03-16 23:54:01 +08:00
gap: var(--space-4);
2026-03-05 21:01:34 +08:00
}
2026-03-08 13:55:10 +08:00
/* 标题栏 */
.selector-header {
display: flex;
align-items: center;
2026-03-08 13:55:10 +08:00
justify-content: space-between;
2026-03-16 23:54:01 +08:00
padding-bottom: var(--space-3);
border-bottom: 1px solid var(--border);
2026-03-05 21:01:34 +08:00
}
2026-03-08 13:55:10 +08:00
.header-left {
2026-03-05 21:01:34 +08:00
display: flex;
align-items: center;
2026-03-16 23:54:01 +08:00
gap: var(--space-2-5);
2026-03-05 21:01:34 +08:00
}
.header-title {
2026-03-16 23:54:01 +08:00
font-size: var(--font-size-md);
2026-03-08 13:55:10 +08:00
font-weight: 600;
2026-03-16 23:54:01 +08:00
color: var(--foreground);
}
2026-03-08 13:55:10 +08:00
.voice-count {
2026-03-16 23:54:01 +08:00
font-size: var(--font-size-xs);
color: var(--muted-foreground);
background: var(--muted);
padding: var(--space-0-5) var(--space-2);
border-radius: var(--radius);
2026-03-05 21:01:34 +08:00
}
2026-03-08 13:55:10 +08:00
.synthesize-btn {
2026-03-17 23:41:49 +08:00
height: 36px;
padding: var(--space-2) var(--space-4);
2026-03-16 23:54:01 +08:00
border-radius: var(--radius);
2026-03-08 13:55:10 +08:00
border: none;
2026-03-16 23:54:01 +08:00
background: var(--primary);
color: var(--primary-foreground);
2026-03-08 13:55:10 +08:00
font-weight: 500;
2026-03-16 23:54:01 +08:00
font-size: var(--font-size-sm);
box-shadow: var(--shadow-sm);
transition: all var(--duration-fast);
2026-03-05 21:01:34 +08:00
2026-03-08 13:55:10 +08:00
&:hover:not(:disabled) {
transform: translateY(-1px);
2026-03-16 23:54:01 +08:00
box-shadow: var(--shadow-md);
2026-03-08 13:55:10 +08:00
}
2026-03-05 21:01:34 +08:00
2026-03-08 13:55:10 +08:00
&:disabled {
2026-03-16 23:54:01 +08:00
background: var(--muted-foreground);
2026-03-08 13:55:10 +08:00
box-shadow: none;
cursor: not-allowed;
transform: none;
}
}
2026-03-05 21:01:34 +08:00
2026-03-08 13:55:10 +08:00
/* 卡片网格 */
.voice-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
2026-03-17 23:41:49 +08:00
gap: 16px;
2026-03-05 21:01:34 +08:00
2026-03-08 13:55:10 +08:00
&.has-many {
max-height: 280px;
overflow-y: auto;
padding-right: 4px;
2026-03-05 21:01:34 +08:00
2026-03-08 13:55:10 +08:00
&::-webkit-scrollbar {
width: 4px;
2026-03-05 21:01:34 +08:00
}
2026-03-08 13:55:10 +08:00
&::-webkit-scrollbar-track {
background: #f1f5f9;
border-radius: 2px;
2026-03-05 21:01:34 +08:00
}
2026-03-08 13:55:10 +08:00
&::-webkit-scrollbar-thumb {
background: #cbd5e1;
border-radius: 2px;
2026-03-05 21:01:34 +08:00
}
}
}
2026-03-08 13:55:10 +08:00
/* 音色卡片 */
.voice-card {
position: relative;
2026-03-16 23:54:01 +08:00
background: var(--card);
border: 2px solid var(--muted);
border-radius: var(--radius-lg);
padding: var(--space-4) var(--space-3);
2026-03-08 13:55:10 +08:00
cursor: pointer;
2026-03-16 23:54:01 +08:00
transition: all var(--duration-base);
2026-03-08 13:55:10 +08:00
overflow: hidden;
text-align: left;
2026-03-08 13:55:10 +08:00
&:hover {
2026-03-16 23:54:01 +08:00
border-color: var(--border);
2026-03-08 13:55:10 +08:00
transform: translateY(-2px);
2026-03-16 23:54:01 +08:00
box-shadow: var(--shadow-lg);
2026-03-08 13:55:10 +08:00
}
&.selected {
2026-03-16 23:54:01 +08:00
border-color: var(--primary);
background: oklch(from var(--primary) l c h / 0.1);
box-shadow: var(--shadow-md);
2026-03-08 13:55:10 +08:00
}
2026-03-05 21:01:34 +08:00
}
2026-03-08 13:55:10 +08:00
/* 头像区域 */
.card-avatar {
position: relative;
width: 48px;
height: 48px;
2026-03-16 23:54:01 +08:00
margin: 0 auto var(--space-2-5);
2026-03-08 13:55:10 +08:00
border-radius: 50%;
2026-03-05 21:01:34 +08:00
display: flex;
align-items: center;
2026-03-08 13:55:10 +08:00
justify-content: center;
2026-03-05 21:01:34 +08:00
2026-03-08 13:55:10 +08:00
.avatar-ring {
2026-03-16 23:54:01 +08:00
background: oklch(from var(--primary) l c h / 0.2);
2026-03-05 21:01:34 +08:00
}
2026-03-08 13:55:10 +08:00
.avatar-icon {
2026-03-16 23:54:01 +08:00
color: var(--primary);
2026-03-05 21:01:34 +08:00
}
2026-03-08 13:55:10 +08:00
}
2026-03-05 21:01:34 +08:00
2026-03-08 13:55:10 +08:00
.avatar-ring {
position: absolute;
inset: 0;
border-radius: 50%;
}
2026-03-05 21:01:34 +08:00
2026-03-08 13:55:10 +08:00
.avatar-icon {
position: relative;
z-index: 1;
width: 28px;
height: 28px;
display: flex;
align-items: center;
justify-content: center;
2026-03-08 13:55:10 +08:00
}
2026-03-05 21:01:34 +08:00
2026-03-08 13:55:10 +08:00
.selected-indicator {
position: absolute;
top: -4px;
right: -4px;
width: 20px;
height: 20px;
2026-03-16 23:54:01 +08:00
background: var(--primary);
2026-03-08 13:55:10 +08:00
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
2026-03-16 23:54:01 +08:00
color: var(--primary-foreground);
font-size: var(--font-size-xs);
box-shadow: var(--shadow-sm);
animation: scaleIn var(--duration-fast);
2026-03-08 13:55:10 +08:00
}
@keyframes scaleIn {
from {
transform: scale(0);
2026-03-05 21:01:34 +08:00
}
2026-03-08 13:55:10 +08:00
to {
transform: scale(1);
}
}
/* 信息区域 */
.card-info {
text-align: center;
}
.voice-name {
2026-03-16 23:54:01 +08:00
font-size: var(--font-size-sm);
2026-03-08 13:55:10 +08:00
font-weight: 600;
2026-03-16 23:54:01 +08:00
color: var(--foreground);
margin-bottom: var(--space-1);
2026-03-08 13:55:10 +08:00
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.voice-desc {
2026-03-16 23:54:01 +08:00
font-size: var(--font-size-xs);
color: var(--muted-foreground);
2026-03-08 13:55:10 +08:00
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
2026-03-05 21:01:34 +08:00
}
/* 播放器区域 */
.player-section {
2026-03-16 23:54:01 +08:00
background: var(--muted);
border-radius: var(--radius-lg);
padding: var(--space-3-5);
border: 1px solid var(--border);
}
2026-02-02 22:36:20 +08:00
2026-03-08 13:55:10 +08:00
.player-header {
display: flex;
align-items: center;
justify-content: space-between;
2026-03-16 23:54:01 +08:00
margin-bottom: var(--space-3);
2026-03-08 13:55:10 +08:00
}
2026-03-05 21:01:34 +08:00
2026-03-08 13:55:10 +08:00
.player-info {
display: flex;
align-items: center;
2026-03-16 23:54:01 +08:00
gap: var(--space-2-5);
2026-03-08 13:55:10 +08:00
}
.player-icon {
width: 36px;
height: 36px;
2026-03-16 23:54:01 +08:00
color: var(--primary);
display: flex;
align-items: center;
justify-content: center;
2026-02-02 22:36:20 +08:00
}
2026-02-02 23:16:38 +08:00
2026-03-08 13:55:10 +08:00
.player-meta {
.player-title {
2026-03-16 23:54:01 +08:00
font-size: var(--font-size-base);
2026-03-08 13:55:10 +08:00
font-weight: 600;
2026-03-16 23:54:01 +08:00
color: var(--foreground);
2026-03-08 13:55:10 +08:00
}
.player-label {
2026-03-16 23:54:01 +08:00
font-size: var(--font-size-xs);
color: var(--muted-foreground);
2026-03-08 13:55:10 +08:00
}
2026-03-05 21:01:34 +08:00
}
.download-btn {
2026-03-16 23:54:01 +08:00
color: var(--muted-foreground);
font-size: var(--font-size-xs);
2026-03-17 23:41:49 +08:00
padding: var(--space-2) var(--space-3);
2026-03-05 21:01:34 +08:00
height: auto;
2026-03-16 23:54:01 +08:00
border-radius: var(--radius-sm);
transition: all var(--duration-fast);
2026-03-05 21:01:34 +08:00
&:hover {
2026-03-16 23:54:01 +08:00
color: var(--primary);
background: oklch(from var(--primary) l c h / 0.08);
2026-03-08 13:55:10 +08:00
}
}
.aplayer-container {
:deep(.aplayer) {
2026-03-16 23:54:01 +08:00
border-radius: var(--radius);
2026-03-08 13:55:10 +08:00
box-shadow: none;
2026-03-16 23:54:01 +08:00
border: 1px solid var(--border);
2026-03-08 13:55:10 +08:00
.aplayer-body {
2026-03-16 23:54:01 +08:00
border-radius: var(--radius);
2026-03-08 13:55:10 +08:00
}
2026-03-05 21:01:34 +08:00
}
}
/* 动画 */
.slide-fade-enter-active {
2026-03-16 23:54:01 +08:00
transition: all var(--duration-base) ease-out;
2026-03-05 21:01:34 +08:00
}
.slide-fade-leave-active {
2026-03-16 23:54:01 +08:00
transition: all var(--duration-fast) ease-in;
2026-03-05 21:01:34 +08:00
}
.slide-fade-enter-from {
2026-03-08 13:55:10 +08:00
transform: translateY(-10px);
2026-03-05 21:01:34 +08:00
opacity: 0;
}
.slide-fade-leave-to {
2026-03-08 13:55:10 +08:00
transform: translateY(-10px);
2026-03-05 21:01:34 +08:00
opacity: 0;
2026-02-02 23:16:38 +08:00
}
</style>