Avoid static constexpr for arrays iterated over at run-time
[openal-soft.git] / utils / sofa-info.cpp
blobbc5b709a958e769fd2f9837938ee376774fd571d
1 /*
2 * SOFA info utility for inspecting SOFA file metrics and determining HRTF
3 * utility compatible layouts.
5 * Copyright (C) 2018-2019 Christopher Fitzgerald
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Or visit: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
24 #include <stdio.h>
26 #include <array>
27 #include <cmath>
28 #include <memory>
29 #include <vector>
31 #include <mysofa.h>
33 #include "win_main_utf8.h"
36 using uint = unsigned int;
37 using double3 = std::array<double,3>;
39 struct MySofaDeleter {
40 void operator()(MYSOFA_HRTF *sofa) { mysofa_free(sofa); }
42 using MySofaHrtfPtr = std::unique_ptr<MYSOFA_HRTF,MySofaDeleter>;
44 // Per-field measurement info.
45 struct HrirFdT {
46 double mDistance{0.0};
47 uint mEvCount{0u};
48 uint mEvStart{0u};
49 std::vector<uint> mAzCounts;
52 static const char *SofaErrorStr(int err)
54 switch(err)
56 case MYSOFA_OK: return "OK";
57 case MYSOFA_INVALID_FORMAT: return "Invalid format";
58 case MYSOFA_UNSUPPORTED_FORMAT: return "Unsupported format";
59 case MYSOFA_INTERNAL_ERROR: return "Internal error";
60 case MYSOFA_NO_MEMORY: return "Out of memory";
61 case MYSOFA_READ_ERROR: return "Read error";
63 return "Unknown";
66 static void PrintSofaAttributes(const char *prefix, struct MYSOFA_ATTRIBUTE *attribute)
68 while(attribute)
70 fprintf(stdout, "%s.%s: %s\n", prefix, attribute->name, attribute->value);
71 attribute = attribute->next;
75 static void PrintSofaArray(const char *prefix, struct MYSOFA_ARRAY *array)
77 PrintSofaAttributes(prefix, array->attributes);
79 for(uint i{0u};i < array->elements;i++)
80 fprintf(stdout, "%s[%u]: %.6f\n", prefix, i, array->values[i]);
83 /* Produces a sorted array of unique elements from a particular axis of the
84 * triplets array. The filters are used to focus on particular coordinates
85 * of other axes as necessary. The epsilons are used to constrain the
86 * equality of unique elements.
88 static uint GetUniquelySortedElems(const uint m, const double3 *aers, const uint axis,
89 const double *const (&filters)[3], const double (&epsilons)[3], double *elems)
91 uint count{0u};
92 for(uint i{0u};i < m;++i)
94 const double elem{aers[i][axis]};
96 uint j;
97 for(j = 0;j < 3;j++)
99 if(filters[j] && std::fabs(aers[i][j] - *filters[j]) > epsilons[j])
100 break;
102 if(j < 3)
103 continue;
105 for(j = 0;j < count;j++)
107 const double delta{elem - elems[j]};
109 if(delta > epsilons[axis])
110 continue;
112 if(delta >= -epsilons[axis])
113 break;
115 for(uint k{count};k > j;k--)
116 elems[k] = elems[k - 1];
118 elems[j] = elem;
119 count++;
120 break;
123 if(j >= count)
124 elems[count++] = elem;
127 return count;
130 /* Given a list of elements, this will produce the smallest step size that
131 * can uniformly cover a fair portion of the list. Ideally this will be over
132 * half, but in degenerate cases this can fall to a minimum of 5 (the lower
133 * limit on elevations necessary to build a layout).
135 static double GetUniformStepSize(const double epsilon, const uint m, const double *elems)
137 auto steps = std::vector<double>(m, 0.0);
138 auto counts = std::vector<uint>(m, 0u);
139 uint count{0u};
141 for(uint stride{1u};stride < m/2;stride++)
143 for(uint i{0u};i < m-stride;i++)
145 const double step{elems[i + stride] - elems[i]};
147 uint j;
148 for(j = 0;j < count;j++)
150 if(std::fabs(step - steps[j]) < epsilon)
152 counts[j]++;
153 break;
157 if(j >= count)
159 steps[j] = step;
160 counts[j] = 1;
161 count++;
165 for(uint i{1u};i < count;i++)
167 if(counts[i] > counts[0])
169 steps[0] = steps[i];
170 counts[0] = counts[i];
174 count = 1;
176 if(counts[0] > m/2)
177 break;
180 if(counts[0] > 255)
182 uint i{2u};
183 while(counts[0]/i > 255 && (counts[0]%i) != 0)
184 ++i;
185 counts[0] /= i;
186 steps[0] *= i;
188 if(counts[0] > 5)
189 return steps[0];
190 return 0.0;
193 /* Attempts to produce a compatible layout. Most data sets tend to be
194 * uniform and have the same major axis as used by OpenAL Soft's HRTF model.
195 * This will remove outliers and produce a maximally dense layout when
196 * possible. Those sets that contain purely random measurements or use
197 * different major axes will fail.
199 static void PrintCompatibleLayout(const uint m, const float *xyzs)
201 auto aers = std::vector<double3>(m, double3{});
202 auto elems = std::vector<double>(m, {});
204 fprintf(stdout, "\n");
206 for(uint i{0u};i < m;++i)
208 float aer[3]{xyzs[i*3], xyzs[i*3 + 1], xyzs[i*3 + 2]};
209 mysofa_c2s(&aer[0]);
210 aers[i][0] = aer[0];
211 aers[i][1] = aer[1];
212 aers[i][2] = aer[2];
215 uint fdCount{GetUniquelySortedElems(m, aers.data(), 2, { nullptr, nullptr, nullptr },
216 { 0.1, 0.1, 0.001 }, elems.data())};
217 if(fdCount > (m / 3))
219 fprintf(stdout, "Incompatible layout (inumerable radii).\n");
220 return;
223 std::vector<HrirFdT> fds(fdCount);
224 for(uint fi{0u};fi < fdCount;fi++)
225 fds[fi].mDistance = elems[fi];
227 for(uint fi{0u};fi < fdCount;fi++)
229 const double dist{fds[fi].mDistance};
230 uint evCount{GetUniquelySortedElems(m, aers.data(), 1, { nullptr, nullptr, &dist },
231 { 0.1, 0.1, 0.001 }, elems.data())};
233 if(evCount > (m / 3))
235 fprintf(stdout, "Incompatible layout (innumerable elevations).\n");
236 return;
239 double step{GetUniformStepSize(0.1, evCount, elems.data())};
240 if(step <= 0.0)
242 fprintf(stdout, "Incompatible layout (non-uniform elevations).\n");
243 return;
246 uint evStart{0u};
247 for(uint ei{0u};ei < evCount;ei++)
249 double ev{90.0 + elems[ei]};
250 double eif{std::round(ev / step)};
251 const uint ev_start{static_cast<uint>(eif)};
253 if(std::fabs(eif - static_cast<double>(ev_start)) < (0.1/step))
255 evStart = ev_start;
256 break;
260 evCount = static_cast<uint>(std::round(180.0 / step)) + 1;
261 if(evCount < 5)
263 fprintf(stdout, "Incompatible layout (too few uniform elevations).\n");
264 return;
267 fds[fi].mEvCount = evCount;
268 fds[fi].mEvStart = evStart;
269 fds[fi].mAzCounts.resize(evCount);
270 auto &azCounts = fds[fi].mAzCounts;
272 for(uint ei{evStart};ei < evCount;ei++)
274 double ev{-90.0 + static_cast<double>(ei)*180.0/static_cast<double>(evCount - 1)};
275 uint azCount{GetUniquelySortedElems(m, aers.data(), 0, { nullptr, &ev, &dist },
276 { 0.1, 0.1, 0.001 }, elems.data())};
278 if(azCount > (m / 3))
280 fprintf(stdout, "Incompatible layout (innumerable azimuths).\n");
281 return;
284 if(ei > 0 && ei < (evCount - 1))
286 step = GetUniformStepSize(0.1, azCount, elems.data());
287 if(step <= 0.0)
289 fprintf(stdout, "Incompatible layout (non-uniform azimuths).\n");
290 return;
293 azCounts[ei] = static_cast<uint>(std::round(360.0f / step));
295 else if(azCount != 1)
297 fprintf(stdout, "Incompatible layout (non-singular poles).\n");
298 return;
300 else
302 azCounts[ei] = 1;
306 for(uint ei{0u};ei < evStart;ei++)
307 azCounts[ei] = azCounts[evCount - ei - 1];
310 fprintf(stdout, "Compatible Layout:\n\ndistance = %.3f", fds[0].mDistance);
312 for(uint fi{1u};fi < fdCount;fi++)
313 fprintf(stdout, ", %.3f", fds[fi].mDistance);
315 fprintf(stdout, "\nazimuths = ");
316 for(uint fi{0u};fi < fdCount;fi++)
318 for(uint ei{0u};ei < fds[fi].mEvCount;ei++)
319 fprintf(stdout, "%d%s", fds[fi].mAzCounts[ei],
320 (ei < (fds[fi].mEvCount - 1)) ? ", " :
321 (fi < (fdCount - 1)) ? ";\n " : "\n");
325 // Load and inspect the given SOFA file.
326 static void SofaInfo(const char *filename)
328 int err;
329 MySofaHrtfPtr sofa{mysofa_load(filename, &err)};
330 if(!sofa)
332 fprintf(stdout, "Error: Could not load source file '%s'.\n", filename);
333 return;
336 /* NOTE: Some valid SOFA files are failing this check. */
337 err = mysofa_check(sofa.get());
338 if(err != MYSOFA_OK)
339 fprintf(stdout, "Warning: Supposedly malformed source file '%s' (%s).\n", filename,
340 SofaErrorStr(err));
342 mysofa_tocartesian(sofa.get());
344 PrintSofaAttributes("Info", sofa->attributes);
346 fprintf(stdout, "Measurements: %u\n", sofa->M);
347 fprintf(stdout, "Receivers: %u\n", sofa->R);
348 fprintf(stdout, "Emitters: %u\n", sofa->E);
349 fprintf(stdout, "Samples: %u\n", sofa->N);
351 PrintSofaArray("SampleRate", &sofa->DataSamplingRate);
352 PrintSofaArray("DataDelay", &sofa->DataDelay);
354 PrintCompatibleLayout(sofa->M, sofa->SourcePosition.values);
357 int main(int argc, char *argv[])
359 GET_UNICODE_ARGS(&argc, &argv);
361 if(argc != 2)
363 fprintf(stdout, "Usage: %s <sofa-file>\n", argv[0]);
364 return 0;
367 SofaInfo(argv[1]);
369 return 0;