-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
277 lines (249 loc) · 7.6 KB
/
Copy pathmain.cpp
File metadata and controls
277 lines (249 loc) · 7.6 KB
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
/**
* @file main.cpp
* @version 1.0
* @brief MP3enc_cpp main application source
* @date Mar 4, 2020
* @author Siwon Kang (kkangshawn@gmail.com)
*/
#include "main.h"
#include <vector>
#include <time.h>
#if defined __linux
#include <dirent.h>
#elif defined _WIN32
#include <Windows.h>
#endif
using namespace std;
MP3enc* MP3enc::m_instance = nullptr;
size_t MP3enc::refCnt = 0;
void
MP3enc::showUsage()
{
cout << "Usage:" << endl;
cout << " MP3enc_cpp <input_directory | input_filename [-o <output_filename>]> [OPTIONS]" << endl;
cout << endl << "Options:" << endl;
cout << " -h Show help" << endl;
cout << " -r Search subdirectories recursively" << endl;
cout << " -q <mode> Set quality level" << endl;
cout << " fast fast encoding with small file size" << endl;
cout << " standard standard quality - default" << endl;
cout << " best best quality" << endl;
cout << " -v Verbose detail" << endl;
cout << endl << "Example:" << endl;
cout << " MP3enc_cpp input.wav -o output.mp3" << endl;
cout << " MP3enc_cpp wav_dir";
cout << DELIMITER;
cout << " -r -q fast -v" << endl;
}
void
MP3enc::checkPath(string path, vector<AudioData*>& v)
{
if (path.size() < 1) {
cerr << "ERROR: Input file is null" << endl;
return;
}
if (path.back() == DELIMITER) {
path.pop_back();
}
#if defined __linux
DIR* dir;
struct dirent* dir_ent;
dir = opendir(path.c_str());
if (!dir) {
/* process single file */
ifstream infile(path);
if (!infile.is_open()) {
cerr << "ERROR: Failed to find " << path << endl;
return;
}
AudioData adata(path, m_opt.outPath);
adata.start();
adata.join();
return;
}
while ((dir_ent = readdir(dir)) != NULL) {
string fullPath = path + DELIMITER + dir_ent->d_name;
switch (dir_ent->d_type) {
case DT_DIR:
if (m_opt.recursive && scmp(dir_ent->d_name, ".") && scmp(dir_ent->d_name, "..")) {
checkPath(fullPath, v);
}
break;
case DT_REG:
if (is_wav(dir_ent->d_name)) {
if (!m_opt.outPath.empty()) {
m_opt.outPath.clear();
DEBUG::WARN("Output filename(-o) option is ignored in case of decoding directory");
}
AudioData* adata = new AudioData(fullPath, m_opt.outPath);
adata->start();
v.push_back(adata);
}
break;
case DT_LNK:
break;
default:
break;
}
}
closedir(dir);
#elif defined _WIN32
HANDLE hFind;
WIN32_FIND_DATAA data;
hFind = FindFirstFileA(path.c_str(), &data);
if (hFind == INVALID_HANDLE_VALUE)
{
cerr << "ERROR: Failed to find " << path << endl;
return;
}
if (data.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE ||
data.dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
{
AudioData* adata = new AudioData(path, m_opt.outPath);
adata->start();
v.push_back(adata);
return;
}
else if (data.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
string findPath = path + DELIMITER + "*";
hFind = FindFirstFileA(findPath.c_str(), &data);
do
{
string fullPath = path + DELIMITER + data.cFileName;
if ((data.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE ||
data.dwFileAttributes == FILE_ATTRIBUTE_NORMAL) && is_wav(data.cFileName))
{
if (!m_opt.outPath.empty())
{
m_opt.outPath.clear();
DEBUG::WARN("Output filename(-o) option is ignored in case of decoding directory");
}
AudioData* adata = new AudioData(fullPath, m_opt.outPath);
adata->start();
v.push_back(adata);
}
else if (data.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY &&
m_opt.recursive &&
scmp(data.cFileName, ".") && scmp(data.cFileName, ".."))
{
checkPath(fullPath, v);
}
} while (FindNextFileA(hFind, &data));
}
FindClose(hFind);
#endif
}
bool
MP3enc::parseOption(int argc, char** argv)
{
if (argc < 2) {
cerr << "ERROR: Argument missing. Please see below usage:" << endl;
m_instance->showUsage();
return false;
}
for (int i = 1; i < argc; i++) {
if (!scmp(argv[i], "-h")) {
showUsage();
DEBUG::CLEAR();
return false;
} else if (!scmp(argv[i], "-o")) {
if (i + 1 >= argc) {
cerr << "ERROR: -o needs output filename" << endl;
return false;
} else if (argv[i + 1][0] == '-') {
cerr << "ERROR: output filename shall not start from '-'" << endl;
return false;
}
m_opt.outPath = argv[i + 1];
i++;
} else if (!scmp(argv[i], "-r")) {
m_opt.recursive = true;
} else if (!scmp(argv[i], "-q")) {
i++;
if (i >= argc) {
cerr << "ERROR: -q needs to specify mode" << endl;
return false;
}
if (!scmp(argv[i], "fast")) {
AudioData::set_quality(AudioData::QL_FAST);
DEBUG::INFO("Quality level: FAST");
} else if (!scmp(argv[i], "standard")) {
AudioData::set_quality(AudioData::QL_STANDARD);
DEBUG::INFO("Quality level: STANDARD");
} else if (!scmp(argv[i], "best")) {
AudioData::set_quality(AudioData::QL_BEST);
DEBUG::INFO("Quality level: BEST");
} else {
cerr << "ERROR: Wrong mode for quality level. Please see below usage:" << endl;
m_instance->showUsage();
return false;
}
} else if (!scmp(argv[i], "-v")) {
m_opt.verbose = true;
DEBUG::SET();
} else {
m_opt.inPath = argv[i];
}
}
vector<AudioData*> filelist = {};
checkPath(m_opt.inPath, filelist);
for (AudioData* d : filelist) {
d->join();
delete d;
}
return true;
}
void
MP3enc::freeInstance()
{
if (!m_instance) {
DEBUG::ERR("No instance to free");
return;
}
if (refCnt > 0) {
refCnt--;
}
if (refCnt == 0) {
delete m_instance;
m_instance = nullptr;
}
}
MP3enc*
MP3enc::getInstance(int argc, char** argv)
{
if (!m_instance) {
m_instance = new MP3enc();
if (!m_instance) {
DEBUG::ERR("Failed to allocate memory");
return nullptr;
}
}
if (m_instance->parseOption(argc, argv) == false) {
DEBUG::ERR("Failed to parse options");
m_instance->freeInstance();
return nullptr;
}
refCnt++;
return m_instance;
}
int
MP3enc::main(int argc, char** argv)
{
cout << "MP3enc_cpp v" << VERSION << endl;
clock_t t = clock();
MP3enc* mp3enc = MP3enc::getInstance(argc, argv);
if (!mp3enc) {
DEBUG::ERR("Failed to get instance");
return 1;
}
mp3enc->freeInstance();
t = clock() - t;
double elapsed = (double)t / CLOCKS_PER_SEC;
cout << "elapsed " << fixed << elapsed << "s" << endl;
return 0;
}
int main(int argc, char** argv)
{
return MP3enc::main(argc, argv);
}