From d78351b8a1cdbd98a60b056f0bc61186d99449b0 Mon Sep 17 00:00:00 2001 From: Yihui Xiong Date: Mon, 4 Jun 2018 18:45:23 +0800 Subject: [PATCH] python3 compatible --- avs/alexa.py | 25 +++++++++++++------------ avs/interface/alerts.py | 2 +- avs/interface/speech_synthesizer.py | 4 +++- avs/mic/__init__.py | 4 ++-- avs/player/__init__.py | 12 ++++++------ 5 files changed, 25 insertions(+), 22 deletions(-) diff --git a/avs/alexa.py b/avs/alexa.py index bbeb002..f8cec1a 100755 --- a/avs/alexa.py +++ b/avs/alexa.py @@ -140,7 +140,7 @@ def _run(self): downchannel_response.headers['content-type'][0].decode('utf-8')) downchannel_boundary = '--{}'.format(pdict['boundary']).encode('utf-8') downchannel = conn.streams[downchannel_id] - downchannel_buffer = '' + downchannel_buffer = b'' eventchannel_boundary = 'seeed-voice-engine' # ping every 5 minutes (60 seconds early for latency) to maintain the connection @@ -148,8 +148,6 @@ def _run(self): self.event_queue.queue.clear() self.System.SynchronizeState() while not self.done: - # logger.info("Waiting for event to send to AVS") - # logger.info("Connection socket can_read %s", conn._sock.can_read) try: event, listener, attachment = self.event_queue.get( timeout=0.25) @@ -237,7 +235,7 @@ def _run(self): if response.status == 200: _, pdict = cgi.parse_header( response.headers['content-type'][0].decode('utf-8')) - boundary = b'--{}'.format(pdict['boundary']) + boundary = '--{}'.format(pdict['boundary']).encode('utf-8') self._parse_response(response.read(), boundary) elif response.status == 204: pass @@ -248,7 +246,7 @@ def _run(self): if listener and callable(listener): listener() - def _parse_response(self, response, boundary, buffer=''): + def _parse_response(self, response, boundary, buffer=b''): directives = [] blen = len(boundary) response = buffer + response @@ -260,17 +258,17 @@ def _parse_response(self, response, boundary, buffer=''): # skip small data block if pos > blen: # a blank line is between parts - parts = response[:pos - 2].split('\r\n\r\n', 1) - if parts[0].find('application/json') >= 0: + parts = response[:pos - 2].split(b'\r\n\r\n', 1) + if parts[0].find(b'application/json') >= 0: metadata = json.loads(parts[1].decode('utf-8')) if 'directive' in metadata: directives.append(metadata['directive']) - elif parts[0].find('application/octet-stream') >= 0: + elif parts[0].find(b'application/octet-stream') >= 0: for line in parts[0].splitlines(): - name, value = line.split(':', 1) - if name.lower() == 'content-id': + name, value = line.split(b':', 1) + if name.lower() == b'content-id': content_id = value.strip()[1:-1] - filename = base64.urlsafe_b64encode(content_id)[:8] + filename = base64.urlsafe_b64encode(content_id)[:8].decode('utf-8') with open(os.path.join(tempfile.gettempdir(), '{}.mp3'.format(filename)), 'wb') as f: f.write(parts[1]) logger.info('write audio to {}.mp3'.format(filename)) @@ -401,7 +399,7 @@ def signal_handler(sig, frame): signal.signal(signal.SIGINT, signal_handler) - while not is_quit.is_set(): + while True: try: input('press ENTER to talk\n') except SyntaxError: @@ -409,6 +407,9 @@ def signal_handler(sig, frame): except NameError: pass + if is_quit.is_set(): + break + alexa.listen() alexa.stop() diff --git a/avs/interface/alerts.py b/avs/interface/alerts.py index 3a500d3..7323cd9 100644 --- a/avs/interface/alerts.py +++ b/avs/interface/alerts.py @@ -40,7 +40,7 @@ def _stop(self): """ Stop all active alerts """ - for token in self.active_alerts.keys(): + for token in list(self.active_alerts.keys()): self.AlertStopped(token) self.active_alerts = {} diff --git a/avs/interface/speech_synthesizer.py b/avs/interface/speech_synthesizer.py index a8e5090..d8c3f42 100644 --- a/avs/interface/speech_synthesizer.py +++ b/avs/interface/speech_synthesizer.py @@ -71,7 +71,7 @@ def Speak(self, directive): self.token = directive['payload']['token'] url = directive['payload']['url'] if url.startswith('cid:'): - filename = base64.urlsafe_b64encode(url[4:])[:8] + filename = base64.urlsafe_b64encode(url[4:].encode('utf-8'))[:8].decode('utf-8') mp3_file = os.path.join(tempfile.gettempdir(), filename + '.mp3') if os.path.isfile(mp3_file): self.mp3_file = mp3_file @@ -87,6 +87,8 @@ def Speak(self, directive): # will be set at SpeechFinished() if the player reaches the End Of Stream or gets a error # self.finished.wait() + else: + logger.warning('not find {}'.format(mp3_file)) def SpeechStarted(self): self._state = 'PLAYING' diff --git a/avs/mic/__init__.py b/avs/mic/__init__.py index 9de57ac..2dd272a 100644 --- a/avs/mic/__init__.py +++ b/avs/mic/__init__.py @@ -5,8 +5,8 @@ recorder_option = os.getenv('RECORDER', 'default').lower() if recorder_option.find('pyaudio') >= 0 or os.system('which arecord >/dev/null') != 0: - from pyaudio_recorder import Audio + from .pyaudio_recorder import Audio else: - from alsa_recorder import Audio + from .alsa_recorder import Audio __all__ = ['Audio'] diff --git a/avs/player/__init__.py b/avs/player/__init__.py index 9d76f07..b1ea0e4 100644 --- a/avs/player/__init__.py +++ b/avs/player/__init__.py @@ -11,19 +11,19 @@ player_option = os.getenv('PLAYER', 'default').lower() if player_option.find('mpv') >= 0: - from mpv_player import Player + from .mpv_player import Player elif player_option.find('mpg123') >= 0: - from mpg123_player import Player + from .mpg123_player import Player elif player_option.find('gstreamer') >= 0: - from gstreamer_player import Player + from .gstreamer_player import Player else: try: - from gstreamer_player import Player + from .gstreamer_player import Player except ImportError: if os.system('which mpv >/dev/null') == 0: - from mpv_player import Player + from .mpv_player import Player elif os.system('which mpg123 >/dev/null') == 0: - from mpg123_player import Player + from .mpg123_player import Player else: raise ImportError('No player available, install one of the players: gstreamer, mpv and mpg123 first')