diff --git a/avs/alexa.py b/avs/alexa.py index 60edc53..bbeb002 100755 --- a/avs/alexa.py +++ b/avs/alexa.py @@ -1,17 +1,16 @@ # -*- coding: utf-8 -*- +import base64 import cgi -import io import json import logging import os +import signal import sys import tempfile -import uuid -import base64 -import signal import threading +import uuid if sys.version_info < (3, 0): import Queue as queue @@ -261,7 +260,7 @@ 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) + parts = response[:pos - 2].split('\r\n\r\n', 1) if parts[0].find('application/json') >= 0: metadata = json.loads(parts[1].decode('utf-8')) if 'directive' in metadata: @@ -277,7 +276,7 @@ def _parse_response(self, response, boundary, buffer=''): logger.info('write audio to {}.mp3'.format(filename)) break - response = response[pos+blen+2:] + response = response[pos + blen + 2:] for directive in directives: self._handle_directive(directive) @@ -396,7 +395,7 @@ def main(): is_quit = threading.Event() - def signal_handler(signal, frame): + def signal_handler(sig, frame): print('Quit') is_quit.set() diff --git a/avs/auth.py b/avs/auth.py index 1d9f7a5..439dfba 100644 --- a/avs/auth.py +++ b/avs/auth.py @@ -1,13 +1,13 @@ +import datetime +import json +import time +import uuid +import click +import requests import tornado.httpserver import tornado.ioloop import tornado.web -import time -import json -import uuid -import requests -import datetime -import click import avs.config @@ -116,4 +116,3 @@ def main(config, output): if __name__ == '__main__': main() - diff --git a/avs/check.py b/avs/check.py index d413bb0..0a3405d 100644 --- a/avs/check.py +++ b/avs/check.py @@ -6,10 +6,11 @@ It depends on respeaker python library (https://github.com/respeaker/respeaker_python_library) """ -import signal import audioop import os +import signal import time + from avs.mic import Audio from avs.player import Player @@ -37,6 +38,7 @@ def main(): player2 = Player() is_quit = [] + def signal_handler(signal, frame): print('Quit') is_quit.append(True) @@ -51,7 +53,6 @@ def signal_handler(signal, frame): time.sleep(3) player2.pause() - audio.stop() diff --git a/avs/interface/alerts.py b/avs/interface/alerts.py index 3e995e6..3a500d3 100644 --- a/avs/interface/alerts.py +++ b/avs/interface/alerts.py @@ -2,12 +2,13 @@ """https://developer.amazon.com/public/solutions/alexa/alexa-voice-service/reference/alerts""" +import datetime import os import time -import datetime -import dateutil.parser -from threading import Timer, Event import uuid +from threading import Timer, Event + +import dateutil.parser # prefer mpg123 player as it is more responsive than mpv and gstreamer if os.system('which mpg123 >/dev/null') == 0: diff --git a/avs/interface/audio_player.py b/avs/interface/audio_player.py index 6f5bdbe..87c8c5b 100644 --- a/avs/interface/audio_player.py +++ b/avs/interface/audio_player.py @@ -2,21 +2,21 @@ """https://developer.amazon.com/public/solutions/alexa/alexa-voice-service/reference/audioplayer""" -import os -import time -import tempfile -import uuid import base64 import hashlib -import requests import logging +import os +import tempfile +import uuid from contextlib import closing + +import requests + try: from urllib.parse import urlparse except ImportError: from urlparse import urlparse - from avs.player import Player logger = logging.getLogger('AudioPlayer') diff --git a/avs/interface/speech_recognizer.py b/avs/interface/speech_recognizer.py index 5c948bf..b42b3f7 100644 --- a/avs/interface/speech_recognizer.py +++ b/avs/interface/speech_recognizer.py @@ -4,8 +4,8 @@ import logging import sys -import time import threading +import time import uuid if sys.version_info < (3, 0): diff --git a/avs/interface/speech_synthesizer.py b/avs/interface/speech_synthesizer.py index e9e8827..a8e5090 100644 --- a/avs/interface/speech_synthesizer.py +++ b/avs/interface/speech_synthesizer.py @@ -1,9 +1,9 @@ +import base64 +import logging import os import tempfile import threading import uuid -import base64 -import logging # prefer mpg123 player as it is more responsive than mpv and gstreamer if os.system('which mpg123 >/dev/null') == 0: diff --git a/avs/interface/system.py b/avs/interface/system.py index 18cbf6b..e274a3a 100755 --- a/avs/interface/system.py +++ b/avs/interface/system.py @@ -1,8 +1,7 @@ - """https://developer.amazon.com/public/solutions/alexa/alexa-voice-service/reference/system""" -import uuid import datetime +import uuid class System(object): diff --git a/avs/main.py b/avs/main.py index 8920900..a19585f 100644 --- a/avs/main.py +++ b/avs/main.py @@ -6,12 +6,11 @@ It depends on respeaker python library (https://github.com/respeaker/respeaker_python_library) """ - -import sys +import logging import signal -import time +import sys import threading -import logging +import time if sys.version_info < (3, 0): import Queue as queue @@ -22,10 +21,10 @@ from avs.mic import Audio from respeaker.pixel_ring import pixel_ring - logger = logging.getLogger(__file__) logging.basicConfig(level=logging.INFO) + class KWS(object): def __init__(self): self.queue = queue.Queue() @@ -112,7 +111,7 @@ def wakeup(keyword): audio.start() is_quit = threading.Event() - + def signal_handler(signal, frame): print('Quit') is_quit.set() diff --git a/avs/mic/__init__.py b/avs/mic/__init__.py index eef842d..9de57ac 100644 --- a/avs/mic/__init__.py +++ b/avs/mic/__init__.py @@ -2,7 +2,6 @@ import os - recorder_option = os.getenv('RECORDER', 'default').lower() if recorder_option.find('pyaudio') >= 0 or os.system('which arecord >/dev/null') != 0: @@ -10,5 +9,4 @@ else: from alsa_recorder import Audio - __all__ = ['Audio'] diff --git a/avs/mic/alsa_recorder.py b/avs/mic/alsa_recorder.py index 0c3765f..b0eeaa3 100755 --- a/avs/mic/alsa_recorder.py +++ b/avs/mic/alsa_recorder.py @@ -1,12 +1,10 @@ # -*- coding: utf-8 -*- -import os import subprocess import threading class Audio(object): - def __init__(self, rate=16000, frames_size=160, channels=1, device_name='default'): self.rate = rate self.frames_size = frames_size diff --git a/avs/mic/pyaudio_recorder.py b/avs/mic/pyaudio_recorder.py index 090bcb3..966de09 100755 --- a/avs/mic/pyaudio_recorder.py +++ b/avs/mic/pyaudio_recorder.py @@ -1,14 +1,13 @@ # -*- coding: utf-8 -*- -import pyaudio import logging +import pyaudio logger = logging.getLogger(__file__) class Audio(object): - def __init__(self, rate=16000, frames_size=None, channels=None, device_index=None): self.sample_rate = rate self.frames_size = frames_size if frames_size else rate / 100 @@ -64,5 +63,3 @@ def link(self, sink): def unlink(self, sink): self.sinks.remove(sink) - - diff --git a/avs/player/__init__.py b/avs/player/__init__.py index 35f3444..9d76f07 100644 --- a/avs/player/__init__.py +++ b/avs/player/__init__.py @@ -27,6 +27,4 @@ else: raise ImportError('No player available, install one of the players: gstreamer, mpv and mpg123 first') - __all__ = ['Player'] - diff --git a/avs/player/gstreamer_player.py b/avs/player/gstreamer_player.py index 0fde5af..8e7cc15 100644 --- a/avs/player/gstreamer_player.py +++ b/avs/player/gstreamer_player.py @@ -2,10 +2,10 @@ """Player using gstreamer.""" -import time +import gi import threading +import time -import gi gi.require_version('Gst', '1.0') from gi.repository import Gst, GLib, GObject @@ -68,8 +68,8 @@ def on_message(self, bus, message): self.player.set_state(Gst.State.NULL) if 'error' in self.callbacks: self.callbacks['error']() - # else: - # print(message.type) + # else: + # print(message.type) @property def duration(self): @@ -101,4 +101,3 @@ def state(self): # GST_STATE_PLAYING the element is PLAYING, the GstClock is running and the data is flowing. _, state, _ = self.player.get_state(Gst.SECOND) return 'FINISHED' if state != Gst.State.PLAYING else 'PLAYING' - diff --git a/avs/player/mpg123_player.py b/avs/player/mpg123_player.py index 2566f3f..0974190 100644 --- a/avs/player/mpg123_player.py +++ b/avs/player/mpg123_player.py @@ -3,13 +3,13 @@ """Player using mpg123""" import os -import signal -import threading import subprocess +import threading if os.system('which mpg123 >/dev/null') != 0: ImportError('mpg123 not found, install it first') + class Player(object): def __init__(self): self.callbacks = {} @@ -30,7 +30,7 @@ def _run(self): print('Playing {}'.format(self.audio)) master, slave = os.openpty() - self.process = subprocess.Popen(['mpg123','-q', '-C', self.audio], stdin=master) + self.process = subprocess.Popen(['mpg123', '-q', '-C', self.audio], stdin=master) self.tty = slave self.process.wait() @@ -48,9 +48,9 @@ def play(self, uri): if self.process and self.process.poll() == None: os.write(self.tty, 'q') - + self.state = 'PLAYING' - + def stop(self): if self.process and self.process.poll() == None: os.write(self.tty, 'q') diff --git a/avs/player/mpv_player.py b/avs/player/mpv_player.py index 358e602..60f5d95 100644 --- a/avs/player/mpv_player.py +++ b/avs/player/mpv_player.py @@ -3,9 +3,8 @@ """Player using MPV""" import os -import signal -import threading import subprocess +import threading if os.system('which mpv >/dev/null') != 0: raise ImportError('mpv not found, install it first') @@ -46,9 +45,9 @@ def play(self, uri): if self.process and self.process.poll() == None: os.write(self.tty, 'q') - + self.state = 'PLAYING' - + def stop(self): if self.process and self.process.poll() == None: os.write(self.tty, 'q')