-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathmain.c
224 lines (201 loc) · 6.85 KB
/
main.c
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
#ifdef __cplusplus
extern "C" {
#endif
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define DR_WAV_IMPLEMENTATION
#include "dr_wav.h"
#define DR_MP3_IMPLEMENTATION
#include "dr_mp3.h"
#include "timing.h"
void wavWrite_f32(char *filename, float *buffer, int sampleRate, uint32_t totalSampleCount, uint32_t channels) {
drwav_data_format format;
format.container = drwav_container_riff;
format.format = DR_WAVE_FORMAT_IEEE_FLOAT;
format.channels = channels;
format.sampleRate = (drwav_uint32) sampleRate;
format.bitsPerSample = 32;
drwav *pWav = drwav_open_file_write(filename, &format);
if (pWav) {
drwav_uint64 samplesWritten = drwav_write(pWav, totalSampleCount, buffer);
drwav_uninit(pWav);
if (samplesWritten != totalSampleCount) {
fprintf(stderr, "write file [%s] error.\n", filename);
exit(1);
}
}
}
float *wavRead_f32(const char *filename, uint32_t *sampleRate, uint64_t *sampleCount, uint32_t *channels) {
drwav_uint64 totalSampleCount = 0;
float *input = drwav_open_file_and_read_pcm_frames_f32(filename, channels, sampleRate, &totalSampleCount);
if (input == NULL) {
drmp3_config pConfig;
input = drmp3_open_file_and_read_f32(filename, &pConfig, &totalSampleCount);
if (input != NULL) {
*channels = pConfig.outputChannels;
*sampleRate = pConfig.outputSampleRate;
}
}
if (input == NULL) {
fprintf(stderr, "read file [%s] error.\n", filename);
exit(1);
}
*sampleCount = totalSampleCount * (*channels);
return input;
}
void splitpath(const char *path, char *drv, char *dir, char *name, char *ext) {
const char *end;
const char *p;
const char *s;
if (path[0] && path[1] == ':') {
if (drv) {
*drv++ = *path++;
*drv++ = *path++;
*drv = '\0';
}
} else if (drv)
*drv = '\0';
for (end = path; *end && *end != ':';)
end++;
for (p = end; p > path && *--p != '\\' && *p != '/';)
if (*p == '.') {
end = p;
break;
}
if (ext)
for (s = end; (*ext = *s++);)
ext++;
for (p = end; p > path;)
if (*--p == '\\' || *p == '/') {
p++;
break;
}
if (name) {
for (s = p; s < end;)
*name++ = *s++;
*name = '\0';
}
if (dir) {
for (s = path; s < p;)
*dir++ = *s++;
*dir = '\0';
}
}
uint64_t Resample_f32(const float *input, float *output, int inSampleRate, int outSampleRate, uint64_t inputSize,
uint32_t channels
) {
if (input == NULL)
return 0;
uint64_t outputSize = (uint64_t) (inputSize * (double) outSampleRate / (double) inSampleRate);
outputSize -= outputSize % channels;
if (output == NULL)
return outputSize;
double stepDist = ((double) inSampleRate / (double) outSampleRate);
const uint64_t fixedFraction = (1LL << 32);
const double normFixed = (1.0 / (1LL << 32));
uint64_t step = ((uint64_t) (stepDist * fixedFraction + 0.5));
uint64_t curOffset = 0;
for (uint32_t i = 0; i < outputSize; i += 1) {
for (uint32_t c = 0; c < channels; c += 1) {
*output++ = (float) (input[c] + (input[c + channels] - input[c]) * (
(double) (curOffset >> 32) + ((curOffset & (fixedFraction - 1)) * normFixed)
)
);
}
curOffset += step;
input += (curOffset >> 32) * channels;
curOffset &= (fixedFraction - 1);
}
return outputSize;
}
uint64_t Resample_s16(const int16_t *input, int16_t *output, int inSampleRate, int outSampleRate, uint64_t inputSize,
uint32_t channels
) {
if (input == NULL)
return 0;
uint64_t outputSize = (uint64_t) (inputSize * (double) outSampleRate / (double) inSampleRate);
outputSize -= outputSize % channels;
if (output == NULL)
return outputSize;
double stepDist = ((double) inSampleRate / (double) outSampleRate);
const uint64_t fixedFraction = (1LL << 32);
const double normFixed = (1.0 / (1LL << 32));
uint64_t step = ((uint64_t) (stepDist * fixedFraction + 0.5));
uint64_t curOffset = 0;
for (uint32_t i = 0; i < outputSize; i += 1) {
for (uint32_t c = 0; c < channels; c += 1) {
*output++ = (int16_t) (input[c] + (input[c + channels] - input[c]) * (
(double) (curOffset >> 32) + ((curOffset & (fixedFraction - 1)) * normFixed)
)
);
}
curOffset += step;
input += (curOffset >> 32) * channels;
curOffset &= (fixedFraction - 1);
}
return outputSize;
}
void printUsage() {
printf("usage:\n");
printf("./Resampler input.wav 48000\n");
printf("./Resampler input.mp3 16000\n");
printf("or\n");
printf("./Resampler input.wav output.wav 8000\n");
printf("./Resampler input.mp3 output.wav 44100\n");
printf("press any key to exit.\n");
getchar();
}
void resampler(char *in_file, char *out_file, uint32_t targetSampleRate) {
if (targetSampleRate == 0) {
printUsage();
return;
}
uint32_t sampleRate = 0;
uint64_t sampleCount = 0;
uint32_t channels = 0;
float *input = wavRead_f32(in_file, &sampleRate, &sampleCount, &channels);
uint64_t targetSampleCount = Resample_f32(input, NULL, sampleRate, targetSampleRate, sampleCount, channels);
if (input) {
float *output = (float *) malloc(targetSampleCount * sizeof(float));
if (output) {
double startTime = now();
Resample_f32(input, output, sampleRate, targetSampleRate, sampleCount / channels, channels);
double time_interval = calcElapsed(startTime, now());
printf("time interval: %f ms\n ", (time_interval * 1000));
wavWrite_f32(out_file, output, targetSampleRate, (uint32_t) targetSampleCount, channels);
free(output);
}
free(input);
}
}
int main(int argc, char *argv[]) {
printf("Audio Processing\n");
printf("blog:http://cpuimage.cnblogs.com/\n");
printf("Audio Resampler\n");
if (argc < 3) {
printUsage();
return -1;
}
char *in_file = argv[1];
if (argc > 3) {
char *out_file = argv[2];
uint32_t targetSampleRate = (uint32_t) atoi(argv[3]);
resampler(in_file, out_file, targetSampleRate);
} else {
uint32_t targetSampleRate = (uint32_t) atoi(argv[2]);
char drive[3];
char dir[256];
char fname[256];
char ext[256];
char out_file[1024];
splitpath(in_file, drive, dir, fname, ext);
sprintf(out_file, "%s%s%s_out.wav", drive, dir, fname);
resampler(in_file, out_file, targetSampleRate);
}
return 0;
}
#ifdef __cplusplus
}
#endif