def __init__(self, text, lang = 'en', slow = False, debug = False): self.debug = debug if lang.lower() not in self.LANGUAGES: raise Exception('Language not supported: %s' % lang) else: self.lang = lang.lower() if not text: raise Exception('No text to speak') else: self.text = text # Read speed if slow: self.speed = self.Speed().SLOW else: self.speed = self.Speed().NORMAL # Split text in parts if self._len(text) <= self.MAX_CHARS: text_parts = [text] else: text_parts = self._tokenize(text, self.MAX_CHARS) # Clean def strip(x): return x.replace('\n', '').strip() text_parts = [strip(x) for x in text_parts] text_parts = [x for x in text_parts if len(x) > 0] self.text_parts = text_parts # Google Translate token self.token = Token()
def __init__(self, text, lang='en', slow=False, debug=False): self.debug = debug if lang.lower() not in self.LANGUAGES: raise Exception('Language not supported: %s' % lang) else: self.lang = lang.lower() if not text: raise Exception('No text to speak') else: self.text = text # Read speed if slow: self.speed = self.Speed().SLOW else: self.speed = self.Speed().NORMAL self.text_parts = self._get_text_parts(self.text) # Google Translate token self.token = Token()
def say(self, message): # Need to subscribe to transport events, this is so that we know # when a given track has finished, and so we can stop it, if # we do not stop it, then it will repeat the text for a second time sub = self.device.avTransport.subscribe() # fade out #prefade_volume = self.device.volume #for v in range(prefade_volume): # self.device.volume -= 1 # time.sleep(0.25) # Take a snapshot of the current sonos device state, we will want # to roll back to this when we are done snap = Snapshot(self.device) snap.snapshot() msg = cgi.escape(message) payload = { 'ie': 'UTF-8', 'q': message, 'tl': 'en', 'total': 1, 'idx': 0, 'client': 't', 'textlen': len(message), 'tk': Token().calculate_token(message) } #trans_URL = "x-rincon-mp3radio://translate.google.com/translate_tts?tl=en&q=%s" % msg trans_URL = "x-rincon-mp3radio://translate.google.com/translate_tts?" + urlencode( payload) print trans_URL #from IPython import embed #embed() self.device.play_uri(trans_URL, title="Speech") #self.device.volume = prefade_volume impatience = time.time() patience = time.time() + 20 while patience > impatience: try: event = sub.events.get(timeout=0.5) print event.variables if 'restart_pending' not in event.variables: continue restart_pending = event.variables['restart_pending'] # About to try and restart, so stop looping and stop the # track before it starts again if restart_pending == '1': break except Empty: pass # Wait another second for the speech to stop playing time.sleep(1) impatience = time.time() time.sleep(0) # Stop the stream playing self.device.stop() # Restore the sonos device back to it's previous state snap.restore() # fade back in #for v in range(prefade_volume): # self.device.volume += 1 # time.sleep(0.25) # We no longer want to receive messages sub.unsubscribe() event_listener.stop()