forked from robbiebarrat/rapping-neural-network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
speech.py
52 lines (39 loc) · 1.39 KB
/
speech.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import pyttsx
import subprocess
import threading
from time import sleep
from threading import Thread
# VARIABLES
beat_to_play = "beat.mp3"
slowdown_rate = 35 # higher value means slower speech... keep it between ~50 and 0 depending on how fast the beat is.
intro = 24 #amount in seconds it takes from the start of the beat to when the rapping should begin... how many seconds the AI waits to start rapping.
def play_mp3(path):
subprocess.Popen(['mpg123', '-q', path]).wait()
engine = pyttsx.init()
def letters(input):
valids = []
for character in input:
if character.isalpha() or character == "," or character == "'" or character == " ":
valids.append(character)
return ''.join(valids)
lyrics = open("neural_rap.txt").read().split("\n") #this reads lines from a file called 'neural_rap.txt'
#print lyrics
rate = engine.getProperty('rate')
engine.setProperty('rate', rate - slowdown_rate)
voices = engine.getProperty('voices')
wholesong = ""
for i in lyrics:
wholesong += i + " ... "
def sing():
for line in wholesong.split(" ... "):
if line == "..." or line == "":
for i in range(18):
engine.say(" ")
else:
engine.say(str(line))
engine.runAndWait()
def beat():
play_mp3(beat_to_play)
Thread(target=beat).start()
sleep(intro) # just waits a little bit to start talking
Thread(target=sing).start()