-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWAVReading.c
111 lines (87 loc) · 2.97 KB
/
WAVReading.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
/* WAVReading.c
*
* Utility functions for reading .wav files.
*/
#include <stdio.h>
#include <stdlib.h>
#include <complex.h>
#include <stdint.h>
/* Reads the header of a WAV file and returns the number of channels in it.
* Leaves the file pointer at the beginning of the sample values in the file.
*/
uint16_t readWAVChannels(FILE * infile) {
uint16_t result;
/* Seek to the beginning of the channels integer. */
if (fseek(infile, 22, SEEK_SET)) {
fprintf(stderr, "readWAVChannels: error seeking file.\n");
exit(1);
}
/* Read the 2-byte channels integer into the result. */
if (fread(&result, 2, 1, infile) != 1) {
fprintf(stderr, "readWAVChannels: error reading file.\n");
exit(1);
}
/* Seek to the beginning of the data section. */
if (fseek(infile, 44, SEEK_SET)) {
fprintf(stderr, "readWAVChannels: error seeking file.\n");
exit(1);
}
return result;
}
/* Read a WAV header and use the number of channels to compute how many samples
* are in one channel from start to finish. */
int readWAVLength(FILE * infile, int channels) {
int result;
int sampleSize;
/* Seek to the location of the sample size integer in the header. */
if (fseek(infile, 34, SEEK_SET)) {
fprintf(stderr, "readWAVLength: error seeking file.\n");
exit(1);
}
/* Read the size of samples in this wav file. */
if (fread(&sampleSize, 2, 1, infile) != 1) {
fprintf(stderr, "readWAVLength: error reading sample size.\n");
exit(1);
}
/* Seek to the location of the data size integer in the header. */
if (fseek(infile, 40, SEEK_SET)) {
fprintf(stderr, "readWAVLength: error seeking file.\n");
exit(1);
}
/* Read the 4-byte size integer into the result. */
if (fread(&result, 4, 1, infile) != 1) {
fprintf(stderr, "readWAVLength: error reading file.\n");
exit(1);
}
/* Seek to the beginning of the data section. */
if (fseek(infile, 44, SEEK_SET)) {
fprintf(stderr, "readWAVLength: error seeking file.\n");
exit(1);
}
return (result / (sampleSize / 8)) / channels;
}
/* Read the next m samples from a WAV file into the array passed in.
* Assumes the file pointer begins somewhere in the samples, header has
* been skipped.
*
* Returns the number of samples read. Will be different from m if EOF reached.
*/
int getNextMValues(FILE * infile,
double complex * output, int m, int channels) {
int samples = 0;
for (int i = 0; i < m; i++) {
uint16_t sample;
if (fread(&sample, 2, 1, infile) == 1)
samples++;
else
break;
output[i] = sample;
/* Seek ahead past the other channels' samples. */
if (fseek(infile, 2 * (channels - 1), SEEK_CUR)) {
/* ASSume: 2 byte sample size. */
fprintf(stderr, "getNextMValues: error seeking file.\n");
exit(1);
}
}
return samples;
}