forked from markondej/fm_transmitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wave_reader.cpp
117 lines (98 loc) · 3.12 KB
/
wave_reader.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
#include "wave_reader.h"
#include <string.h>
#include <exception>
#include <iostream>
#include <fstream>
#include <math.h>
WaveReader::WaveReader(std::string filename)
{
std::vector<char> buffer;
unsigned int length;
int error;
std::ifstream is(filename.c_str(), std::ifstream::binary);
if (!is) {
std::cout << "Error: cannot open '" << filename << "', file does not exist" << std::endl;
throw std::exception();
}
is.seekg(0, is.end);
length = is.tellg();
is.seekg(0, is.beg);
if (length < sizeof(PCMWaveHeader)) {
std::cout << "Error: data corrupted" << std::endl;
throw std::exception();
}
buffer.resize(length);
is.read(&buffer[0], length);
is.close();
memcpy(&header, &buffer[0], sizeof(PCMWaveHeader));
if ((error = checkDataFormat(&header)) > 0) {
std::cout << "Error: unsupported data format (" << error << ")" << std::endl;
throw std::exception();
}
if (length < sizeof(PCMWaveHeader) + header.subchunk2Size) {
std::cout << "Error: data corrupted" << std::endl;
throw std::exception();
}
data.resize(header.subchunk2Size);
memcpy(&data[0], &buffer[sizeof(PCMWaveHeader)], header.subchunk2Size);
}
WaveReader::~WaveReader()
{
}
int WaveReader::checkDataFormat(PCMWaveHeader *header)
{
int error = 0;
if (std::string(header->chunkID, 4) != std::string("RIFF")) {
error |= 0x01;
}
if (std::string(header->format, 4) != std::string("WAVE")) {
error |= (0x01 << 1);
}
if (std::string(header->subchunk1ID, 4) != std::string("fmt ")) {
error |= (0x01 << 2);
}
if (header->subchunk1Size != 16) {
error |= (0x01 << 3);
}
if (header->audioFormat != WAVE_FORMAT_PCM) {
error |= (0x01 << 4);
}
if (header->byteRate != (header->bitsPerSample >> 3) * header->channels * header->sampleRate) {
error |= (0x01 << 5);
}
if (header->blockAlign != (header->bitsPerSample >> 3) * header->channels) {
error |= (0x01 << 6);
}
if (((header->bitsPerSample >> 3) != 1) && ((header->bitsPerSample >> 3) != 2)) {
error |= (0x01 << 7);
}
if (std::string(header->subchunk2ID, 4) != std::string("data")) {
error |= (0x01 << 8);
}
return error;
}
std::vector<float> *WaveReader::getSamples()
{
std::vector<float> *samples = new std::vector<float>();
unsigned char frameSize = header.channels * (header.bitsPerSample >> 3);
unsigned int framesCount = header.subchunk2Size / frameSize;
unsigned int frameOffset = 0, dataOffset;
while (frameOffset < framesCount) {
dataOffset = frameOffset * frameSize;
if (header.channels != 1) {
// TODO: Stereo to mono conversion
} else {
if (header.bitsPerSample != 8) {
samples->push_back((signed char)data[dataOffset + 1] / (float)0x80);
} else {
samples->push_back(data[dataOffset] / (float)0x80 - 1.0f);
}
}
frameOffset++;
}
return samples;
}
PCMWaveHeader *WaveReader::getHeader()
{
return &header;
}