Advertisement
AceScottie

SR6

Apr 18th, 2019
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.06 KB | None | 0 0
  1. ##required SpeechRecognition, pypiwin32, pyAudio, sendKeys
  2. ##required espeak windows commandline + python bindings. set espeak.exe in path
  3.  
  4. import speech_recognition as sr
  5. import espeak
  6. import sys
  7. import os
  8. import SendKeys
  9. import win32com.client
  10. import time
  11. import threading
  12.  
  13. r = sr.Recognizer()
  14.  
  15. srname = "computer"
  16. for index, name in enumerate(sr.Microphone.list_microphone_names()):
  17.     print("Microphone with name \"{1}\" found for `Microphone(device_index={0})`".format(index, name))
  18. dindex = int(raw_input("Select device to use: "))
  19.  
  20. def pthrd(tts):
  21.     es = espeak.ESpeak()
  22.     print("tts = %s") %tts
  23.     es.say(tts)
  24.  
  25. def talk(tts):
  26.     t = threading.Thread(target=pthrd,args=[tts])
  27.     t.start()
  28.  
  29. def savetofile(r, sr, dindex):
  30.     recording = True
  31.     shell = win32com.client.Dispatch("WScript.Shell")
  32.     shell.Run("notepad")
  33.     shell.AppActivate("notepad")
  34.     talk("recording audio in text file")
  35.     while recording:
  36.         with sr.Microphone(device_index=dindex) as source:
  37.             r.adjust_for_ambient_noise(source)
  38.             print("Listening to audio")
  39.             audio = r.listen(source)
  40.         try:
  41.             command = r.recognize_google(audio).lower()
  42.         except sr.UnknownValueError:
  43.             talk("error: could not understand")
  44.         print command
  45.         if command == "stop recording":
  46.             recording = False
  47.         elif command == "new line":
  48.             shell.SendKeys("{~}")
  49.         elif command != "":
  50.             shell.SendKeys(command)
  51.  
  52. def active(r, sr, command, dindex):
  53.     print("running command: " + command)
  54.     if command == "record":
  55.         savetofile(r, sr, dindex)
  56.     else:
  57.         responde(command)
  58.        
  59. def pather(paths):
  60.     user = os.environ.get('USERNAME')
  61.     default_path = "\"C:\Users\%s\\" %user
  62.     paths = paths.split()
  63.    
  64.    
  65.     return full_path
  66.    
  67.    
  68. def responde(command):
  69.     global srname
  70.     print(command)
  71.     if command == "stop":
  72.         talk("stopping")
  73.         sys.exit()
  74.     elif command[:3] == "run":
  75.         talk("running " + command[4:])
  76.         os.system("start " + command[4:])
  77.     elif command[:5] == "close":
  78.         os.system("taskkill /F /IM " + command[6:] + ".exe")
  79.     elif command[:6] == "rename":
  80.         talk("my new name is " +command[7:])
  81.         srname = command[7:]
  82.     elif command[:4] == "open":
  83.         open_file(command[5:])
  84.     else:
  85.         talk("unknown command")
  86.        
  87.  
  88.        
  89. def open_file(command):
  90.     path = pather(command)
  91.     os.system("start explorer.exe " + path)
  92.     print("opening " + path + location)
  93.     talk("opening " + path + location)
  94.  
  95. def transcribe(r, sr, audio):
  96.     global srname
  97.     command = ""
  98.     i = len(srname)
  99.     j = len(srname) + 1
  100.     try:
  101.         command = r.recognize_google(audio).lower()
  102.     except sr.UnknownValueError:
  103.         pass
  104.     print command
  105.     if command[:i] ==  srname:
  106.         active(r, sr, command[j:], dindex)
  107.  
  108. def main(r, sr, dindex):
  109.     global srname
  110.     command = ""
  111.     i = len(srname)
  112.     j = len(srname) + 1
  113.     with sr.Microphone(device_index=dindex) as source:
  114.         r.adjust_for_ambient_noise(source)
  115.         print("Waiting for activation with command " + srname)
  116.         audio = r.listen(source)
  117.     t = threading.Thread(target=transcribe,args=[r, sr, audio])
  118.     t.start()
  119.     t.join()
  120.  
  121.  
  122. if __name__ == "__main__":
  123.     while True:
  124.         try:
  125.             main(r, sr, dindex)
  126.         except KeyboardInterrupt:
  127.             break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement