Advertisement
lukethenerd

Untitled

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