Advertisement
Guest User

Untitled

a guest
May 14th, 2012
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.06 KB | None | 0 0
  1. import pyttsx
  2.  
  3. import threading
  4. from threading import *
  5. import datetime
  6. import os
  7. import sys
  8. import ctypes
  9. from ctypes import wintypes
  10. import win32clipboard
  11. import win32con
  12.  
  13. import time
  14. from Queue import Queue
  15.  
  16. queue =   Queue()
  17. pauselocation =  [0]
  18. wordsToSay = ''
  19. reduceSpeedBy = 20      
  20. HOTKEYS = {
  21.     1 : (win32con.VK_F3, win32con.MOD_WIN),
  22.     2 : (win32con.VK_F4, win32con.MOD_WIN),
  23.     3 : (win32con.VK_F2, win32con.MOD_WIN)
  24. }
  25.  
  26. def runNewThread(wordsToSay, startingPoint):    
  27.     global queue, pauselocation
  28.    
  29.     e1 = (queue, wordsToSay, pauselocation, startingPoint)
  30.     t1 = threading.Thread(target=saythread,args=e1)
  31.     #t1.daemon = True
  32.     t1.start()
  33.    
  34.  
  35. def handle_win_f3 ():
  36.    
  37.     print 'opening'
  38.     os._exit(1)
  39.     print 'opened'
  40.  
  41. play = 0
  42.  
  43. def handle_win_f4 ():
  44.     global queue
  45.     global pauselocation
  46.     global play, wordsToSay
  47.    
  48.     if play == 0:
  49.         print 'pausing'
  50.         play = 1
  51.         queue.put(True)
  52.                
  53.     elif play == 1:
  54.         print 'playing'
  55.         play = 0
  56.         print type(pauselocation) ,  pauselocation
  57.         startingPoint = 0
  58.         for i in pauselocation[1:]:
  59.             startingPoint += i
  60.        # wordsToSay =  wordsToSay[pauselocation[-1]:]
  61.        # print wordsToSay
  62.         runNewThread(wordsToSay,startingPoint)
  63.        
  64. def handle_win_f2 ():
  65.     global queue
  66.     global pauselocation
  67.     global play, wordsToSay
  68.    # print 'pausing'
  69.     queue.put(True)
  70.     time.sleep(2)
  71.     #print 'playing'
  72.     #print type(pauselocation) ,  pauselocation
  73.     startingPoint = 0
  74.     for i in pauselocation:
  75.         startingPoint += i
  76.     print 'beofre reducing', startingPoint , wordsToSay[startingPoint:]      
  77.     startingPoint -= 50
  78.    # wordsToSay =  wordsToSay[pauselocation[-1]:]
  79.    # print wordsToSay
  80.    
  81.     print 'after reducing', startingPoint , wordsToSay[startingPoint:]
  82.     runNewThread(wordsToSay,startingPoint)
  83.        
  84.        
  85. HOTKEY_ACTIONS = {
  86.     1 : handle_win_f3,
  87.     2 : handle_win_f4,
  88.     3 : handle_win_f2
  89. }
  90.  
  91.  
  92. def saythread(queue , text , pauselocation, startingPoint):
  93.     global reduceSpeedBy
  94.     #print type(pauselocation) ,  pauselocation[-1]
  95.     #print type(saythread.pauselocation) ,  saythread.pauselocation[0]
  96.     saythread.pauselocation = pauselocation
  97.     saythread.pause = 0
  98.     #print 'saythread' ,startingPoint
  99.     saythread.engine = pyttsx.init()
  100.      
  101.     saythread.pausequeue1 = False
  102.    
  103.     def onWord(name, location, length):
  104.         #print 'onWord'
  105.        
  106.        
  107.         saythread.pausequeue1  = queue.get(False)
  108.         #print  'passed to queue- ', saythread.pausequeue1
  109.         saythread.pause = location
  110.         saythread.pauselocation.append(location)
  111.  
  112.         if saythread.pausequeue1 == True :
  113.             saythread.engine.stop()
  114.                
  115.     def onFinishUtterance(name, completed):
  116.         if completed == True:
  117.             os._exit(0)            
  118.                
  119.     def engineRun():
  120.         #print text
  121.          
  122.         if len(saythread.pauselocation) == 1:
  123.             rate = saythread.engine.getProperty('rate')
  124.             print rate
  125.             saythread.engine.setProperty('rate', rate-reduceSpeedBy)
  126. #    
  127. #        
  128.         textMod = text[startingPoint:]
  129.         #print "startingPoint " , startingPoint , textMod
  130.        
  131.         saythread.engine.say(text[startingPoint:])
  132.         token = saythread.engine.connect("started-word" , onWord )
  133.         saythread.engine.connect("finished-utterance" , onFinishUtterance )
  134.         saythread.engine.startLoop(True)
  135.         #engine.disconnect(token)
  136.    
  137.     engineRun()
  138.    
  139.     #pauselocation =  saythread.pause
  140.     if saythread.pausequeue1 == False:
  141.         #print 'exiting from thread1'
  142.         os._exit(1)
  143.    # print 'after everything'
  144.  
  145.  
  146.    
  147. if __name__ == '__main__':
  148.  
  149.     wordsToSay = sys.argv[1:]
  150.    
  151.     if len(wordsToSay) == 0:
  152.         win32clipboard.OpenClipboard()
  153.         try:
  154.             wordsToSay = win32clipboard.GetClipboardData(win32con.CF_TEXT)
  155.             win32clipboard.CloseClipboard()
  156.             if wordsToSay == None:
  157.                 wordsToSay = "Copy some text to the clipboard"
  158.         except TypeError as e:
  159.             wordsToSay = "Copy some text to the clipboard"
  160.          
  161.        
  162.     else:
  163.         wordsToSay = " ".join(wordsToSay)      
  164.     print wordsToSay ;
  165.     runNewThread(wordsToSay,0)
  166.     time.sleep(1)
  167.    
  168.     byref = ctypes.byref
  169.     user32 = ctypes.windll.user32
  170.    
  171.        
  172.     for id, (vk, modifiers) in HOTKEYS.items ():
  173.         print "Registering id", id, "for key", vk
  174.         if not user32.RegisterHotKey (None, id, modifiers, vk):
  175.             print "Unable to register id", id
  176.    
  177.     msg = wintypes.MSG()
  178.     while  user32.GetMessageA (byref (msg), None, 0, 0) != 0  :
  179.         if msg.message == win32con.WM_HOTKEY:
  180.             action_to_take = HOTKEY_ACTIONS.get(msg.wParam)
  181.             #print action_to_take
  182.             if action_to_take:
  183.                 action_to_take ()
  184.    # words = raw_input("what do you want me to say?")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement