-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaveFileDemo.cpp
298 lines (260 loc) · 6.54 KB
/
WaveFileDemo.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
// WaveFileDemo.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "WaveFile.h"
#include "Crossover.h"
#include "FirFilter.h"
#include "IirFilter.h"
#include "fdacoefs.h"
#include "iircoefs-lc50-44100.h"
#include <string>
#include <cstdio>
#include "biphase_encode.h"
#define TEST_FILE_PATH //"F:/Documents/MFC/WaveFileDemo/Debug/"
#define TEST_FILE_NAME "output"
#define MAX_PATH 260
char TestPath[MAX_PATH];
int FormatTest(int argc, char* argv[])
{
int i;
for (i = 1; i < argc; ++i) {
WaveFile wav;
wav.ReadFile(argv[i]);
// float
sprintf_s(TestPath, TEST_FILE_PATH "float" "(%d).wav", i);
if (WF_FAILURE == wav.WriteFile(DT_FLOAT, TestPath)) {
printf("Write file failed! [%s]\n", TestPath);
}
// int16
sprintf_s(TestPath, TEST_FILE_PATH "int16" "(%d).wav", i);
if (WF_FAILURE == wav.WriteFile(DT_INT16, TestPath)) {
printf("Write file failed! [%s]\n", TestPath);
}
// int24
sprintf_s(TestPath, TEST_FILE_PATH "int24" "(%d).wav", i);
if (WF_FAILURE == wav.WriteFile(DT_INT24, TestPath)) {
printf("Write file failed! [%s]\n", TestPath);
}
// int32
sprintf_s(TestPath, TEST_FILE_PATH "int32" "(%d).wav", i);
if (WF_FAILURE == wav.WriteFile(DT_INT32, TestPath)) {
printf("Write file failed! [%s]\n", TestPath);
}
// uint8
sprintf_s(TestPath, TEST_FILE_PATH "uint8" "(%d).wav", i);
if (WF_FAILURE == wav.WriteFile(DT_UINT8, TestPath)) {
printf("Write file failed! [%s]\n", TestPath);
}
}
#ifdef _DEBUG
printf("输入任意键继续\n");
getchar();
#endif
return 0;
}
int TrimTest(int argc, char* argv[])
{
int i;
float sec;
printf("输入裁剪秒数:");
scanf_s("%f", &sec);
fflush(stdin);
for (i = 1; i < argc; ++i) {
WaveFile wav;
wav.ReadFile(argv[i]);
// float
strcpy_s(TestPath, MAX_PATH, argv[i]);
size_t len = strlen(TestPath);
if (len < 4) {
printf("Invalid path: %s\n", TestPath);
continue;
}
if (strcmp(TestPath + len - 4, ".wav") != 0) {
printf("Invalid extension: %s\n", TestPath);
continue;
}
sprintf(TestPath + len - 4, "_trim[%gs].wav", sec);
DWORD sr = wav.GetSampleRate();
if (WF_FAILURE == wav.WriteFile(DT_AUTO, TestPath, (int)((float)sr * sec))) {
printf("Write file failed! [%s]\n", TestPath);
}
}
#ifdef _DEBUG
printf("输入任意键继续\n");
getchar();
#endif
return 0;
}
int WavClear(int argc, char* argv[])
{
int i;
for (i = 1; i < argc; ++i) {
WaveFile wav;
wav.ReadFile(argv[i]);
// float
strcpy_s(TestPath, MAX_PATH, argv[i]);
size_t len = strlen(TestPath);
if (len < 4) {
printf("Invalid path: %s\n", TestPath);
continue;
}
if (WF_FAILURE == wav.WriteFile(DT_AUTO, argv[i])) {
printf("Write file failed! [%s]\n", argv[i]);
}
}
#ifdef _DEBUG
printf("输入任意键继续\n");
getchar();
#endif
return 0;
}
#if 1
#define CO_TEST_FILE "E:/Audio/Temp/sweep"
#else
#define CO_TEST_FILE "E:/Audio/Temp/sine440"
#endif
#define CO_TEST_EXT ".wav"
int CrossoverTest(int argc, char* argv[])
{
WaveFile wav;
wav.ReadFile(CO_TEST_FILE CO_TEST_EXT);
Crossover crs(10.0, 0.6);
crs.ProcessWav(wav);
wav.WriteFile(DT_AUTO, CO_TEST_FILE "_out.wav");
return 0;
}
//int _tmain(int argc, _TCHAR* argv[])
int FilterTest(int argc, char* argv[])
{
WaveFile wav;
wav.ReadFile(CO_TEST_FILE CO_TEST_EXT);
FirFilter ft(B + 250, 251);
ft.ProcessWav(wav);
wav.WriteFile(DT_AUTO, CO_TEST_FILE "_out.wav");
return 0;
}
int IIRTest(int argc, char* argv[])
{
WaveFile wav;
wav.ReadFile(CO_TEST_FILE CO_TEST_EXT);
mult_biquad_state sec1 = {
gain_sec1,
denominator_sec1[0], denominator_sec1[1], denominator_sec1[2],
numerator_sec1[0], numerator_sec1[1], numerator_sec1[2],
};
mult_biquad_state sec2 = {
gain_sec2,
denominator_sec2[0], denominator_sec2[1], denominator_sec2[2],
numerator_sec2[0], numerator_sec2[1], numerator_sec2[2],
};
IirFilter iir(sec1, sec2);
iir.ProcessWav(wav);
wav.WriteFile(DT_AUTO, CO_TEST_FILE "_out.wav");
return 0;
}
int OneKeyLowCut(int argc, char* argv[])
{
int i;
mult_biquad_state sec1 = {
gain_sec1,
denominator_sec1[0], denominator_sec1[1], denominator_sec1[2],
numerator_sec1[0], numerator_sec1[1], numerator_sec1[2],
};
mult_biquad_state sec2 = {
gain_sec2,
denominator_sec2[0], denominator_sec2[1], denominator_sec2[2],
numerator_sec2[0], numerator_sec2[1], numerator_sec2[2],
};
IirFilter iir(sec1, sec2);
for (i = 1; i < argc; ++i) {
WaveFile wav;
wav.ReadFile(argv[i]);
// float
strcpy_s(TestPath, MAX_PATH, argv[i]);
size_t len = strlen(TestPath);
if (len < 4) {
printf("Invalid path: %s\n", TestPath);
continue;
}
if (strcmp(TestPath + len - 4, ".wav") != 0) {
printf("Invalid extension: %s\n", TestPath);
continue;
}
sprintf(TestPath + len - 4, "_lowcut.wav");
iir.ProcessWav(wav);
if (WF_FAILURE == wav.WriteFile(DT_AUTO, TestPath)) {
printf("Write file failed! [%s]\n", TestPath);
}
}
return 0;
}
int ConvertChannels(int argc, char* argv[])
{
for (int i = 1; i < argc; ++i) {
WaveFile wav;
wav.ReadFile(argv[i]);
// float
strcpy_s(TestPath, MAX_PATH, argv[i]);
size_t len = strlen(TestPath);
if (len < 4) {
printf("Invalid path: %s\n", TestPath);
continue;
}
if (strcmp(TestPath + len - 4, ".wav") != 0) {
printf("Invalid extension: %s\n", TestPath);
continue;
}
if (wav.GetChannels() == 2) {
sprintf(TestPath + len - 4, "_L.wav");
WaveFile wavL(wav, CHANNEL_MONO_L);
wavL.WriteFile(DT_AUTO, TestPath);
sprintf(TestPath + len - 4, "_R.wav");
WaveFile wavR(wav, CHANNEL_MONO_R);
wavR.WriteFile(DT_AUTO, TestPath);
}
}
return 0;
}
int ConvertAndTrim(int argc, char* argv[])
{
char* outfile = nullptr;
if (argc == 2) {
outfile = argv[1];
} else if (argc > 2) {
outfile = argv[2];
}
WaveFile* pWav = new WaveFile();
pWav->ReadFile(argv[1]);
if (pWav->GetChannels() == 2) {
WaveFile* pWavTemp = new WaveFile(*pWav, CHANNEL_MONO_L);
delete pWav;
pWav = pWavTemp;
}
size_t datalen = pWav->GetLastNonZero();
printf("%d / %d\n", datalen, pWav->GetChannelLen());
pWav->Resize(datalen);
pWav->WriteFile(DT_AUTO, outfile);
delete pWav;
pWav = nullptr;
return 0;
}
#define __RETURN_IF_ARGS_LESSTHAN(argc, n) do {if ((argc) < (n)) {return -1;} } while(0)
int GenBiphaseWave(int argc, char* argv[])
{
__RETURN_IF_ARGS_LESSTHAN(argc, 2);
BYTE packet[3];
packet[0] = 1;
packet[1] = 0x35;
packet[2] = Checksum8(packet, 2);
size_t sample_len;
sample_t* pdata = AllocSamples(packet, 3, 44100, &sample_len);
WaveFile wav(1, 44100);
wav.ImportData(sample_len, pdata);
wav.WriteFile(DT_AUTO, argv[1]);
delete [] pdata;
return 0;
}
int main(int argc, char* argv[])
{
return OneKeyLowCut(argc, argv);
}