QTextToSpeech#

The QTextToSpeech class provides a convenient access to text-to-speech engines. More

Inheritance diagram of PySide6.QtTextToSpeech.QTextToSpeech

Synopsis#

Properties#

  • engine - The engine used to synthesize text to speech

  • engineCapabilities - The capabilities implemented by the current engine

  • locale - The current locale in use

  • pitch - The voice pitch, ranging from -1.0 to 1.0

  • rate - The current voice rate, ranging from -1.0 to 1.0

  • state - The current state of the speech synthesizer

  • voice - The voice that will be used for the speech

  • volume - The current volume, ranging from 0.0 to 1.0

Functions#

Slots#

Signals#

Static functions#

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

Detailed Description#

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Use say() to start reading text to the default audio device, and stop() , pause() , and resume() to control the reading of the text.

ui.speakButton.clicked.connect(m_speech, [this]{
    m_speech.say(ui.plainTextEdit.toPlainText())
})
ui.stopButton.clicked.connect(m_speech, [this]{
    m_speech.stop()
})
ui.pauseButton.clicked.connect(m_speech, [this]{
    m_speech.pause()
})
ui.resumeButton.clicked.connect(m_speech.resume)

To synthesize text into PCM data for further processing, use synthesize() .

Use findVoices() to get a list of matching voices, or use availableVoices() to get the list of voices that support the current locale. Change the locale property, using one of the availableLocales() that is a good match for the language that the input text is in, and for the accent of the desired voice output. This will change the list of available voices on most platforms. Then use one of the available voices in a call to setVoice() .

Not every engine supports all features. Use the engineCapabilities() function to test which features are available, and adjust the usage of the class accordingly.

Note

Which locales and voices the engine supports depends usually on the Operating System configuration. E.g. on macOS, end users can install voices through the Accessibility panel in System Preferences.

class PySide6.QtTextToSpeech.QTextToSpeech([parent=None])#

PySide6.QtTextToSpeech.QTextToSpeech(engine[, parent=None])

PySide6.QtTextToSpeech.QTextToSpeech(engine, params[, parent=None])

Parameters:
  • params – Dictionary with keys of type .QString and values of type QVariant.

  • engine – str

  • parentPySide6.QtCore.QObject

Loads a text-to-speech engine from a plug-in that uses the default engine plug-in and constructs a QTextToSpeech object as the child of parent.

The default engine is platform-specific.

If the engine initializes correctly, then the state of the engine will change to Ready ; note that this might happen asynchronously. If the plugin fails to load, then state will be set to Error .

Loads a text-to-speech engine from a plug-in that matches parameter engine and constructs a QTextToSpeech object as the child of parent.

If engine is empty, the default engine plug-in is used. The default engine is platform-specific.

If the engine initializes correctly, the state of the engine will be set to Ready . If the plugin fails to load, or if the engine fails to initialize, the engine’s state will be set to Error .

Loads a text-to-speech engine from a plug-in that matches parameter engine and constructs a QTextToSpeech object as the child of parent, passing params through to the engine.

If engine is empty, the default engine plug-in is used. The default engine is platform-specific. Which key/value pairs in params are supported depends on the engine. See the engine documentation for details. Unsupported entries will be ignored.

If the engine initializes correctly, the state of the engine will be set to Ready . If the plugin fails to load, or if the engine fails to initialize, the engine’s state will be set to Error .

Note

Properties can be used directly when from __feature__ import true_property is used or via accessor functions otherwise.

property PᅟySide6.QtTextToSpeech.QTextToSpeech.engine: str#

This property holds the engine used to synthesize text to speech..

Changing the engine stops any ongoing speech.

On most platforms, changing the engine will update the list of available locales and available voices .

Access functions:
property PᅟySide6.QtTextToSpeech.QTextToSpeech.engineCapabilities: Combination of QDBusConnection.ConnectionCapability#

This property holds the capabilities implemented by the current engine.

See also

engine

Access functions:
property PᅟySide6.QtTextToSpeech.QTextToSpeech.locale: PySide6.QtCore.QLocale#

This property holds the current locale in use..

By default, the system locale is used.

On some platforms, changing the locale will update the list of available voices , and if the current voice is not available with the new locale, a new voice will be set.

See also

voice findVoices()

Access functions:
property PᅟySide6.QtTextToSpeech.QTextToSpeech.pitch: float#

This property holds the voice pitch, ranging from -1.0 to 1.0..

The default of 0.0 is the normal speech pitch.

Access functions:
property PᅟySide6.QtTextToSpeech.QTextToSpeech.rate: float#

This property holds the current voice rate, ranging from -1.0 to 1.0..

The default value of 0.0 is normal speech flow.

Access functions:
property PᅟySide6.QtTextToSpeech.QTextToSpeech.state: State#

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

This property holds the current state of the speech synthesizer..

def stateChanged(self, state):


    if state == QTextToSpeech.Speaking:
        ui.statusbar.showMessage(tr("Speech started..."))
        break
    elif state == QTextToSpeech.Ready:
        ui.statusbar.showMessage(tr("Speech stopped..."), 2000)
        break
    elif state == QTextToSpeech.Paused:
        ui.statusbar.showMessage(tr("Speech paused..."))
        break
    else:
        ui.statusbar.showMessage(tr("Speech error!"))
        break

    ui.pauseButton.setEnabled(state == QTextToSpeech.Speaking)
    ui.resumeButton.setEnabled(state == QTextToSpeech.Paused)
    ui.stopButton.setEnabled(state == QTextToSpeech.Speaking or state == QTextToSpeech.Paused)

Use say() to start synthesizing text with the current voice and locale .

Access functions:
property PᅟySide6.QtTextToSpeech.QTextToSpeech.voice: PySide6.QtTextToSpeech.QVoice#

This property holds the voice that will be used for the speech..

The voice needs to be one of the voices available for the engine.

On some platforms, setting the voice changes other voice attributes such as locale , pitch , and so on. These changes trigger the emission of signals.

See also

findVoices()

Access functions:
property PᅟySide6.QtTextToSpeech.QTextToSpeech.volume: float#

This property holds the current volume, ranging from 0.0 to 1.0..

The default value is the platform’s default volume.

Access functions:
PySide6.QtTextToSpeech.QTextToSpeech.State#

This enum describes the current state of the text-to-speech engine.

Constant

Description

QTextToSpeech.Ready

The synthesizer is ready to start a new text. This is also the state after a text was finished.

QTextToSpeech.Speaking

Text is being spoken.

QTextToSpeech.Synthesizing

Text is being synthesized into PCM data. The synthesized() signal will be emitted with chunks of data.

QTextToSpeech.Paused

The synthesis was paused and can be resumed with resume() .

QTextToSpeech.Error

An error has occurred. Details are given by errorReason() .

See also

ErrorReason errorReason() errorString()

PySide6.QtTextToSpeech.QTextToSpeech.ErrorReason#

This enum describes the current error, if any, of the QTextToSpeech engine.

Constant

Description

QTextToSpeech.ErrorReason.NoError

No error has occurred.

QTextToSpeech.ErrorReason.Initialization

The backend could not be initialized, e.g. due to a missing driver or operating system requirement.

QTextToSpeech.ErrorReason.Configuration

The given backend configuration is inconsistent, e.g. due to wrong voice name or parameters.

QTextToSpeech.ErrorReason.Input

The given text could not be synthesized, e.g. due to invalid size or characters.

QTextToSpeech.ErrorReason.Playback

Audio playback failed e.g. due to missing audio device, wrong format or audio streaming interruption.

Use errorReason() to obtain the current error and errorString() to get the related error message.

See also

errorOccurred()

PySide6.QtTextToSpeech.QTextToSpeech.BoundaryHint#

describes when speech should be stopped and paused.

Constant

Description

QTextToSpeech.BoundaryHint.Default

Uses the engine specific default behavior.

QTextToSpeech.BoundaryHint.Immediate

The engine should stop playback immediately.

QTextToSpeech.BoundaryHint.Word

Stop speech when the current word is finished.

QTextToSpeech.BoundaryHint.Sentence

Stop speech when the current sentence is finished.

QTextToSpeech.BoundaryHint.Utterance

Stop speech when the current utterance is finished. An utterance is the block of text used in a call to say() or enqueue() .

Note

These are hints to the engine. The current engine might not support all options.

PySide6.QtTextToSpeech.QTextToSpeech.Capability#

This enum describes the capabilities of a text-to-speech engine.

Constant

Description

QTextToSpeech.Capability.None

(inherits enum.Flag) The engine implements none of the capabilities.

QTextToSpeech.Capability.Speak

The engine can play audio output from text.

QTextToSpeech.Capability.PauseResume

The engine can pause and then resume the audo output.

QTextToSpeech.Capability.WordByWordProgress

The engine emits the sayingWord() signal for each word that gets spoken.

QTextToSpeech.Capability.Synthesize

The engine can synthesize PCM audio data from text.

New in version 6.6.

PySide6.QtTextToSpeech.QTextToSpeech.SynthesizeOverload#
PySide6.QtTextToSpeech.QTextToSpeech.aboutToSynthesize(id)#
Parameters:

id – int

This signal gets emitted just before the engine starts to synthesize the speech audio for id. The id is the value returned by a call to enqueue() , Applications can use this signal to make last-minute changes to voice attributes, or to track the process of text enqueued via enqueue() .

See also

enqueue() synthesize() voice

PySide6.QtTextToSpeech.QTextToSpeech.allVoices(locale)#
Parameters:

localePySide6.QtCore.QLocale

Return type:

.list of QVoice

static PySide6.QtTextToSpeech.QTextToSpeech.availableEngines()#
Return type:

list of strings

Gets the list of supported text-to-speech engine plug-ins.

See also

engine

PySide6.QtTextToSpeech.QTextToSpeech.availableLocales()#
Return type:

.list of QLocale

Returns the list of locales that are supported by the active engine .

See also

availableVoices() findVoices()

PySide6.QtTextToSpeech.QTextToSpeech.availableVoices()#
Return type:

.list of QVoice

Returns the list of voices available for the current locale .

Note

If no locale has been set, the system locale is used.

See also

availableLocales() findVoices()

PySide6.QtTextToSpeech.QTextToSpeech.engine()#
Return type:

str

See also

setEngine()

Getter of property engine .

PySide6.QtTextToSpeech.QTextToSpeech.engineCapabilities()#
Return type:

Combination of QTextToSpeech.Capability

PySide6.QtTextToSpeech.QTextToSpeech.engineChanged(engine)#
Parameters:

engine – str

Notification signal of property engine .

PySide6.QtTextToSpeech.QTextToSpeech.enqueue(text)#
Parameters:

text – str

Return type:

int

Adds utterance to the queue of texts to be spoken, and starts speaking. Returns the index of the text in the queue, or -1 in case of an error.

If the engine’s state is currently Ready, utterance will be spoken immediately. Otherwise, the engine will start to speak utterance once it has finished speaking the current text.

Each time the engine proceeds to the next text entry in the queue, the aboutToSynthesize() signal gets emitted. This allows applications to keep track of the progress, and to make last-minute changes to voice attributes.

Calling stop() clears the queue. To pause the engine at the end of a text, use the Utterance boundary hint.

See also

say() stop() aboutToSynthesize() synthesize()

PySide6.QtTextToSpeech.QTextToSpeech.errorOccurred(error, errorString)#
Parameters:

This signal is emitted after an error occurred and the state has been set to Error . The reason parameter specifies the type of error, and the errorString provides a human-readable error description.

ErrorReason is not a registered metatype, so for queued connections, you will have to register it with Q_DECLARE_METATYPE() and qRegisterMetaType().

See also

errorReason() errorString() Creating Custom Qt Types

PySide6.QtTextToSpeech.QTextToSpeech.errorReason()#
Return type:

ErrorReason

Returns the reason why the engine has reported an error.

PySide6.QtTextToSpeech.QTextToSpeech.errorString()#
Return type:

str

Returns the current engine error message.

See also

errorOccurred()

PySide6.QtTextToSpeech.QTextToSpeech.locale()#
Return type:

PySide6.QtCore.QLocale

See also

setLocale()

Getter of property locale .

PySide6.QtTextToSpeech.QTextToSpeech.localeChanged(locale)#
Parameters:

localePySide6.QtCore.QLocale

Notification signal of property locale .

PySide6.QtTextToSpeech.QTextToSpeech.pause([boundaryHint=QTextToSpeech.BoundaryHint.Default])#
Parameters:

boundaryHintBoundaryHint

Pauses the current speech at boundaryHint.

Whether the boundaryHint is respected depends on the engine .

See also

resume() PauseResume

PySide6.QtTextToSpeech.QTextToSpeech.pitch()#
Return type:

float

See also

setPitch()

Getter of property pitch .

PySide6.QtTextToSpeech.QTextToSpeech.pitchChanged(pitch)#
Parameters:

pitch – float

Notification signal of property pitch .

PySide6.QtTextToSpeech.QTextToSpeech.rate()#
Return type:

float

See also

setRate()

Getter of property rate .

PySide6.QtTextToSpeech.QTextToSpeech.rateChanged(rate)#
Parameters:

rate – float

Notification signal of property rate .

PySide6.QtTextToSpeech.QTextToSpeech.resume()#

Resume speaking after pause() has been called.

Note

On Android, resuming paused speech will restart from the beginning. This is a limitation of the underlying text-to-speech engine.

See also

pause()

PySide6.QtTextToSpeech.QTextToSpeech.say(text)#
Parameters:

text – str

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Starts speaking the text.

This function starts sythesizing the speech asynchronously, and reads the text to the default audio output device.

ui.speakButton.clicked.connect(m_speech, [this]{
    m_speech.say(ui.plainTextEdit.toPlainText())
})

Note

All in-progress readings are stopped before beginning to read the recently synthesized text.

The current state is available using the state property, and is set to Speaking once the reading starts. When the reading is done, state will be set to Ready .

See also

enqueue() stop() pause() resume() synthesize()

PySide6.QtTextToSpeech.QTextToSpeech.sayingWord(word, id, start, length)#
Parameters:
  • word – str

  • id – int

  • start – int

  • length – int

This signal is emitted when the word, which is the slice of text indicated by start and length in the utterance id, gets played to the audio device.

Note

This signal requires that the engine has the WordByWordProgress capability.

See also

Capability say()

PySide6.QtTextToSpeech.QTextToSpeech.setEngine(engine[, params=QVariantMap()])#
Parameters:
  • engine – str

  • params – Dictionary with keys of type .QString and values of type QVariant.

Return type:

bool

Sets the engine used by this QTextToSpeech object to engine, passing params through to the engine constructor.

Returns whether engine could be set successfully.

Which key/value pairs in params are supported depends on the engine. See the engine documentation for details. Unsupported entries will be ignored.

See also

engine()

PySide6.QtTextToSpeech.QTextToSpeech.setLocale(locale)#
Parameters:

localePySide6.QtCore.QLocale

See also

locale()

Setter of property locale .

PySide6.QtTextToSpeech.QTextToSpeech.setPitch(pitch)#
Parameters:

pitch – float

See also

pitch()

Setter of property pitch .

PySide6.QtTextToSpeech.QTextToSpeech.setRate(rate)#
Parameters:

rate – float

See also

rate()

Setter of property rate .

PySide6.QtTextToSpeech.QTextToSpeech.setVoice(voice)#
Parameters:

voicePySide6.QtTextToSpeech.QVoice

See also

voice()

Setter of property voice .

PySide6.QtTextToSpeech.QTextToSpeech.setVolume(volume)#
Parameters:

volume – float

See also

volume()

Setter of property volume .

PySide6.QtTextToSpeech.QTextToSpeech.state()#
Return type:

State

Getter of property state .

PySide6.QtTextToSpeech.QTextToSpeech.stateChanged(state)#
Parameters:

stateState

Notification signal of property state .

PySide6.QtTextToSpeech.QTextToSpeech.stop([boundaryHint=QTextToSpeech.BoundaryHint.Default])#
Parameters:

boundaryHintBoundaryHint

Stops the current reading at boundaryHint, and clears the queue of pending texts.

The reading cannot be resumed. Whether the boundaryHint is respected depends on the engine.

PySide6.QtTextToSpeech.QTextToSpeech.voice()#
Return type:

PySide6.QtTextToSpeech.QVoice

See also

setVoice()

Getter of property voice .

PySide6.QtTextToSpeech.QTextToSpeech.voiceChanged(voice)#
Parameters:

voicePySide6.QtTextToSpeech.QVoice

Notification signal of property voice .

PySide6.QtTextToSpeech.QTextToSpeech.volume()#
Return type:

float

See also

setVolume()

Getter of property volume .

PySide6.QtTextToSpeech.QTextToSpeech.volumeChanged(volume)#
Parameters:

volume – float

Notification signal of property volume .