代码
import ChatTTS
import torch
import torchaudio
chat = ChatTTS.Chat()
chat.load(compile=False) # Set to True for better performance
###################################
inputs_en = """
chat T T S is a text to speech model designed for dialogue applications.
[uv_break]it supports mixed language input [uv_break]and offers multi speaker
capabilities with precise control over prosodic elements like
[uv_break]laughter[uv_break][laugh], [uv_break]pauses, [uv_break]and intonation.
[uv_break]it delivers natural and expressive speech,[uv_break]so please
[uv_break] use the project responsibly at your own risk.[uv_break]
""".replace('\n', '') # English is still experimental.
params_refine_text = ChatTTS.Chat.RefineTextParams(
prompt='[oral_2][laugh_0][break_4]',
)
audio_array_en = chat.infer(inputs_en, params_refine_text=params_refine_text)
torchaudio.save("self_introduction_output.wav", torch.from_numpy(audio_array_en[0]), 24000)
最后出错了。
如下:
UserWarning: The use of `x.T` on tensors of dimension other than 2 to reverse their shape is deprecated and it will throw an error in a future release. Consider `x.mT` to transpose batches of matrices or `x.permute(*torch.arange(x.ndim - 1, -1, -1))` to reverse the dimensions of a tensor. (Triggered internally at ../aten/src/ATen/native/TensorShape.cpp:3683.)
src = src.T
Traceback (most recent call last):
File "/home/duyicheng/gitee/ChatTTS/intr.py", line 22, in <module>
torchaudio.save("self_introduction_output.wav", torch.from_numpy(audio_array_en[0]), 24000)
File "/home/duyicheng/anaconda3/envs/chattts/lib/python3.12/site-packages/torchaudio/_backend/utils.py", line 313, in save
return backend.save(
^^^^^^^^^^^^^
File "/home/duyicheng/anaconda3/envs/chattts/lib/python3.12/site-packages/torchaudio/_backend/ffmpeg.py", line 316, in save
save_audio(
File "/home/duyicheng/anaconda3/envs/chattts/lib/python3.12/site-packages/torchaudio/_backend/ffmpeg.py", line 248, in save_audio
s.add_audio_stream(
File "/home/duyicheng/anaconda3/envs/chattts/lib/python3.12/site-packages/torio/io/_streaming_media_encoder.py", line 278, in add_audio_stream
self._s.add_audio_stream(
RuntimeError: Failed to open codec: (Invalid argument)
Exception raised from open_codec at /__w/audio/audio/pytorch/audio/src/libtorio/ffmpeg/stream_writer/encode_process.cpp:194 (most recent call first):
frame #0: c10::Error::Error(c10::SourceLocation, std::string) + 0x96 (0x75c2fb4b2446 in /home/duyicheng/anaconda3/envs/chattts/lib/python3.12/site-packages/torch/lib/libc10.so)
frame #1: c10::detail::torchCheckFail(char const*, char const*, unsigned int, std::string const&) + 0x64 (0x75c2fb45c6e4 in /home/duyicheng/anaconda3/envs/chattts/lib/python3.12/site-packages/torch/lib/libc10.so)
frame #2: <unknown function> + 0x46c3f (0x75c2f28cac3f in /home/duyicheng/anaconda3/envs/chattts/lib/python3.12/site-packages/torio/lib/libtorio_ffmpeg6.so)
frame #3: torio::io::get_audio_encode_process(AVFormatContext*, int, int, std::string const&, std::optional<std::string> const&, std::optional<std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > > > const&, std::optional<std::string> const&, std::optional<int> const&, std::optional<int> const&, std::optional<torio::io::CodecConfig> const&, std::optional<std::string> const&, bool) + 0x250 (0x75c2f28d14d0 in /home/duyicheng/anaconda3/envs/chattts/lib/python3.12/site-packages/torio/lib/libtorio_ffmpeg6.so)
frame #4: torio::io::StreamingMediaEncoder::add_audio_stream(int, int, std::string const&, std::optional<std::string> const&, std::optional<std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > > > const&, std::optional<std::string> const&, std::optional<int> const&, std::optional<int> const&, std::optional<torio::io::CodecConfig> const&, std::optional<std::string> const&) + 0x90 (0x75c2f28d8dd0 in /home/duyicheng/anaconda3/envs/chattts/lib/python3.12/site-packages/torio/lib/libtorio_ffmpeg6.so)
frame #5: <unknown function> + 0x3acbb (0x75c22799ecbb in /home/duyicheng/anaconda3/envs/chattts/lib/python3.12/site-packages/torio/lib/_torio_ffmpeg6.so)
frame #6: <unknown function> + 0x31dfc (0x75c227995dfc in /home/duyicheng/anaconda3/envs/chattts/lib/python3.12/site-packages/torio/lib/_torio_ffmpeg6.so)
<omitting python frames>
frame #18: <unknown function> + 0x2a1ca (0x75c30022a1ca in /lib/x86_64-linux-gnu/libc.so.6)
frame #19: __libc_start_main + 0x8b (0x75c30022a28b in /lib/x86_64-linux-gnu/libc.so.6)
解决:未验证:明天再说。
根据你提供的错误信息,问题主要集中在两个方面:
- **使用 `x.T` 进行张量转置的弃用警告**:
错误信息提示 `x.T` 在维度不是2的情况下使用已被弃用,并将在未来版本中抛出错误。
建议使用 `x.mT` 来转置批次矩阵,或者使用 `x.permute(*torch.arange(x.ndim - 1, -1, -1))` 来反转张量的所有维度。
- **保存音频文件时的运行时错误**:
错误信息显示在尝试保存音频文件时,FFmpeg 编解码器无法打开,原因是无效参数。
具体错误发生在 `torio::io::get_audio_encode_process` 函数中。
解决方案
1. 处理 `x.T` 弃用警告
如果你的代码中有类似 `x.T` 的用法,建议替换为推荐的方法。例如:
```python
替换 x.T 为 x.mT 或 x.permute(*torch.arange(x.ndim - 1, -1, -1))
示例:
x = x.mT # 如果 x 是一个批次矩阵
或者
x = x.permute(*torch.arange(x.ndim - 1, -1, -1)) # 反转所有维度
```
2. 解决音频保存错误
音频保存错误可能是由于音频数据格式或参数设置不正确导致的。以下是一些可能的解决方案:
**检查音频数据格式**:
确保 `audio_array_en[0]` 是一个正确的音频数组,通常是一个二维数组,其中第一维是通道数,第二维是样本数。
使用 `torch.from_numpy(audio_array_en[0])` 转换后,确保得到的是一个浮点类型的张量,并且值在合理范围内(通常是 -1 到 1)。
**调整采样率**:
确认 `24000` 是正确的采样率。如果不正确,修改为正确的采样率。
**检查 FFmpeg 安装**:
确保 FFmpeg 已正确安装并且配置正确。可以通过命令行运行 `ffmpeg -version` 来验证。
**调试音频数据**:
尝试先将音频数据保存为其他格式(如 `.wav`),看看是否能成功保存,以排除数据本身的问题。
**查看文档和社区支持**:
查阅 `torchaudio` 和 `torio` 的官方文档,寻找关于音频保存的更多信息。
如果问题依然存在,可以在相关的 GitHub 讨论区或 Stack Overflow 上寻求帮助。
希望这些建议能帮助你解决问题!
解决方法
torchaudio.save("word_level_output.wav", torch.from_numpy(audio_array_en[0]).unsqueeze(0), 24000)
# torchaudio.save("self_introduction_output.wav", torch.from_numpy(audio_array_en[0]), 24000)