import time, pyaudio, wave, os, urllib,urllib2,pycurl,httplib,sys,win32api,win32con,string from ctypes import *
def Talk(text): def downloadFile(url, fileName): fp = open(fileName, "wb") curl = pycurl.Curl() curl.setopt(pycurl.URL, url) curl.setopt(pycurl.WRITEDATA, fp) curl.perform() curl.close() fp.close() def getGoogleSpeechURL(phrase): googleTranslateURL = "http://translate.google.com/translate_tts?tl=en&" parameters = {'q': phrase} data = urllib.urlencode(parameters) googleTranslateURL = "%s%s" % (googleTranslateURL,data) return googleTranslateURL def speakSpeechFromText(phrase): googleSpeechURL = getGoogleSpeechURL(phrase) downloadFile(googleSpeechURL,"ans.mp3")#, ans.mp3 speakSpeechFromText(text) # , winmm = windll.winmm winmm.mciSendStringA('Open "ans.mp3" Type MPEGVideo Alias theMP3',0,0,0) winmm.mciSendStringA('Play theMP3 Wait',0,0,0) winmm.mciSendStringA("Close theMP3","",0,0)
def Record(): CHUNK = 1024 FORMAT = pyaudio.paInt16 CHANNELS = 2 RATE = 16000 RECORD_SECONDS = 5 WAVE_OUTPUT_FILENAME = "output.wav" p = pyaudio.PyAudio() stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) print("Recording...") frames = [] for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): data = stream.read(CHUNK) frames.append(data) print("Done recording.") stream.stop_stream() stream.close() p.terminate() wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb') wf.setnchannels(CHANNELS) wf.setsampwidth(p.get_sample_size(FORMAT)) wf.setframerate(RATE) wf.writeframes(b''.join(frames)) wf.close()
def Convert(): print "Converting" os.system('C:\Users\\Desktop\\TotalAudioConverter\AudioConverter.exe C:\Users\\Desktop\\output.wav C:\Users\\Desktop\\output.flac') print "Done"
def Send(): global ANSWER url = 'https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-EN'# flac=open('output.flac',"rb").read() header = {'Content-Type' : 'audio/x-flac; rate=16000'} req = urllib2.Request(url, flac, header) data = urllib2.urlopen(req) a = data.read() ANSWER = eval(a) if ANSWER['status'] == 5: print 'Sorry, I do not understand you.' Talk('Sorry, I do not understand you.') ANSWER = 0 else: ANSWER = ANSWER['hypotheses'][0]['utterance']# google ( ) print ANSWER os.remove('C:\Users\\Desktop\\output.wav')# os.remove('C:\Users\\Desktop\\output.flac') return ANSWER
def Processing(): global ANSWER if ANSWER == 0: return 0 elif 'chrome' in ANSWER.lower(): os.system('C:\Users\\AppData\Local\Google\Chrome\Application\chrome.exe')# Google Chrome, chrome) elif 'skype' in ANSWER.lower(): os.system('C:\Users\\Downloads\SkypePortable\SkypePortable.exe')# elif 'cd rom' in ANSWER.lower() or\ 'cd-rom' in ANSWER.lower() or\ 'open d' in ANSWER.lower() or\ 'dvd' in ANSWER.lower() or\ 'dvd-rom' in ANSWER.lower() or\ 'dvd rom' in ANSWER.lower() or\ 'cdrom' in ANSWER.lower() or\ 'cd - rom' in ANSWER.lower(): winmm = windll.winmm winmm.mciSendStringA("set cdaudio door open", "", 0,0)# - dvd
print 'Hi, what do you want?' Talk('Hi, what do you want?') Record() Convert() print ('Sending...') Send() print 'Done' Processing() while True: ANSWER = None #Talk('Done.') print 'Do you want something else? (Your command\No)' Talk('Do you want something else??') Record() Convert() print 'Sending...' Send() print 'Done' #print ANSWER if ANSWER == 0: continue if ANSWER.lower()== 'no' or\# , ANSWER.lower()== 'nope' or\ ANSWER.lower()== 'not' or\ ANSWER.lower()== 'nay': break else: Processing() print 'Okay, bye' Talk('Okay, bye')
Source: https://habr.com/ru/post/263423/
All Articles