-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
187 lines (160 loc) · 5.12 KB
/
main.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
#include <iostream>
#include <fstream>
#include <cassert>
#include <vector>
#include <filesystem>
#include "Utils.h"
#include "indywv.h"
#include "labn.h"
#include "wav_writer.h"
#include "wav_format.h"
#include "unit_test.h"
#include <unordered_map>
const char* kInArg = "-in";
const char* kOutArg = "-out";
const char* kUnitTestArg = "-unit_test";
std::string get_filename_noext(const std::string& filepath)
{
auto filename = filepath.substr(filepath.find_last_of("/\\") + 1);
size_t lastindex = filename.find_last_of(".");
return filename.substr(0, lastindex);
}
std::string get_file_ext(const std::string& filepath)
{
auto filename = filepath.substr(filepath.find_last_of("/\\") + 1);
size_t lastindex = filename.find_last_of(".");
return filename.substr(lastindex + 1, filename.size() - lastindex - 1);
}
std::string getOutFolderPath(const std::string& outArg)
{
namespace fs = std::filesystem;
if (fs::is_directory(outArg))
return fs::path(outArg).string();
else
return fs::path(outArg).parent_path().string();
}
std::string getOutFilePath(const std::string& inPath, const std::string& outArg, const std::string& outExt)
{
std::string baseFileName = get_filename_noext(inPath);
namespace fs = std::filesystem;
if (fs::is_directory(outArg))
{
auto path = fs::path(outArg) / (baseFileName + "." + outExt);
return path.string();
}
else
{
auto ext = get_file_ext(outArg);
return outArg;
}
}
using string_map = std::unordered_map<std::string, std::vector<std::string>>;
template <class Keyword>
string_map generic_parse(std::vector<std::string> as, Keyword keyword)
{
string_map result;
std::string flag;
for (auto&& x : as)
{
auto f = keyword(x);
if (f.empty())
{
result[flag].push_back(x);
}
else
{
flag = f.front();
result[flag]; // Ensure the flag exists
flag = f.back();
}
}
return result;
}
void printUsage()
{
std::cout << "Usage:\n"
<< "-in <FilePath> : full path of input file, INDYWV or LAB. Type will be auto-deduced)\n"
<< "-out <FileOrFolderPath> : path of output file or folder\n"
<< "[-unit_test] : performs unit test - checks algorithm integrity (optional)\n";
}
int main(int argc, const char* argv[])
{
std::vector<std::string> args(argv + 1, argv + argc);
std::unordered_map<std::string, std::string> keys = {
{ kInArg, kInArg },
{ kOutArg, kOutArg },
{ kUnitTestArg, kUnitTestArg }
};
auto result = generic_parse(args, [&](auto&& s) -> std::vector<std::string> {
if (keys.count(s) > 0)
return { keys[s] };
else
return {};
});
if (result.find(kUnitTestArg) != result.end())
{
auto converter = IndyWV();
std::string wvFile = "..\\..\\UnitTest\\dice_mono_adpcm.wv";
std::string wavFile = "..\\..\\UnitTest\\dice_mono_adpcm.wav";
{
std::string tmpWavFile = wvFile + "_temp.wav";
IndyWV().wv_to_wav(wvFile, tmpWavFile);
UnitTest::unit_test("ADPCM wv->wav", tmpWavFile, wavFile);
std::remove(tmpWavFile.c_str());
}
{
std::string tmpWvFile = wvFile + "_temp.wv";
IndyWV().wav_to_wv(wavFile, tmpWvFile);
UnitTest::unit_test("ADPCM wav->wv", tmpWvFile, wvFile);
std::remove(tmpWvFile.c_str());
}
{
wvFile = "..\\..\\UnitTest\\stereo_wvsm_test.wv";
wavFile = "..\\..\\UnitTest\\stereo_wvsm_test.wav";
std::string tmpWavFile = wvFile + "_temp.wav";
IndyWV().wv_to_wav(wvFile, tmpWavFile);
UnitTest::unit_test("WVSM wv->wav", tmpWavFile, wavFile);
std::remove(tmpWavFile.c_str());
}
return 0;
}
if (result.find(kInArg) == result.end() || result[kInArg].empty())
{
std::cerr << "Missing -in parameter\n";
printUsage();
return -1;
}
if (result.find(kOutArg) == result.end() || result[kOutArg].empty())
{
std::cerr << "Missing -out parameter\n";
printUsage();
return -1;
}
std::string inputFile = result[kInArg][0];
std::string outArg = result[kOutArg][0];
std::ifstream file (inputFile, std::ios::in | std::ios::binary | std::ios::ate);
if (!file.is_open())
return -1;
file.seekg(0, std::ios::beg);
char header[6];
Utils::peekChar(file, header, 6);
if (strncmp(header, LABN::kLABNId, 4) == 0)
{
auto outFolderPath = getOutFolderPath(outArg);
LABN().decompressLabFile(inputFile, outFolderPath);
}
else if (strncmp(header, IndyWV::kIndyWV, 6) == 0)
{
auto outFilePath = getOutFilePath(inputFile, outArg, "wav");
IndyWV().wv_to_wav(inputFile, outFilePath);
}
else if (strncmp(header, WavFormat::kRIFF, 4) == 0)
{
auto outFilePath = getOutFilePath(inputFile, outArg, "wv");
IndyWV().wav_to_wv(inputFile, outFilePath);
}
else
{
std::cerr << "Unrecognized input file type\n";
}
}