forked from DougDougGithub/TwitchPlays
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HandleTTS.py
120 lines (101 loc) · 4.75 KB
/
HandleTTS.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#Skeleton of the file created by DougDoug and later updated by AAxolotl1 to turn messages into audio.
import configparser
import Twitch_Connection
import random
import keyboard
from gtts import gTTS
from io import BytesIO
from pygame import mixer
class TTS:
def __init__(self):
#Setup ConfigParser to read in data from Settings.ini
self.settingsFile = 'Settings.ini'
self.settings = configparser.ConfigParser()
self.settings.read(self.settingsFile)
#Read in settings using the ConfigParser
self.twitchName = self.setTwitchName(self.settings.get('Settings', 'Channel_Name'))
try:
self.ttsChance = float(self.settings.get('Settings', 'TTS_Chance'))
except:
self.ttsChance = self.setTTSChance(input("TTS Chance is not a vaild number. Please input a value between 0 & 1: "))
try:
self.volume = int(self.settings.get('Settings', 'Volume'))
except:
self.volume = self.setVolume(input('Volume was not set at a valid number. Please choose a number between 0 and 100'))
#Connect to your twitch channel
self.t = Twitch_Connection.Twitch()
self.t.twitch_connect(self.twitchName)
mixer.init()
#Setters for class variables
def setTwitchName(self, capture):
self.settings.set('Settings', 'Channel_Name', capture)
with open(self.settingsFile, 'w') as configfile:
self.settings.write(configfile)
return capture
def setTTSChance(self, capture):
while True:
try:
while True:
capture = float(capture)
if capture > 0 and capture <= 1:
self.settings.set('Settings', 'TTS_Chance', str(capture))
with open(self.settingsFile, 'w') as configfile:
self.settings.write(configfile)
return capture
else:
capture = input("Please Input a value between 0 & 1: ")
except:
capture = input("Please input a value between 0 & 1: ")
def setVolume(self, capture):
while True:
try:
while True:
capture = int(capture)/100
if capture >= 0 and capture <= 1:
self.settings.set('Settings', 'Volume', str(capture * 100))
with open(self.settingsFile, 'w') as configfile:
self.settings.write(configfile)
return capture
else: capture = input("Please input a value between 0 & 100: ")
except:
capture = input("Please choose a number between 0 & 100: ")
#Read in messages and convert them into audio
def RunTTS(self):
while True:
messages = self.t.twitch_receive_messages()
if not messages:
if keyboard.is_pressed('shift+backspace'):
break
else:
continue
else:
try:
for message in messages:
if keyboard.is_pressed('shift+backspace'):
break
if random.random() < self.ttsChance:
msg = message['message'].lower()
username = message['username'].lower()
print("Recieved message from " + username + ": " + msg)
tts = gTTS(text=msg, lang='en')
mp3 = BytesIO()
tts.write_to_fp(mp3)
mp3.seek(0)
sound = mixer.Sound(mp3)
sound.set_volume(self.volume)
sound.play()
mp3.close()
except Exception as e:
print("Encountered an error: " + str(e))
#Class for ConfigureTTS.py
class TTSConfigure(TTS):
def __init__(self):
self.settingsFile = 'Settings.ini'
self.settings = configparser.ConfigParser()
self.settings.read(self.settingsFile)
self.twitchName = self.setTwitchName(input("Please input your twitch username (or twitch channel you want to connect Text-to-Speech to): "))
self.ttsChance = self.setTTSChance(input("Please choose a number between 0 to 1 to set as your Text-to-Speech chance: "))
self.volume = self.setVolume(input("Please choose a number between 0 & 100: "))
self.t = Twitch_Connection.Twitch()
self.t.twitch_connect(self.twitchName)
mixer.init()