Advertisement
lukethenerd

speech.py

Feb 23rd, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1. import os
  2. import ctypes
  3. import sys
  4. import subprocess
  5. import threading
  6. import mpg123
  7. import ffmpeg
  8. import time
  9. from mpg123 import Mpg123, Out123
  10. from time import sleep
  11. from threading import Thread
  12. import pyttsx3
  13.  
  14.  
  15. class _TTS:
  16.     engine = None
  17.     rate = None
  18.  
  19.     def __init__(self):
  20.         self.engine = pyttsx3.init()
  21.  
  22.  
  23. # VARIABLES
  24.  
  25.  
  26. beat_to_play = "beat.mp3"
  27. lyricfile = "FILE-YOU-GENERATED.txt"
  28.  
  29. slowdown_rate = 35  # higher value means slower speech... keep it between ~50 and 0 depending on how fast the beat is.
  30. intro = 4  # how many seconds the AI waits to start rapping.
  31.  
  32.  
  33. def play_mp3(path):
  34.     mp3 = Mpg123(beat_to_play)
  35.     out = Out123()
  36.  
  37.  
  38. engine = pyttsx3.init()
  39.  
  40.  
  41. def letters(input):
  42.     valids = []
  43.     for character in input:
  44.         if character.isalpha() or character == "," or character == "'" or character == " ":
  45.             valids.append(character)
  46.     return ''.join(valids)
  47.  
  48.  
  49. lyrics = open(lyricfile).read().split("\n")  # this is where the text file goes
  50.  
  51. rate = engine.getProperty('rate')
  52. engine.setProperty('rate', 'rate - slowdown_rate')
  53. voices = engine.getProperty('voices')
  54.  
  55. wholesong = ""
  56. for i in lyrics:
  57.     wholesong += i + " ... "
  58.  
  59.  
  60. def start():
  61.     for line in wholesong.split(" ... "):
  62.         if line == "..." or line == "":
  63.             for i in range(18):
  64.                 engine.say("    ")
  65.         else:
  66.             engine.say(str(line))
  67.     engine.runAndWait()
  68.  
  69.  
  70. def beat():
  71.     for frame in mp3.iter_frames(out.start):
  72.         out.play(frame)
  73.  
  74.  
  75. Thread(target=beat).start()
  76. sleep(intro)  # just waits a little bit to start talking
  77. Thread(target=start()).start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement