From 5ce2689994af760b83920499fdc2bac96f1528c9 Mon Sep 17 00:00:00 2001 From: karl Date: Wed, 13 Nov 2019 08:48:55 +0000 Subject: [PATCH] add working music verion --- 3_HeyRobot/latestWorkingWithMusic.py | 108 +++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 3_HeyRobot/latestWorkingWithMusic.py diff --git a/3_HeyRobot/latestWorkingWithMusic.py b/3_HeyRobot/latestWorkingWithMusic.py new file mode 100644 index 0000000..5aacf01 --- /dev/null +++ b/3_HeyRobot/latestWorkingWithMusic.py @@ -0,0 +1,108 @@ +#!/usr/bin/env python3 +# Copyright 2017 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Run a recognizer using the Google Assistant Library. + +The Google Assistant Library has direct access to the audio API, so this Python +code doesn't need to record audio. Hot word detection "OK, Google" is supported. + +It is available for Raspberry Pi 2/3 only; Pi Zero is not supported. +""" + +import logging +import platform +import subprocess +import sys +import os +import time + +from google.assistant.library.event import EventType + +from aiy.assistant import auth_helpers +from aiy.assistant.library import Assistant +from aiy.util import Led +from aiy.voice import tts + +def power_off_pi(): + tts.say('Good bye!') + # subprocess.call('sudo shutdown now', shell=True) + + +def reboot_pi(): + tts.say('See you in a bit!') + # subprocess.call('sudo reboot', shell=True) + + +def say_ip(): + ip_address = subprocess.check_output("hostname -I | cut -d' ' -f1", shell=True) + tts.say('My IP address is %s' % ip_address.decode('utf-8')) + +playshell = None + +def play(say): + track = say.split('glass')[-1] + + global playshell + if (playshell == None): + playshell = subprocess.Popen(["/usr/local/bin/mpsyt",""], stdin=subprocess.PIPE) + + playshell.stdin.write(bytes('/' + track + '\n1\n', 'utf-8')) + playshell.stdin.flush() + + +def process_event(assistant, led, event): + logging.info(event) + if event.type == EventType.ON_START_FINISHED: + led.state = Led.OFF # Ready. + print('Say "OK, Google" then speak, or press Ctrl+C to quit...') + elif event.type == EventType.ON_CONVERSATION_TURN_STARTED: + led.state = Led.ON # Listening. + elif event.type == EventType.ON_RECOGNIZING_SPEECH_FINISHED and event.args: + print('You said:', event.args['text']) + text = event.args['text'].lower() + if text == 'power off': + assistant.stop_conversation() + power_off_pi() + elif text == 'reboot': + assistant.stop_conversation() + reboot_pi() + elif text == 'ip address': + assistant.stop_conversation() + say_ip() + elif 'glass' in text: + play(text) + elif 'stop' in text: + pkill = subprocess.Popen(["/usr/bin/pkill","vlc"],stdin=subprocess.PIPE) + elif event.type == EventType.ON_END_OF_UTTERANCE: + led.state = Led.PULSE_QUICK # Thinking. + elif (event.type == EventType.ON_CONVERSATION_TURN_FINISHED + or event.type == EventType.ON_CONVERSATION_TURN_TIMEOUT + or event.type == EventType.ON_NO_RESPONSE): + led.state = Led.OFF # Ready. + elif event.type == EventType.ON_ASSISTANT_ERROR and event.args and event.args['is_fatal']: + sys.exit(1) + + +def main(): + logging.basicConfig(level=logging.INFO) + + credentials = auth_helpers.get_assistant_credentials() + with Led(channel=17) as led, Assistant(credentials) as assistant: + for event in assistant.start(): + process_event(assistant, led, event) + + +if __name__ == '__main__': + main()