552 lines
14 KiB
Vue
552 lines
14 KiB
Vue
<template>
|
|
<div class="voice-selector">
|
|
<div v-if="userVoiceCards.length === 0" class="empty-voices">
|
|
<a-empty :image="simpleImage" description="还没有配音">
|
|
</a-empty>
|
|
</div>
|
|
|
|
<div v-else class="voice-selector-wrapper">
|
|
<!-- 选择器卡片 -->
|
|
<div class="voice-card" :class="{ 'has-audio': audioUrl }">
|
|
<div class="voice-card-header">
|
|
<div class="header-icon">
|
|
<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
<path d="M12 14C13.1046 14 14 13.1046 14 12C14 10.8954 13.1046 10 12 10C10.8954 10 10 10.8954 10 12C10 13.1046 10.8954 14 12 14Z" fill="currentColor"/>
|
|
<path d="M12 6C14.813 6 17.125 8.156 17.469 10.875C17.5 11.125 17.719 11.313 17.969 11.313H18.031C18.313 11.313 18.531 11.063 18.5 10.781C18.094 7.5 15.344 5 12 5C8.656 5 5.906 7.5 5.5 10.781C5.469 11.063 5.687 11.313 5.969 11.313H6.031C6.281 11.313 6.5 11.125 6.531 10.875C6.875 8.156 9.187 6 12 6Z" fill="currentColor" opacity="0.6"/>
|
|
<path d="M12 3C16.5 3 20.188 6.5 20.469 11C20.5 11.25 20.719 11.438 20.969 11.438H21.031C21.313 11.438 21.531 11.188 21.5 10.906C21.156 5.875 17 2 12 2C7 2 2.844 5.875 2.5 10.906C2.469 11.188 2.687 11.438 2.969 11.438H3.031C3.281 11.438 3.5 11.25 3.531 11C3.813 6.5 7.5 3 12 3Z" fill="currentColor" opacity="0.3"/>
|
|
</svg>
|
|
</div>
|
|
<span class="header-title">音色选择</span>
|
|
<span v-if="currentVoiceName" class="header-badge">{{ currentVoiceName }}</span>
|
|
</div>
|
|
|
|
<div class="voice-card-body">
|
|
<div class="select-wrapper">
|
|
<a-select
|
|
v-model:value="selectedVoiceId"
|
|
placeholder="请选择音色"
|
|
class="voice-select"
|
|
:options="voiceOptions"
|
|
@change="handleVoiceChange"
|
|
>
|
|
<template #suffixIcon>
|
|
<DownOutlined class="select-arrow" />
|
|
</template>
|
|
</a-select>
|
|
</div>
|
|
|
|
<a-button
|
|
class="synthesize-btn"
|
|
:class="{ 'btn-active': selectedVoiceId }"
|
|
:disabled="!selectedVoiceId || isPlayerInitializing"
|
|
:loading="previewLoadingVoiceId === selectedVoiceId"
|
|
@click="handleSynthesize"
|
|
>
|
|
<template #icon>
|
|
<SoundOutlined />
|
|
</template>
|
|
<span class="btn-text">合成试听</span>
|
|
</a-button>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- 播放器区域 -->
|
|
<transition name="slide-fade">
|
|
<div v-if="audioUrl" class="player-section">
|
|
<div ref="playerContainer" class="aplayer-container"></div>
|
|
<div class="player-actions">
|
|
<a-button
|
|
type="text"
|
|
size="small"
|
|
@click="downloadAudio"
|
|
class="download-btn"
|
|
>
|
|
<template #icon>
|
|
<DownloadOutlined />
|
|
</template>
|
|
下载音频
|
|
</a-button>
|
|
</div>
|
|
</div>
|
|
</transition>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup>
|
|
import { ref, computed, onMounted, onBeforeUnmount, nextTick, watch } from 'vue'
|
|
import { Empty, message } from 'ant-design-vue'
|
|
import { SoundOutlined, DownloadOutlined, DownOutlined } from '@ant-design/icons-vue'
|
|
import { useVoiceCopyStore } from '@/stores/voiceCopy'
|
|
import { useTTS, TTS_PROVIDERS } from '@/composables/useTTS'
|
|
import APlayer from 'aplayer'
|
|
|
|
const props = defineProps({
|
|
synthText: {
|
|
type: String,
|
|
default: ''
|
|
},
|
|
speechRate: {
|
|
type: Number,
|
|
default: 1.0
|
|
}
|
|
})
|
|
|
|
const simpleImage = Empty.PRESENTED_IMAGE_SIMPLE
|
|
|
|
const voiceStore = useVoiceCopyStore()
|
|
|
|
const emit = defineEmits(['select'])
|
|
|
|
let player = null
|
|
const playerContainer = ref(null)
|
|
const audioUrl = ref('')
|
|
const currentVoiceName = ref('')
|
|
const isPlayerInitializing = ref(false)
|
|
|
|
// 默认封面图片(音频波形图标)
|
|
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())}`
|
|
|
|
// 使用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 || '未命名',
|
|
category: '',
|
|
gender: profile.gender || 'female',
|
|
description: profile.note || '我的配音',
|
|
fileUrl: profile.fileUrl,
|
|
transcription: profile.transcription || '',
|
|
source: 'user',
|
|
voiceId: profile.voiceId
|
|
}))
|
|
)
|
|
|
|
const voiceOptions = computed(() =>
|
|
userVoiceCards.value.map(voice => ({
|
|
value: voice.id,
|
|
label: voice.name,
|
|
data: voice
|
|
}))
|
|
)
|
|
|
|
const handleVoiceChange = (value, option) => {
|
|
const voice = option.data
|
|
selectedVoiceId.value = value
|
|
currentVoiceName.value = voice.name
|
|
emit('select', voice)
|
|
}
|
|
|
|
const handleSynthesize = () => {
|
|
if (!selectedVoiceId.value) return
|
|
// 防止在播放器初始化过程中重复点击
|
|
if (isPlayerInitializing.value) return
|
|
|
|
const voice = userVoiceCards.value.find(v => v.id === selectedVoiceId.value)
|
|
if (!voice) return
|
|
|
|
currentVoiceName.value = voice.name
|
|
handlePlayVoiceSample(voice)
|
|
}
|
|
|
|
// 监听 prop 变化,更新 TTS 参数
|
|
watch(() => props.synthText, (newText) => {
|
|
setText(newText || '')
|
|
}, { immediate: true })
|
|
|
|
watch(() => props.speechRate, (newRate) => {
|
|
setSpeechRate(newRate)
|
|
}, { immediate: true })
|
|
|
|
const handlePlayVoiceSample = (voice) => {
|
|
currentVoiceName.value = voice.name
|
|
playVoiceSample(
|
|
voice,
|
|
(data) => {
|
|
const url = data.audioUrl || data.objectUrl
|
|
if (!url) return
|
|
initPlayer(url)
|
|
},
|
|
(error) => {
|
|
// 音频播放失败,静默处理
|
|
},
|
|
{ autoPlay: false } // 禁用自动播放,由 APlayer 控制
|
|
)
|
|
}
|
|
|
|
/**
|
|
* 初始化 APlayer
|
|
*/
|
|
const initPlayer = (url) => {
|
|
// 防止并发初始化
|
|
if (isPlayerInitializing.value) {
|
|
return
|
|
}
|
|
|
|
isPlayerInitializing.value = true
|
|
destroyPlayer()
|
|
audioUrl.value = url
|
|
|
|
nextTick(() => {
|
|
try {
|
|
// 检查容器是否存在
|
|
if (!playerContainer.value) {
|
|
message.error('播放器容器未就绪')
|
|
isPlayerInitializing.value = false
|
|
audioUrl.value = ''
|
|
return
|
|
}
|
|
|
|
player = new APlayer({
|
|
container: playerContainer.value,
|
|
autoplay: true,
|
|
theme: '#3b82f6',
|
|
volume: 0.7,
|
|
loop: 'none',
|
|
audio: [{
|
|
name: currentVoiceName.value || '语音合成',
|
|
artist: '合成',
|
|
url: url,
|
|
cover: defaultCover
|
|
}]
|
|
})
|
|
|
|
player.on('ended', () => {
|
|
player.seek(0)
|
|
})
|
|
|
|
player.on('error', (e) => {
|
|
console.error('APlayer 播放错误:', e)
|
|
message.error('音频播放失败,请重试')
|
|
})
|
|
|
|
player.on('canplay', () => {
|
|
isPlayerInitializing.value = false
|
|
})
|
|
} catch (e) {
|
|
console.error('APlayer 初始化失败:', e)
|
|
message.error('播放器初始化失败,请重试')
|
|
isPlayerInitializing.value = false
|
|
audioUrl.value = ''
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 下载音频
|
|
*/
|
|
const downloadAudio = () => {
|
|
if (!audioUrl.value) return
|
|
|
|
const filename = `${currentVoiceName.value || '语音合成'}.mp3`
|
|
const link = document.createElement('a')
|
|
link.href = audioUrl.value
|
|
link.download = filename
|
|
document.body.appendChild(link)
|
|
link.click()
|
|
document.body.removeChild(link)
|
|
}
|
|
|
|
/**
|
|
* 销毁播放器
|
|
*/
|
|
const destroyPlayer = () => {
|
|
isPlayerInitializing.value = false
|
|
if (player) {
|
|
try {
|
|
// 先暂停播放,防止销毁过程中出错
|
|
player.pause()
|
|
player.destroy()
|
|
} catch (e) {
|
|
console.error('销毁播放器失败:', e)
|
|
}
|
|
player = null
|
|
}
|
|
// 只对 blob URL 调用 revokeObjectURL
|
|
if (audioUrl.value && audioUrl.value.startsWith('blob:')) {
|
|
URL.revokeObjectURL(audioUrl.value)
|
|
}
|
|
audioUrl.value = ''
|
|
}
|
|
|
|
onMounted(async () => {
|
|
await voiceStore.refresh()
|
|
})
|
|
|
|
onBeforeUnmount(() => {
|
|
destroyPlayer()
|
|
})
|
|
</script>
|
|
|
|
<style scoped lang="less">
|
|
.voice-selector {
|
|
width: 100%;
|
|
}
|
|
|
|
.empty-voices {
|
|
padding: 24px 0;
|
|
background: var(--color-surface);
|
|
border: 1px dashed var(--color-border);
|
|
border-radius: 12px;
|
|
}
|
|
|
|
/* 主容器 */
|
|
.voice-selector-wrapper {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 16px;
|
|
}
|
|
|
|
/* 音色卡片 */
|
|
.voice-card {
|
|
background: linear-gradient(135deg, rgba(59, 130, 246, 0.05) 0%, rgba(139, 92, 246, 0.05) 100%);
|
|
border: 1px solid rgba(59, 130, 246, 0.15);
|
|
border-radius: 12px;
|
|
padding: 16px;
|
|
transition: all 0.3s ease;
|
|
|
|
&:hover {
|
|
border-color: rgba(59, 130, 246, 0.3);
|
|
box-shadow: 0 4px 12px rgba(59, 130, 246, 0.1);
|
|
}
|
|
|
|
&.has-audio {
|
|
border-color: rgba(59, 130, 246, 0.4);
|
|
background: linear-gradient(135deg, rgba(59, 130, 246, 0.08) 0%, rgba(139, 92, 246, 0.08) 100%);
|
|
}
|
|
}
|
|
|
|
/* 卡片头部 */
|
|
.voice-card-header {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 10px;
|
|
margin-bottom: 14px;
|
|
}
|
|
|
|
.header-icon {
|
|
width: 28px;
|
|
height: 28px;
|
|
border-radius: 8px;
|
|
background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 100%);
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
color: white;
|
|
|
|
svg {
|
|
width: 16px;
|
|
height: 16px;
|
|
}
|
|
}
|
|
|
|
.header-title {
|
|
font-size: 14px;
|
|
font-weight: 500;
|
|
color: var(--color-text);
|
|
}
|
|
|
|
.header-badge {
|
|
margin-left: auto;
|
|
padding: 2px 10px;
|
|
background: rgba(59, 130, 246, 0.1);
|
|
border-radius: 12px;
|
|
font-size: 12px;
|
|
color: #3b82f6;
|
|
font-weight: 500;
|
|
max-width: 120px;
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
/* 卡片主体 */
|
|
.voice-card-body {
|
|
display: flex;
|
|
gap: 12px;
|
|
align-items: stretch;
|
|
}
|
|
|
|
.select-wrapper {
|
|
flex: 1;
|
|
|
|
:deep(.ant-select) {
|
|
width: 100%;
|
|
height: 40px;
|
|
|
|
.ant-select-selector {
|
|
height: 40px !important;
|
|
border-radius: 10px !important;
|
|
border-color: rgba(59, 130, 246, 0.2) !important;
|
|
background: rgba(255, 255, 255, 0.8) !important;
|
|
transition: all 0.3s ease !important;
|
|
|
|
&:hover {
|
|
border-color: #3b82f6 !important;
|
|
}
|
|
}
|
|
|
|
&.ant-select-focused .ant-select-selector {
|
|
border-color: #3b82f6 !important;
|
|
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1) !important;
|
|
}
|
|
|
|
.ant-select-selection-item {
|
|
line-height: 38px !important;
|
|
}
|
|
|
|
.ant-select-selection-placeholder {
|
|
line-height: 38px !important;
|
|
}
|
|
}
|
|
}
|
|
|
|
.select-arrow {
|
|
color: #3b82f6;
|
|
transition: transform 0.3s ease;
|
|
}
|
|
|
|
/* 合成按钮 */
|
|
.synthesize-btn {
|
|
height: 40px;
|
|
padding: 0 20px;
|
|
border-radius: 10px;
|
|
border: none;
|
|
background: linear-gradient(135deg, #e2e8f0 0%, #cbd5e1 100%);
|
|
color: #64748b;
|
|
font-weight: 500;
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 6px;
|
|
transition: all 0.3s ease;
|
|
white-space: nowrap;
|
|
|
|
&:hover:not(:disabled) {
|
|
background: linear-gradient(135deg, #cbd5e1 0%, #94a3b8 100%);
|
|
transform: translateY(-1px);
|
|
}
|
|
|
|
&:disabled {
|
|
opacity: 0.6;
|
|
cursor: not-allowed;
|
|
}
|
|
|
|
&.btn-active {
|
|
background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 100%);
|
|
color: white;
|
|
box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
|
|
|
|
&:hover:not(:disabled) {
|
|
background: linear-gradient(135deg, #2563eb 0%, #7c3aed 100%);
|
|
box-shadow: 0 6px 16px rgba(59, 130, 246, 0.4);
|
|
}
|
|
}
|
|
|
|
.btn-text {
|
|
font-size: 13px;
|
|
}
|
|
}
|
|
|
|
/* 播放器区域 */
|
|
.player-section {
|
|
background: rgba(255, 255, 255, 0.6);
|
|
border-radius: 12px;
|
|
padding: 14px;
|
|
border: 1px solid rgba(59, 130, 246, 0.1);
|
|
}
|
|
|
|
.aplayer-container {
|
|
:deep(.aplayer) {
|
|
border-radius: 10px;
|
|
box-shadow: none;
|
|
border: 1px solid rgba(0, 0, 0, 0.06);
|
|
|
|
.aplayer-body {
|
|
border-radius: 10px;
|
|
}
|
|
}
|
|
}
|
|
|
|
.player-actions {
|
|
margin-top: 10px;
|
|
display: flex;
|
|
justify-content: flex-end;
|
|
}
|
|
|
|
.download-btn {
|
|
color: #3b82f6;
|
|
font-size: 12px;
|
|
padding: 4px 8px;
|
|
height: auto;
|
|
transition: all 0.2s ease;
|
|
|
|
&:hover {
|
|
color: #2563eb;
|
|
background: rgba(59, 130, 246, 0.1);
|
|
}
|
|
}
|
|
|
|
/* 动画 */
|
|
.slide-fade-enter-active {
|
|
transition: all 0.3s ease-out;
|
|
}
|
|
|
|
.slide-fade-leave-active {
|
|
transition: all 0.2s ease-in;
|
|
}
|
|
|
|
.slide-fade-enter-from {
|
|
transform: translateY(-10px);
|
|
opacity: 0;
|
|
}
|
|
|
|
.slide-fade-leave-to {
|
|
transform: translateY(-10px);
|
|
opacity: 0;
|
|
}
|
|
</style>
|