I'm making a Discord bot in Python with discord.py, and I am implementing a tts command. Basically, I have a function that saves the audio data I get to a byte type variable, and then (for testing purposes) writes it to a file. This is the code:
def speak(text):
engine.setProperty("rate", 150)
engine.say(text)
engine.runAndWait()
fileData = bytes()
filePath = './sound.mp3'
engine.save_to_file(text, fileData)
engine.runAndWait()
with open(filePath, 'w+b') as f:
f.write(fileData)
f.close()
print("Wrote to file")
return os.path.abspath(filePath)
However, after all is said and done, sound.mp3 is 0 bytes, and also the Python interpreter doesn't throw any errors or warnings. You could say that I can write directly into the file, but the goal here is to store it in memory so there is less wear on my Raspberry Pi's SD (that's where I'm hosting the bot). Discord.py also normally requires a file to play audio, but I'm using a patch that allows me to play bytes and byte-like objects, so that is also taken care of. I just need help with the above code. All help is appreciated. Thank you!