Skip to content

Commit

Permalink
Fix some formatting issues exposed by pep8
Browse files Browse the repository at this point in the history
  • Loading branch information
enetor committed Jul 12, 2017
1 parent ce2d369 commit 9c0f3e3
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 165 deletions.
1 change: 1 addition & 0 deletions src/aiy/_drivers/_alsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
"""Helpers for ALSA tools, including aplay and arecord."""


def sample_width_to_string(sample_width):
"""Convert sample width (bytes) to ALSA format string."""
return {1: 's8', 2: 's16', 4: 's32'}[sample_width]
1 change: 1 addition & 0 deletions src/aiy/_drivers/_led.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import RPi.GPIO as GPIO


class LED:
"""Starts a background thread to show patterns with the LED.
Expand Down
1 change: 1 addition & 0 deletions src/aiy/assistant/_auth_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def save_credentials(credentials_path, credentials):
'scopes': credentials.scopes
}, f)


def try_to_get_credentials(client_secrets):
"""Try to get credentials, or print an error and quit on failure."""

Expand Down
87 changes: 44 additions & 43 deletions src/aiy/assistant/grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,55 +27,56 @@
# Expected location of the Assistant credentials file:
ASSISTANT_CREDENTIALS_FILE = os.path.expanduser('~/assistant.json')


class _AssistantRecognizer(object):
"""Your personal Google Assistant."""
"""Your personal Google Assistant."""

def __init__(self, credentials_file):
self._request = aiy._apis._speech.AssistantSpeechRequest(credentials_file)
self._recorder = aiy.audio.get_recorder()
def __init__(self, credentials_file):
self._request = aiy._apis._speech.AssistantSpeechRequest(credentials_file)
self._recorder = aiy.audio.get_recorder()

def recognize(self):
"""Recognizes the user's speech and gets answers from Google Assistant.
def recognize(self):
"""Recognizes the user's speech and gets answers from Google Assistant.
This function listens to the user's speech via the VoiceHat speaker and
sends the audio to the Google Assistant Library. The response is returned in
both text and audio.
This function listens to the user's speech via the VoiceHat speaker and
sends the audio to the Google Assistant Library. The response is returned in
both text and audio.
Usage:
transcript, audio = my_recognizer.recognize()
if transcript is not None:
print('You said ', transcript)
aiy.audio.play_audio(audio)
"""
self._request.reset()
self._request.set_endpointer_cb(self._endpointer_callback)
self._recorder.add_processor(self._request)
response = self._request.do_request()
return response.transcript, response.response_audio
Usage:
transcript, audio = my_recognizer.recognize()
if transcript is not None:
print('You said ', transcript)
aiy.audio.play_audio(audio)
"""
self._request.reset()
self._request.set_endpointer_cb(self._endpointer_callback)
self._recorder.add_processor(self._request)
response = self._request.do_request()
return response.transcript, response.response_audio

def _endpointer_callback(self):
self._recorder.remove_processor(self._request)
def _endpointer_callback(self):
self._recorder.remove_processor(self._request)

def get_assistant():
"""Returns a recognizer that uses Google Assistant APIs.

Sample usage:
button = aiy.voicehat.get_button()
recognizer = aiy.assistant.grpc.get_recognizer()
print('Your Google Assistant is ready.')
while True:
print('Press the button and speak')
button.wait_for_press()
print('Listening...')
transcript, audio = recognizer.recognize()
if transcript is not None:
print('Assistant said ', transcript)
if audio is not None:
aiy.audio.play_audio(audio)
"""
global assistant_recognizer
if assistant_recognizer is None:
credentials = aiy.assistant._auth_helpers.try_to_get_credentials(ASSISTANT_CREDENTIALS_FILE)
assistant_recognizer = _AssistantRecognizer(credentials)
return assistant_recognizer
def get_assistant():
"""Returns a recognizer that uses Google Assistant APIs.
Sample usage:
button = aiy.voicehat.get_button()
recognizer = aiy.assistant.grpc.get_recognizer()
print('Your Google Assistant is ready.')
while True:
print('Press the button and speak')
button.wait_for_press()
print('Listening...')
transcript, audio = recognizer.recognize()
if transcript is not None:
print('Assistant said ', transcript)
if audio is not None:
aiy.audio.play_audio(audio)
"""
global assistant_recognizer
if assistant_recognizer is None:
credentials = aiy.assistant._auth_helpers.try_to_get_credentials(ASSISTANT_CREDENTIALS_FILE)
assistant_recognizer = _AssistantRecognizer(credentials)
return assistant_recognizer
59 changes: 32 additions & 27 deletions src/aiy/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,42 +25,47 @@
voicehat_recorder = None
voicehat_player = None


def get_player():
"""Returns a driver to control the VoiceHat speaker.
"""Returns a driver to control the VoiceHat speaker.
The aiy modules automatically use this player. So usually you do not need to
use this. Instead, use 'aiy.audio.play_wave' if you would like to play some
audio.
"""
global voicehat_player
if voicehat_player is None:
voicehat_player = aiy._drivers._player.Player()
return voicehat_player

The aiy modules automatically use this player. So usually you do not need to
use this. Instead, use 'aiy.audio.play_wave' if you would like to play some
audio.
"""
global voicehat_player
if voicehat_player is None:
voicehat_player = aiy._drivers._player.Player()
return voicehat_player

def get_recorder():
"""Returns a driver to control the VoiceHat microphones.
"""Returns a driver to control the VoiceHat microphones.
The aiy modules automatically use this recorder. So usually you do not need to
use this.
"""
global voicehat_recorder
if voicehat_recorder is None:
voicehat_recorder = aiy._drivers._recorder.Recorder()
return voicehat_recorder

The aiy modules automatically use this recorder. So usually you do not need to
use this.
"""
global voicehat_recorder
if voicehat_recorder is None:
voicehat_recorder = aiy._drivers._recorder.Recorder()
return voicehat_recorder

def play_wave(wave_file):
"""Plays the given wave file.
"""Plays the given wave file.
The wave file has to be mono and small enough to be loaded in memory.
"""
player = get_player()
player.play_wav(wave_file)

The wave file has to be mono and small enough to be loaded in memory.
"""
player = get_player()
player.play_wav(wave_file)

def play_audio(audio_data):
"""Plays the given audio data."""
player = get_player()
player.play_bytes(audio_data, sample_width=AUDIO_SAMPLE_SIZE, sample_rate=AUDIO_SAMPLE_RATE_HZ)
"""Plays the given audio data."""
player = get_player()
player.play_bytes(audio_data, sample_width=AUDIO_SAMPLE_SIZE, sample_rate=AUDIO_SAMPLE_RATE_HZ)


def say(words, lang='en-US'):
"""Says the given words in the given language with Google TTS engine."""
aiy._drivers._tts.say(aiy.audio.get_player(), words, lang=lang)
"""Says the given words in the given language with Google TTS engine."""
aiy._drivers._tts.say(aiy.audio.get_player(), words, lang=lang)
89 changes: 45 additions & 44 deletions src/aiy/cloudspeech.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,58 +26,59 @@
# Expected location of the CloudSpeech credentials file:
CLOUDSPEECH_CREDENTIALS_FILE = os.path.expanduser('~/cloud_speech.json')


class _CloudSpeechRecognizer(object):
"""A speech recognizer backed by the Google CloudSpeech APIs.
"""
"""A speech recognizer backed by the Google CloudSpeech APIs.
"""

def __init__(self, credentials_file):
self._request = aiy._apis._speech.CloudSpeechRequest(credentials_file)
self._recorder = aiy.audio.get_recorder()
def __init__(self, credentials_file):
self._request = aiy._apis._speech.CloudSpeechRequest(credentials_file)
self._recorder = aiy.audio.get_recorder()

def recognize(self):
"""Recognizes the user's speech and transcript it into text.
def recognize(self):
"""Recognizes the user's speech and transcript it into text.
This function listens to the user's speech via the VoiceHat speaker. Then it
contacts Google CloudSpeech APIs and returns a textual transcript if possible.
"""
self._request.reset()
self._request.set_endpointer_cb(self._endpointer_callback)
self._recorder.add_processor(self._request)
return self._request.do_request().transcript
This function listens to the user's speech via the VoiceHat speaker. Then it
contacts Google CloudSpeech APIs and returns a textual transcript if possible.
"""
self._request.reset()
self._request.set_endpointer_cb(self._endpointer_callback)
self._recorder.add_processor(self._request)
return self._request.do_request().transcript

def expect_phrase(self, phrase):
"""Explicitly tells the engine that the phrase is more likely to appear.
def expect_phrase(self, phrase):
"""Explicitly tells the engine that the phrase is more likely to appear.
This method is optional and makes speech recognition more accurate
especially when certain commands are expected.
This method is optional and makes speech recognition more accurate
especially when certain commands are expected.
For example, a light control system may want to add the following commands:
For example, a light control system may want to add the following commands:
recognizer.expect_phrase('light on')
recognizer.expect_phrase('light off')
"""
self._request.add_phrase(phrase)
recognizer.expect_phrase('light on')
recognizer.expect_phrase('light off')
"""
self._request.add_phrase(phrase)

def _endpointer_callback(self):
self._recorder.remove_processor(self._request)
def _endpointer_callback(self):
self._recorder.remove_processor(self._request)

def get_recognizer():
"""Returns a recognizer that uses Google CloudSpeech APIs.
Sample usage:
button = aiy.voicehat.get_button()
recognizer = aiy.cloudspeech.get_recognizer()
while True:
print('Press the button and speak')
button.wait_for_press()
text = recognizer.recognize()
if 'light on' in text:
turn_on_light()
elif 'light off' in text:
turn_off_light()
"""
global cloudspeech_recognizer
if cloudspeech_recognizer is None:
cloudspeech_recognizer = _CloudSpeechRecognizer(CLOUDSPEECH_CREDENTIALS_FILE)
return cloudspeech_recognizer

def get_recognizer():
"""Returns a recognizer that uses Google CloudSpeech APIs.
Sample usage:
button = aiy.voicehat.get_button()
recognizer = aiy.cloudspeech.get_recognizer()
while True:
print('Press the button and speak')
button.wait_for_press()
text = recognizer.recognize()
if 'light on' in text:
turn_on_light()
elif 'light off' in text:
turn_off_light()
"""
global cloudspeech_recognizer
if cloudspeech_recognizer is None:
cloudspeech_recognizer = _CloudSpeechRecognizer(CLOUDSPEECH_CREDENTIALS_FILE)
return cloudspeech_recognizer
Loading

0 comments on commit 9c0f3e3

Please sign in to comment.