Recording synthesized text-to-speech to a file in Python

笑着哭i 提交于 2019-11-30 07:30:23

问题


I am attempting to find a way to take synthesized speech and record it to an audio file. I am currently using pyttsx as my text-to-speech library, but there isn't a mechanism for saving the output to a file, only playing it directly from the speakers. I've looked into detecting and recording audio as well as PyAudio, but these seem to take input from a microphone rather than redirecting outgoing audio to a file. Is there a known way to do this?


回答1:


You can call espeak with the -w argument using subprocess.

import subprocess

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

textToWav('hello world','hello')

This will write file_name.wav without reading out loud. If your text is in a file (e.g. text.txt) you need to call espeak with the -f parameter ("-f"+text). I'd recommend reading the espeak man pages to see all the options you have.

Hope this helps.




回答2:


You can use more advanced SAPI wrapper to save output to the wav file. For example you can try

https://github.com/DeepHorizons/tts

The code should look like this:

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



回答3:


Here is an example which gives you access to the 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)



回答4:


use a VB in and out emulator cable with audacity to record whatever engine.play() outputs.

You will need to download the VB Cables, then set them up through windows as default with windows sound settings,then set Audacity's input to Microsoft sound mapper input, hit record on Audacity and play your script. You will notice once's it's finished on Audacity and then export what you need to loop.

This method may be out of the scope is not perfect but works on pyttsx3 and records in better quality than espeak.




回答5:


You can use Amazon Polly API as well:

An example using python is available below:

https://aws.amazon.com/blogs/machine-learning/convert-your-text-into-an-mp3-file-with-amazon-polly-and-a-simple-python-script/



来源:https://stackoverflow.com/questions/9900137/recording-synthesized-text-to-speech-to-a-file-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!