如何在python中的pyaudio中播放TTS numpy数组

bqjvbblv  于 2023-04-30  发布在  Python
关注(0)|答案(1)|浏览(219)

我试图将声音直接从Coqui TTS创建的numpy数组传递给pyaudio播放,但失败得很惨。

from TTS.api import TTS
from subprocess import call
import pyaudio

# Running a multi-speaker and multi-lingual model

# List available 🐸TTS models and choose the first one
model_name = TTS.list_models()[0]
# Init TTS
tts = TTS(model_name)
# Run TTS
text="King Charles III. King of the United Kingdom and 14 other Commonwealth realms. Prince Charles acceded to the throne on September 8 2022 upon the death of his mother, Queen Elizabeth II. He was the longest-serving heir apparent in British history and was the oldest person to assume the throne, doing so at the age of 73."
# ❗ Since this model is multi-speaker and multi-lingual, we must set the target speaker and the language
speaker=tts.speakers[0]
print(speaker)
language=tts.languages[0]
print(language)

# Text to speech with a numpy output
data = tts.tts(text, speaker=speaker, language=language)
print(data)
# Text to speech to a file
#tts.tts_to_file(text=text, speaker=speaker, file_path="output.wav")
#call(['aplay', 'output.wav'])

FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 16000

play=pyaudio.PyAudio()
stream_play=play.open(format=FORMAT,
                      channels=CHANNELS,
                      rate=RATE,
                      output=True
                      )
stream_play.write(data)
stream_play.stop_stream()
stream_play.close()
play.terminate()

我已经注解掉了我将声音录制到wav文件的版本,然后播放wav文件,效果很好。
但是,我想直接将结果传递给pyaudio播放,而不是每次都写一个wav文件。
我知道TTS创建了一个numpy数组,但我不知道如何将其转换为声音。
我也不明白pyaudio需要正确读取数组的设置。我一直在尝试尝试错误,但没有运气。现在我试着去理解它的原理,但它超出了我的理解范围。
任何帮助或指针将不胜感激。
鲁珀特

vcirk6k6

vcirk6k61#

我用SoundDevice得到了这个,你可能想切换到它。

...
import os
os.environ['KMP_DUPLICATE_LIB_OK']='True'   # to avoid duplicate clash on OpenMP package (may not be required, I haven't tidied up previous attempts)
import sounddevice as sd
...
wav = tts.tts(text, speaker, language)

sd.play(wav, samplerate=22050)
status = sd.wait()  # Wait until file is done playing

我通过反复试验得到了采样率。每种型号都不一样。
注意:我在设置它时遇到了一些安装冲突,但将numpy更改为v。1.23.0修复了它们,你可能也要这样做。

相关问题