-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
498 lines (431 loc) · 16.8 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QAction>
#include <QKeySequence>
#include <QScrollArea>
#include <QToolBar>
#include <QToolButton>
#include <QComboBox>
#include <QLabel>
#include <QDateTime>
#include <QDir>
#include <QDirIterator>
#include <QStringList>
#include <QApplication>
#include <QList>
#include <QTimer>
#include <QListWidgetItem>
#include <QMessageBox>
#include <QInputDialog>
#include "soundplayer.h"
#include "Recorder/soundrecorder.h"
#include "Recorder/autosoundrecorder.h"
#include "Recorder/dpsoundrecorder.h"
#include "Recorder/dpsoundrecorder.h"
#include "graphswindow.h"
#include "graphsevalwindow.h"
#include "settingsdialog.h"
#include "drawer.h"
#include "drawerdp.h"
extern "C" {
#include "./SPTK/SPTK.h"
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWindow),
recorder(NULL),
autoRecorder(NULL),
dpRecorder(NULL),
settingsDialog(NULL)
{
qDebug() << "MainWindow" << SETTINGS_FILE << LOG_DATA;
ui->setupUi(this);
this->initUI();
this->updateFileList();
this->settingsDialog = SettingsDialog::getInstance(this);
}
MainWindow::~MainWindow()
{
qDebug() << "~MainWindow" << SETTINGS_FILE << LOG_DATA;
if(this->recorder) this->recorder->deleteLater();
if(this->autoRecorder) this->autoRecorder->deleteLater();
if(this->dpRecorder) this->dpRecorder->deleteLater();
delete ui;
}
void MainWindow::initUI()
{
qDebug() << "" << SETTINGS_FILE << LOG_DATA;
QToolBar * trainingToolBar = addToolBar(tr("Training"));
trainingToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
trainingToolBar->setIconSize(QSize(16,16));
QAction * ratingDpAct = new QAction(tr("Show DP"), this);
connect(ratingDpAct, SIGNAL(triggered()), this, SLOT(evaluationDP()));
trainingToolBar->addAction(ratingDpAct);
// sound actions
QAction * playAct = new QAction(tr("&Play"), this);
playAct->setIcon(QIcon(":/icons/icons/speaker-26.png"));
playAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_P));
playAct->setStatusTip(tr("Play current audio"));
connect(playAct, SIGNAL(triggered()), this, SLOT(playRecord()));
this->recordingAct = new QAction(tr("&Record"), this);
this->recordingAct->setIcon(QIcon(":/icons/icons/record-26.png"));
this->recordingAct->setStatusTip(tr("Start/Stop recording audio"));
connect(this->recordingAct, SIGNAL(triggered()), this, SLOT(manualRecording()));
this->autoRecordingAct = new QAction(tr("&Auto Record"), this);
this->autoRecordingAct->setIcon(QIcon(":/icons/icons/voice_recognition_scan-26.png"));
this->recordingAct->setStatusTip(tr("Start/Stop auto recording audio"));
connect(this->autoRecordingAct, SIGNAL(triggered()), this, SLOT(autoRecording()));
this->dpRecordingAct = new QAction(tr("&DP Record"), this);
this->dpRecordingAct->setIcon(QIcon(":/icons/icons/voice_recognition_scan-26.png"));
this->recordingAct->setStatusTip(tr("Start/Stop auto recording audio"));
connect(this->dpRecordingAct, SIGNAL(triggered()), this, SLOT(dpRecording()));
QToolBar * actionToolBar = addToolBar(tr("Actions"));
actionToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
actionToolBar->setIconSize(QSize(16,16));
actionToolBar->addAction(this->recordingAct);
actionToolBar->addAction(this->autoRecordingAct);
actionToolBar->addAction(this->dpRecordingAct);
actionToolBar->addAction(playAct);
// Files' actions
QAction * removeAct = new QAction(tr("Remove"), this);
removeAct->setIcon(QIcon(":/icons/icons/delete-26.png"));
removeAct->setStatusTip(tr("Remove selected files"));
connect(removeAct, SIGNAL(triggered()), this, SLOT(remove()));
QAction * renameAct = new QAction(tr("Rename"), this);
renameAct->setIcon(QIcon(":/icons/icons/edit-26.png"));
renameAct->setStatusTip(tr("Rename selected file"));
connect(renameAct, SIGNAL(triggered()), this, SLOT(rename()));
QToolBar * fileToolBar = addToolBar(tr("Files Operations"));
fileToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
fileToolBar->setIconSize(QSize(16,16));
fileToolBar->addAction(removeAct);
fileToolBar->addAction(renameAct);
// Programm settings tool bar
QAction * settingsAct = new QAction(tr("Settings"), this);
settingsAct->setIcon(QIcon(":/icons/icons/settings-26.png"));
settingsAct->setStatusTip(tr("Open settings dialog"));
connect(settingsAct, SIGNAL(triggered()), this, SLOT(settingsShow()));
QToolBar * settingsToolBar = addToolBar(tr("Audio Devices"));
settingsToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
settingsToolBar->setIconSize(QSize(16,16));
settingsToolBar->addAction(settingsAct);
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateVolume()));
timer->start(200);
connect( this->ui->filesList, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
this, SLOT(plottingGraph(QListWidgetItem*)) );
}
void MainWindow::autoRecording()
{
oal_device * currentDevice = this->settingsDialog->getInputDevice();
if (!currentDevice) return;
this->raise();
if(this->autoRecorder == NULL)
{
qDebug() << "new SoundRecorder: " << currentDevice->name << LOG_DATA;
this->autoRecorder = new AutoSoundRecorder(currentDevice, sizeof(short int));
qDebug() << "is recording: " << this->autoRecorder->isRecording() << LOG_DATA;
connect(this->autoRecorder, SIGNAL(resultReady(SoundRecorder *)), this, SLOT(recordFinished(SoundRecorder *)));
this->autoRecordingAct->setIconText(tr("&Auto Recording..."));
this->recordingAct->setEnabled(false);
}
this->recording(this->autoRecorder);
}
void MainWindow::dpRecording()
{
oal_device * currentDevice = this->settingsDialog->getInputDevice();
if (!currentDevice) return;
this->raise();
if(this->dpRecorder == NULL)
{
QString path = QApplication::applicationDirPath() + DATA_PATH;
bool ok = true;
QList<QListWidgetItem*> items = ui->filesList->selectedItems();
if(items.size() > 0)
{
path += items.at(0)->text();
} else {
QStringList items;
for(int i=0;i<this->ui->filesList->count();i++)
items.append(this->ui->filesList->item(i)->text());
path += QInputDialog::getItem(this, tr("Pattern"), tr("Record"), items, 0, false, &ok);
}
if (ok && !path.isEmpty()){
qDebug() << "Start DP Recorder for " << path << LOG_DATA;
qDebug() << "new SoundRecorder: " << currentDevice->name << LOG_DATA;
this->dpRecorder = new DPSoundRecorder(path, currentDevice, sizeof(short int), this);
qDebug() << "is recording: " << this->dpRecorder->isRecording() << LOG_DATA;
connect(this->dpRecorder, SIGNAL(resultReady(SoundRecorder *)), this, SLOT(recordFinished(SoundRecorder *)));
this->dpRecordingAct->setIconText(tr("&DP Recording..."));
this->recordingAct->setEnabled(false);
}
}
this->recording(this->dpRecorder);
}
void MainWindow::manualRecording()
{
oal_device * currentDevice = this->settingsDialog->getInputDevice();
if (!currentDevice) return;
this->setFocus();
if(this->recorder == NULL)
{
qDebug() << "new SoundRecorder: " << currentDevice->name << LOG_DATA;
this->recorder = new SoundRecorder(currentDevice, sizeof(short int), this);
qDebug() << "is recording: " << this->recorder->isRecording() << LOG_DATA;
connect(this->recorder, SIGNAL(resultReady(SoundRecorder *)), this, SLOT(recordFinished(SoundRecorder *)));
this->recordingAct->setIconText(tr("&Recording..."));
this->autoRecordingAct->setEnabled(false);
}
this->recording(this->recorder);
}
void MainWindow::recording(SoundRecorder * recorder)
{
if(recorder->isRecording())
{
qDebug() << "stop recording" << LOG_DATA;
recorder->stopRecording();
}else{
qDebug() << "start recording" << LOG_DATA;
recorder->startRecording();
}
}
void MainWindow::recordFinished(SoundRecorder * recorder)
{
qDebug() << "recordFinished" << LOG_DATA;
this->recordingAct->setIconText(tr("&Record"));
this->recordingAct->setEnabled(true);
this->autoRecordingAct->setIconText(tr("&Auto Record"));
this->autoRecordingAct->setEnabled(true);
this->dpRecordingAct->setIconText(tr("&DP Record"));
this->dpRecordingAct->setEnabled(true);
char *data;
int size = recorder->getData((void**) &data);
qDebug() << "result size " << size << LOG_DATA;
QDateTime dateTime = QDateTime::currentDateTime();
QString path = USER_DATA_PATH + dateTime.toString("dd.MM.yyyy hh.mm.ss.zzz");
path = QApplication::applicationDirPath() + DATA_PATH + path + WAVE_TYPE;
qDebug() << "write wave to: " << path << LOG_DATA;
WaveFile *waveFile = makeWaveFileFromRawData(
(char *)data,
size,
NUMBER_OF_CHANNELS,
RECORD_FREQ,
SIGNIFICANT_BITS_PER_SAMPLE,
NULL,
NULL,
NULL,
NULL
);
saveWaveFile(waveFile, path.toLocal8Bit().data());
waveCloseFile(waveFile);
recorder->deleteLater();
this->recorder = NULL;
this->autoRecorder = NULL;
this->dpRecorder = NULL;
MainWindow::cleanRecordFiles();
this->updateFileList();
if(SettingsDialog::getInstance()->getMathGLSettings()->autoOpen)
{
GraphsWindow * graph = this->showGraph(path);
graph->show();
graph->fullFit();
}
}
QStringList scanDirIter(QDir dir)
{
QStringList files;
QString path = dir.absolutePath();
qDebug() << "Search in " << path << LOG_DATA;
QDirIterator iterator(path, QDirIterator::Subdirectories);
while (iterator.hasNext()) {
iterator.next();
if (!iterator.fileInfo().isDir()) {
QString filename = iterator.filePath();
if (filename.endsWith(WAVE_TYPE))
files.append(filename.remove(path));
}
}
return files;
}
void MainWindow::updateFileList()
{
ui->filesList->clear();
QDir directory(QApplication::applicationDirPath() + DATA_PATH);
QStringList files = scanDirIter(directory);
for(int i=0; i<files.size();i++)
{
ui->filesList->addItem(files.at(i));
}
}
void MainWindow::cleanRecordFiles()
{
SPTK_SETTINGS * sptk_settings = SettingsDialog::getSPTKsettings();
QString path = QApplication::applicationDirPath() + DATA_PATH + USER_DATA_PATH;
QDir directory(path);
QStringList files = scanDirIter(directory);
files.sort();
qDebug() << "Files list" << files;
int count = files.size();
int i=sptk_settings->dp->recordingMaxFiles - 1;
for(i; i<count; i++)
{
QFile(path + files.at(i)).remove();
qDebug() << "delete" << files.at(i);
}
}
void MainWindow::compare()
{
QStringList items;
for(int i=0;i<this->ui->filesList->count();i++)
items.append(this->ui->filesList->item(i)->text());
QListWidgetItem* item = ui->filesList->currentItem();
QString path = QApplication::applicationDirPath() + DATA_PATH;
QString path1 = path + item->text();
items.removeAll(item->text());
bool ok;
QString path2 = path + QInputDialog::getItem(this, tr("Compare with"), tr("Record"), items, 0, false, &ok);
if (ok && !path2.isEmpty()){
qDebug() << "Draw graphs for " << path1 << " and " << path2 << LOG_DATA;
QWidget * window = new QWidget();
QVBoxLayout *layout = new QVBoxLayout(window);
GraphsWindow * first = this->showGraph(path1);
GraphsWindow * second = this->showGraph(path2);
connect(first, SIGNAL(changeSig(int)), second, SLOT(setK(int)));
connect(second, SIGNAL(changeSig(int)), first, SLOT(setK(int)));
layout->addWidget(first);
layout->addWidget(second);
window->show();
first->fullFit();
second->fullFit();
}
}
void MainWindow::evaluationDP()
{
evaluation(new DrawerDP());
}
void MainWindow::evaluation(Drawer * drawer)
{
QString path = QApplication::applicationDirPath() + DATA_PATH;
QList<QListWidgetItem*> items = ui->filesList->selectedItems();
if(items.size() > 0)
{
QString file = items.at(0)->text();
GraphsEvalWindow * window = this->evaluation(file, drawer);
if(items.size() > 1)
{
QString file2 = path + items.at(1)->text();
window->drawFile(file2);
}
}
}
GraphsEvalWindow * MainWindow::evaluation(QString filePath, Drawer * drawer)
{
QString path = QApplication::applicationDirPath() + DATA_PATH;
QString file = path + filePath;
qDebug() << "Draw graphs for evaluation " << file << LOG_DATA;
GraphsEvalWindow * window = new GraphsEvalWindow(file, drawer);
connect(window, SIGNAL(autoRec()), this, SLOT(autoRecording()));
connect(window, SIGNAL(rec()), this, SLOT(manualRecording()));
connect(window, SIGNAL(recFinish()), this, SLOT(updateFileList()));
window->show();
window->fullFit();
return window;
}
void MainWindow::plotting()
{
QString path = QApplication::applicationDirPath() + DATA_PATH;
QList<QListWidgetItem*> items = ui->filesList->selectedItems();
if(items.size() > 0)
{
QString file = path + items.at(0)->text();
qDebug() << "Draw graphs for " << path << LOG_DATA;
GraphsWindow * graph = this->showGraph(file);
graph->show();
graph->fullFit();
}
}
void MainWindow::plottingGraph(QListWidgetItem*)
{
this->plotting();
}
GraphsWindow * MainWindow::showGraph(QString path)
{
GraphsWindow * graph = new GraphsWindow(path);
connect(graph, SIGNAL(autoRec()), this, SLOT(autoRecording()));
connect(graph, SIGNAL(rec()), this, SLOT(manualRecording()));
return graph;
}
void MainWindow::updateVolume()
{
if(this->recorder != NULL && this->recorder->isRecording())
{
long int avg = this->recorder->getVolumeLevel();
long int max = this->recorder->getMaxVolumeLevel();
this->ui->volumeBar->setMaximum(max);
this->ui->volumeBar->setValue(avg);
}
}
void MainWindow::remove()
{
int ret = QMessageBox::warning(this, tr("Delete"),
tr("Are you sure you want to delete the selected files?"),
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
QMessageBox::No);
if(ret == QMessageBox::Yes)
{
qDebug() << "removing files" << LOG_DATA;
QString path = QApplication::applicationDirPath() + DATA_PATH;
QList<QListWidgetItem*> items = ui->filesList->selectedItems();
for(int i=0; i<items.size(); i++)
{
qDebug() << "delete file " << items.at(i)->text() << LOG_DATA;
QString file = path + items.at(i)->text();
QFile(file).remove();
}
this->updateFileList();
}
}
void MainWindow::rename()
{
QList<QListWidgetItem*> items = ui->filesList->selectedItems();
for(int i=0; i<items.size(); i++)
{
QString orig_name, name = orig_name = items.at(i)->text();
QString old_name = name.remove(name.lastIndexOf('.'), name.length());
bool ok;
QString new_name = QInputDialog::getText(this, tr("Rename"),
tr("New file name:"),
QLineEdit::Normal,
old_name, &ok);
if (ok && !new_name.isEmpty()){
QString path = QApplication::applicationDirPath() + DATA_PATH;
QFile(path + orig_name).rename(QDir(path).absoluteFilePath(new_name + WAVE_TYPE));
}
}
this->updateFileList();
}
void MainWindow::settingsShow()
{
if(this->settingsDialog)
{
this->settingsDialog->loadSettings();
this->settingsDialog->show();
}
}
void MainWindow::playRecord()
{
QList<QListWidgetItem*> items = ui->filesList->selectedItems();
for(int i=0; i<items.size(); i++)
{
qDebug() << "play file " << items.at(i)->text() << LOG_DATA;
this->playRecord(items.at(i)->text());
}
}
void MainWindow::playRecord(QString filePath)
{
QString path = QApplication::applicationDirPath() + DATA_PATH;
QString file = path +filePath;
SoundPlayer * player = new SoundPlayer(file);
player->start();
}