0

I am able to store the TTS output to a file using the following code

import pyttsx3

engine = pyttsx3.init()

engine.save_to_file('Hello World!', 'hello.mp3')
engine.runAndWait()

Is it is possible to store this to a temporary file instead for an Api service I am creating.

I did some digging around the pyttsx3 source code and found this

    def save_to_file(self, text, filename, name):
        '''
        Called by the engine to push a say command onto the queue.
        @param text: Text to speak
        @type text: unicode
        @param name: Name to associate with the utterance
        @type name: str
        '''
        self._push(self._driver.save_to_file, (text, filename), name)

Which invokes the following code

 def save_to_file(self, text, filename):
        cwd = os.getcwd()
        stream = comtypes.client.CreateObject('SAPI.SPFileStream')
        stream.Open(filename, SpeechLib.SSFMCreateForWrite)
        temp_stream = self._tts.AudioOutputStream
        self._tts.AudioOutputStream = stream
        self._tts.Speak(fromUtf8(toUtf8(text)))
        self._tts.AudioOutputStream = temp_stream
        stream.close()
        os.chdir(cwd)

However I am not able to quite understand how to implement what I have in mind.

InsertCheesyLine
  • 1,112
  • 2
  • 11
  • 19

1 Answers1

0

Credits to @Jiangshan00001

By making changes to the sapi5 driver script you can pass a BytesIO object instead of filename.

import pyttsx4
from io import BytesIO

engine = pyttsx4.init()
b = BytesIO()
engine.save_to_file('Hello World!', b)
engine.runAndWait()
InsertCheesyLine
  • 1,112
  • 2
  • 11
  • 19