-
Notifications
You must be signed in to change notification settings - Fork 1
/
WaveReader.cpp
executable file
·151 lines (118 loc) · 3.03 KB
/
WaveReader.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
#include "WaveReader.h"
WaveReader::WaveReader(const char* filename) {
mWavFile = fopen(filename, "rb");
}
WaveReader::~WaveReader() {
fclose(mWavFile);
}
void WaveReader::readWav() {
printf("Reading header...\n");
info = readHeader();
printf("Reading data...\n");
data = readData();
}
int WaveReader::readInt32() {
int data = 0;
char buffer[4];
fread(buffer, 4, 1, mWavFile);
data += 0x00ff & buffer[3];
data = data << 8;
data += 0x00ff & buffer[2];
data = data << 8;
data += 0x00ff & buffer[1];
data = data << 8;
data += 0x00ff & buffer[0];
return data;
}
int WaveReader::readInt16() {
int data = 0;
char buffer[2];
fread(buffer, 2, 1, mWavFile);
data += 0x00ff & buffer[1];
data = data << 8;
data += 0x00ff & buffer[0];
return data;
}
int WaveReader::readInt8() {
int data = 0;
char buffer[1];
fread(buffer, 1, 1, mWavFile);
data = 0x00ff & buffer[0];
return data;
}
int WaveReader::readSample(int bytes) {
switch (bytes) {
case 1:
return readInt8();
case 2:
return readInt16();
case 4:
return readInt32();
}
return 0;
}
WaveInfo WaveReader::readHeader() {
int chunkId = 0;
bool dataChunk = false;
WaveInfo info;
while (!dataChunk) {
chunkId = readInt32();
switch (chunkId) {
case FORMAT:
//printf("Format\n");
info.formatSize = readInt32();
info.format = readInt16();
info.channels = readInt16();
info.sampleRate = readInt32();
info.bitsPerSecond = readInt32();
info.formatBlockAlign = readInt16();
info.bitDepth = readInt16();
if (info.formatSize == 18) {
int extraData = readInt16();
fseek(mWavFile, extraData, SEEK_CUR);
}
break;
case RIFF_HEADER:
//printf("Riff Header\n");
info.headerId = chunkId;
info.memSize = readInt32();
info.riffStyle = readInt32();
break;
case DATA:
//printf("Data\n");
dataChunk = true;
info.dataSize = readInt32();
break;
default:
int skipSize = readInt32();
//printf("Skip Size = %d\n", skipSize);
fseek(mWavFile, skipSize, SEEK_CUR);
break;
}
}
return info;
}
WaveData WaveReader::readData() {
WaveData data;
data.channelCount = info.channels;
data.dataLength = info.dataSize / data.channelCount;
data.data = new int*[data.channelCount];
for (int i = 0; i < data.channelCount; i++) {
data.data[i] = new int[data.dataLength];
}
int bytesPerSample;
switch (info.format) {
case PULSE_CODE_MODULATION:
case EXTENSIBLE:
default:
bytesPerSample = (info.bitDepth / data.channelCount) / 8;
break;
}
int byteCount = info.dataSize / data.channelCount;
for (int i = 0; i < byteCount; i++) {
for (int j = 0; j < data.channelCount; j++) {
data.data[j][i] = readSample(bytesPerSample);
}
}
return data;
}