-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparameters.cpp
439 lines (376 loc) · 8.85 KB
/
parameters.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
/*
* Copyright (C) 2019 - 2023 Judd Niemann - All Rights Reserved.
* You may use, distribute and modify this code under the
* terms of the GNU Lesser General Public License, version 2.1
*
* You should have received a copy of GNU Lesser General Public License v2.1
* with this file. If not, please refer to: https://github.com/jniemann66/ReSampler
*/
#include "parameters.h"
#include "window.h"
#ifdef FS_AVAILABLE
#include "directory.h"
#endif
#ifdef SNDSPEC_VERSION
#define STRINGIFY_(s) #s
#define STRINGIFY(s) STRINGIFY_(s)
#define VERSION_STRING STRINGIFY(SNDSPEC_VERSION)
#endif
#include <iostream>
#include <cmath>
#include <sstream>
#include <iterator>
#include <regex>
namespace Sndspec {
constexpr int minImgWidth = 160;
constexpr int minImgHeight = 160;
std::vector<std::string> Parameters::getInputFiles() const
{
return inputFiles;
}
void Parameters::setInputFiles(const std::vector<std::string> &value)
{
inputFiles = value;
}
std::string Parameters::getOutputPath() const
{
return outputPath;
}
void Parameters::setOutputPath(const std::string &value)
{
outputPath = value;
}
int Parameters::getImgWidth() const
{
return imgWidth;
}
void Parameters::setImgWidth(int value)
{
imgWidth = value;
}
int Parameters::getImgHeight() const
{
return imgHeight;
}
void Parameters::setIngHeight(int value)
{
imgHeight = value;
}
double Parameters::getDynRange() const
{
return dynRange;
}
void Parameters::setDynRange(double value)
{
dynRange = value;
}
std::string Parameters::fromArgs(const std::vector<std::string> &args)
{
auto argsIt = args.cbegin();
while(argsIt != args.cend()) {
OptionID optionID = OptionID::Filenames; // unrecognized options to be treated as filenames
// option search
for(const auto& option : options) {
if((*argsIt == option.longOption) || (!option.shortOption.empty() && (*argsIt == option.shortOption))) {
optionID = option.optionID;
break;
}
}
// process option
switch(optionID) {
case Filenames:
// keep reading filenames until end reached, or another option detected
do {
inputFiles.push_back(*argsIt);
argsIt++;
} while (argsIt != args.cend() && argsIt->compare(0, 1, "-") != 0);
break;
case DynRange:
if(++argsIt != args.cend()) {
dynRange = std::abs(std::stod(*argsIt));
++argsIt;
}
break;
case OutputDir:
if(++argsIt != args.cend()) {
outputPath = *argsIt;
++argsIt;
}
break;
case Height:
if(++argsIt != args.cend()) {
imgHeight = std::max(minImgHeight, std::stoi(*argsIt));
++argsIt;
}
break;
case Width:
if(++argsIt != args.cend()) {
imgWidth = std::max(minImgWidth, std::stoi(*argsIt));
++argsIt;
}
break;
case TimeRange:
if(++argsIt != args.cend()) {
timeRange = true;
start = std::max(0.0, std::stod(*argsIt));
if(++argsIt != args.cend()) {
finish = std::max(0.0, std::stod(*argsIt));
}
++argsIt;
}
break;
case WhiteBackground:
whiteBackground = true;
++argsIt;
break;
case WindowFunction:
if(++argsIt != args.cend()) {
showWindowFunctionLabel = true;
auto wp = Window<double>::findWindow(*argsIt);
if(wp.windowType == Unknown) {
windowFunction = "kaiser";
windowFunctionDisplayName = "Kaiser";
} else {
windowFunction = wp.name;
windowFunctionDisplayName = wp.displayName;
}
++argsIt;
}
break;
case ShowWindows:
showWindows = true;
++argsIt;
return showWindowList();
case SpectrumMode:
spectrumMode = true;
++argsIt;
break;
case Smoothing:
if(++argsIt != args.cend()) {
std::string s{*argsIt};
// convert name to lowercase
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) -> unsigned char {
return std::tolower(c);
});
if(s.compare(0, 6, "moving") == 0) {
spectrumSmoothingMode = MovingAverage;
} else if(s.compare(0, 4, "peak") == 0) {
spectrumSmoothingMode = Peak;
} else if(s.compare(0, 4, "none") == 0 ) {
spectrumSmoothingMode = None;
}
++argsIt;
break;
}
break;
case Channel:
{
std::vector<std::string> channelArgs;
while (++argsIt != args.cend() && argsIt->compare(0, 1, "-") != 0) {
channelArgs.push_back(*argsIt);
}
processChannelArgs(channelArgs);
break;
}
case LinearMag:
linearMag = true;
dynRange = 100.0;
++argsIt;
break;
#ifdef FS_AVAILABLE
case Recursive:
recursiveDirectoryTraversal = true;
++argsIt;
break;
#endif
#ifdef SNDSPEC_VERSION
case Version:
++argsIt;
return std::string{VERSION_STRING};
#endif
case Help:
++argsIt;
return showHelp();
}
}
#ifdef FS_AVAILABLE
std::vector<std::string> expandedFileList;
for(const auto& path : inputFiles) {
auto list = expand(path, fileTypes, recursiveDirectoryTraversal);
expandedFileList.insert(expandedFileList.end(), list.begin(), list.end());
}
inputFiles = expandedFileList;
#endif
return {};
}
std::string Parameters::showHelp()
{
static const int col0width = 50;
std::string helpString{"Usage: sndspec filename [filename2 ...] [options]\n\n"};
for(const auto& option : options) {
std::string line;
if(!option.shortOption.empty()) {
line.append(option.shortOption);
line.append(", ");
}
line.append(option.longOption).append(" ");
for(const auto& arg : option.args) {
line.append("<").append(arg).append("> ");
}
line.append(std::max(0, col0width - static_cast<int>(line.length())), ' ');
line.append(option.description).append("\n");
helpString.append(line);
}
return helpString;
}
std::string Parameters::showWindowList()
{
const auto names = Sndspec::getWindowNames();
const char* const delim = "\n";
std::ostringstream oss;
std::copy(names.begin(), names.end(), std::ostream_iterator<std::string>(oss, delim));
return oss.str();
}
double Parameters::getStart() const
{
return start;
}
double Parameters::getFinish() const
{
return finish;
}
bool Parameters::hasTimeRange() const
{
return timeRange;
}
void Parameters::setHasTimeRange(bool value)
{
timeRange = value;
}
bool Parameters::hasWhiteBackground() const
{
return whiteBackground;
}
std::string Parameters::getWindowFunction() const
{
return windowFunction;
}
bool Parameters::getShowWindows() const
{
return showWindows;
}
std::string Parameters::getWindowFunctionDisplayName() const
{
return windowFunctionDisplayName;
}
void Parameters::setWindowFunctionDisplayName(const std::string &value)
{
windowFunctionDisplayName = value;
}
bool Parameters::getShowWindowFunctionLabel() const
{
return showWindowFunctionLabel;
}
bool Parameters::getSpectrumMode() const
{
return spectrumMode;
}
SpectrumSmoothingMode Parameters::getSpectrumSmoothingMode() const
{
return spectrumSmoothingMode;
}
std::set<int> Parameters::getSelectedChannels() const
{
return selectedChannels;
}
ChannelMode Parameters::getChannelMode() const
{
return channelMode;
}
bool Parameters::getLinearMag() const
{
return linearMag;
}
void Parameters::setLinearMag(bool value)
{
linearMag = value;
}
void Parameters::setChannelMode(const ChannelMode &value)
{
channelMode = value;
}
void Parameters::setSelectedChannels(const std::set<int> &value)
{
selectedChannels = value;
}
void Parameters::setSpectrumSmoothingMode(const SpectrumSmoothingMode &value)
{
spectrumSmoothingMode = value;
}
void Parameters::setSpectrumMode(bool value)
{
spectrumMode = value;
}
void Parameters::setShowWindowFunctionLabel(bool value)
{
showWindowFunctionLabel = value;
}
void Parameters::setShowWindows(bool value)
{
showWindows = value;
}
void Parameters::setWindowFunction(const std::string &value)
{
windowFunction = value;
}
void Parameters::setHasWhiteBackground(bool value)
{
whiteBackground = value;
}
void Parameters::setStart(double value)
{
start = value;
}
void Parameters::setFinish(double value)
{
finish = value;
}
void Parameters::processChannelArgs(const std::vector<std::string>& args)
{
selectedChannels.clear();
channelMode = Normal;
std::string s;
for(const auto& arg : args) {
s.append(arg);
}
// convert s to lowercase
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) -> unsigned char {
return std::tolower(c);
});
std::regex rx{"(\\d{1}|[lr]|all|sum|diff|norm)", std::regex::icase};
auto it_begin = std::sregex_iterator(s.begin(), s.end(), rx);
auto it_end = std::sregex_iterator();
for(std::regex_iterator it = it_begin; it != it_end; ++it){
std::smatch match = *it;
std::string match_str = match.str();
if(match_str == "l") {
selectedChannels.insert(0);
} else if(match_str == "r") {
selectedChannels.insert(1);
} else if(match_str == "all") {
selectedChannels.clear();
} else if(match_str == "sum") {
channelMode = Sum;
} else if(match_str == "diff") {
channelMode = Difference;
} else if(match_str == "norm") {
channelMode = Normal;
} else {
try {
selectedChannels.insert(std::stoi(match_str));
}
catch (...) {}
}
}
}
} // namespace Sndspec