没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
Graph.cpp
#include "graph.h"
Graph::Graph(QWidget *parent) : QWidget(parent) //类的构造
{
graph_space = 0; //相关数据的初始化
for (int i = 0; i < 450; ++i) {
graph_data[i] = 0;
}
}
void Graph::startTime(double timer_t) //计时器的开始
{
timerID = startTimer(timer_t);
}
void Graph::stopTime() //计时器的结束
{
killTimer(timerID);
}
void Graph::paintEvent(QPaintEvent *) //用于进行图像的重绘
{
QPainter painter(this); //新建画笔
painter.setPen(Qt::gray); //画笔颜色-表示边框颜色
painter.setBrush(Qt::white); //花刷颜色-表示内部颜色
painter.drawRect(0, 0, 465, 405);
for (int i = 1; i < 4; ++i) { //绘制横向坐标线
painter.drawLine(0, i * 101, 465, i * 101);
}
for (int i = 1; i < 5; ++i) { //绘制纵向坐标线
painter.drawLine(i * 93, 0, i * 93, 405);
}
painter.setPen(QColor(30, 160, 200)); //设置画笔颜色
int old_y = 0, new_y;
for (int i = 0; i < 450; ++i) {
new_y = (405 / 2) * (1 - graph_data[(graph_space + i) % 450]); //计算当前数值在位图中的像素位
置
painter.drawLine(i - 1, old_y, i, new_y); //线的绘制是从前点至后点
old_y = new_y;
}
}
void Graph::timerEvent(QTimerEvent *) //用于触发计时器的操作
{
graph_data[graph_space] = paragraph_voice / qPow(2, SIZE); //取出数据存放到位图中
graph_space++; //位标后移
update(); //更新位图
}
Graph.h
#ifndef GRAPH_H
#define GRAPH_H
#include "voice.h"
#include <QWidget>
#include <qpainter.h> //涉及到图像的更新操作
#include <QtCore/qmath.h> //用于支持 qpow
class Graph : public QWidget //本类用于描述波形的变化-显示类
{
Q_OBJECT
public:
explicit Graph(QWidget *parent = 0);
void startTime(double timer_t); //计时器的开始
void stopTime(); //计时器的结束
signals:
public slots :
private:
float graph_data[450]; //描述显示图中的数据的内容(采用队列的方式减少内存消耗)
int graph_space; //描述当前输出的位于 graph_data 中的位置
int timerID; //计时器的 ID,用于结束时使用
void paintEvent(QPaintEvent*); //用于进行图像的重绘
void timerEvent(QTimerEvent*); //用于触发计时器的操作
};
Main.cpp
#endif // GRAPH_H
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#define BUFFER_SIZE 2048
#define HINTS_SIZE 100
#define GRAMID_LEN 128
#define FRAME_LEN 640
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton_conc, SIGNAL(clicked()), this, SLOT(ClickButton_Conc())); //UI 函数绑定
connect(ui->pushButton_start, SIGNAL(clicked()), this, SLOT(ClickButton_Start())); //UI 函数绑定
connect(ui->pushButton_stop, SIGNAL(clicked()), this, SLOT(ClickButton_Stop())); //UI 函数绑定
connect(ui->pushButton_play, SIGNAL(clicked()), this, SLOT(ClickButton_Play())); //UI 函数绑定
connect(ui->pushButton_reco, SIGNAL(clicked()), this, SLOT(ClickButton_Reco())); //UI 函数绑定
control_state = false;
paragraph_voice = 0;
drive_ready = false;
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8")); //使得本软件支持中文
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8")); //使得本软件支持中文
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8")); //使得本软件支持中文
}
MainWindow::~MainWindow()
{
::close(voice_fp);
::close(play_fp);
delete ui;
}
void MainWindow::paintEvent(QPaintEvent *)
{
}
bool MainWindow::connectDrive() //连接设备
{
int status;
if (OpenDrive(&voice_fp, &play_fp) == false) {
QMessageBox::critical(this, tr("Error"), tr("File open failed !"), QMessageBox::Yes);
return false;
}
if ((status = SetDrive(&voice_fp, &play_fp)) == -1) {
QMessageBox::critical(this, tr("Error"), tr("File set failed !"), QMessageBox::Yes);
return false;
}
else if (status == 0) {
QMessageBox::StandardButton rebutton =
QMessageBox::warning(this, tr("Warning"), tr("Some set defferent !"), QMessageBox::Yes |
QMessageBox::Ignore);
if (rebutton == QMessageBox::Yes) {
return false;
}
}
readThread.setAudio(voice_fp);
playThread.setAudio(play_fp);
return true;
}
void MainWindow::createVoice() //创建语音
{
int dataLen = speech_voice.size() * BUFSIZE * sizeof(ElemType);
Wav_file default_wavfile = {
{ 'R', 'I', 'F', 'F' },
dataLen + sizeof(Wav_file) - 8,
{'W', 'A', 'V', 'E'},
{'f', 'm', 't', ' '},
16,
1,
CHANNELS,
RATE,
RATE * SIZE / 8,
SIZE / 8,
SIZE,
{'d', 'a', 't', 'a'},
dataLen
};
FILE* fp = NULL;
if ((fp = fopen("wav/upload.wav", "wb")) == NULL) {
QMessageBox::critical(this, tr("Error"), tr("File open failed !"), QMessageBox::Yes);
return;
}
fwrite(&default_wavfile, sizeof(Wav_file), 1, fp);
int temp = dataLen + sizeof(Wav_file) - 8;
fseek(fp, 4, 0);
fwrite(&temp, sizeof(int), 1, fp);
fseek(fp, 40, 0);
fwrite(&dataLen, sizeof(int), 1, fp);
for (int i = 0; i < speech_voice.size(); ++i) {
fwrite(speech_voice[i], BUFSIZE * sizeof(ElemType), 1, fp);
}
fclose(fp);
}
void MainWindow::ClickButton_Conc() //连接硬件按钮响应函数
{
if (connectDrive()) {
ui->pushButton_conc->setEnabled(false); //若连接完设备则按钮无用
drive_ready = true;
}
else {
drive_ready = false;
}
}
void MainWindow::ClickButton_Start() //开始录音按钮响应函数
{
if (!drive_ready) {
QMessageBox::warning(this, tr("Warning"), tr("You should connect the audio !"), QMessageBox::Yes);
return;
}
if (control_state == true) {
return;
}
ui->graph->startTime(GRAPH_SPEED); //图像开始刷新
readThread.start(); //开始录音
control_state = true;
}
void MainWindow::ClickButton_Stop() //停止录音按钮响应函数
{
if (!drive_ready) {
QMessageBox::warning(this, tr("Warning"), tr("You should connect the audio !"), QMessageBox::Yes);
return;
}
if (control_state == false) {
return;
}
readThread.stop(); //录音结束
ui->graph->stopTime(); //图像停止刷新
control_state = false;
createVoice();
}
void MainWindow::ClickButton_Play() //播放录音按钮响应函数
{
if (!drive_ready) {
QMessageBox::warning(this, tr("Warning"), tr("You should connect the audio !"), QMessageBox::Yes);
return;
}
playThread.start();
}
void MainWindow::ClickButton_Reco() //识别录音按钮响应函数
{
if (!drive_ready) {
QMessageBox::warning(this, tr("Warning"), tr("You should connect the audio !"), QMessageBox::Yes);
return;
}
bool reco_f = true;
char* result = NULL;
if (recognitiononlineThread.isLogin == false) {
if ((reco_f = recognitiononlineThread.doLogin(NULL, NULL)) == false) {
while (!recognitiononlineThread.errorInfo.empty()) {
QMessageBox::critical(this, tr("Error"), recognitiononlineThread.errorInfo.pop(),
QMessageBox::Yes);
}
return;
}
}
if ((reco_f = recognitiononlineThread.uploadData("prammar.abnf", "dtt = abnf, sub = asr")) == false) {
while (!recognitiononlineThread.errorInfo.empty()) {
QMessageBox::critical(this, tr("Error"), recognitiononlineThread.errorInfo.pop(), QMessageBox::Yes);
}
return;
}
else {
ui->textBrowser_gram->clear();
ui->textBrowser_gram->append(recognitiononlineThread.getGrammarId());
}
result = recognitiononlineThread.recognition("wav/upload.wav", "sub = asr, result_type = plain, result_encoding
= utf8");
if (result == NULL) {
while (!recognitiononlineThread.errorInfo.empty()) {
QMessageBox::critical(this, tr("Error"), recognitiononlineThread.errorInfo.pop(), QMessageBox::Yes);
剩余48页未读,继续阅读
daidaiyijiu
- 粉丝: 13
- 资源: 322
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0