Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongyihui committed May 30, 2018
1 parent eef6c7e commit 95901e5
Show file tree
Hide file tree
Showing 16 changed files with 47 additions and 60 deletions.
13 changes: 6 additions & 7 deletions avs/alexa.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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()

Expand Down
13 changes: 6 additions & 7 deletions avs/auth.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -116,4 +116,3 @@ def main(config, output):

if __name__ == '__main__':
main()

5 changes: 3 additions & 2 deletions avs/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -37,6 +38,7 @@ def main():
player2 = Player()

is_quit = []

def signal_handler(signal, frame):
print('Quit')
is_quit.append(True)
Expand All @@ -51,7 +53,6 @@ def signal_handler(signal, frame):
time.sleep(3)
player2.pause()


audio.stop()


Expand Down
7 changes: 4 additions & 3 deletions avs/interface/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions avs/interface/audio_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion avs/interface/speech_recognizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import logging
import sys
import time
import threading
import time
import uuid

if sys.version_info < (3, 0):
Expand Down
4 changes: 2 additions & 2 deletions avs/interface/speech_synthesizer.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
3 changes: 1 addition & 2 deletions avs/interface/system.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
11 changes: 5 additions & 6 deletions avs/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -112,7 +111,7 @@ def wakeup(keyword):
audio.start()

is_quit = threading.Event()

def signal_handler(signal, frame):
print('Quit')
is_quit.set()
Expand Down
2 changes: 0 additions & 2 deletions avs/mic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import os


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
else:
from alsa_recorder import Audio


__all__ = ['Audio']
2 changes: 0 additions & 2 deletions avs/mic/alsa_recorder.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 1 addition & 4 deletions avs/mic/pyaudio_recorder.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -64,5 +63,3 @@ def link(self, sink):

def unlink(self, sink):
self.sinks.remove(sink)


2 changes: 0 additions & 2 deletions avs/player/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,4 @@
else:
raise ImportError('No player available, install one of the players: gstreamer, mpv and mpg123 first')


__all__ = ['Player']

9 changes: 4 additions & 5 deletions avs/player/gstreamer_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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'

10 changes: 5 additions & 5 deletions avs/player/mpg123_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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()
Expand All @@ -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')
Expand Down
7 changes: 3 additions & 4 deletions avs/player/mpv_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 95901e5

Please sign in to comment.