2025.10.15故事生成系统(语音合成功能实现)
第5天:语音合成功能实现
📋 第5天概述
实现语音合成功能,将生成的文字故事转换为语音文件,支持多种语音风格和格式。
🎯 第5天目标
主要任务:构建语音合成服务,实现文字转语音功能
核心需求:支持多种语音风格、格式转换、音频文件管理
🔧 核心代码实现
语音合成服务类
class VoiceSynthesisService:
def __init__(self, aliyun_client):
self.client = aliyun_client
self.voice_styles = {
"甜美女声": "xiaoyun",
"温柔女声": "xiaoyan",
"磁性男声": "siyue",
"活泼童声": "xiaoxin"
}
def synthesize_speech(self, text, voice_style="甜美女声",
format="wav", sample_rate=16000):
"""语音合成核心方法"""
voice_code = self.voice_styles.get(voice_style, "xiaoyun")
# 调用阿里云语音合成API
audio_data = self.client.synthesize_speech(
text=text,
voice=voice_code,
format=format,
sample_rate=sample_rate
)
# 保存音频文件
filename = f"story_audio_{int(time.time())}.{format}"
filepath = os.path.join("uploads", filename)
with open(filepath, "wb") as f:
f.write(audio_data)
return {
"success": True,
"filepath": filepath,
"filename": filename,
"duration": self._get_audio_duration(filepath)
}
def _get_audio_duration(self, filepath):
"""获取音频时长"""
# 使用音频处理库获取时长
return 60 # 示例值
Web API路由
@voice_bp.route('/api/synthesize', methods=['POST'])
def synthesize_voice():
data = request.get_json()
result = voice_service.synthesize_speech(
text=data['text'],
voice_style=data.get('voice_style', '甜美女声'),
format=data.get('format', 'wav')
)
return jsonify(result)
前端界面
<div class="voice-synthesis">
<h3>🔊 语音合成</h3>
<select id="voiceStyle">
<option value="甜美女声">甜美女声</option>
<option value="温柔女声">温柔女声</option>
</select>
<button onclick="synthesize()">生成语音</button>
<audio id="audioPlayer" controls></audio>
</div>
📊 第5天成果
✅ 语音合成服务实现
✅ 多种语音风格支持
✅ 音频文件管理
✅ Web API集成
预计耗时:4-6小时
关键文件:src/services/voice_service.py, src/web/routes/voice_routes.py

浙公网安备 33010602011771号