Qt 5.8 音频、文本转换引擎
来源:互联网 发布:mac安装完win7启动失败 编辑:程序博客网 时间:2024/06/13 12:59
Qt音频引擎模块负责访问音频的功能, 讲语音转换为文本,或者将文本转化成为音频。 这是是否有用的模块, 在Linux 操作系统上依赖于libspeechd 库, 在其他的平台上提供了特定的API进行转换。
所包含的头文件为:
#include <QTextToSpeech>
在工程文件中加入 :
QT += texttospeech
简单实例:
在工程文件中加入:
TEMPLATE = app
QT += widgets texttospeech
头文件源代码:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtWidgets/qmainwindow.h>
#include "ui_mainwindow.h"
#include <QTextToSpeech>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
public slots:
void speak();
void stop();
void setRate(int);
void setPitch(int);
void setVolume(int volume);
void stateChanged(QTextToSpeech::State state);
void engineSelected(int index);
void languageSelected(int language);
void voiceSelected(int index);
void localeChanged(const QLocale &locale);
private:
Ui::MainWindow ui;
QTextToSpeech *m_speech;
QVector<QVoice> m_voices;
};
#endif
源文件:
#include "mainwindow.h"
#include <QLoggingCategory>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
m_speech(0)
{
ui.setupUi(this);
QLoggingCategory::setFilterRules(QStringLiteral("qt.speech.tts=true \n qt.speech.tts.*=true"));
// Populate engine selection list
ui.engine->addItem("Default", QString("default"));
foreach (QString engine, QTextToSpeech::availableEngines())
ui.engine->addItem(engine, engine);
ui.engine->setCurrentIndex(0);
engineSelected(0);
connect(ui.speakButton, &QPushButton::clicked, this, &MainWindow::speak);
connect(ui.pitch, &QSlider::valueChanged, this, &MainWindow::setPitch);
connect(ui.rate, &QSlider::valueChanged, this, &MainWindow::setRate);
connect(ui.volume, &QSlider::valueChanged, this, &MainWindow::setVolume);
connect(ui.engine, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::engineSelected);
}
void MainWindow::speak()
{
m_speech->say(ui.plainTextEdit->toPlainText());
}
void MainWindow::stop()
{
m_speech->stop();
}
void MainWindow::setRate(int rate)
{
m_speech->setRate(rate / 10.0);
}
void MainWindow::setPitch(int pitch)
{
m_speech->setPitch(pitch / 10.0);
}
void MainWindow::setVolume(int volume)
{
m_speech->setVolume(volume / 100.0);
}
void MainWindow::stateChanged(QTextToSpeech::State state)
{
if (state == QTextToSpeech::Speaking) {
ui.statusbar->showMessage("Speech started...");
} else if (state == QTextToSpeech::Ready)
ui.statusbar->showMessage("Speech stopped...", 2000);
else if (state == QTextToSpeech::Paused)
ui.statusbar->showMessage("Speech paused...");
else
ui.statusbar->showMessage("Speech error!");
ui.pauseButton->setEnabled(state == QTextToSpeech::Speaking);
ui.resumeButton->setEnabled(state == QTextToSpeech::Paused);
ui.stopButton->setEnabled(state == QTextToSpeech::Speaking || QTextToSpeech::Paused);
}
void MainWindow::engineSelected(int index)
{
QString engineName = ui.engine->itemData(index).toString();
delete m_speech;
if (engineName == "default")
m_speech = new QTextToSpeech(this);
else
m_speech = new QTextToSpeech(engineName, this);
disconnect(ui.language, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::languageSelected);
ui.language->clear();
// Populate the languages combobox before connecting its signal.
QVector<QLocale> locales = m_speech->availableLocales();
QLocale current = m_speech->locale();
foreach (const QLocale &locale, locales) {
QString name(QString("%1 (%2)")
.arg(QLocale::languageToString(locale.language()))
.arg(QLocale::countryToString(locale.country())));
QVariant localeVariant(locale);
ui.language->addItem(name, localeVariant);
if (locale.name() == current.name())
current = locale;
}
setRate(ui.rate->value());
setPitch(ui.pitch->value());
setVolume(ui.volume->value());
connect(ui.stopButton, &QPushButton::clicked, m_speech, &QTextToSpeech::stop);
connect(ui.pauseButton, &QPushButton::clicked, m_speech, &QTextToSpeech::pause);
connect(ui.resumeButton, &QPushButton::clicked, m_speech, &QTextToSpeech::resume);
connect(m_speech, &QTextToSpeech::stateChanged, this, &MainWindow::stateChanged);
connect(m_speech, &QTextToSpeech::localeChanged, this, &MainWindow::localeChanged);
connect(ui.language, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::languageSelected);
localeChanged(current);
}
void MainWindow::languageSelected(int language)
{
QLocale locale = ui.language->itemData(language).toLocale();
m_speech->setLocale(locale);
}
void MainWindow::voiceSelected(int index)
{
m_speech->setVoice(m_voices.at(index));
}
void MainWindow::localeChanged(const QLocale &locale)
{
QVariant localeVariant(locale);
ui.language->setCurrentIndex(ui.language->findData(localeVariant));
disconnect(ui.voice, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::voiceSelected);
ui.voice->clear();
m_voices = m_speech->availableVoices();
QVoice currentVoice = m_speech->voice();
foreach (const QVoice &voice, m_voices) {
ui.voice->addItem(QString("%1 - %2 - %3").arg(voice.name())
.arg(QVoice::genderName(voice.gender()))
.arg(QVoice::ageName(voice.age())));
if (voice.name() == currentVoice.name())
ui.voice->setCurrentIndex(ui.voice->count() - 1);
}
connect(ui.voice, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::voiceSelected);
}
主函数:
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow win;
win.show();
return app.exec();
}
测试程序完成后, 提示说没有发现音频转换文本引擎的插件, 这个后续研究 。 发现, Qt 5.8 是默认使用静态库编译的,可能需要重新编译插件。
linux 上也可以测试一下, 找到一个开源的转换引擎。
所包含的头文件为:
#include <QTextToSpeech>
在工程文件中加入 :
QT += texttospeech
简单实例:
在工程文件中加入:
TEMPLATE = app
QT += widgets texttospeech
头文件源代码:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtWidgets/qmainwindow.h>
#include "ui_mainwindow.h"
#include <QTextToSpeech>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
public slots:
void speak();
void stop();
void setRate(int);
void setPitch(int);
void setVolume(int volume);
void stateChanged(QTextToSpeech::State state);
void engineSelected(int index);
void languageSelected(int language);
void voiceSelected(int index);
void localeChanged(const QLocale &locale);
private:
Ui::MainWindow ui;
QTextToSpeech *m_speech;
QVector<QVoice> m_voices;
};
#endif
源文件:
#include "mainwindow.h"
#include <QLoggingCategory>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
m_speech(0)
{
ui.setupUi(this);
QLoggingCategory::setFilterRules(QStringLiteral("qt.speech.tts=true \n qt.speech.tts.*=true"));
// Populate engine selection list
ui.engine->addItem("Default", QString("default"));
foreach (QString engine, QTextToSpeech::availableEngines())
ui.engine->addItem(engine, engine);
ui.engine->setCurrentIndex(0);
engineSelected(0);
connect(ui.speakButton, &QPushButton::clicked, this, &MainWindow::speak);
connect(ui.pitch, &QSlider::valueChanged, this, &MainWindow::setPitch);
connect(ui.rate, &QSlider::valueChanged, this, &MainWindow::setRate);
connect(ui.volume, &QSlider::valueChanged, this, &MainWindow::setVolume);
connect(ui.engine, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::engineSelected);
}
void MainWindow::speak()
{
m_speech->say(ui.plainTextEdit->toPlainText());
}
void MainWindow::stop()
{
m_speech->stop();
}
void MainWindow::setRate(int rate)
{
m_speech->setRate(rate / 10.0);
}
void MainWindow::setPitch(int pitch)
{
m_speech->setPitch(pitch / 10.0);
}
void MainWindow::setVolume(int volume)
{
m_speech->setVolume(volume / 100.0);
}
void MainWindow::stateChanged(QTextToSpeech::State state)
{
if (state == QTextToSpeech::Speaking) {
ui.statusbar->showMessage("Speech started...");
} else if (state == QTextToSpeech::Ready)
ui.statusbar->showMessage("Speech stopped...", 2000);
else if (state == QTextToSpeech::Paused)
ui.statusbar->showMessage("Speech paused...");
else
ui.statusbar->showMessage("Speech error!");
ui.pauseButton->setEnabled(state == QTextToSpeech::Speaking);
ui.resumeButton->setEnabled(state == QTextToSpeech::Paused);
ui.stopButton->setEnabled(state == QTextToSpeech::Speaking || QTextToSpeech::Paused);
}
void MainWindow::engineSelected(int index)
{
QString engineName = ui.engine->itemData(index).toString();
delete m_speech;
if (engineName == "default")
m_speech = new QTextToSpeech(this);
else
m_speech = new QTextToSpeech(engineName, this);
disconnect(ui.language, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::languageSelected);
ui.language->clear();
// Populate the languages combobox before connecting its signal.
QVector<QLocale> locales = m_speech->availableLocales();
QLocale current = m_speech->locale();
foreach (const QLocale &locale, locales) {
QString name(QString("%1 (%2)")
.arg(QLocale::languageToString(locale.language()))
.arg(QLocale::countryToString(locale.country())));
QVariant localeVariant(locale);
ui.language->addItem(name, localeVariant);
if (locale.name() == current.name())
current = locale;
}
setRate(ui.rate->value());
setPitch(ui.pitch->value());
setVolume(ui.volume->value());
connect(ui.stopButton, &QPushButton::clicked, m_speech, &QTextToSpeech::stop);
connect(ui.pauseButton, &QPushButton::clicked, m_speech, &QTextToSpeech::pause);
connect(ui.resumeButton, &QPushButton::clicked, m_speech, &QTextToSpeech::resume);
connect(m_speech, &QTextToSpeech::stateChanged, this, &MainWindow::stateChanged);
connect(m_speech, &QTextToSpeech::localeChanged, this, &MainWindow::localeChanged);
connect(ui.language, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::languageSelected);
localeChanged(current);
}
void MainWindow::languageSelected(int language)
{
QLocale locale = ui.language->itemData(language).toLocale();
m_speech->setLocale(locale);
}
void MainWindow::voiceSelected(int index)
{
m_speech->setVoice(m_voices.at(index));
}
void MainWindow::localeChanged(const QLocale &locale)
{
QVariant localeVariant(locale);
ui.language->setCurrentIndex(ui.language->findData(localeVariant));
disconnect(ui.voice, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::voiceSelected);
ui.voice->clear();
m_voices = m_speech->availableVoices();
QVoice currentVoice = m_speech->voice();
foreach (const QVoice &voice, m_voices) {
ui.voice->addItem(QString("%1 - %2 - %3").arg(voice.name())
.arg(QVoice::genderName(voice.gender()))
.arg(QVoice::ageName(voice.age())));
if (voice.name() == currentVoice.name())
ui.voice->setCurrentIndex(ui.voice->count() - 1);
}
connect(ui.voice, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::voiceSelected);
}
主函数:
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow win;
win.show();
return app.exec();
}
测试程序完成后, 提示说没有发现音频转换文本引擎的插件, 这个后续研究 。 发现, Qt 5.8 是默认使用静态库编译的,可能需要重新编译插件。
linux 上也可以测试一下, 找到一个开源的转换引擎。
0 0
- Qt 5.8 音频、文本转换引擎
- QT文本编码转换方法
- QT文本编码转换方法
- QT文本编码转换方法
- qt之文本编码转换工具
- QT:富文本转换成pdf
- TTS5.1语音引擎(中文)实现文本阅读和音频输出为WAV
- 音频转换
- FMOD Ex 音频引擎
- cocos2d-x 音频引擎
- webrtc音频引擎总结
- WebRTC NetEQ 音频引擎
- Qt浅谈之八:富文本转换成pdf
- Qt浅谈之八:富文本转换成pdf
- 文本转成音频,C#
- Qt音频播放器
- QT开发之音频
- Qt 采集音频
- Python 文件操作
- 专业开发程序员下载不同版本的FireFox的开发版本 下载地址
- caffe︱ImageData层、DummyData层作为原始数据导入的应用
- Python 字符串操作
- Python 面向对象编程
- Qt 5.8 音频、文本转换引擎
- 一致性hash-java实现treemap版
- 【Oracle10g】Windows 8.1下如何成功安装Oracle 10g 亲试成功
- poj2155
- AS 自定义主题字体颜色和背景
- JS事件之DOM事件流
- 【STM32】STM32之DRV8834电机驱动(PWM方式)
- 设计模式学习笔记之策略模式
- Xcode项目文件丢失