开发者社区 问答 正文

语音合成python SDK执行时无法生成wav文件

win10环境下下载python sdk并配置完服务端需要的设置,初次运行的时候在命令行执行python tests/test_tts/py 文件, 生成了test_tts.pcm 文件且输出信息如下

thread:thread0 start..
thread0: session start
2022-05-12 10:22:17,902 - DEBUG - callback args:[]
2022-05-12 10:22:18,038 - DEBUG - get token 28843902abe946b486d0039e5e5b1ac2
2022-05-12 10:22:18,039 - DEBUG - ws run...
2022-05-12 10:22:18,039 - DEBUG - wait cond wakeup
2022-05-12 10:22:18,152 - DEBUG - core_on_open:(<nls._core.NlsCore object at 0x000002130F03CCD0>, '{"header": {"message_id": "cd6a7d2a6d3941bdae56d4030c3e6605", "task_id": "1cb0c2a08e5148ea862bdc67103ca047", "namespace": "SpeechSynthesizer", "name": "StartSynthesis", "appkey": "rBjTtVQwMUzU7pjD"}, "payload": {"text": "\\u5927\\u58ee\\u6b63\\u60f3\\u53bb\\u6458\\u53d6\\u82b1\\u74e3\\uff0c\\u8c01\\u77e5\\u963f\\u4e3d\\u548c\\u963f\\u5f3a\\u7a81\\u7136\\u5185\\u8ba7\\uff0c\\u963f\\u4e3d\\u62ff\\u53bb\\u624b\\u67aa\\u5411\\u6811\\u5e72\\u8fb9\\u7684\\u963f\\u5f3a\\u5c04\\u51fb\\uff0c\\u4e24\\u58f0\\u67aa\\u54cd\\uff0c\\u963f\\u5f3a\\u76f4\\u63a5\\u5012\\u5165\\u6c34\\u4e2d", "voice": "ailun", "format": "pcm", "sample_rate": 16000, "volume": 50, "speech_rate": 0, "pitch_rate": 0}}')
2022-05-12 10:22:20,535 - DEBUG - core_on_msg:{"header":{"namespace":"SpeechSynthesizer","name":"SynthesisCompleted","status":20000000,"message_id":"43cd7a65da94408db1d203b2eacd0765","task_id":"1cb0c2a08e5148ea862bdc67103ca047","status_text":"Gateway:SUCCESS:Success."}}
2022-05-12 10:22:20,536 - DEBUG - __syn_core_on_msg:msg={"header":{"namespace":"SpeechSynthesizer","name":"SynthesisCompleted","status":20000000,"message_id":"43cd7a65da94408db1d203b2eacd0765","task_id":"1cb0c2a08e5148ea862bdc67103ca047","status_text":"Gateway:SUCCESS:Success."}} args=()
2022-05-12 10:22:20,536 - DEBUG - __handle_message
2022-05-12 10:22:20,536 - DEBUG - __synthesis_completed
2022-05-12 10:22:20,559 - DEBUG - __synthesis_completed shutdown done
on_completed:args=>('thread0',) message=>{"header":{"namespace":"SpeechSynthesizer","name":"SynthesisCompleted","status":20000000,"message_id":"43cd7a65da94408db1d203b2eacd0765","task_id":"1cb0c2a08e5148ea862bdc67103ca047","status_text":"Gateway:SUCCESS:Success."}}
2022-05-12 10:22:20,560 - DEBUG - core_on_close
thread0: tts done with result:True
2022-05-12 10:22:20,560 - DEBUG - __sr_core_on_close
on_close: args=>('thread0',)
2022-05-12 10:22:20,561 - DEBUG - ws exit...

通过对比文档已知此次执行结果为正常,但疑问如下

  1. 合成数据被写成了pcm文件,如何将文件类型改写输出为wav。尝试修改test_tts.py内的语句
t = TestTts(name, "tests/test_tts.pcm")

,但改变后缀名的方法无效 。尝试修改nls/ _speech_synthesizer.py 内的

def start(self, text="", voice="xiaoyun", aformat="pcm",

类型为wav,同样无效 2. 执行语句后线程一直处于循环执行,虽然通过修改while语句的值可以终止执行。但想了解如何通过控制线程来终止线程执行

展开
收起
游客xdaumztxpljnw 2022-05-12 14:51:31 781 分享 版权
阿里云 AI 助理回答

要将输出的音频文件从PCM格式转换为WAV格式,你不能仅仅通过改变文件后缀名来实现。你需要使用音频处理库来完成这个转换。Python中一个常用的库是pydub,但请注意它依赖于ffmpeg。

首先,确保你已经安装了pydub库和ffmpeg。可以通过以下命令安装pydub

pip install pydub

然后,确保你的系统中已安装ffmpeg,并且其路径被添加到系统的PATH环境变量中。

接下来,在你的代码中加入转换PCM到WAV的部分。这里是一个简单的示例,展示如何在生成PCM文件之后将其转换为WAV格式:

from pydub import AudioSegment

# 假设pcm_file_path是你的PCM文件路径,wav_file_path是你希望保存的WAV文件路径
pcm_file_path = "tests/test_tts.pcm"
wav_file_path = "tests/test_tts.wav"

# 使用pydub读取PCM文件并指定采样率(根据你的合成设置)
audio = AudioSegment.from_raw(pcm_file_path, format="pcm", frame_rate=16000, sample_width=2)

# 导出为WAV格式
audio.export(wav_file_path, format="wav")

关于线程控制的问题,如果你想要优雅地终止线程执行,可以考虑以下几种方法:

  1. 使用标志位:在线程运行的循环中检查一个全局或类级别的布尔变量(比如stop_thread),当需要停止时,修改这个变量的值。线程应该定期检查这个变量,一旦发现它变为True,则退出循环。
import threading

stop_thread = False

def your_thread_function():
    global stop_thread
    while not stop_thread:
        # 线程执行逻辑...
    
    # 清理工作,如果有的话

# 在其他地方设置stop_thread为True以停止线程
stop_thread = True
  1. 使用Event对象:Python的threading.Event提供了一个更高级的方式来控制线程的启动和停止。
import threading

stop_event = threading.Event()

def your_thread_function():
    while not stop_event.is_set():
        # 线程执行逻辑...

# 在其他地方调用set()方法来停止线程
stop_event.set()

这两种方法都允许你以非暴力的方式结束线程,避免了直接中断可能带来的资源未释放等问题。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答