-
Notifications
You must be signed in to change notification settings - Fork 1
/
wavHeader.cpp
148 lines (95 loc) · 2.53 KB
/
wavHeader.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
/////////////////////////////////////////////////////////
//
// Name: wavHeader.cpp
//
// Copyright: Igor Baklykov (c) 2017
//
// Author: Igor Baklykov
//
// Date: 18.12.2017 16:49
//
// Description: WAVE file header class
// definition
//
/////////////////////////////////////////////////////////
#include "wavHeader.hpp"
// Default c-tor
wavHeader::wavHeader() {
sampleRate = 48000;
dataSize = 0;
numOfChannels = 1;
bitsPerSample = 16;
}
// Copy c-tor
wavHeader::wavHeader(const wavHeader &_w) {
if (&_w != this) {
sampleRate = _w.sampleRate;
dataSize = _w.dataSize;
numOfChannels = _w.numOfChannels;
bitsPerSample = _w.bitsPerSample;
}
}
// Get sample rate
unsigned int wavHeader::getSampleRate() const {
return sampleRate;
}
// Get data size
unsigned int wavHeader::getDataSize() const {
return dataSize;
}
// Get number of channels
unsigned short wavHeader::getNumOfChannels() const {
return numOfChannels;
}
// Get bits per sample
unsigned short wavHeader::getBitsPerSample() const {
return bitsPerSample;
}
// Get byte rate
unsigned int wavHeader::getByteRate() const {
return sampleRate * getBlockAlign();
}
// Get block align
unsigned short wavHeader::getBlockAlign() const {
return numOfChannels * bitsPerSample / 8;
}
// Set sample rate
void wavHeader::setSampleRate(const unsigned int &_sampleRate) {
sampleRate = _sampleRate;
}
// Set data size
void wavHeader::setDataSize(const unsigned int &_dataSize) {
dataSize = _dataSize;
}
// Set number of channels
void wavHeader::setNumOfChannels(const unsigned short &_numOfChannels) {
numOfChannels = _numOfChannels;
}
// Set bits per sample
void wavHeader::setBitsPerSample(const unsigned short &_bitsPerSample) {
bitsPerSample = _bitsPerSample;
}
// Write header
bool wavHeader::write(const char *_buffer) const {
*(unsigned int*)&_buffer[0] = WAV_RIFF_CHUNK_ID;
*(unsigned int*)&_buffer[4] = 36 + dataSize;
*(unsigned int*)&_buffer[8] = WAV_WAVE_CHUNK_ID;
*(unsigned int*)&_buffer[12] = WAV_FMT__SUBCHUNK_ID;
*(unsigned int*)&_buffer[16] = 16;
*(unsigned short*)&_buffer[20] = 1;
*(unsigned short*)&_buffer[22] = numOfChannels;
*(unsigned int*)&_buffer[24] = sampleRate;
*(unsigned int*)&_buffer[28] = getByteRate();
*(unsigned short*)&_buffer[32] = getBlockAlign();
*(unsigned short*)&_buffer[34] = bitsPerSample;
*(unsigned int*)&_buffer[36] = WAV_DATA_SUBCHUNK_ID;
*(unsigned int*)&_buffer[40] = dataSize;
return true;
}
// D-tor
wavHeader::~wavHeader() {
sampleRate = 0;
dataSize = 0;
numOfChannels = 0;
bitsPerSample = 0;
}