def editConfig(): import sys if sys.platform.startswith('win'): cmd = 'cmd /c "%s"' % CONFIG_PATH elif sys.platform.startswith('darwin'): cmd = 'open "%s"' % CONFIG_PATH else: cmd = os.environ.get('EDITOR') or os.environ.get('HGEDITOR') or os.environ.get('VISUAL') if not cmd: if util.commandIsAvailable('nano'): cmd = 'nano %s' % CONFIG_PATH elif util.commandIsAvailable('pico'): cmd = 'pico %s' % CONFIG_PATH else: cmd = 'vi %s' % CONFIG_PATH os.system(cmd)
def update(self): self.voice = self.setting('voice') self.rate = self.setting('speed') self.useAOSS = self.setting('use_aoss') if self.useAOSS and not util.commandIsAvailable('aoss'): util.LOG('Cepstral: Use aoss is enabled, but aoss is not found. Disabling.') self.useAOSS = False volume = self.setting('volume') self.volume = int(round(100 * (10**(volume/20.0)))) #convert from dB to percent pitch = self.setting('pitch') self.pitch = 0.4 + ((pitch+6)/20.0) * 2 #Convert from (-6 to +14) value to (0.4 to 2.4)
def available(): try: subprocess.call(['flite', '--help'], stdout=(open(os.path.devnull, 'w')), stderr=subprocess.STDOUT) except (OSError, IOError): return util.isATV2() and util.commandIsAvailable('flite') return True #class FliteTTSBackend(TTSBackendBase): # provider = 'Flite' # def __init__(self): # import ctypes # self.flite = ctypes.CDLL('libflite.so.1',mode=ctypes.RTLD_GLOBAL) # flite_usenglish = ctypes.CDLL('libflite_usenglish.so.1',mode=ctypes.RTLD_GLOBAL) #analysis:ignore # flite_cmulex = ctypes.CDLL('libflite_cmulex.so.1',mode=ctypes.RTLD_GLOBAL) #analysis:ignore # flite_cmu_us_slt = ctypes.CDLL('libflite_cmu_us_slt.so.1') # self.flite.flite_init() # self.voice = flite_cmu_us_slt.register_cmu_us_slt() # # def say(self,text,interrupt=False): # if not text: return # self.flite.flite_text_to_speech(text,self.voice,'play') # # # @staticmethod # def available(): # try: # import ctypes # ctypes.CDLL('libflite.so.1') # except (OSError, IOError): # return False # return True #class FliteTTSBackend(TTSBackendBase): # provider = 'Flite' # # def say(self,text,interrupt=False): # if not text: return # voice = self.currentVoice() or 'kal16' # subprocess.call(['flite', '-voice', voice, '-t', text]) # # def voices(self): # return subprocess.check_output(['flite','-lv']).split(': ',1)[-1].strip().split(' ') # # @staticmethod # def available(): # try: # subprocess.call(['flite', '--help'], stdout=(open(os.path.devnull, 'w')), stderr=subprocess.STDOUT) # except (OSError, IOError): # return False # return True
class GoogleTTSBackend(base.SimpleTTSBackendBase): provider = 'Google' displayName = 'Google' ttsURL = 'http://translate.google.com/translate_tts?tl={0}&q={1}' canStreamWav = util.commandIsAvailable('mpg123') playerClass = audio.MP3AudioPlayerHandler settings = { 'language': 'en', 'player': 'mpg123', 'volume': 0, 'pipe': False } def init(self): self.process = None self.update() def threadedSay(self, text): if not text: return sections = textwrap.wrap(text, 100) if self.mode == self.PIPE: for text in sections: source = self.runCommandAndPipe(text) if not source: continue self.player.pipeAudio(source) else: for text in sections: outFile = self.player.getOutFile(text) if not self.runCommand(text, outFile): return self.player.play() def runCommand(self, text, outFile): url = self.ttsURL.format(self.language, urllib.quote(text.encode('utf-8'))) req = urllib2.Request( url, headers={ 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36' }) try: resp = urllib2.urlopen(req) except: util.ERROR('Failed to open Google TTS URL', hide_tb=True) return False with open(outFile, 'wb') as out: shutil.copyfileobj(resp, out) return True def runCommandAndPipe(self, text): url = self.ttsURL.format(self.language, urllib.quote(text.encode('utf-8'))) req = urllib2.Request( url, headers={ 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36' }) try: resp = urllib2.urlopen(req) except: util.ERROR('Failed to open Google TTS URL', hide_tb=True) return None return resp def getWavStream(self, text): wav_path = os.path.join(util.getTmpfs(), 'speech.wav') mp3_path = os.path.join(util.getTmpfs(), 'speech.mp3') self.runCommand(text, mp3_path) self.process = subprocess.Popen(['mpg123', '-w', wav_path, mp3_path], stdout=(open(os.path.devnull, 'w')), stderr=subprocess.STDOUT) while self.process.poll() == None and self.active: util.sleep(10) os.remove(mp3_path) return open(wav_path, 'rb') def update(self): self.language = self.setting('language') self.setPlayer(self.setting('player')) self.setVolume(self.setting('volume')) self.setMode(self.getMode()) def getMode(self): if self.setting('pipe'): return base.SimpleTTSBackendBase.PIPE else: return base.SimpleTTSBackendBase.WAVOUT def stop(self): if not self.process: return try: self.process.terminate() except: pass @classmethod def settingList(cls, setting, *args): if setting == 'language': return LANGUAGES return None @staticmethod def available(): return audio.MP3AudioPlayerHandler.canPlay()