Open tomhup opened 5 months ago
我有一个简单的实现, AgentBase def speak( self, content: Union[str, dict], ) -> None: if self.is_tts: logger.chat(content) asyncio.run(async_get_speech(content.content)) else: logger.chat(content)
import asyncio import io import os import re import tempfile from pydub import AudioSegment from pydub.playback import play import edge_tts import logging logger = logging.getLogger(name)
async def async_get_speech(phrase,voice="zh-CN-XiaoxiaoNeural"): try:
with tempfile.NamedTemporaryFile(delete=False) as tmpfile: tmpfile_name = tmpfile.name text=phrase text = re.sub(r'\*+', '', text) # 替换所有 * 字符 text = re.sub(r'#+', '', text) phrase=text # print(phrase) # 生成语音并保存到临时文件 tts = edge_tts.Communicate(text=phrase,voice=voice) await tts.save(tmpfile_name) # 读取临时文件到 BytesIO 对象 with open(tmpfile_name, "rb") as tmpfile: mp3_data = tmpfile.read() mp3_buffer = io.BytesIO(mp3_data) # 删除临时文件 os.remove(tmpfile_name) # 从 BytesIO 对象加载音频并播放 mp3_buffer.seek(0) # 重置指针到开始 audio_data = AudioSegment.from_file(mp3_buffer, format="mp3") play(audio_data) except Exception as e: logger.error(f"Error: {e}")
async def main(): phrase = "*Hello, #this is a test." await async_get_speech(phrase)
if name == "main": asyncio.run(main())
Thanks for your contributions to AgentScope. We will consider adding this feature to our AgentScope Studio :)
我有一个简单的实现, AgentBase def speak( self, content: Union[str, dict], ) -> None: if self.is_tts: logger.chat(content) asyncio.run(async_get_speech(content.content)) else: logger.chat(content)
采用edget_tts 库实现语音播放
import asyncio import io import os import re import tempfile from pydub import AudioSegment from pydub.playback import play import edge_tts import logging logger = logging.getLogger(name)
async def async_get_speech(phrase,voice="zh-CN-XiaoxiaoNeural"): try:
创建一个临时文件
async def main(): phrase = "*Hello, #this is a test." await async_get_speech(phrase)
if name == "main": asyncio.run(main())