-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConvoluter.cpp
More file actions
300 lines (231 loc) · 7.06 KB
/
Convoluter.cpp
File metadata and controls
300 lines (231 loc) · 7.06 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
/*
==============================================================================
Convoluter.cpp
Created: 22 Apr 2021 11:17:04am
Author: Eric
==============================================================================
*/
#include "Convoluter.h"
Convoluter::Convoluter() {
currSamplesPerBlock = -1;
bufferSize = -1;
inputBuffer = juce::AudioBuffer<float>();
outputBuffer = juce::AudioBuffer<float>();
convolutedSignal = juce::AudioBuffer<float>();;
overflowStorage = juce::AudioBuffer<float>(2, 200);
inputPos = 0;
outputPos = 0;
elevation = 0;
azimuth = 0;
load_hrir_l();
load_hrir_r();
}
Convoluter::~Convoluter() {
}
void Convoluter::setSamplesPerBlock(int samplesPerBlock) {
if (samplesPerBlock != currSamplesPerBlock) {
bufferSize = samplesPerBlock * 10;
inputBuffer = juce::AudioBuffer<float>(2, bufferSize);
outputBuffer = juce::AudioBuffer<float>(2, bufferSize);
convolutedSignal = juce::AudioBuffer<float>(2, bufferSize + 200);;
overflowStorage = juce::AudioBuffer<float>(2, 200);
}
}
void Convoluter::readInput(juce::AudioBuffer<float>& buffer) {
auto numInputChannels = buffer.getNumChannels();
//for each channel add the current block to the input buffer
for (int channel = 0; channel < numInputChannels; channel++) {
auto* writePointer = inputBuffer.getWritePointer(channel, inputPos);
auto* readPointer = buffer.getReadPointer(channel, 0);
for (int i = 0; i < buffer.getNumSamples(); i++) {
writePointer[i] = readPointer[i];
}
}
inputPos += buffer.getNumSamples();
//if the input buffer is full, convolute the signal and reset the input buffer
if (inputPos + 1 >= bufferSize) {
convolute();
inputPos = 0;
inputBuffer.clear();
}
}
void Convoluter::convolute() {
int num_conv = bufferSize + overflowSize ;
int i, j, k;
float temp;
/*
* for each channel, convolute the signal based on the current azimuth and elevation
* and add the samples from the overflow storage to the begining of the covoluted signal
*/
for (int channel = 0; channel < convolutedSignal.getNumChannels(); channel++) {
auto* writePointer = convolutedSignal.getWritePointer(channel);
auto* readPointer = inputBuffer.getReadPointer(channel);
float* hrtf;
if (channel == 0) {
hrtf =
get_hrir_l(
closest_azimuth_index(correctAzimuth(azimuth)),
closest_elevation_index(correctElevation(elevation, azimuth))
);
}
else if (channel == 1) {
hrtf =
get_hrir_r(
closest_azimuth_index(correctAzimuth(azimuth)),
closest_elevation_index(correctElevation(elevation, azimuth))
);
}
for (i = 0; i < num_conv; i++) {
k = i;
temp = 0.0;
//for each sample in filter
for (j = 0; j < 200; j++) {
if (k >= 0 && k < bufferSize) {
temp = temp + (readPointer[k] * hrtf[j]);
}
k--;
writePointer[i] = temp;
}
}
//add the overflow storage to the begining of the convoluted signal
auto* overflowPointer = overflowStorage.getReadPointer(channel);
for (int z = 0; z < 200; z++) {
writePointer[z] += overflowPointer[z];
}
}
overflowStorage.clear();
/*
* push convoluted signal to the output buffer
* generate the overflow storage from the convoluted signal
*/
for (int channel = 0; channel < convolutedSignal.getNumChannels(); channel++) {
auto* readPointer = convolutedSignal.getReadPointer(channel);
auto* readPointerOverflow = convolutedSignal.getReadPointer(channel, bufferSize + 1);
outputBuffer.copyFrom(channel, 0, readPointer, bufferSize);
overflowStorage.copyFrom(channel, 0, readPointerOverflow, overflowSize);
}
convolutedSignal.clear();
}
void Convoluter::applyOutput(juce::AudioBuffer<float>& buffer) {
auto numInputChannels = buffer.getNumChannels();
for (int channel = 0; channel < numInputChannels; channel++) {
auto* writePointer = buffer.getWritePointer(channel);
auto* readPointer = outputBuffer.getReadPointer(channel, outputPos);
for (int i = 0; i < buffer.getNumSamples(); i++) {
writePointer[i] = readPointer[i];
//writePointer[i] = 1.0f / (float)i;
}
}
outputPos += buffer.getNumSamples();
//if the output buffer at the end, reset the output buffer
if (outputPos + 1 >= bufferSize) {
outputPos = 0;
outputBuffer.clear();
}
}
float Convoluter::correctAzimuth(float azimuth) {
//this function takes in an angle from 0-360 and spits out the azimuth translated to what our HRTF can use
/*Structure:*/
if (azimuth >= 270.0 && azimuth <= 360.0) {
return -1.0 * (360.0 - azimuth);
}
else if (azimuth >= 0.0 && azimuth <= 90.0) {
return azimuth;
}
else if (azimuth > 90.0 && azimuth <= 180.0) {
return (180 - azimuth);
}
else if (azimuth > 180.0 && azimuth < 270.0) {
return -1.0 * (azimuth - 180.0);
}
else
return 0.0;
}
float Convoluter::correctElevation(float elevation, float azimuth) {
//this function takes in an angle from -45 to 90 and spits out the correct elevation based on the azimuth
float elevbuff = 0.0f;
if (azimuth >= 270.0 && azimuth <= 360.0) {
return elevation;
}
else if (azimuth >= 0.0 && azimuth <= 90.0) {
return elevation;
}
else if (azimuth > 90.0 && azimuth < 270.0) {
elevbuff = elevation + (180 - 2 * elevation);
if (elevbuff > 230.625)
elevbuff = 230.625;
return elevbuff;
}
else
return 0.0;
}
int Convoluter::closest_elevation_index(float elevation) {
float min_diff = std::numeric_limits<float>::max();
int index_found = -1;
for (int i = 0; i < 50; i++) {
float temp = abs(elevation - elevation_values[i]);
if (temp < min_diff) {
min_diff = temp;
index_found = i;
}
}
return index_found;
}
int Convoluter::closest_azimuth_index(float azimuth) {
float min_diff = std::numeric_limits<float>::max();
int index_found = -1;
for (int i = 0; i < 25; i++) {
float temp = abs(azimuth - azimuth_values[i]);
if (temp < min_diff) {
min_diff = temp;
index_found = i;
}
}
return index_found;
}
int Convoluter::load_hrir_l() {
juce::File file = DATA_DIR.getChildFile("SoundStage/hrir_l.txt");
juce::String s = file.loadFileAsString();
if (!file.exists()) {
return -1;
}
int pos = 0;
juce::StringArray stringArray;
file.readLines(stringArray);
for (int i = 0; i < 25; i++) {
for (int j = 0; j < 50; j++) {
for (int k = 0; k < 200; k++) {
hrir_l[i][j][k] = stringArray[(i * 50 * 200) + (j * 200) + k].getFloatValue();
}
}
}
return 0;
}
int Convoluter::load_hrir_r() {
juce::File file = DATA_DIR.getChildFile("SoundStage/hrir_r.txt");
juce::String s = file.loadFileAsString();
if (!file.exists()) {
juce::Logger::outputDebugString("failed to load right");
juce::Logger::outputDebugString(juce::String(file.getFullPathName()));
return -1;
}
int pos = 0;
juce::StringArray stringArray;
file.readLines(stringArray);
for (int i = 0; i < 25; i++) {
for (int j = 0; j < 50; j++) {
for (int k = 0; k < 200; k++) {
hrir_r[i][j][k] = stringArray[(i * 50 * 200) + (j * 200) + k].getFloatValue();
}
}
}
return 0;
}
float* Convoluter::get_hrir_l(int az, int elevation) {
float* value = hrir_l[az][elevation];
return value;
}
float* Convoluter::get_hrir_r(int az, int elevation) {
float* value = hrir_r[az][elevation];
return value;
}