-
Notifications
You must be signed in to change notification settings - Fork 20
/
batchdenoisedlg.cpp
205 lines (135 loc) · 4.31 KB
/
batchdenoisedlg.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
#include "batchdenoisedlg.h"
#include "ui_batchdenoisedlg.h"
#include <QFileDialog>
#include <QDir>
#include <QDirIterator>
#include "mainwindow.h"
#define ManWi ((MainWindow*)pMainWindow)
BatchDenoiseDlg::BatchDenoiseDlg(QWidget *parent) :
QDialog(parent),
ui(new Ui::BatchDenoiseDlg)
{
ui->setupUi(this);
ProcessedFiles = 0;
CurrentIndex = 0;
Failures = 0;
}
// can't define in header because InferDetails belongs to mainwindow.h and including it in this dlg's .h would case circular dependency error
InferDetails MakeInferDetails(const std::vector<float>& InAudat,const QString& FilePath,unsigned InSampleRate,int32_t OutSampleRate)
{
InferDetails Dets;
Dets.F0 = 0.0f;
Dets.Speed = 0.0f;
Dets.Energy = 0.0f;
Dets.pItem = nullptr; // the mainwindow's function will make an item for us.
Dets.Prompt = "";
Dets.SpeakerID = OutSampleRate; // SpeakerID will double as resample when a denoise only job is requested.
Dets.EmotionID = -1;
Dets.Denoise = true;
Dets.Amplification = 1.f;
Dets.ExportFileName = FilePath;
Dets.VoiceName = "";
Dets.ForcedAudio = InAudat;
Dets.SampleRate = InSampleRate;
return Dets;
}
BatchDenoiseDlg::~BatchDenoiseDlg()
{
delete ui;
}
void BatchDenoiseDlg::IterateDo()
{
if (ProcessedFiles == Files.size() && ManWi->GetCountItems() == 0)
{
// It's done!
delete timIter;
SetControls(true);
return;
}
if (ManWi->GetCountItems() != 0)
return;
ManWi->DenDone = 0;
if (CurrentIndex + ui->spbBatchSz->value() > Files.size())
ManWi->DenBatchSize = Files.size() - CurrentIndex;
for (int32_t i = 0;i < ui->spbBatchSz->value();i++)
{
QString CurrentFn = Files[CurrentIndex];
AudioFile<float> AudFile;
InferDetails CurrentDets;
try {
AudFile.load(CurrentFn.toStdString());
CurrentDets = MakeInferDetails(AudFile.samples[0],CurrentFn,AudFile.getSampleRate(),ui->spbOutSR->value());
} catch (...) {
CurrentIndex += 1; // NOT i !!!!!!!
ProcessedFiles += 1;
++Failures;
if (CurrentIndex > Files.size() - 1)
break;
continue;
}
ManWi->PushToInfers(CurrentDets);
CurrentIndex += 1; // NOT i !!!!!!!
ProcessedFiles += 1;
if (CurrentIndex > Files.size() - 1)
break;
}
SetLabel();
}
void BatchDenoiseDlg::on_btnFindFolder_clicked()
{
QString Dir = QFileDialog::getExistingDirectory(this, tr("Find base folder of your WAVs"),
"",
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
ui->edtFolPath->setText(Dir);
UpdateDirectory();
}
void BatchDenoiseDlg::on_edtFolPath_editingFinished()
{
UpdateDirectory();
}
void BatchDenoiseDlg::SetLabel()
{
ui->lblFiles->setText(QString(QString::number(ProcessedFiles) + " / " + QString::number(Files.size()) + " files, " + QString::number(Failures) + " failures.") );
ui->pgFiles->setValue(ProcessedFiles);
ui->pgFiles->update();
}
void BatchDenoiseDlg::UpdateDirectory()
{
if (ui->edtFolPath->text().isEmpty())
return;
if (Files.size())
Files.clear();
QDirIterator DirIt(ui->edtFolPath->text(),QDirIterator::Subdirectories);
while (DirIt.hasNext())
{
DirIt.next();
if (QFileInfo(DirIt.filePath()).isFile() && QFileInfo(DirIt.filePath()).suffix() == "wav")
Files.push_back(DirIt.filePath());
}
CurrentIndex = 0;
ProcessedFiles = 0;
Failures = 0;
ui->pgFiles->setRange(0,Files.size());
SetLabel();
}
void BatchDenoiseDlg::on_btnStart_clicked()
{
CurrentIndex = 0;
ProcessedFiles = 0;
Failures = 0;
ManWi->DenBatchSize = ui->spbBatchSz->value();
timIter = new QTimer(this);
timIter->setSingleShot(false);
timIter->setInterval(1000);
connect(timIter,&QTimer::timeout,this,&BatchDenoiseDlg::IterateDo);
timIter->start();
SetControls(false);
}
void BatchDenoiseDlg::SetControls(bool En)
{
ui->edtFolPath->setEnabled(En);
ui->spbBatchSz->setEnabled(En);
ui->btnStart->setEnabled(En);
ui->btnFindFolder->setEnabled(En);
}