-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwave2data.cpp
107 lines (86 loc) · 2.37 KB
/
wave2data.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
#include "wave2data.h"
void showHelp() {
std::cout << "Arguments:" << std::endl;
std::cout << "\t-h \t" << "show help" << std::endl;
std::cout << "\t-v \t" << "show version" << std::endl;
std::cout << "\t-f <filename>\t" << "set input filename" << std::endl;
std::cout << "\t-o <filename>\t" << "set output filename" << std::endl;
}
bool setopts(int argc, char* argv[], std::string &fn, std::string &output) {
int result;
auto odef = "output.dat";
fn = "";
output = odef;
auto version = "wave2data 0.1.2";
if (argc < 2) {
return false;
}
while ((result=getopt(argc, argv, "hvf:o:"))!=-1) {
switch (result) {
case 'h':
// help
showHelp();
break;
case 'v':
// version
std::cout << version << std::endl;
break;
case 'f':
fn = std::string(optarg);
if (output == odef) {
output = fn + ".dat";
}
break;
case 'o':
output = std::string(optarg);
break;
default:
break;
}
}
return true;
}
std::string file2string(std::string fn) {
std::ifstream ifs(fn, std::ios::in | std::ios::binary);
if (!ifs) {
std::perror(fn.c_str());
return std::string();
}
std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
ifs.close();
return str;
}
bool dataCheck(std::string data, std::string fn) {
if (data.empty()) {
std::cout << fn << ": Unknown Error" << std::endl;
return false;
}
// RIFF Check
if (data.substr(0,4) != "RIFF") {
std::cout << fn << ": not RIFF File." << std::endl;
return false;
}
// Wave Check
if (data.substr(8, 4) != "WAVE") {
std::cout << fn << ": not Wave File." << std::endl;
return false;
}
return true;
}
int getDataSize(std::string s_hex) {
int sum = 0;
for (int i = 0; i < 4; i++) {
sum += static_cast<unsigned char>(s_hex.at(i)) << (8*i);
}
return sum;
}
bool createData(std::string fn, std::string data) {
std::ofstream ofs(fn, std::ios::out | std::ios::binary);
if (!ofs) {
std::perror(fn.c_str());
return false;
}
ofs << data;
ofs.close();
return true;
}