-
Notifications
You must be signed in to change notification settings - Fork 4
/
mainwindow.cpp
126 lines (101 loc) · 2.78 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "mainwindow.h"
//#include "audio.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QDebug>
#include <QFile>
#include <QMessageBox>
#include <QDataStream>
#include <QRadioButton>
#include <QCheckBox>
#include <QLabel>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
// ui(new Ui::MainWindow)
{
// ui->setupUi(this);
setupUI();
audio.initAudio();
pcm = new QByteArray();
connect(&audio, SIGNAL(collectData(QByteArray)), this, SLOT(appendText(QByteArray)));
recording = false;
timer = new QTimer(this);
connect(btnPlay, SIGNAL(clicked(bool)), this, SLOT(playOrPause()));
// connect(timer, SIGNAL(timeout()), &audio, SLOT(record()));
// connect(btnPlay, SIGNAL(pressed()), this, SLOT(startTimer()));
// connect(btnPlay, SIGNAL(released()), this, SLOT(stopTimer()));
connect(btnSave, SIGNAL(clicked(bool)), this, SLOT(savePCM()));
}
MainWindow::~MainWindow()
{
// delete win;
audio.uninitAudio();
}
void MainWindow::appendText(QByteArray data)
{
txtEdit->append(data.mid(0, 20));
pcm->append(data);
}
void MainWindow::playOrPause()
{
if (recording) {
audio.stopRecord();
} else{
audio.startRecord();
}
recording = !recording;
}
void MainWindow::startTimer()
{
timer->start(500);
}
void MainWindow::stopTimer()
{
timer->stop();
}
void MainWindow::savePCM()
{
QFile f("D:/a.pcm");
if (!f.open(QIODevice::WriteOnly))
{
QMessageBox::warning(this,tr("警告"),tr("打开文件失败"));
return;
}
QDataStream out(&f);
out.setVersion(QDataStream::Qt_5_6);
out << *pcm;
qDebug() << "pcm size:" << pcm->length();
// f.(pcm->data(), pcm->length());
f.close();
}
void MainWindow::setupUI()
{
QWidget *win = new QWidget();
txtEdit = new QTextEdit();
btnPlay = new QPushButton("Capture");
btnSave = new QPushButton("Save");
QVBoxLayout *lay_main = new QVBoxLayout(win);
lay_main->addWidget(btnPlay);
lay_main->addWidget(txtEdit);
lay_main->addStretch();
QHBoxLayout *radio_lay = new QHBoxLayout;
QCheckBox *radio_one = new QCheckBox("one");
QCheckBox *radio_two = new QCheckBox("two");
radio_lay->addWidget(radio_one);
radio_lay->addWidget(radio_two);
lay_main->addLayout(radio_lay);
lay_main->addWidget(btnSave);
setCentralWidget(win);
this->setWindowTitle("Audio Capture");
this->setFixedSize(300,600);
QString qss;
QFile qssFile(":/myQss.qss");
qssFile.open(QFile::ReadOnly);
if(qssFile.isOpen())
{
qss = QLatin1String(qssFile.readAll());
this->setStyleSheet(qss);
qssFile.close();
}
// setStyleSheet("");
}