class 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

Methods

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 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() .

class 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()

class 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.

class 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.

Added in version 6.6.

class SynthesizeOverload

Note

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

property 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 engineCapabilitiesᅟ: Combination of QDBusConnection.ConnectionCapability

This property holds the capabilities implemented by the current engine.

See also

engine

Access functions:
property localeᅟ: 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 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 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 stateᅟ: QTextToSpeech.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 voiceᅟ: 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 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:
__init__([parent=None])
Parameters:

parentQObject

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 .

__init__(engine[, parent=None])
Parameters:
  • engine – str

  • parentQObject

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 .

__init__(engine, params[, parent=None])
Parameters:
  • engine – str

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

  • parentQObject

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 .

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

allVoices(locale)
Parameters:

localeQLocale

Return type:

.list of QVoice

static availableEngines()
Return type:

list of strings

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

See also

engine

availableLocales()
Return type:

.list of QLocale

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

See also

availableVoices() findVoices()

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()

engine()
Return type:

str

See also

setEngine()

Getter of property engineᅟ .

engineCapabilities()
Return type:

Combination of Capability

engineChanged(engine)
Parameters:

engine – str

Notification signal of property engineᅟ .

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()

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

errorReason()
Return type:

ErrorReason

Returns the reason why the engine has reported an error.

errorString()
Return type:

str

Returns the current engine error message.

See also

errorOccurred()

locale()
Return type:

QLocale

See also

setLocale()

Getter of property localeᅟ .

localeChanged(locale)
Parameters:

localeQLocale

Notification signal of property localeᅟ .

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

pitch()
Return type:

float

See also

setPitch()

Getter of property pitchᅟ .

pitchChanged(pitch)
Parameters:

pitch – float

Notification signal of property pitchᅟ .

rate()
Return type:

float

See also

setRate()

Getter of property rateᅟ .

rateChanged(rate)
Parameters:

rate – float

Notification signal of property rateᅟ .

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()

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()

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()

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()

setLocale(locale)
Parameters:

localeQLocale

See also

locale()

Setter of property localeᅟ .

setPitch(pitch)
Parameters:

pitch – float

See also

pitch()

Setter of property pitchᅟ .

setRate(rate)
Parameters:

rate – float

See also

rate()

Setter of property rateᅟ .

setVoice(voice)
Parameters:

voiceQVoice

See also

voice()

Setter of property voiceᅟ .

setVolume(volume)
Parameters:

volume – float

See also

volume()

Setter of property volumeᅟ .

state()
Return type:

State

Getter of property stateᅟ .

stateChanged(state)
Parameters:

stateState

Notification signal of property stateᅟ .

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.

voice()
Return type:

QVoice

See also

setVoice()

Getter of property voiceᅟ .

voiceChanged(voice)
Parameters:

voiceQVoice

Notification signal of property voiceᅟ .

volume()
Return type:

float

See also

setVolume()

Getter of property volumeᅟ .

volumeChanged(volume)
Parameters:

volume – float

Notification signal of property volumeᅟ .