[go: up one dir, main page]

Menu

[r49]: / lodewav.cpp  Maximize  Restore  History

Download this file

320 lines (256 with data), 9.4 kB

  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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
LodeWAV version 20071117
Copyright (c) 2007 Lode Vandevenne
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "lodewav.h"
#include <vector>
namespace LodeWAV
{
void encode(std::vector<unsigned char>& out, const std::vector<std::vector<double> >& in, const InfoWAV& info)
{
if(info.channels < 1) return; //nothing to encode
//convert the input data to unsigned char data with channels interleaved
std::vector<unsigned char> data;
long range = 1 << info.bitsPerSample; //the range of values for the integers of samples in the wav file
unsigned long halfrange = range / 2;
for(size_t x = 0; x < in[0].size(); x++)
{
for(int j = 0; j < info.channels; j++)
{
double v = in[j][x];
unsigned long vn = (unsigned long)((1 << info.bitsPerSample) * ((v + 1.0) / 2.0)); //normalized
//now make the number two's complement, center level is integer value 0 in the wav
if(vn >= halfrange)
{
vn -= halfrange;
}
else
{
vn += halfrange;
}
for(int i = 0; i < info.bitsPerSample / 8; i++)
{
unsigned char c = ((unsigned long)(vn) >> (i * 8)) & 255;
data.push_back(c);
}
}
}
//calculate parameters for WAV file
int blockAlign = (info.channels * info.bitsPerSample) / 8;
int byteRate = info.samplerate * blockAlign;
int filesize = 44 + data.size();
out.clear();
//== RIFF header ==
//"RIFF"
out.push_back('R');
out.push_back('I');
out.push_back('F');
out.push_back('F');
//size of the file minus 8 (= number of bytes that comes after these 4 bytes)
out.push_back( (filesize - 8) % 256);
out.push_back(((filesize - 8) / 256) % 256);
out.push_back(((filesize - 8) / 65536) % 256);
out.push_back(((filesize - 8) / 16777216) % 256);
//"WAVE"
out.push_back('W');
out.push_back('A');
out.push_back('V');
out.push_back('E');
//== FORMAT chunk ==
//ID (4 bytes)
out.push_back('f');
out.push_back('m');
out.push_back('t');
out.push_back(' ');
//chunkSize (excluding the 8 bytes for ID and chunkSize)
out.push_back(16);
out.push_back(0);
out.push_back(0);
out.push_back(0);
//wFormatTag
out.push_back(1); //1 = no compression
out.push_back(0);
//wChannels
out.push_back(info.channels);
out.push_back(0);
//dwSamplesPerSec
out.push_back( (info.samplerate) % 256);
out.push_back(((info.samplerate) / 256) % 256);
out.push_back(((info.samplerate) / 65536) % 256);
out.push_back(((info.samplerate) / 16777216) % 256);
//dwAvgBytesPerSec
out.push_back( (byteRate) % 256);
out.push_back(((byteRate) / 256) % 256);
out.push_back(((byteRate) / 65536) % 256);
out.push_back(((byteRate) / 16777216) % 256);
//wBlockAlign
out.push_back(blockAlign);
out.push_back(0);
//wBitsPerSample
out.push_back(info.bitsPerSample);
out.push_back(0);
//== DATA chunk ==
//ID (4 bytes)
out.push_back('d');
out.push_back('a');
out.push_back('t');
out.push_back('a');
//chunkSize (excluding the 8 bytes for ID and chunkSize)
out.push_back( data.size() % 256);
out.push_back((data.size() / 256) % 256);
out.push_back((data.size() / 65536) % 256);
out.push_back((data.size() / 16777216) % 256);
//the data
for(size_t x = 0; x < data.size(); x++)
{
out.push_back(data[x]);
}
}
int decode(std::vector<std::vector<double> >& out, InfoWAV& info, const std::vector<unsigned char>& in)
{
std::vector<unsigned char> data;
size_t pos = 0;
//== RIFF header ==
if(in.size() < 44) return 10; //error: file too small
//"RIFF"
if(in[0] != 'R' || in[1] != 'I' || in[2] != 'F' || in[3] != 'F') return 11; //error: no RIFF
//size of the file minus 8 (= number of bytes that comes after these 4 bytes)
//not of interest, we already know the file size
//"WAVE"
if(in[8] != 'W' || in[9] != 'A' || in[10] != 'V' || in[11] != 'E') return 12; //error: no WAVE
pos += 12;
//chunks
bool format_chunk_read = false;
bool done = false;
while(!done)
{
if(pos >= in.size() - 8) return 16; //error: size too short for next block
if(in[pos] == 'f' && in[pos + 1] == 'm' && in[pos + 2] == 't' && in[pos + 3] == ' ')
{
pos += 4;
format_chunk_read = true;
int chunkSize = in[pos] + 256 * in[pos + 1] + 65536 * in[pos + 2] + 16777216 * in[pos + 3];
pos += 4;
if(pos + chunkSize >= in.size()) return 17;
if(chunkSize < 16) return 13; //fmt block must have this size
short wFormatTag = in[pos] + 256 * in[pos + 1];
pos += 2;
unsigned short wChannels = in[pos] + 256 * in[pos + 1];
pos += 2;
unsigned long dwSamplesPerSec = in[pos] + 256 * in[pos + 1] + 65536 * in[pos + 2] + 16777216 * in[pos + 3];
pos += 4;
//unsigned long dwAvgBytesPerSec = in[pos] + 256 * in[pos + 1] + 65536 * in[pos + 2] + 16777216 * in[pos + 3];
pos += 4;
//unsigned short wBlockAlign = in[pos] + 256 * in[pos + 1];
pos += 2;
unsigned short wBitsPerSample = in[pos] + 256 * in[pos + 1];
pos += 2;
//there can be two more bytes here that specify the length of extra format bytes. However this doesn't occur if wFormatTag is 1 (no compression)
if(wFormatTag != 1) return 14; //error: unsupported format
info.channels = wChannels;
info.samplerate = dwSamplesPerSec;
info.bitsPerSample = wBitsPerSample;
}
else if(in[pos] == 'd' && in[pos + 1] == 'a' && in[pos + 2] == 't' && in[pos + 3] == 'a')
{
pos += 4;
if(!format_chunk_read) return 15; //error: fmt chunk must come before this!
size_t chunkSize = in[pos] + 256 * in[pos + 1] + 65536 * in[pos + 2] + 16777216 * in[pos + 3];
pos += 4;
if(pos + chunkSize > in.size()) return 17;
data.clear();
data.insert(data.begin(), in.begin() + pos, in.begin() + pos + chunkSize);
pos += chunkSize;
}
else //unknown chunk type, skip over it
{
pos += 4;
size_t chunkSize = in[pos] + 256 * in[pos + 1] + 65536 * in[pos + 2] + 16777216 * in[pos + 3];
pos += 4;
//if(pos + chunkSize >= in.size()) return 17;
pos += chunkSize;
}
if(pos >= in.size())
{
done = true;
}
}
//convert the input data to unsigned char data with channels interleaved (= as they are, so we just convert samples in the same order)
out.clear();
out.resize(info.channels);
int bytesPerSample = info.bitsPerSample / 8;
long range = 1 << info.bitsPerSample; //the range of values for the integers of samples in the wav file
long halfrange = range / 2;
double factor = 1.0 / halfrange; //factor to convert to format where the audio is in the range -32768 - +32767
for(size_t x = 0; x < data.size(); x += bytesPerSample * info.channels)
{
for(int c = 0; c < info.channels; c++)
{
size_t index = x + c * bytesPerSample;
long v = 0;
for(int i = 0; i < bytesPerSample; i++)
{
v += (data[index + i] << (i * 8));
}
//in the wav, the value 0 is the center (zero level), values up to range / 2 are positive, values higher than that are negative (where range is (1 << info.bitsPerSample))
//now I shift this so that 0 is the lowest negative level, range / 2 the zero level and range - 1 the maximum positive level
if(v < halfrange)
{
v += halfrange;
}
else
{
v -= halfrange;
}
double vn = (v * factor) - 1.0;
out[c].push_back(vn);
}
}
return 0;
}
void encode(std::vector<unsigned char>& out, const std::vector<std::vector<int> >& in, const InfoWAV& info)
{
std::vector<std::vector<double> > d(in.size());
for(size_t i = 0; i < in.size(); i++)
{
d[i].resize(in[i].size());
for(size_t j = 0; j < in[i].size(); j++)
{
d[i][j] = in[i][j] / 32768.0;
}
}
encode(out, d, info);
}
int decode(std::vector<std::vector<int> >& out, InfoWAV& info, const std::vector<unsigned char>& in)
{
std::vector<std::vector<double> > d;
int result = decode(d, info, in);
if(result == 0)
{
out.resize(d.size());
for(size_t i = 0; i < d.size(); i++)
{
out[i].resize(d[i].size());
for(size_t j = 0; j < d[i].size(); j++)
{
out[i][j] = int(d[i][j] * 32768.0);
}
}
}
return result;
}
} //namespace LodeWAV