本文整理匯總了Python中recorder.Recorder.convert_to_flac方法的典型用法代碼示例。如果您正苦於以下問題:Python Recorder.convert_to_flac方法的具體用法?Python Recorder.convert_to_flac怎麽用?Python Recorder.convert_to_flac使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類recorder.Recorder
的用法示例。
在下文中一共展示了Recorder.convert_to_flac方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from recorder import Recorder [as 別名]
# 或者: from recorder.Recorder import convert_to_flac [as 別名]
class SpeechToSpeech:
def __init__(self, config_path):
# read config file
config = ConfigParser.RawConfigParser()
config.read(config_path)
# general
self.tmp_file = config.get('general', 'tmp_file')
# gsr api
self.gsr_api_lang = config.get('gsr_api', 'language')
# moses
self.e = config.get('moses', 'e')
self.f = config.get('moses', 'f')
self.moses_config_orig = config.get('moses', 'config_orig')
self.moses_config_fix = config.get('moses', 'config_fix')
self.moses_dir = config.get('moses', 'moses_dir')
self.model_dir = config.get('moses', 'model_dir')
self.model_dir_jenkins = config.get('moses', 'model_dir_jenkins')
self.threads = config.getint('moses', 'threads')
# pyttsx
self.tts_voice_id = config.get('pyttsx', 'voice')
self.tts_voice_rate = config.getint('pyttsx', 'rate')
# create recorder
self.recorder = Recorder(language=self.gsr_api_lang)
@staticmethod
def replace_lines(in_file, out_file, from_line, to_line):
with open(out_file, 'wt') as cfg_fix:
with open(in_file, 'rt') as cfg_orig:
for line in cfg_orig:
cfg_fix.write(line.replace(from_line, to_line))
def fix_moses_config(self):
# fix moses config
cfg_fix = self.model_dir + self.moses_config_fix
cfg_orig = self.model_dir + self.moses_config_orig
line_orig = self.model_dir_jenkins
line_fix = self.model_dir
self.replace_lines(cfg_orig, cfg_fix, line_orig, line_fix)
def moses_translation(self, stt_file):
# use shell for translation
shell_cmd = 'cat {5} |' \
'{0}scripts/tokenizer/tokenizer.perl -threads {2} -l {3} |' \
'{0}bin/moses -threads {2} -f {1}moses-sts.ini |' \
'{0}scripts/tokenizer/detokenizer.perl -threads {2} -l {4}'.format(self.moses_dir,
self.model_dir,
self.threads,
self.f,
self.e,
stt_file)
# run translation
moses_process = subprocess.Popen(shell_cmd, shell=True, stdout=subprocess.PIPE)
return moses_process.stdout.read()
def record_sample(self):
flac_file = self.recorder.record()
file_name, _ = os.path.splitext(flac_file)
stt_file = self.recorder.speech_to_text(flac_file)
return stt_file
def translate(self, stt_file):
if os.path.isfile(stt_file) and os.path.getsize(stt_file) > 0:
file_name, _ = os.path.splitext(stt_file)
# fix moses config file downloaded from jenkins
self.fix_moses_config()
translation = self.moses_translation(stt_file)
# save translation
trans_file_name = file_name + '-translated.txt'
with open(trans_file_name, 'wt') as trans_file:
trans_file.write(translation)
print '* saved translation in', trans_file_name
return translation.decode('utf8')
def translate_file(self, file):
flac_file = self.recorder.convert_to_flac(file)
stt_file = self.recorder.speech_to_text(flac_file)
return self.translate(stt_file)
def translate_text(self, text):
with open(self.tmp_file, 'wb') as tmp_file:
tmp_file.write(text)
return self.translate(self.tmp_file)
def tts(self, text):
print '* tts:', text
engine = pyttsx.init()
#.........這裏部分代碼省略.........