-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstenographer.cpp
216 lines (184 loc) · 6.12 KB
/
stenographer.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "stenographer.h"
#include "wavpcmfile.h"
#include "recthread.h"
#include "googlespeech.h"
#include "downloadandplay.h"
#include <QFile>
#include <QDir>
#include <QTextStream>
#include <QUrl>
#include <QTranslator>
Stenographer::Stenographer(QObject *parent) :
QObject(parent)
{
this->lastWavFile = "";
this->m_file = NULL;
m_autoMod = true;
m_isActive = false;
m_saveTmpFiles = false;
lang = "ru-ru";
this->downloadAndPlay = new DownloadAndPlay();
this->googleSpeech = new GoogleSpeech();
QObject::connect(googleSpeech,SIGNAL(getText(QString)),this,SLOT(addRecText(QString)));
QObject::connect(googleSpeech,SIGNAL(logging(QString)),this,SLOT(logging(QString)));
//QAudioFormat format;
format.setFrequency(16000);
format.setChannels(1);
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);
recThread = new RecThread(format);
recThread->isNeedToSavaTmpFiles(m_saveTmpFiles);
connect(recThread, SIGNAL(logging(QString)), this, SLOT(logging(QString)));
connect(recThread, SIGNAL(setVolumeRange(QVariant)), this, SLOT(getVolumeRange(QVariant)));
connect(recThread, SIGNAL(setVolumeValue(QVariant)), this, SLOT(getVolumeValue(QVariant)));
connect(recThread, SIGNAL(needToStop()), this, SLOT(stopRec()));
connect(recThread, SIGNAL(testStopped(int)), this, SLOT(_testStopped(int)));
recThread->switchAutoMod(m_autoMod);
outputAudio = new QAudioOutput(format, this);
QObject::connect(outputAudio,SIGNAL(stateChanged(QAudio::State)),this,SLOT(closeFile()));
}
void Stenographer::getOptions()
{
emit setAudioDevices(QVariant(recThread->getDevices()));
}
void Stenographer::changeAudioDevice(QVariant val){
int iDev = val.toInt();
this->recThread->changeAudioDevice(iDev);
}
void Stenographer::getVolumeRange(QVariant data)
{ emit setVolumeRange(data); }
void Stenographer::getVolumeValue(QVariant data)
{ emit setVolumeValue(data); }
void Stenographer::stopRec()
{
this->lastWavFile = this->recThread->getWavFile();
if(!m_autoMod)
emit needToStop();
else{
if(!this->m_isActive) return;
this->convertAndSent();
this->beginStopRec();
this->m_isActive = true;
}
}
void Stenographer::beginStopRec()
{
if(recThread->isRunning())
{
emit recThread->stopRecording();
this->lastWavFile = this->recThread->getWavFile();
emit logging("<Stenographer> Recorded file name: " + this->lastWavFile);
while(recThread->isRunning()){;}
}
if(!this->m_isActive || this->m_autoMod)
{
recThread->run();
}
this->m_isActive = !this->m_isActive;
}
void Stenographer::playLast(QVariant text)
{
QString args("q="+text.toString().trimmed().replace(" ","+"));
QUrl url("http://translate.google.com/translate_tts?"+args);
emit logging("<DownloadAndPlay> " + args);
this->downloadAndPlay->doDownload(url);
}
void Stenographer::closeFile()
{
if(this->outputAudio->state() == QAudio::StoppedState){
outputAudio->stop();
m_file->close();
}
//delete m_file;
}
void Stenographer::convertAndSent()
{
emit logging("<Stenographer> Begin convert");
QFile key_file("key");
QString key = "AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw";
if(key_file.open(QIODevice::ReadOnly)) {
QTextStream in(&key_file);
key = in.readLine();
}
googleSpeech->sent(lastWavFile, lang, key);
}
void Stenographer::setNewLang(QVariant lang)
{
this->lang = lang.toString();
}
void Stenographer::setSaveTempFiles(QVariant _is)
{
m_saveTmpFiles = _is.toBool();
recThread->isNeedToSavaTmpFiles(m_saveTmpFiles);
}
void Stenographer::addRecText(QString data)
{
if(m_saveTmpFiles){
QFile file(this->lastWavFile.replace(".wav",".txt"));
if (file.open(QIODevice::WriteOnly | QIODevice::Text)){
file.write(data.toUtf8());
file.close();
}
}
emit updateData(QVariant(QObject::tr(data.toStdString().c_str())));
}
void Stenographer::saveText(QVariant data, QVariant name)
{
QString text = data.toString();
QString f_name = name.toString();
QFile file(f_name);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)){
file.write(text.toUtf8());
file.close();
}
}
void Stenographer::logging(QString str)
{
emit updateLog(QVariant(QObject::tr(str.toStdString().c_str())));
}
void Stenographer::startTest()
{
this->recThread->startTest();
}
void Stenographer::_testStopped(int result)
{
emit testStopped(QVariant(result));
}
void Stenographer::switchAutoMod()
{
m_autoMod = !m_autoMod;
emit logging(QString("<Stenographer> Auto mod: ") + (m_autoMod?"on":"off"));
this->recThread->switchAutoMod(m_autoMod);
}
void Stenographer::setOverK(QVariant val)
{
emit logging(QString("<Stenographer> Factor is: ") + QString::number(val.toDouble()));
this->recThread->setOverK(val.toDouble());
}
void Stenographer::setIT1(QVariant val)
{
emit logging(QString("<Stenographer> First integration time is: ") + QString::number(val.toInt()));
this->recThread->setIT1(val.toInt());
}
void Stenographer::setITstep1(QVariant val)
{
emit logging(QString("<Stenographer> First integration step is: ") + QString::number(val.toInt()));
this->recThread->setITstep1(val.toInt());
}
void Stenographer::setIT2(QVariant val)
{
emit logging(QString("<Stenographer> Second integration time is: ") + QString::number(val.toInt()));
this->recThread->setIT2(val.toInt());
}
void Stenographer::setITstep2(QVariant val)
{
emit logging(QString("<Stenographer> Second integration step is: ") + QString::number(val.toInt()));
this->recThread->setITstep2(val.toInt());
}
void Stenographer::setDelay(QVariant val)
{
emit logging(QString("<Stenographer> Delay is: ") + QString::number(val.toInt()));
this->recThread->setDelay(val.toInt());
}