飞道的博客

Qt文档阅读笔记-Hello Speak Example

322人阅读  评论(0)

官方的这个例子比较有意思,在此记录下,方便以后查阅。

Hello Speak Example

这个例子主要是使用QTextToSpeech类将用户自定义输入的文本转换为口语,包括高低音、声音大小、读速。并且能够选择语言和声音。

包含的文件如下:

 

本篇博文,主要记录下如何使用QTextToSpeech将文字转换为口语。

QTextToSpeech Class

这里先来看下这个类,使用他需要在pro文件添加texttospeech。关于这个类的描述如下:

QTextToSpeech类提供了text-to-speech的引擎,这个引擎使用及其方便。

使用say(),就能将文字转换为口语,并且他能通过setLocale()指定语言,使用setVoice()选择声音。这里有点要注意这些语言和声音依赖于本地机器是否支持。

下面是关于QTextToSpeech::State

constant value description
QTextToSpeech::Ready 0 文本输入完后,准备将输入的文本转换为口语。
QTextToSpeech::Speaking 1 文本正在转换为口语。
QTextToSpeech::Paused 2 暂停,调用resume()进行恢复。
QTextToSpeech::BackendError 3 转换口语后端发生错误时

解析

下面主要是来看下,这个例子,是如何将文本转换为口语的。

关键.h文件:

mainwindow.h


  
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QtWidgets/qmainwindow.h>
  4. #include "ui_mainwindow.h"
  5. #include <QTextToSpeech>
  6. class MainWindow : public QMainWindow
  7. {
  8. Q_OBJECT
  9. public:
  10. MainWindow(QWidget *parent = 0);
  11. public slots:
  12. void speak();
  13. void stop();
  14. void setRate(int);
  15. void setPitch(int);
  16. void setVolume(int volume);
  17. void stateChanged(QTextToSpeech::State state);
  18. void engineSelected(int index);
  19. void languageSelected(int language);
  20. void voiceSelected(int index);
  21. void localeChanged(const QLocale &locale);
  22. private:
  23. Ui::MainWindow ui;
  24. QTextToSpeech *m_speech;
  25. QVector<QVoice> m_voices;
  26. };
  27. #endif

关键.cpp文件

mainwindow.cpp


  
  1. #include "mainwindow.h"
  2. #include <QLoggingCategory>
  3. MainWindow:: MainWindow(QWidget *parent)
  4. : QMainWindow(parent),
  5. m_speech( 0)
  6. {
  7. ui. setupUi( this);
  8. //QLoggingCategory::setFilterRules(QStringLiteral("qt.speech.tts=true \n qt.speech.tts.*=true"));
  9. // Populate engine selection list
  10. ui.engine-> addItem( "Default", QString( "default"));
  11. foreach (QString engine, QTextToSpeech:: availableEngines())
  12. ui.engine-> addItem(engine, engine);
  13. ui.engine-> setCurrentIndex( 0);
  14. engineSelected( 0);
  15. connect(ui.speakButton, &QPushButton::clicked, this, &MainWindow::speak);
  16. connect(ui.pitch, &QSlider::valueChanged, this, &MainWindow::setPitch);
  17. connect(ui.rate, &QSlider::valueChanged, this, &MainWindow::setRate);
  18. connect(ui.volume, &QSlider::valueChanged, this, &MainWindow::setVolume);
  19. connect(ui.engine, static_cast< void (QComboBox::*)( int)>(&QComboBox::currentIndexChanged), this, &MainWindow::engineSelected);
  20. }
  21. void MainWindow::speak()
  22. {
  23. m_speech-> say(ui.plainTextEdit-> toPlainText());
  24. }
  25. void MainWindow::stop()
  26. {
  27. m_speech-> stop();
  28. }
  29. void MainWindow::setRate(int rate)
  30. {
  31. m_speech-> setRate(rate / 10.0);
  32. }
  33. void MainWindow::setPitch(int pitch)
  34. {
  35. m_speech-> setPitch(pitch / 10.0);
  36. }
  37. void MainWindow::setVolume(int volume)
  38. {
  39. m_speech-> setVolume(volume / 100.0);
  40. }
  41. void MainWindow::stateChanged(QTextToSpeech::State state)
  42. {
  43. if (state == QTextToSpeech::Speaking) {
  44. ui.statusbar-> showMessage( "Speech started...");
  45. } else if (state == QTextToSpeech::Ready)
  46. ui.statusbar-> showMessage( "Speech stopped...", 2000);
  47. else if (state == QTextToSpeech::Paused)
  48. ui.statusbar-> showMessage( "Speech paused...");
  49. else
  50. ui.statusbar-> showMessage( "Speech error!");
  51. ui.pauseButton-> setEnabled(state == QTextToSpeech::Speaking);
  52. ui.resumeButton-> setEnabled(state == QTextToSpeech::Paused);
  53. ui.stopButton-> setEnabled(state == QTextToSpeech::Speaking || state == QTextToSpeech::Paused);
  54. }
  55. void MainWindow::engineSelected(int index)
  56. {
  57. QString engineName = ui.engine-> itemData(index). toString();
  58. delete m_speech;
  59. if (engineName == "default")
  60. m_speech = new QTextToSpeech( this);
  61. else
  62. m_speech = new QTextToSpeech(engineName, this);
  63. disconnect(ui.language, static_cast< void (QComboBox::*)( int)>(&QComboBox::currentIndexChanged), this, &MainWindow::languageSelected);
  64. ui.language-> clear();
  65. // Populate the languages combobox before connecting its signal.
  66. QVector<QLocale> locales = m_speech-> availableLocales();
  67. QLocale current = m_speech-> locale();
  68. foreach ( const QLocale &locale, locales) {
  69. QString name(QString("%1 (%2)")
  70. .arg(QLocale::languageToString(locale.language()))
  71. .arg(QLocale::countryToString(locale.country())));
  72. QVariant localeVariant(locale);
  73. ui.language-> addItem(name, localeVariant);
  74. if (locale. name() == current. name())
  75. current = locale;
  76. }
  77. setRate(ui.rate-> value());
  78. setPitch(ui.pitch-> value());
  79. setVolume(ui.volume-> value());
  80. connect(ui.stopButton, &QPushButton::clicked, m_speech, &QTextToSpeech::stop);
  81. connect(ui.pauseButton, &QPushButton::clicked, m_speech, &QTextToSpeech::pause);
  82. connect(ui.resumeButton, &QPushButton::clicked, m_speech, &QTextToSpeech::resume);
  83. connect(m_speech, &QTextToSpeech::stateChanged, this, &MainWindow::stateChanged);
  84. connect(m_speech, &QTextToSpeech::localeChanged, this, &MainWindow::localeChanged);
  85. connect(ui.language, static_cast< void (QComboBox::*)( int)>(&QComboBox::currentIndexChanged), this, &MainWindow::languageSelected);
  86. localeChanged(current);
  87. }
  88. void MainWindow::languageSelected(int language)
  89. {
  90. QLocale locale = ui.language-> itemData(language). toLocale();
  91. m_speech-> setLocale(locale);
  92. }
  93. void MainWindow::voiceSelected(int index)
  94. {
  95. m_speech-> setVoice(m_voices. at(index));
  96. }
  97. void MainWindow::localeChanged(const QLocale &locale)
  98. {
  99. QVariant localeVariant(locale);
  100. ui.language-> setCurrentIndex(ui.language-> findData(localeVariant));
  101. disconnect(ui.voice, static_cast< void (QComboBox::*)( int)>(&QComboBox::currentIndexChanged), this, &MainWindow::voiceSelected);
  102. ui.voice-> clear();
  103. m_voices = m_speech-> availableVoices();
  104. QVoice currentVoice = m_speech-> voice();
  105. foreach ( const QVoice &voice, m_voices) {
  106. ui.voice-> addItem( QString( "%1 - %2 - %3"). arg(voice. name())
  107. . arg(QVoice:: genderName(voice. gender()))
  108. . arg(QVoice:: ageName(voice. age())));
  109. if (voice. name() == currentVoice. name())
  110. ui.voice-> setCurrentIndex(ui.voice-> count() - 1);
  111. }
  112. connect(ui.voice, static_cast< void (QComboBox::*)( int)>(&QComboBox::currentIndexChanged), this, &MainWindow::voiceSelected);
  113. }

下面来跟一下这个m_speech是如何用的。

在.cpp中可以看到:

将engineName传入了QTextToSpeech的构造函数,而engineName的值来自于

QTextToSpeech::availableEngines()。

这里来看下,他是如何选择语言和音频的,


  
  1. connect(ui.language, static_cast< void (QComboBox::*)( int)>(&QComboBox::currentIndexChanged), this, &MainWindow::languageSelected);
  2. connect(ui.voice, static_cast< void (QComboBox::*)( int)>(&QComboBox::currentIndexChanged), this, &MainWindow::voiceSelected);

对应的槽函数分别为:


  
  1. void MainWindow::languageSelected(int language)
  2. {
  3. QLocale locale = ui.language-> itemData(language). toLocale();
  4. m_speech-> setLocale(locale);
  5. }
  6. void MainWindow::voiceSelected(int index)
  7. {
  8. m_speech-> setVoice(m_voices. at(index));
  9. }

从中可以看到,这里封装好的,应用层调用的确是简单太多。

下面看下,他是如何播放、暂停、续播:


  
  1. connect(ui.stopButton, &QPushButton::clicked, m_speech, &QTextToSpeech::stop);
  2. connect(ui.pauseButton, &QPushButton::clicked, m_speech, &QTextToSpeech::pause);
  3. connect(ui.resumeButton, &QPushButton::clicked, m_speech, &QTextToSpeech::resume);

关于设置音调,读速的相关代码如下:


  
  1. void MainWindow::setRate(int rate)
  2. {
  3. m_speech-> setRate(rate / 10.0);
  4. }
  5. void MainWindow::setPitch(int pitch)
  6. {
  7. m_speech-> setPitch(pitch / 10.0);
  8. }
  9. void MainWindow::setVolume(int volume)
  10. {
  11. m_speech-> setVolume(volume / 100.0);
  12. }

转载:https://blog.csdn.net/qq78442761/article/details/127486924
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场