-
Notifications
You must be signed in to change notification settings - Fork 12
/
wave_test.c
75 lines (66 loc) · 2.79 KB
/
wave_test.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "wave_reader.h"
#include "wave_writer.h"
#define BUFFER_SIZE 4096
void test_read_write(const char *filename) {
wave_reader *wr;
wave_reader *wr2;
wave_reader_error rerror;
wave_writer *ww;
wave_writer_error werror;
wave_writer_format format;
char copied[256];
wr = wave_reader_open(filename, &rerror);
if (wr) {
snprintf(copied, sizeof(copied), "%s.copy", filename);
format.num_channels = wave_reader_get_num_channels(wr);
format.sample_rate = wave_reader_get_sample_rate(wr);
format.sample_bits = wave_reader_get_sample_bits(wr);
ww = wave_writer_open(copied, &format, &werror);
if (ww) {
unsigned char *buf;
int n;
printf("filename=%s format=%d num_channels=%d sample_rate=%d sample_bits=%d num_samples=%d\n",
filename,
wave_reader_get_format(wr),
wave_reader_get_num_channels(wr),
wave_reader_get_sample_rate(wr),
wave_reader_get_sample_bits(wr),
wave_reader_get_num_samples(wr));
buf = (unsigned char *)malloc(BUFFER_SIZE * format.num_channels * format.sample_bits / 8);
while (0 < (n = wave_reader_get_samples(wr, BUFFER_SIZE, buf))) {
wave_writer_put_samples(ww, n, buf);
}
assert(wave_reader_get_format(wr) == wave_writer_get_format(ww));
assert(wave_reader_get_num_channels(wr) == wave_writer_get_num_channels(ww));
assert(wave_reader_get_sample_rate(wr) == wave_writer_get_sample_rate(ww));
assert(wave_reader_get_sample_bits(wr) == wave_writer_get_sample_bits(ww));
assert(wave_reader_get_num_samples(wr) == wave_writer_get_num_samples(ww));
wave_writer_close(ww, &werror);
free(buf);
wr2 = wave_reader_open(copied, &rerror);
if (wr2) {
assert(wave_reader_get_format(wr) == wave_reader_get_format(wr2));
assert(wave_reader_get_num_channels(wr) == wave_reader_get_num_channels(wr2));
assert(wave_reader_get_sample_rate(wr) == wave_reader_get_sample_rate(wr2));
assert(wave_reader_get_sample_bits(wr) == wave_reader_get_sample_bits(wr2));
assert(wave_reader_get_num_samples(wr) == wave_reader_get_num_samples(wr2));
} else {
printf("rerror=%d\n", rerror);
}
wave_reader_close(wr2);
wave_reader_close(wr);
} else {
printf("werror=%d\n", werror);
}
} else {
printf("rerror=%d\n", rerror);
}
}
int main(void) {
test_read_write("11k16bitpcm.wav");
return 0;
}