0

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!

  • Hi not sure about the details here but if you use the `with` context code, you do not need to run `f.close()`, Python should take care of it. – patrick Jan 13 '21 at 16:17
  • @patrick Thanks for the tip, I'll keep that in mind! – Κοσμάς Ράπτης Jan 13 '21 at 17:10
  • Hello, this is an older post but I am currently facing a very similar problem. I am writing tts files and some (not all weirdly enough) are ending up as 0 bytes files, which then in return crashes my program later on when the files are being read again. Do you remember how you fixed this/what the problem was? – Sokker Sep 25 '22 at 19:15
  • @Sokker Hi! I never managed to figure this out (also why I never closed this), I instead used it in a different way that allowed me to pass the data directly to Discord's voice chat API. I'd be willing to ask for your code and see if I could help fix anything, but let's not stray away from the main topic here, a new thread would probably be better. – Κοσμάς Ράπτης Sep 26 '22 at 20:05

1 Answers1

0

The pyttsx module has a save_to_file method, i recommend you to use that instead, and regarding your code ........ are you converting the audio to bytes? The code just says the text in the host where the code is running and writes empty bytes to the file. This is why the file is 0 bytes, it never actually writes audio into it.

Usage:

import pyttsx3
engine = pyttsx3.init()
engine.save_to_file('Hello World' , 'test.mp3')
engine.runAndWait()

In your code you use fileData = bytes() but inside the bytes u haven't passed anything so how do you expect the filedata variable to have anything and in return the output file also is empty.

  • Hey manu, your answer looks good jus a tidbit, could you please add a usage example of `save_to_file` method? Thanks – Gyowanny Pessatto Queiroz Jan 13 '21 at 16:57
  • Hello, I'm using `save_to_file()`, just instead of a file I am saving to the variable. Also, I think that this is the way that I am supposed to pass the text to `save_to_file()`, and then it takes care of producing it etc. itself. – Κοσμάς Ράπτης Jan 13 '21 at 17:10