2016-08-18 13 views
0

私のコードが正しく動作しない理由を解明しようとしています。私はRecording synthesized text-to-speech to a file in Pythonから解決策を使用し、それはちょっと私のために働いていませんでした。問題は、なぜ2つのメソッド/関数text_to_wavとall_texts_to_filesが私にとってうまくいかないかということです。ここPyTTSxの出力をwavファイルに保存するには

import json 
import pyttsx 
from openpyxl import load_workbook 
import subprocess 

class Ver2ProjectWithTTS(object): 

    def __init__(self): 
     self.list_merge = [] 

    def do_the_job(self): 
     self.read_json_file() 
     self.read_xml_file() 
     #self.say_something() 
     self.all_texts_to_files() 

    def read_json_file(self): 
     with open("json-example.json", 'r') as df: 
      json_data = json.load(df) 
      df.close() 
     for k in json_data['sentences']: 
      text_json = k['text'] 
      speed_json = int(k['speed']) 
      volume_json = float(k['volume']) 
      dict_json = {'text': text_json, 'speed': speed_json, 'volume': volume_json} 
      self.list_merge.append(dict_json) 

    def read_xml_file(self): 
     tree = et.parse('xml-example.xml') 
     root = tree.getroot() 
     for k in range(0, len(root)): 
      text_xml = root[k][0].text 
      speed_xml = int(root[k][1].text) 
      volume_xml = float(root[k][2].text) 
      dict_xml = {'text': text_xml, 'speed': speed_xml, 'volume': volume_xml} 
      self.list_merge.append(dict_xml) 

    def say_something(self): 
     for item in self.list_merge: 
      engine = pyttsx.init() 
      engine.getProperty('rate') 
      engine.getProperty('volume') 
      engine.setProperty('rate', item['speed']) 
      engine.setProperty('volume', item['volume']) 
      engine.say(cleared_text) 
      engine.runAndWait() 

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

    def all_texts_to_files(self): 
     for item in self.list_merge: 
      cleared_text = self.clear_text_from_underscores(item['text']) 
      self.text_to_wav(cleared_text, item['text']) 

if __name__ == '__main__': 
    a = Ver2ProjectWithTTS() 
    a.do_the_job() 

エラーコード:

#In my project: 
line 91, in <module> a.do_the_job() 
line 21, in do_the_job self.all_texts_to_files() 
line 85, in all_texts_to_files self.text_to_wav(cleared_text, item['text']) 
line 80, in text_to_wav subprocess.call(["espeak", "-w"+file_name+".wav", text]) 
#in subprocess: 
line 523, in call return Popen(*popenargs, **kwargs).wait() 
line 711, in __init__ errread, errwrite) 
line 959, in _execute_child startupinfo) 
WindowsError: [Error 2] The system cannot find the file specified 
+0

はパス上のespeakですか? – dabhand

+0

ええ、それは道にあります。 – degath

+0

'item ['text']'にフルパスのファイル名が含まれていますか? 'text_to_wav'メソッドの中の' file_name'に出力し、 – Mourya

答えて

0

フル出力ファイルのパスを、あなたがサブプロセスへの完全なパスを指定する必要があります 、勝利のOS上でのPythonを使っていると仮定 そしてもちろん例えば、 ;

espeak_path = "C:/Program Files/eSpeak/command_line/espeak.exe" 
file_name = "C:/temp/test" 
subprocess.call([espeak_path,"-w"+file_name+".wav", text]) 
+0

sys.path.insert(0、espeak_module) – pyaddict

+0

開始時にPython用のsys.pathが含まれている可能性があります。 – pyaddict

+0

サブプロセスを呼び出すときに見落とされる可能性がありますが、これはLinux OS上で問題ではないようです。 – pyaddict

-1
from gtts import gTTS 
import os 
tts = gTTS(text='hi how r u', lang='en') 
tts.save("good.wav") 
os.system("mpg321 good.wav") 

このコードの意志の出力はuがインストールウルのpythonフォルダに保存されます。さまざまなオーディオフォーマットの は、拡張ファイルを変更するだけです。

+0

接尾辞を指定したため、 '.wav'ファイルを保存していないようです。同じテキストの場合、生成されたオーディオファイルは、接尾辞に関係なく、ビットごとに同一のファイルを持ちます。 –

関連する問題