用Python将合成的texttospeech记录到文件中

2024-05-17 11:13:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图找到一种方法,采取合成语音,并记录到一个音频文件。我目前正在使用pyttsx作为文本到语音库,但没有将输出保存到文件的机制,只能直接从扬声器播放。我已经研究过detecting and recording audioPyAudio,但它们似乎从麦克风接收输入,而不是将输出音频重定向到文件。有没有已知的方法可以做到这一点?


Tags: and文件方法文本记录语音音频audio
3条回答

可以使用更高级的SAPI包装器将输出保存到wav文件。例如,你可以尝试

https://github.com/DeepHorizons/tts

代码应该如下所示:

import tts.sapi
voice = tts.sapi.Sapi()
voice.set_voice("Joey")
voice.create_recording('hello.wav', "Hello")

下面是一个允许您访问NSSpeechSynthesizer API的示例

#!/usr/bin/env python

from  AppKit import NSSpeechSynthesizer
import sys
import Foundation


if len(sys.argv) < 2:
   text = raw_input('type text to speak> ')
else:
   text = sys.argv[1]

nssp = NSSpeechSynthesizer
ve = nssp.alloc().init()
ve.setRate_(100)
url = Foundation.NSURL.fileURLWithPath_('yourpath/test.aiff')
ve.startSpeakingString_toURL_(text,url)

可以使用subprocess使用-w参数调用espeak

import subprocess

def textToWav(text,file_name):
   subprocess.call(["espeak", "-w"+file_name+".wav", text])

textToWav('hello world','hello')

这将在不大声读出的情况下写入file_name.wav。如果您的文本在一个文件中(例如text.txt),则需要使用-f参数(“-f”+text)调用espeak。我建议您阅读espeak man pages以查看所有选项。

希望这有帮助。

相关问题 更多 >