-
Notifications
You must be signed in to change notification settings - Fork 4
/
ImageToSoundscape.cpp
370 lines (330 loc) · 8.22 KB
/
ImageToSoundscape.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// raspivoice
// Based on:
// http://www.seeingwithsound.com/hificode_OpenCV.cpp
// C program for soundscape generation. (C) P.B.L. Meijer 1996
// hificode.c modified for camera input using OpenCV. (C) 2013
// Last update: December 29, 2014; released under the Creative
// Commons Attribution 4.0 International License (CC BY 4.0),
// see http://www.seeingwithsound.com/im2sound.htm for details
// License: https://creativecommons.org/licenses/by/4.0/
#include <cstdlib>
#include <cinttypes>
#include <cmath>
#include <stdexcept>
#include "ImageToSoundscape.h"
#define TwoPi 6.283185307179586476925287
ImageToSoundscapeConverter::ImageToSoundscapeConverter(int rows, int columns, double freq_lowest, double freq_highest,
int sample_freq_Hz, double total_time_s, bool use_exponential,
bool use_stereo, bool use_delay, bool use_fade,
bool use_diffraction, bool use_bspline, float speed_of_sound_m_s,
float acoustical_size_of_head_m) :
rows(rows),
columns(columns), freq_lowest(freq_lowest),
freq_highest(freq_highest),
sample_freq_Hz(sample_freq_Hz),
total_time_s(total_time_s),
use_exponential(use_exponential),
use_stereo(use_stereo),
use_delay(use_delay),
use_fade(use_fade),
use_diffraction(use_diffraction),
use_bspline(use_bspline),
speed_of_sound_m_s(speed_of_sound_m_s),
acoustical_size_of_head_m(acoustical_size_of_head_m),
sampleCount(2L * (uint32_t)(0.5 * sample_freq_Hz * total_time_s)),
samplesPerColumn((uint32_t)(sampleCount / columns)),
timePerSample_s(1.0 / sample_freq_Hz),
scale(0.5 / sqrt((float)rows)),
audioData(0, sample_freq_Hz, sampleCount, use_stereo),
omega(std::vector<float>(rows)),
phi0(std::vector<float>(rows)),
waveformCacheLeftChannel(std::vector<float>(sampleCount*rows)),
waveformCacheRightChannel(std::vector<float>(sampleCount*rows))
{
// Set lin|exp (0|1) frequency distribution and random initial phase
if (use_exponential)
{
for (int i = 0; i < rows; i++)
{
omega[i] = TwoPi * freq_lowest * pow(1.0 * freq_highest / freq_lowest, 1.0 * i / (rows - 1));
}
}
else
{
for (int i = 0; i < rows; i++)
{
omega[i] = TwoPi * freq_lowest + TwoPi * (freq_highest - freq_lowest) * i / (rows - 1);
}
}
for (int i = 0; i<rows; i++)
{
phi0[i] = TwoPi * rnd();
}
initWaveformCacheStereo();
}
float ImageToSoundscapeConverter::rnd()
{
static uint32_t ir = 0L;
uint32_t ia = 9301, ic = 49297, im = 233280;
ir = (ir*ia + ic) % im;
return ir / (1.0 * im);
}
void ImageToSoundscapeConverter::Process(const std::vector<float> &image)
{
if (!use_stereo)
{
processMono(image);
}
else
{
processStereo(image);
}
}
void ImageToSoundscapeConverter::processMono(const std::vector<float> &image)
{
throw std::runtime_error("Mono audio not implemented");
/*
float tau1 = 0.5 / omega[rows - 1];
float tau2 = 0.25 * tau1*tau1;
float y = yl = yr = z = zl = zr = 0.0;
while (int sample < sampleCount)
{
if (use_bspline)
{
q = 1.0 * (sample%samplesPerColumn) / (samplesPerColumn - 1);
q2 = 0.5*q*q;
}
j = sample / samplesPerColumn;
if (j > columns - 1)
{
j = columns - 1;
}
float s = 0.0;
t = sample * timePerSample_s;
if (sample < sample_count / (5 * columns))
{
s = (2.0*rnd() - 1.0) / scale; // "click"
}
else
{
for (int i = 0; i < rows; i++)
{
float a;
if (use_bspline)
{
// Quadratic B-spline for smooth C1 time window
if (j == 0)
{
a = (1.0 - q2)*image[j][i] + q2*image[j+1][i];
}
else if (j == columns - 1)
{
a = (q2 - q + 0.5)*image[j-1][i] + (0.5 + q - q2)*image[j][i];
}
else
{
a = (q2 - q + 0.5)*image[j-1][i] + (0.5 + q - q*q)*image[j][i] + q2*image[j+1][i];
}
}
else
{
a = image[j][i]; // Rectangular time window
}
s += a * sin(omega[i] * t + phi0[i]);
}
}
yp = y;
y = tau1 / timePerSample_s + tau2 / (timePerSample_s*timePerSample_s);
y = (s + y * yp + tau2 / timePerSample_s * z) / (1.0 + y);
z = (y - yp) / timePerSample_s;
l = sso + 0.5 + scale * ssm * y; // y = 2nd order filtered s
if (l >= sso - 1 + ssm)
{
l = sso - 1 + ssm;
}
if (l < sso - ssm)
{
l = sso - ssm;
}
ss = (unsigned int)l;
wi(ss);
sample++;
}
*/
}
void ImageToSoundscapeConverter::processStereo(const std::vector<float> &image)
{
float tau1 = 0.5 / omega[rows - 1];
float tau2 = 0.25 * tau1*tau1;
float yl = 0.0, yr = 0.0;
float zl = 0.0, zr = 0.0;
for (int sample = 0; sample < sampleCount; sample++)
{
float q, q2, f1, f2;
if (use_bspline)
{
q = 1.0 * (sample % samplesPerColumn) / (samplesPerColumn - 1);
q2 = 0.5 * q * q;
f1 = (q2 - q + 0.5);
f2 = (0.5 + q - q*q);
}
int j = sample / samplesPerColumn;
if (j > columns - 1)
{
j = columns - 1;
}
float r = 1.0 * sample / (sampleCount - 1); // Binaural attenuation/delay parameter
float theta = (r - 0.5) * TwoPi / 3;
float x = 0.5 * acoustical_size_of_head_m * (theta + sin(theta));
float tl = sample * timePerSample_s;
float tr = tl;
if (use_delay)
{
tr += x / speed_of_sound_m_s; // Time delay model
}
x = fabs(x);
float sl = 0.0, sr = 0.0;
const float *im1, *im2, *im3;
if (j > 0)
{
im1 = &image[IDX2D(0, j - 1)];
}
im2 = &image[IDX2D(0, j)];
if (j < columns - 1)
{
im3 = &image[IDX2D(0, j + 1)];
}
for (int i = 0; i < rows; i++)
{
float a;
if (use_bspline)
{
if (j == 0)
{
a = (1.0 - q2)*im2[i] + q2*im3[i];
}
else if (j == columns - 1)
{
a = f1*im1[i] + f2*im2[i];
}
else
{
a = f1*im1[i] + f2*im2[i] + q2*im3[i];
}
}
else
{
a = im2[i];
}
sl += a * waveformCacheLeftChannel[(sample * rows) + i];
sr += a * waveformCacheRightChannel[(sample * rows) + i];
}
if (sample < sampleCount / (5 * columns))
{
sl = (2.0*rnd() - 1.0) / scale; // Left "click"
}
if (tl < 0.0)
{
sl = 0.0;
}
if (tr < 0.0)
{
sr = 0.0;
}
float ypl = yl;
yl = tau1 / timePerSample_s + tau2 / (timePerSample_s*timePerSample_s);
yl = (sl + yl * ypl + tau2 / timePerSample_s * zl) / (1.0 + yl);
zl = (yl - ypl) / timePerSample_s;
float ypr = yr;
yr = tau1 / timePerSample_s + tau2 / (timePerSample_s*timePerSample_s);
yr = (sr + yr * ypr + tau2 / timePerSample_s * zr) / (1.0 + yr);
zr = (yr - ypr) / timePerSample_s;
uint16_t* sampleBuffer = audioData.Data();
int32_t l = 0.5 + scale * 32768.0 * yl;
if (l > 32767)
{
l = 32767;
} else if (l < -32768)
{
l = -32768;
}
sampleBuffer[2 * sample] = (uint16_t)l;
l = 0.5 + scale * 32768.0 * yr;
if (l > 32767)
{
l = 32767;
}
else if (l < -32768)
{
l = -32768;
}
sampleBuffer[2 * sample + 1] = (uint16_t)l;
}
}
void ImageToSoundscapeConverter::initWaveformCacheStereo()
{
//waveformcache
float tau1 = 0.5 / omega[rows - 1];
float tau2 = 0.25 * tau1*tau1;
float q, q2;
float yl = 0.0, yr = 0.0;
float zl = 0.0, zr = 0.0;
for (int sample = 0; sample < sampleCount; sample++)
{
if (use_bspline)
{
q = 1.0 * (sample % samplesPerColumn) / (samplesPerColumn - 1);
q2 = 0.5 * q * q;
}
int j = sample / samplesPerColumn;
if (j > columns - 1)
{
j = columns - 1;
}
float r = 1.0 * sample / (sampleCount - 1); // Binaural attenuation/delay parameter
float theta = (r - 0.5) * TwoPi / 3;
float x = 0.5 * acoustical_size_of_head_m * (theta + sin(theta));
float tl = sample * timePerSample_s;
float tr = tl;
if (use_delay)
{
tr += x / speed_of_sound_m_s; // Time delay model
}
x = fabs(x);
float hrtfl = 1.0, hrtfr = 1.0;
for (int i = 0; i < rows; i++)
{
if (use_diffraction)
{
// First order frequency-dependent azimuth diffraction model
float hrtf;
if (TwoPi*speed_of_sound_m_s / omega[i] > x)
{
hrtf = 1.0;
}
else
{
hrtf = TwoPi*speed_of_sound_m_s / (x*omega[i]);
}
if (theta < 0.0)
{
hrtfl = 1.0;
hrtfr = hrtf;
}
else
{
hrtfl = hrtf;
hrtfr = 1.0;
}
}
if (use_fade)
{
// Simple frequency-independent relative fade model
hrtfl *= (1.0 - 0.7*r);
hrtfr *= (0.3 + 0.7*r);
}
waveformCacheLeftChannel[(sample * rows) + i] = hrtfl * sin(omega[i] * tl + phi0[i]);
waveformCacheRightChannel[(sample * rows) + i] = hrtfr * sin(omega[i] * tr + phi0[i]);
}
}
}