-
Notifications
You must be signed in to change notification settings - Fork 0
/
recthread.cpp
executable file
·181 lines (153 loc) · 4.19 KB
/
recthread.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "recthread.h"
#include "wavpcmfile.h"
#include "audioinfo.h"
#include <QAudioOutput>
#include <QAudioFormat>
#include <QTextStream>
#include <QDate>
#include <QTime>
#include <QDir>
#include <QTimer>
RecThread::RecThread(QAudioFormat format) :
QObject(0)
, isTesting(false)
, m_file(NULL)
{
isRun = false;
this->m_format = format;
this->m_device = QAudioDeviceInfo::defaultInputDevice();
this->devices = QAudioDeviceInfo::availableDevices(QAudio::AudioInput);
this->inputAudio = new QAudioInput(m_device, m_format, this);
this->m_audioInfo = new AudioInfo(m_format, this);
connect(m_audioInfo, SIGNAL(update()), SLOT(refreshVolumeInfo()));
connect(m_audioInfo, SIGNAL(needToStop()), SLOT(stopRecording()));
}
QList<QString> RecThread::getDevices()
{
QList<QString> * dev = new QList<QString>();
foreach (const QAudioDeviceInfo &deviceInfo, this->devices)
dev->append(deviceInfo.deviceName());
return *dev;
}
void RecThread::changeAudioDevice(int iDev)
{
this->m_device = this->devices.at(iDev);
delete this->inputAudio;
this->inputAudio = new QAudioInput(m_device, m_format, this);
}
RecThread::~RecThread()
{
delete this->inputAudio;
delete this->m_audioInfo;
delete this->m_file;
}
void RecThread::run()
{
emit logging("<RecThread> Starting...");
emit setVolumeRange(QVariant(m_audioInfo->getMaxAmplitude()));
isRun = true;
// inputAudio = new QAudioInput(m_format, this);
inputAudio->reset();
QDir(QDir::currentPath()).mkdir("wav");
QString fname = "wav"+QString(QDir::separator())+"record_"
+ QDate::currentDate().toString("dd_MM_yy")
+ "_"
+ QTime::currentTime().toString("hh_mm_ss_zzz")
+ ".wav";
m_audioInfo->setTmpFileName(fname);
m_file = new WavPcmFile(fname, m_format, this);
if(m_file->open()) {
emit logging("<RecThread> Start recoding");
inputAudio->start(m_audioInfo->start(m_file));
} else {
emit logging("<RecThread> Error open file");
emit logging("<RecThread> Error info: " + m_file->errorString());
}
checkRecording();
}
void RecThread::stopRecording(){
isRun = false;
emit logging("<RecThread> Stoping...");
}
void RecThread::checkRecording()
{
if(isRun){
QTimer::singleShot(1, this, SLOT(checkRecording()));
return;
}
emit logging("<RecThread> Stop recoding");
// Stops the recording
inputAudio->stop();
m_audioInfo->stop();
m_file->close();
emit needToStop();
}
void RecThread::refreshVolumeInfo()
{
qreal value = m_audioInfo->level();
//emit logging("Amplitude: "+QString::number(value));
emit setVolumeValue(QVariant(value));
}
void RecThread::startTest()
{
QTextStream out(stdout);
out << "Start test\n";
isTesting = true;
inputAudio->start(m_audioInfo->start());
QTimer::singleShot(1000,this, SLOT(stopTest()));
}
void RecThread::stopTest()
{
QTextStream out(stdout);
out << "Stop test\n";
isTesting = false;
inputAudio->stop();
m_audioInfo->stop();
int result = m_audioInfo->calculateNoiseLevel();
emit testStopped(result);
}
void RecThread::isNeedToSavaTmpFiles(bool is)
{
m_audioInfo->isNeedSaveTmpFiles(is);
}
void RecThread::switchAutoMod(bool val)
{
this->m_audioInfo->switchAutoMod(val);
}
QString RecThread::getWavFile()
{
if ( m_file != NULL)
return m_file->fileName();
return "";
}
void RecThread::setOverK(double val)
{
QTextStream out(stdout);
out << val << "\n";
this->m_audioInfo->setOverK(val);
}
void RecThread::setIT1(int val){
QTextStream out(stdout);
out << val << "\n";
this->m_audioInfo->setIT1(val);
}
void RecThread::setITstep1(int val){
QTextStream out(stdout);
out << val << "\n";
this->m_audioInfo->setITstep1(val);
}
void RecThread::setIT2(int val){
QTextStream out(stdout);
out << val << "\n";
this->m_audioInfo->setIT2(val);
}
void RecThread::setITstep2(int val){
QTextStream out(stdout);
out << val << "\n";
this->m_audioInfo->setITstep2(val);
}
void RecThread::setDelay(int val){
QTextStream out(stdout);
out << val << "\n";
this->m_audioInfo->setDelay(val);
}