forked from ibsh/libKeyFinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudiodata.cpp
executable file
·224 lines (190 loc) · 7.17 KB
/
audiodata.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
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
/*************************************************************************
Copyright 2011-2013 Ibrahim Sha'ath
This file is part of LibKeyFinder.
LibKeyFinder is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LibKeyFinder is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LibKeyFinder. If not, see <http://www.gnu.org/licenses/>.
*************************************************************************/
#include "audiodata.h"
namespace KeyFinder {
AudioData::AudioData(): samples(0), channels(0), frameRate(0) { }
unsigned int AudioData::getChannels() const {
return channels;
}
void AudioData::setChannels(unsigned int newChannels) {
if (newChannels < 1) throw Exception("Channels must be > 0");
channels = newChannels;
}
unsigned int AudioData::getFrameRate() const {
return frameRate;
}
void AudioData::setFrameRate(unsigned int newFrameRate) {
if (newFrameRate < 1) throw Exception("Frame rate must be > 0");
frameRate = newFrameRate;
}
void AudioData::append(const AudioData& that) {
if (channels == 0 && frameRate == 0) {
channels = that.channels;
frameRate = that.frameRate;
}
if (that.channels != channels)
throw Exception("Cannot append audio data with a different number of channels");
if (that.frameRate != frameRate)
throw Exception("Cannot append audio data with a different frame rate");
unsigned int oldSampleCount = getSampleCount();
addToSampleCount(that.getSampleCount());
for (unsigned int s = 0; s < that.getSampleCount(); s++)
setSample(oldSampleCount + s, that.getSample(s));
}
// get sample by absolute index
float AudioData::getSample(unsigned int index) const {
if (index >= getSampleCount()) {
std::ostringstream ss;
ss << "Cannot get out-of-bounds sample (" << index << "/" << getSampleCount() << ")";
throw Exception(ss.str().c_str());
}
return samples[index];
}
// get sample by frame and channel
float AudioData::getSampleByFrame(unsigned int frame, unsigned int channel) const {
if (frame >= getFrameCount()) {
std::ostringstream ss;
ss << "Cannot get out-of-bounds frame (" << frame << "/" << getFrameCount() << ")";
throw Exception(ss.str().c_str());
}
if (channel >= channels) {
std::ostringstream ss;
ss << "Cannot get out-of-bounds channel (" << channel << "/" << channels << ")";
throw Exception(ss.str().c_str());
}
return getSample(frame * channels + channel);
}
// set sample by absolute index
void AudioData::setSample(unsigned int index, float value) {
if (index >= getSampleCount()) {
std::ostringstream ss;
ss << "Cannot set out-of-bounds sample (" << index << "/" << getSampleCount() << ")";
throw Exception(ss.str().c_str());
}
if (!boost::math::isfinite(value)) {
throw Exception("Cannot set sample to NaN");
}
samples[index] = value;
}
// set sample by frame and channel
void AudioData::setSampleByFrame(unsigned int frame, unsigned int channel, float value) {
if (frame >= getFrameCount()) {
std::ostringstream ss;
ss << "Cannot set out-of-bounds frame (" << frame << "/" << getFrameCount() << ")";
throw Exception(ss.str().c_str());
}
if (channel >= channels) {
std::ostringstream ss;
ss << "Cannot set out-of-bounds channel (" << channel << "/" << channels << ")";
throw Exception(ss.str().c_str());
}
setSample(frame * channels + channel, value);
}
void AudioData::addToSampleCount(unsigned int newSamples) {
samples.resize(getSampleCount() + newSamples);
}
void AudioData::addToFrameCount(unsigned int newFrames) {
if (channels < 1) throw Exception("Channels must be > 0");
addToSampleCount(newFrames * channels);
}
unsigned int AudioData::getSampleCount() const {
return samples.size();
}
unsigned int AudioData::getFrameCount() const {
if (channels < 1) throw Exception("Channels must be > 0");
return getSampleCount() / channels;
}
void AudioData::reduceToMono() {
if (channels == 1) return;
resetIterators();
while (readIteratorWithinUpperBound()) {
float mean = 0.0;
for (unsigned int c = 0; c < channels; c++) {
mean += getSampleAtReadIterator() / channels;
advanceReadIterator();
}
setSampleAtWriteIterator(mean);
advanceWriteIterator();
}
samples.resize(getSampleCount() / channels);
channels = 1;
}
/*
* Strictly to be applied AFTER low pass filtering
*/
void AudioData::downsample(unsigned int factor, bool shortcut) {
if (factor == 1) return;
if (channels > 1) throw Exception("Apply to monophonic only");
resetIterators();
while (readIteratorWithinUpperBound()) {
float mean = 0.0;
if (shortcut) {
mean = getSampleAtReadIterator();
advanceReadIterator(factor);
} else {
for (unsigned int s = 0; s < factor; s++) {
if (readIteratorWithinUpperBound()) {
mean += getSampleAtReadIterator() / (float)factor;
advanceReadIterator();
}
}
}
setSampleAtWriteIterator(mean);
advanceWriteIterator();
}
setFrameRate(getFrameRate() / factor);
samples.resize(ceil((float)getSampleCount() / (float)factor));
}
void AudioData::discardFramesFromFront(unsigned int discardFrameCount) {
if (discardFrameCount > getFrameCount()) {
std::ostringstream ss;
ss << "Cannot discard " << discardFrameCount << " frames of " << getFrameCount();
throw Exception(ss.str().c_str());
}
unsigned int discardSampleCount = discardFrameCount * channels;
samples.erase(samples.begin(), samples.begin() + discardSampleCount);
}
void AudioData::discardFramesFromBack(unsigned int discardFrameCount) {
if (discardFrameCount > getFrameCount()) {
std::ostringstream ss;
ss << "Cannot discard " << discardFrameCount << " frames of " << getFrameCount();
throw Exception(ss.str().c_str());
}
unsigned int discardSampleCount = discardFrameCount * channels;
samples.resize(getSampleCount() - discardSampleCount);
}
void AudioData::resetIterators() {
readIterator = samples.begin();
writeIterator = samples.begin();
}
bool AudioData::readIteratorWithinUpperBound() const {
return (readIterator < samples.end());
}
bool AudioData::writeIteratorWithinUpperBound() const {
return (writeIterator < samples.end());
}
void AudioData::advanceReadIterator(unsigned int by) {
readIterator += by;
}
void AudioData::advanceWriteIterator(unsigned int by) {
writeIterator += by;
}
float AudioData::getSampleAtReadIterator() const {
return *readIterator;
}
void AudioData::setSampleAtWriteIterator(float value) {
*writeIterator = value;
}
}