Added TTS support

master
Chloe Fontenot 🏳️‍⚧️ 2020-02-27 21:35:09 +07:00
parent 2f2e74f13d
commit aae82f3184
28 changed files with 106 additions and 34 deletions

Binary file not shown.

Binary file not shown.

@ -2,7 +2,7 @@
# Toggle me for debugging # Toggle me for debugging
debug = 1 debug = 1
# Displays warning message # Displays warning message
testing = 0 testing = 1
# Import the libraries we will use # Import the libraries we will use
#from mega import Mega #from mega import Mega
from datetime import datetime from datetime import datetime
@ -17,7 +17,9 @@ import ffmpeg
#import talkey #import talkey
async def gameLoop(): async def gameLoop():
#Init tts, and connect voice to channel, hackily reinit connection if broken # In the future we will detect what channel we were summoned in, but for now:
ativeChannel = 656233549837631508
#Init tts, and connect voice to channel, reinit connection if broken
voiceChannel = client.get_channel(682688245964079127) voiceChannel = client.get_channel(682688245964079127)
voice = await voiceChannel.connect() voice = await voiceChannel.connect()
if voice.is_connected() == True: if voice.is_connected() == True:
@ -81,25 +83,33 @@ async def gameLoop():
await channel.send(str("Type a noun, verb, adjective, or adverb depending on what it asks you, followed by enter.")) await channel.send(str("Type a noun, verb, adjective, or adverb depending on what it asks you, followed by enter."))
for loopCount in range(replacedNumber): for loopCount in range(replacedNumber):
print("Times looped: "+str(loopCount))
#Wait for user to reply #Wait for user to reply
await channel.send("Give me a(n) "+"**"+str(filtered[loopCount])+"**"+": ") await channel.send("Give me a(n) "+"**"+str(filtered[loopCount])+"**"+": ")
#if we see our own message, ignore it
async def on_message(message):
if message.author == self.user:
return
#if we see a message in a channel that isn't in the active one, ignore it
if message.channel.id != activeChannelID:
return
# Push text to gTTS and save it to a file # Push text to gTTS and save it to a file
tts = gTTS(text="Give me a(n) "+str(filtered[loopCount])+": ") tts = gTTS(text="Give me a(n) "+str(filtered[loopCount])+": ")
os.system("rm badCurrentTTS.mp3") os.system("rm badCurrentTTS.mp3")
os.system("rm currentTTS.mp3") os.system("rm currentTTS.wav")
tts.save("badCurrentTTS.mp3") tts.save("badCurrentTTS.mp3")
# gTTS is stupid and gives us a file that discord.py doesn't understand, so we have to convert it # gTTS is stupid and gives us a file that discord.py doesn't understand, so we have to convert it
( (
ffmpeg ffmpeg
.input('badCurrentTTS.mp3') .input('badCurrentTTS.mp3')
.output('currentTTS.mp3', audio_bitrate=48000, format='wav', sample_fmt='s16', ac='2') .output('currentTTS.wav', audio_bitrate=48000, format='wav', sample_fmt='s16', ac='2')
.run() .run()
) )
if voice.is_playing() == True: if voice.is_playing() == True:
print("Audio is playing! Stopping playback!"+'\n') print("Audio is playing! Stopping playback!"+'\n')
voice.stop() voice.stop()
print("Attempting to play audio"+'\n') print("Attempting to play audio"+'\n')
voice.play(discord.FFmpegPCMAudio("currentTTS.mp3")) voice.play(discord.FFmpegPCMAudio("currentTTS.wav"))
raw_message = await client.wait_for('message') raw_message = await client.wait_for('message')
replaceVar = raw_message.content replaceVar = raw_message.content
print("You gave me: "+replaceVar) print("You gave me: "+replaceVar)
@ -131,7 +141,8 @@ async def gameLoop():
# Send Story to Discord # Send Story to Discord
await channel.send(generatedStory) await channel.send(generatedStory)
await channel.send("Processing TTS, please wait!") await channel.send("Processing TTS, please wait!")
tts = gTTS(text=generatedStory+"This story was generated by Caleb Fontenot's MadLibs.py", lang='en') async with channel.typing():
tts = gTTS(text=generatedStory+"This story was generated by CCF_100's MadLibs.py", lang='en')
os.system("rm badCurrentStory.mp3") os.system("rm badCurrentStory.mp3")
tts.save("badCurrentStory.mp3") tts.save("badCurrentStory.mp3")
# gTTS is stupid and gives us a file that discord.py doesn't understand, so we have to convert it # gTTS is stupid and gives us a file that discord.py doesn't understand, so we have to convert it
@ -146,7 +157,6 @@ async def gameLoop():
voice.stop() voice.stop()
print("Attempting to play audio"+'\n') print("Attempting to play audio"+'\n')
voice.play(discord.FFmpegPCMAudio(saveFile+".mp3")) voice.play(discord.FFmpegPCMAudio(saveFile+".mp3"))
#exit() #exit()
#Alright! We're done! Let's save the story to a file #Alright! We're done! Let's save the story to a file
if os.path.exists("saved stories"): if os.path.exists("saved stories"):
@ -157,10 +167,8 @@ async def gameLoop():
await channel.send("Saving story to .txt file") await channel.send("Saving story to .txt file")
async with channel.typing(): async with channel.typing():
file = open(saveFile+'.txt', 'w+') file = open(saveFile+'.txt', 'w+')
line_offset = [] line_offset = []
offset = 0 offset = 0
for line in file: for line in file:
line_offset.append(offset) line_offset.append(offset)
offset += len(line) offset += len(line)
@ -174,18 +182,18 @@ async def gameLoop():
discordFile = discord.File(saveFile+'.txt', filename="generatedStory.txt") discordFile = discord.File(saveFile+'.txt', filename="generatedStory.txt")
await channel.send(file=discordFile) await channel.send(file=discordFile)
#Send generated .mp3 file to Discord #Send generated .mp3 file to Discord
#If file is above 8 MB, upload it to MEGA
mp3File = saveFile+'.mp3' mp3File = saveFile+'.mp3'
def file_size(mp3File): def file_size(fname):
statinfo = os.stat(mp3File) statinfo = os.stat(fname)
return statinfo.st_size return statinfo.st_size
echo("MP3 is "+file_size+" bytes.") print("MP3 is "+str(file_size(mp3File))+" bytes.")
await channel.send("MP3 is "+file_size+" bytes." await channel.send("Converted WAV is "+str(file_size(mp3File))+" bytes.")
if int(file_size) <= int(8389999): if int(file_size(mp3File)) <= int(8389999):
# File is over 8 MiB! This will fail if we send the file corrected for transmission via Discord's voice chat. Let's send the original instead. # File is over 8 MiB! This will fail if we send the file corrected for transmission via Discord's voice chat. Let's send the original instead.
discordFile = discord.File(badCurrentStory.mp3, filename=saveFile+'.mp3') discordFile = discord.File("badCurrentStory.mp3", filename=saveFile+'.mp3')
await channel.send(file=discordFile) await channel.send(file=discordFile)
discordFile = discord.File(saveFile+'.mp3', filename=saveFile+'.mp3') else:
discordFile = discord.File(saveFile+'.wav', filename=saveFile+'.wav')
await channel.send(file=discordFile) await channel.send(file=discordFile)
#Setup Discord functions and announce on discord that we are ready #Setup Discord functions and announce on discord that we are ready
@ -205,14 +213,18 @@ class MyClient(discord.Client):
channel = client.get_channel(656233549837631508) channel = client.get_channel(656233549837631508)
await gameLoop() await gameLoop()
await channel.send("Done!") await channel.send("Done!")
#Disconnect Voice
await asyncio.sleep(60)
voiceChannel = client.get_channel(682688245964079127)
await voice.disconnect() await voice.disconnect()
#Run main Game loop #Run main Game loop
# The Discord bot ID isn't stored in this script for security reasons, so we have to go get it # The Discord bot ID isn't stored in this script for security reasons, so we have to go get it
f = open('botID.txt', 'r') f = open('botID.txt', 'r')
BotID = f.read() BotID = f.read()
#Cleanup from previous session
#os.system("rm badCurrentTTS.mp3")
#os.system("rm currentTTS.wav")
# Connect Bot To Discord and start running # Connect Bot To Discord and start running
client = MyClient() client = MyClient()
client.run(BotID) client.run(BotID)

@ -0,0 +1,5 @@
Awards Show
Welcome everyone to asdf and the asdf MadLib awards show. Here are the nominees for asdf. First, sadf. Next, asdf. And finally, asdf. And the winner is... asdf! asdf could not be here tonight so he recorded a message for us to see. Take a look. Winner: Hello all. Thank you, the fans, for voting. I really appreciate it. I am sorry I couldn't be there.
Generated by Caleb Fontenot's madlibs.py

@ -0,0 +1,5 @@
MadLib Gameshow
asdf on down! You're the next contestant on asdf's new gameshow, MadLib! Here's how it works. You will sadf over a asdf at a rate of asdf mph. After you jump over that item, you will g over to the l l and press the l at the end. l time wins! Oh. I forgot to introduce our host. He's a l person who can't wait to l in to the show. Please welcome, l! Alright players, on your mark. Get set. l! Alright, here goes l on the first stage of the competition. And.. WOAH! He just l over the thing! That's gonna hurt him tommorow. But it'll be all worth it after he wins the grand prize of l dollars! Which he says will go towards l. Very l investment, but it's his money! Here he comes around the second leg. AND WOAH, AGAIN HE LOOKS l ON NETWORK TELEVISION! This is what primetime is all about here on the network of k. And, he presses it! He finishes with a time of l. Will that be enough to win? Find out on l when l will challenge him for the grand prize. For all of us here at MadLib, good night everyone!
Generated by Caleb Fontenot's madlibs.py

@ -0,0 +1,5 @@
Three Little Pigs
Once upon a time, there were three l pigs. One day, their mother said, "You are all grown up and must l on your own." So they left to l their houses. The first little pig wanted only to l all day and quickly built his house out of l. The second little pig wanted to l and l all day so he l his house with l. The third l pig knew the wolf lived nearby and worked hard to l his house out of l. One day, the wolf knocked on the first pig's l. "Let me in or I'll l your house down!" The pig didn't, so the wolf l down the l. The wolf knocked on the second pig's l. "Let me in or I'll blow your l down!" The pig didn't, so the wolf l down the house. Then the wolf knocked on the third l pig's door. "Let me in or I'll blow your house down!" The little pig didn't, so the wolf l and l.He could not blow the house down. All the pigs went to live in the l house and they all l happily ever after.
Generated by Caleb Fontenot's madlibs.py

@ -0,0 +1,5 @@
Three Little Pigs
Once upon a time, there were three l pigs. One day, their mother said, "You are all grown up and must l on your own." So they left to l their houses. The first little pig wanted only to l all day and quickly built his house out of l. The second little pig wanted to l and l all day so he Give me a(n) **past tense verb**: his house with l. The third l pig knew the wolf lived nearby and worked hard to l his house out of l. One day, the wolf knocked on the first pig's l. "Let me in or I'll l your house down!" The pig didn't, so the wolf l down the l. The wolf knocked on the second pig's l. "Let me in or I'll blow your l down!" The pig didn't, so the wolf l down the house. Then the wolf knocked on the third l pig's door. "Let me in or I'll blow your house down!" The little pig didn't, so the wolf l and l.He could not blow the house down. All the pigs went to live in the l house and they all l happily ever after.
Generated by Caleb Fontenot's madlibs.py

@ -0,0 +1,5 @@
AMC Cinemas Pre-Show Policy
Thank you for choosing AMC Cinemas. Where we make movie moments AMC l. Your safety is l to us, so remember, keep a l on your valuables. Be sure to report any l behavior. And please take a moment to find any emergency exits near you. Make sure to stock up on l and l at the concessions bar. Enjoy your movie, l and we thank you for choosing AMC Cinemas as your destination to watch that movie. Please, silence your l now and be quiet and courteous of your peers. And now, ladies and gentlemen, here are some special previews coming to an AMC near you.
Generated by Caleb Fontenot's madlibs.py

@ -0,0 +1,5 @@
Three Little Pigs
Once upon a time, there were three asdf pigs. One day, their mother said, "You are all grown up and must asdf on your own." So they left to l their houses. The first little pig wanted only to l all day and quickly built his house out of l. The second little pig wanted to l and l all day so he l his house with l. The third l pig knew the wolf lived nearby and worked hard to l his house out of l. One day, the wolf knocked on the first pig's Give me a(n) **noun**:. "Let me in or I'll l your house down!" The pig didn't, so the wolf l down the l. The wolf knocked on the second pig's l. "Let me in or I'll blow your l down!" The pig didn't, so the wolf l down the house. Then the wolf knocked on the third pig's door. "Let me in or I'll blow your house down!" The little pig didn't, so the wolf l and l.He could not blow the house down. All the pigs went to live in the l house and they all l happily ever after.
Generated by Caleb Fontenot's madlibs.py

@ -0,0 +1,5 @@
AMC Cinemas Pre-Show Policy
Thank you for choosing AMC Cinemas. Where we make movie moments AMC l. Your safety is l to us, so remember, keep a l on your valuables. Be sure to report any l behavior. And please take a moment to find any emergency exits near you. Make sure to stock up on l and l at the concessions bar. Enjoy your movie, l and we thank you for choosing AMC Cinemas as your destination to watch that movie. Please, silence your l now and be quiet and courteous of your peers. And now, ladies and gentlemen, here are some special previews coming to an AMC near you.
Generated by Caleb Fontenot's madlibs.py

@ -0,0 +1,5 @@
Three Little Pigs
Once upon a time, there were three l pigs. One day, their mother said, "You are all grown up and must l on your own." So they left to l their houses. The first little pig wanted only to l all day and quickly built his house out of l. The second little pig wanted to l and Give me a(n) **verb**: all day so he l his house with l. The third l pig knew the wolf lived nearby and worked hard to l his house out of l. One day, the wolf knocked on the first pig's l. "Let me in or I'll l your house down!" The pig didn't, so the wolf l down the l. The wolf knocked on the second pig's l. "Let me in or I'll blow your l down!" The pig didn't, so the wolf l down the house. Then the wolf knocked on the third l pig's door. "Let me in or I'll blow your house down!" The little pig didn't, so the wolf and l.He could not blow the house down. All the pigs went to live in the l house and they all l happily ever after.
Generated by Caleb Fontenot's madlibs.py

@ -0,0 +1,5 @@
Three Little Pigs
Once upon a time, there were three l pigs. One day, their mother said, "You are all grown up and must l on your own." So they left to l their houses. The first little pig wanted only to l all day and quickly built his house out of Give me a(n) **plural noun**:. The second little pig wanted to l and l all day so he l his house with l. The third l pig knew the wolf lived nearby and worked hard to l his house out of k. One day, the wolf knocked on the first pig's f. "Let me in or I'll a your house down!" The pig didn't, so the wolf e down the v. The wolf knocked on the second pig's q. "Let me in or I'll blow your w down!" The pig didn't, so the wolf r down the house. Then the wolf knocked on the third t pig's door. "Let me in or I'll blow your house down!" The little pig didn't, so the wolf k and l.He could not blow the house down. All the pigs went to live in the l house and they all l happily ever after.
Generated by Caleb Fontenot's madlibs.py

@ -0,0 +1,5 @@
AMC Cinemas Pre-Show Policy
Thank you for choosing AMC Cinemas. Where we make movie moments AMC running. Your safety is strange to us, so remember, keep a knuckles on your valuables. Be sure to report any odd behavior. And please take a moment to find any emergency exits near you. Make sure to stock up on Sparkling Ice and Pizza at the concessions bar. Enjoy your movie, and we thank you for choosing AMC Cinemas as your destination to watch that movie. Please, silence your asdf now and be quiet and courteous of your peers. And now, ladies and gentlemen, here are some special previews coming to an AMC near you.
Generated by Caleb Fontenot's madlibs.py

@ -0,0 +1,5 @@
Awards Show
Welcome everyone to Jerusalem and the 2473 MadLib awards show. Here are the nominees for idklol. First, Barack Obama. Next, Kate Mulgrew. And finally, Donald Trump. And the winner is... Kate Mulgrew! could not be here tonight so he recorded a message for us to see. Take a look. Winner: Hello all. Thank you, the fans, for voting. I really appreciate it. I am sorry I couldn't be there.
Generated by Caleb Fontenot's madlibs.py

@ -0,0 +1,5 @@
AMC Cinemas Pre-Show Policy
Thank you for choosing AMC Cinemas. Where we make movie moments AMC l. Your safety is l to us, so remember, keep a asdf on your valuables. Be sure to report any asdf behavior. And please take a moment to find any emergency exits near you. Make sure to stock up on asdfdfdsasd and sdf at the concessions bar. Enjoy your movie, asdf and we thank you for choosing AMC Cinemas as your destination to watch that movie. Please, silence your asdf now and be quiet and courteous of your peers. And now, ladies and gentlemen, here are some special previews coming to an AMC near you.
Generated by Caleb Fontenot's madlibs.py