TextToSpeech QML Type

The TextToSpeech type provides access to text-to-speech engines. 更多...

导入语句: import QtTextToSpeech

特性

信号

方法

详细描述

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

    TextToSpeech {
        id: tts
        volume: volumeSlider.value
        pitch: pitchSlider.value
        rate: rateSlider.value
    ...
        RowLayout {
            Button {
                text: qsTr("Speak")
                enabled: [TextToSpeech.Paused, TextToSpeech.Ready].includes(tts.state)
                onClicked: {
                    tts.say(input.text)
                }
            }
            Button {
                text: qsTr("Pause")
                enabled: tts.state == TextToSpeech.Speaking
                onClicked: tts.pause()
                visible: tts.engineCapabilities & TextToSpeech.Capabilities.PauseResume
            }
            Button {
                text: qsTr("Resume")
                enabled: tts.state == TextToSpeech.Paused
                onClicked: tts.resume()
                visible: tts.engineCapabilities & TextToSpeech.Capabilities.PauseResume
            }
    ...
					

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

To set a voice, use the VoiceSelector attached property like this:

TextToSpeech {
    VoiceSelector.locale: Qt.locale("en_UK")
    VoiceSelector.gender: Voice.Male
}
					

The first voice that matches all specified criteria will be used. If no voice matches all criteria, then the voice will not change.

Alternatively, 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 the voice 特性。

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

注意: 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 可访问性 panel in System Preferences .

特性文档编制

engine : string

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 .


[since 6.6] engineCapabilities : enumeration

This property holds the capabilities implemented by the current engine.

This property was introduced in Qt 6.6.

另请参阅 engine and QTextToSpeech::Capability .


engineParameters : map

This property holds engine-specific parameters.

另请参阅 engine .


locale : locale

This property holds the current locale in use.

By default, the system locale is used.

另请参阅 voice .


pitch : double

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

The default of 0.0 is the normal speech pitch.


rate : double

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

The default of 0.0 is the normal speech flow.


state : enumeration

This property holds the current state of the speech synthesizer.

        onStateChanged: updateStateLabel(state)
        function updateStateLabel(state)
        {
            switch (state) {
                case TextToSpeech.Ready:
                    statusLabel.text = qsTr("Ready")
                    break
                case TextToSpeech.Speaking:
                    statusLabel.text = qsTr("Speaking")
                    break
                case TextToSpeech.Paused:
                    statusLabel.text = qsTr("Paused...")
                    break
                case TextToSpeech.Error:
                    statusLabel.text = qsTr("Error!")
                    break
            }
        }
					

另请参阅 QTextToSpeech::State , say (), stop (),和 pause ().


voice : Voice

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.


volume : double

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

The default value is the platform's default volume.


信号文档编制

[since 6.6] aboutToSynthesize ( number id )

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

注意: 相应处理程序是 onAboutToSynthesize .

This signal was introduced in Qt 6.6.

另请参阅 enqueue () 和 voice .


void errorOccured ( enumeration reason , string errorString )

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

注意: 相应处理程序是 onErrorOccured .

另请参阅 state , errorReason (),和 errorString ().


[since 6.6] sayingWord ( string word , int id , int start , int length )

此信号被发射当 word , which is the slice of text indicated by start and length in the utterance id , gets played to the audio device.

注意: This signal requires that the engine has the WordByWordProgress 能力。

The following code highlights the word that is spoken in a TextArea input :

        onSayingWord: (word, id, start, length)=> {
            input.select(start, start + length)
        }
					

注意: 相应处理程序是 onSayingWord .

This signal was introduced in Qt 6.6.

另请参阅 QTextToSpeech::Capability and say ().


方法文档编制

list < string > availableEngines ()

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


list < Voice > availableLocales ()

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


list < Voice > availableVoices ()

Holds the list of voices available for the current locale .


[since 6.6] enqueue ( string utterance )

添加 utterance to the queue of text to be spoken, and starts speaking.

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.

调用 stop () clears the queue.

This method was introduced in Qt 6.6.

另请参阅 say (), stop (),和 aboutToSynthesize ().


enumeration errorReason ()

Returns the reason why the engine has reported an error.

另请参阅 QTextToSpeech::ErrorReason .


string errorString ()

Returns the current engine error message.


[since 6.6] list < voice > findVoices ( map criteria )

Returns the list of voices that match all the specified criteria .

criteria is a map from voice property name to property value, supporting combinations of search criteria such as:

let daniel = tts.findVoices({
    "name": "Daniel"
})
let maleEnglish = tts.findVoices({
    "gender": Voice.Male,
    "language": Qt.locale('en')
})
					

This method was introduced in Qt 6.6.

另请参阅 VoiceSelector .


pause ( BoundaryHint boundaryHint )

Pauses the current speech at boundaryHint .

是否 boundaryHint is respected depends on the engine .

另请参阅 resume (), QTextToSpeech::BoundaryHint ,和 PauseResume .


resume ()

Resume speaking after pause () 被调用。

另请参阅 pause ().


say ( string text )

Starts synthesizing the text .

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

        RowLayout {
            Button {
                text: qsTr("Speak")
                enabled: [TextToSpeech.Paused, TextToSpeech.Ready].includes(tts.state)
                onClicked: {
                    tts.say(input.text)
                }
            }
					

注意: 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 QTextToSpeech::Speaking once the reading starts. When the reading is done, state will be set to QTextToSpeech::Ready .

另请参阅 stop (), pause (),和 resume ().


stop ( BoundaryHint boundaryHint )

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.

另请参阅 say (), enqueue (), pause (),和 QTextToSpeech::BoundaryHint .