通过PyAudio直接gTTS输出

时间:2019-01-11 19:07:23

标签: python pyaudio bytesio gtts

我正在尝试将识别的文本直接播放到扬声器,而不将其保存到磁盘。我找到了具有BytesIO功能的io库,因此我将可识别的文本从gTTS库保存到BytesIO,然后尝试通过PyAudio播放。

我试图将BytesIO阵列保存到磁盘并通过VLC播放它,并且可以正常工作。但是当我运行脚本时,我可以听到类似“ bzzzz”的声音。我是pyaudio&gtts的初学者,所以我真的不知道自己在做什么错。

def speak(self, convertToAudio):
    audioOutput = self.convertToAudio(convertToAudio) # returns BytesIO array with text-speech audio

    #TEST AUDIO FILE:
    #with open("D:\\test.mp3", "wb") as f:
    #   f.write(audioOutput.getvalue())

    print(audioOutput)

    p = pyaudio.PyAudio()

    stream = p.open(format=p.get_format_from_width(2), channels=1, rate=24000, output=True, frames_per_buffer=1024)

    stream.write(audioOutput.getvalue())

    stream.stop_stream()
    stream.close()
    p.terminate()

    return

0 个答案:

没有答案