Advertisement
Peaser

Bootleg Text-To-Speech in Python

Jul 6th, 2014
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | None | 0 0
  1. #"Rec" folder is seperate.
  2.  
  3. import os, sys, pygame
  4. from time import sleep as s
  5. from pygame.locals import *
  6. import pygame.mixer         #no games here, just using pygame for audio purposes
  7.  
  8. print "'@quit' to exit\n\n\n"
  9.  
  10. resdir = os.getcwd()+"/Rec/"
  11. #letters = "abcdefghijklmnopqrstuvwxyz " #I don't feel like importing string
  12. allowed = [i for i in "1234567890abcdefghijklmnopqrstuvwxyz !@$&?%*()/+-=_^.#><:;"]
  13.  
  14. soundlink = {
  15. "1":"1.wav",
  16. "2":"2.wav",
  17. "3":"3.wav",
  18. "4":"4.wav",
  19. "5":"5.wav",
  20. "6":"6.wav",
  21. "7":"7.wav",
  22. "8":"8.wav",
  23. "9":"9.wav",
  24. "0":"0.wav",
  25. "a":"a.wav",
  26. "b":"b.wav",
  27. "c":"c.wav",
  28. "d":"d.wav",
  29. "e":"e.wav",
  30. "f":"f.wav",
  31. "g":"g.wav",
  32. "h":"h.wav",
  33. "i":"i.wav",
  34. "j":"j.wav",
  35. "k":"k.wav",
  36. "l":"l.wav",
  37. "m":"m.wav",
  38. "n":"n.wav",
  39. "o":"o.wav",
  40. "p":"p.wav",
  41. "q":"q.wav",
  42. "r":"r.wav",
  43. "s":"s.wav",
  44. "t":"t.wav",
  45. "u":"u.wav",
  46. "v":"v.wav",
  47. "w":"w.wav",
  48. "x":"x.wav",
  49. "y":"y.wav",
  50. "z":"z.wav",
  51. " ":"space.wav",
  52. "!":"exclamation.wav",
  53. "?":"question.wav",
  54. "@":"at.wav",
  55. "$":"dollar.wav",
  56. "&":"and.wav",
  57. "%":"percent.wav",
  58. "*":"times.wav",
  59. "(":"openp.wav",
  60. ")":"closedp.wav",
  61. "/":"dividedby.wav",
  62. "+":"plus.wav",
  63. "-":"minus.wav",
  64. "=":"equals.wav",
  65. "_":"underscore.wav",
  66. ".":"point.wav",
  67. "^":"carat.wav",
  68. "#":"number.wav",
  69. ">":"greaterthan.wav",
  70. "<":"lessthan.wav",
  71. ":":"colon.wav",
  72. ";":"semicolon.wav"
  73.  
  74. #no brackets :(
  75. #no braces :(
  76. #no tilde :(
  77. #no quote :(
  78. #no apostrophe :(
  79. #no vertical line :(
  80. #no backslash :(
  81. #no comma :(
  82. #no Unicode characters :(
  83.  
  84. }
  85. pygame.mixer.init()
  86. loop = 1
  87. while loop == 1:
  88.     request = raw_input("Speech > ").lower()
  89.  
  90.     if request.lower() == "@quit":
  91.         loop = 0
  92.  
  93.     elif request.lower() == "@mmmmm":                   ######
  94.         pygame.mixer.Sound(resdir+"mmm.wav").play()          #
  95.     elif request.lower() == "@augh":                         #
  96.         pygame.mixer.Sound(resdir+"ow.wav").play()           #####--- Because why not?
  97.     elif request.lower() == "@slop":                         #
  98.         pygame.mixer.Sound(resdir+"squish.wav").play()  ######
  99.  
  100.  
  101.     else:
  102.         test = []
  103.         for i in request:
  104.             if i in allowed:
  105.                 test.append(str(i))     #"sanitizing" the input. allowing only the specified characters
  106.         request = test
  107.  
  108.         #print test
  109.  
  110.         for i in test:
  111.             audio = resdir+soundlink[i] #path to file
  112.             length = pygame.mixer.Sound(audio).get_length() #length of sound file in seconds
  113.             pygame.mixer.Sound(audio).play() #play the sound
  114.             s(length/1.5) #wait the length of sound
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement