More consistently use doubles in makemhr and loadsofa
[openal-soft.git] / utils / sofa-info.cpp
bloba0ed9ff45f6056d4e0e2bd68e85a844f4e635efd
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 return steps[0];
180 if(counts[0] > 5)
181 return steps[0];
182 return 0.0;
185 /* Attempts to produce a compatible layout. Most data sets tend to be
186 * uniform and have the same major axis as used by OpenAL Soft's HRTF model.
187 * This will remove outliers and produce a maximally dense layout when
188 * possible. Those sets that contain purely random measurements or use
189 * different major axes will fail.
191 static void PrintCompatibleLayout(const uint m, const float *xyzs)
193 auto aers = std::vector<double3>(m, double3{});
194 auto elems = std::vector<double>(m, {});
196 fprintf(stdout, "\n");
198 for(uint i{0u};i < m;++i)
200 float aer[3]{xyzs[i*3], xyzs[i*3 + 1], xyzs[i*3 + 2]};
201 mysofa_c2s(&aer[0]);
202 aers[i][0] = aer[0];
203 aers[i][1] = aer[1];
204 aers[i][2] = aer[2];
207 uint fdCount{GetUniquelySortedElems(m, aers.data(), 2, { nullptr, nullptr, nullptr },
208 { 0.1, 0.1, 0.001 }, elems.data())};
209 if(fdCount > (m / 3))
211 fprintf(stdout, "Incompatible layout (inumerable radii).\n");
212 return;
215 std::vector<HrirFdT> fds(fdCount);
216 for(uint fi{0u};fi < fdCount;fi++)
217 fds[fi].mDistance = elems[fi];
219 for(uint fi{0u};fi < fdCount;fi++)
221 const double dist{fds[fi].mDistance};
222 uint evCount{GetUniquelySortedElems(m, aers.data(), 1, { nullptr, nullptr, &dist },
223 { 0.1, 0.1, 0.001 }, elems.data())};
225 if(evCount > (m / 3))
227 fprintf(stdout, "Incompatible layout (innumerable elevations).\n");
228 return;
231 double step{GetUniformStepSize(0.1, evCount, elems.data())};
232 if(step <= 0.0)
234 fprintf(stdout, "Incompatible layout (non-uniform elevations).\n");
235 return;
238 uint evStart{0u};
239 for(uint ei{0u};ei < evCount;ei++)
241 double ev{90.0 + elems[ei]};
242 double eif{std::round(ev / step)};
243 const uint ev_start{static_cast<uint>(eif)};
245 if(std::fabs(eif - static_cast<double>(ev_start)) < (0.1/step))
247 evStart = ev_start;
248 break;
252 evCount = static_cast<uint>(std::round(180.0 / step)) + 1;
253 if(evCount < 5)
255 fprintf(stdout, "Incompatible layout (too few uniform elevations).\n");
256 return;
259 fds[fi].mEvCount = evCount;
260 fds[fi].mEvStart = evStart;
261 fds[fi].mAzCounts.resize(evCount);
262 auto &azCounts = fds[fi].mAzCounts;
264 for(uint ei{evStart};ei < evCount;ei++)
266 double ev{-90.0 + static_cast<double>(ei)*180.0/static_cast<double>(evCount - 1)};
267 uint azCount{GetUniquelySortedElems(m, aers.data(), 0, { nullptr, &ev, &dist },
268 { 0.1, 0.1, 0.001 }, elems.data())};
270 if(azCount > (m / 3))
272 fprintf(stdout, "Incompatible layout (innumerable azimuths).\n");
273 return;
276 if(ei > 0 && ei < (evCount - 1))
278 step = GetUniformStepSize(0.1, azCount, elems.data());
279 if(step <= 0.0)
281 fprintf(stdout, "Incompatible layout (non-uniform azimuths).\n");
282 return;
285 azCounts[ei] = static_cast<uint>(std::round(360.0f / step));
287 else if(azCount != 1)
289 fprintf(stdout, "Incompatible layout (non-singular poles).\n");
290 return;
292 else
294 azCounts[ei] = 1;
298 for(uint ei{0u};ei < evStart;ei++)
299 azCounts[ei] = azCounts[evCount - ei - 1];
302 fprintf(stdout, "Compatible Layout:\n\ndistance = %.3f", fds[0].mDistance);
304 for(uint fi{1u};fi < fdCount;fi++)
305 fprintf(stdout, ", %.3f", fds[fi].mDistance);
307 fprintf(stdout, "\nazimuths = ");
308 for(uint fi{0u};fi < fdCount;fi++)
310 for(uint ei{0u};ei < fds[fi].mEvCount;ei++)
311 fprintf(stdout, "%d%s", fds[fi].mAzCounts[ei],
312 (ei < (fds[fi].mEvCount - 1)) ? ", " :
313 (fi < (fdCount - 1)) ? ";\n " : "\n");
317 // Load and inspect the given SOFA file.
318 static void SofaInfo(const char *filename)
320 int err;
321 MySofaHrtfPtr sofa{mysofa_load(filename, &err)};
322 if(!sofa)
324 fprintf(stdout, "Error: Could not load source file '%s'.\n", filename);
325 return;
328 /* NOTE: Some valid SOFA files are failing this check. */
329 err = mysofa_check(sofa.get());
330 if(err != MYSOFA_OK)
331 fprintf(stdout, "Warning: Supposedly malformed source file '%s' (%s).\n", filename,
332 SofaErrorStr(err));
334 mysofa_tocartesian(sofa.get());
336 PrintSofaAttributes("Info", sofa->attributes);
338 fprintf(stdout, "Measurements: %u\n", sofa->M);
339 fprintf(stdout, "Receivers: %u\n", sofa->R);
340 fprintf(stdout, "Emitters: %u\n", sofa->E);
341 fprintf(stdout, "Samples: %u\n", sofa->N);
343 PrintSofaArray("SampleRate", &sofa->DataSamplingRate);
344 PrintSofaArray("DataDelay", &sofa->DataDelay);
346 PrintCompatibleLayout(sofa->M, sofa->SourcePosition.values);
349 int main(int argc, char *argv[])
351 GET_UNICODE_ARGS(&argc, &argv);
353 if(argc != 2)
355 fprintf(stdout, "Usage: %s <sofa-file>\n", argv[0]);
356 return 0;
359 SofaInfo(argv[1]);
361 return 0;