def record_save_wav(): assistant = src.aiy.assistant.grpc.get_assistant() temp_file, temp_path = tempfile.mkstemp(suffix='.wav') os.close(temp_file) perm_path = os.path.expanduser('~/' + str(temp_file) + '.wav') try: gtts.speak('recording') print('Recording...') src.aiy.audio.record_to_wave(temp_path, 5) src.aiy.audio.play_wave(temp_path) gtts.speak('would you like to save the file?') text, audio = assistant.recognize() print(text) if text == 'yes': # move to perm_path shutil.move(temp_path, perm_path) gtts.speak('saved as ' + str(temp_file)) if text == 'no': gtts.speak('ok file was not saved') except: gtts.speak('i did not get that') print('Problems with recording') pass finally: try: os.unlink(temp_path) except FileNotFoundError: pass
def try_again(function): status_ui = aiy.voicehat.get_status_ui() gtts.speak('would you like to try again?') text, audio = assistant.recognize() if text == 'yes': function() else: gtts.speak('ok. aborting email service') status_ui.status('ready') can_start_conversation = True
def play_wav_file(): gtts.speak("what file do you want to play?") wav_file = find_wav_file() if wav_file: src.aiy.audio.play_wave(wav_file) else: gtts.speak("i can't find the file") print('file not found') pass
def find_wav_file(): assistant = src.aiy.assistant.grpc.get_assistant() # refer to file by temp_file integer referenced when saving file text, audio = assistant.recognize() file_queried = os.path.expanduser('~/' + text + '/.wav') try: if os.path.isfile(file_queried): return file_queried except FileNotFoundError: gtts.speak("file wasn't found") print('file not found') pass
def record_playback_simple(): temp_file, temp_path = tempfile.mkstemp(suffix='.wav') os.close(temp_file) try: input("When you're ready, press enter and say 'Testing, 1 2 3'...") print('Recording...') gtts.speak('recording') src.aiy.audio.record_to_wave(temp_path, 5) src.aiy.audio.play_wave(temp_path) finally: try: os.unlink(temp_path) except FileNotFoundError: pass
def send_files(recipient, files): assert type(recipient) == list assert type(files) == list msg = MIMEMultipart() msg['From'] = user msg['To'] = COMMASPACE.join(recipient) msg['Date'] = formatdate(localtime=True) msg['Subject'] = 'files from the cardboard' # msg.attach(MIMEText(body)) for file in files: try: part = MIMEBase('application', "octet-stream") with open(file, 'rb') as fp: part.set_payload(fp.read()) encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment', filename=os.path.basename(file)) msg.attach(part) except: print('Unable to open one of the attachments. Error: ', sys.exc_info()[0]) raise try: with smtplib.SMTP('smtp.gmail.com', 587) as s: s.ehlo() s.starttls() s.ehlo() s.login(user, password) s.sendmail(user, recipient, msg.as_string()) s.close() print('Email sent!') gtts.speak('email sent') s.quit() except: print('Unable to send the email. Error: ', sys.exc_info()[0]) gtts.speak('unable to send email') try_again(send_files) finally: recipient = [] files = []
def confirm_user_response(phrase, response, function): gtts.speak('is this correct?') text, audio = assistant.recognize() if text == 'yes': gtts.speak(phrase) gtts.speak(response) return response elif text == 'no': try_again(function)
def _delete_wav_file(): gtts.speak("what file would you like to delete?") wav_file = find_wav_file() if wav_file: gtts.speak('i have deleted ' + wav_file) os.remove(wav_file) else: gtts.speak("i can't find the file") print('file not found') pass
def set_recipients(): gtts.speak('who should i send the email to?') print('Listening ...') recipient, audio = assistant.recognize() if recipient is not None: gtts.speak('You said ' + recipient) print('You said, "', recipient, '"') confirm_user_response('ok email will be sent to', recipient, set_recipients) else: print('i did not hear you') gtts.speak('i did not hear you') try_again(set_recipients)
def confirm_information(): status_ui = aiy.voicehat.get_status_ui() recipient = set_recipients() gtts.speak('let\'s confirm. you would like audio.wav sent to ' + recipient + '. is this correct?') text, audio = assistant.recognize() if text == 'yes': gtts.speak('ok sending email') send_files(recipient, ['audio.wav']) elif text == 'no': gtts.speak('ok. aborting email service') status_ui.status('ready') can_start_conversation = True
def reboot(): print('reboot') gtts.speak('Restarting') destroy_GPIO()
def shutdown(): print('shut down') gtts.speak('Turning Off') destroy_GPIO()