Use one PulseAudio mainloop per device
[openal-soft.git] / utils / sofa-info.cpp
blob32bef938cbb183e3a25f0d08849c7e4ef87a5323
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 <cmath>
27 #include <memory>
28 #include <vector>
30 #include <mysofa.h>
32 #include "win_main_utf8.h"
35 using uint = unsigned int;
37 struct MySofaDeleter {
38 void operator()(MYSOFA_HRTF *sofa) { mysofa_free(sofa); }
40 using MySofaHrtfPtr = std::unique_ptr<MYSOFA_HRTF,MySofaDeleter>;
42 // Per-field measurement info.
43 struct HrirFdT {
44 float mDistance{0.0f};
45 uint mEvCount{0u};
46 uint mEvStart{0u};
47 std::vector<uint> mAzCounts;
50 static const char *SofaErrorStr(int err)
52 switch(err)
54 case MYSOFA_OK: return "OK";
55 case MYSOFA_INVALID_FORMAT: return "Invalid format";
56 case MYSOFA_UNSUPPORTED_FORMAT: return "Unsupported format";
57 case MYSOFA_INTERNAL_ERROR: return "Internal error";
58 case MYSOFA_NO_MEMORY: return "Out of memory";
59 case MYSOFA_READ_ERROR: return "Read error";
61 return "Unknown";
64 static void PrintSofaAttributes(const char *prefix, struct MYSOFA_ATTRIBUTE *attribute)
66 while(attribute)
68 fprintf(stdout, "%s.%s: %s\n", prefix, attribute->name, attribute->value);
69 attribute = attribute->next;
73 static void PrintSofaArray(const char *prefix, struct MYSOFA_ARRAY *array)
75 PrintSofaAttributes(prefix, array->attributes);
77 for(uint i{0u};i < array->elements;i++)
78 fprintf(stdout, "%s[%u]: %.6f\n", prefix, i, array->values[i]);
81 /* Produces a sorted array of unique elements from a particular axis of the
82 * triplets array. The filters are used to focus on particular coordinates
83 * of other axes as necessary. The epsilons are used to constrain the
84 * equality of unique elements.
86 static uint GetUniquelySortedElems(const uint m, const float *triplets, const uint axis,
87 const float *const (&filters)[3], const float (&epsilons)[3],
88 float *elems)
90 uint count{0u};
91 for(uint i{0u};i < 3*m;i += 3)
93 float elem = triplets[i + axis];
95 uint j;
96 for(j = 0;j < 3;j++)
98 if(filters[j] && std::fabs(triplets[i + j] - *filters[j]) > epsilons[j])
99 break;
101 if(j < 3)
102 continue;
104 for(j = 0;j < count;j++)
106 const float delta{elem - elems[j]};
108 if(delta > epsilons[axis])
109 continue;
111 if(delta >= -epsilons[axis])
112 break;
114 for(uint k{count};k > j;k--)
115 elems[k] = elems[k - 1];
117 elems[j] = elem;
118 count++;
119 break;
122 if(j >= count)
123 elems[count++] = elem;
126 return count;
129 /* Given a list of elements, this will produce the smallest step size that
130 * can uniformly cover a fair portion of the list. Ideally this will be over
131 * half, but in degenerate cases this can fall to a minimum of 5 (the lower
132 * limit on elevations necessary to build a layout).
134 static float GetUniformStepSize(const float epsilon, const uint m, const float *elems)
136 std::vector<float> steps(m, 0.0f);
137 std::vector<uint> counts(m, 0u);
138 uint count{0u};
140 for(uint stride{1u};stride < m/2;stride++)
142 for(uint i{0u};i < m-stride;i++)
144 const float step{elems[i + stride] - elems[i]};
146 uint j;
147 for(j = 0;j < count;j++)
149 if(std::fabs(step - steps[j]) < epsilon)
151 counts[j]++;
152 break;
156 if(j >= count)
158 steps[j] = step;
159 counts[j] = 1;
160 count++;
164 for(uint i{1u};i < count;i++)
166 if(counts[i] > counts[0])
168 steps[0] = steps[i];
169 counts[0] = counts[i];
173 count = 1;
175 if(counts[0] > m/2)
176 return steps[0];
179 if(counts[0] > 5)
180 return steps[0];
181 return 0.0f;
184 /* Attempts to produce a compatible layout. Most data sets tend to be
185 * uniform and have the same major axis as used by OpenAL Soft's HRTF model.
186 * This will remove outliers and produce a maximally dense layout when
187 * possible. Those sets that contain purely random measurements or use
188 * different major axes will fail.
190 static void PrintCompatibleLayout(const uint m, const float *xyzs)
192 std::vector<float> aers(3*m, 0.0f);
193 std::vector<float> elems(m, 0.0f);
195 fprintf(stdout, "\n");
197 for(uint i{0u};i < 3*m;i += 3)
199 aers[i] = xyzs[i];
200 aers[i + 1] = xyzs[i + 1];
201 aers[i + 2] = xyzs[i + 2];
202 mysofa_c2s(&aers[i]);
205 uint fdCount{GetUniquelySortedElems(m, aers.data(), 2, { nullptr, nullptr, nullptr }, { 0.1f, 0.1f, 0.001f }, elems.data())};
206 if(fdCount > (m / 3))
208 fprintf(stdout, "Incompatible layout (inumerable radii).\n");
209 return;
212 std::vector<HrirFdT> fds(fdCount);
213 for(uint fi{0u};fi < fdCount;fi++)
214 fds[fi].mDistance = elems[fi];
216 for(uint fi{0u};fi < fdCount;fi++)
218 float dist{fds[fi].mDistance};
219 uint evCount{GetUniquelySortedElems(m, aers.data(), 1, { nullptr, nullptr, &dist }, { 0.1f, 0.1f, 0.001f }, elems.data())};
221 if(evCount > (m / 3))
223 fprintf(stdout, "Incompatible layout (innumerable elevations).\n");
224 return;
227 float step{GetUniformStepSize(0.1f, evCount, elems.data())};
228 if(step <= 0.0f)
230 fprintf(stdout, "Incompatible layout (non-uniform elevations).\n");
231 return;
234 uint evStart{0u};
235 for(uint ei{0u};ei < evCount;ei++)
237 float ev{90.0f + elems[ei]};
238 float eif{std::round(ev / step)};
239 const uint ev_start{static_cast<uint>(eif)};
241 if(std::fabs(eif - static_cast<float>(ev_start)) < (0.1f/step))
243 evStart = ev_start;
244 break;
248 evCount = static_cast<uint>(std::round(180.0f / step)) + 1;
249 if(evCount < 5)
251 fprintf(stdout, "Incompatible layout (too few uniform elevations).\n");
252 return;
255 fds[fi].mEvCount = evCount;
256 fds[fi].mEvStart = evStart;
257 fds[fi].mAzCounts.resize(evCount);
258 auto &azCounts = fds[fi].mAzCounts;
260 for(uint ei{evStart};ei < evCount;ei++)
262 float ev{-90.0f + static_cast<float>(ei)*180.0f/static_cast<float>(evCount - 1)};
263 uint azCount{GetUniquelySortedElems(m, aers.data(), 0, { nullptr, &ev, &dist }, { 0.1f, 0.1f, 0.001f }, elems.data())};
265 if(azCount > (m / 3))
267 fprintf(stdout, "Incompatible layout (innumerable azimuths).\n");
268 return;
271 if(ei > 0 && ei < (evCount - 1))
273 step = GetUniformStepSize(0.1f, azCount, elems.data());
274 if(step <= 0.0f)
276 fprintf(stdout, "Incompatible layout (non-uniform azimuths).\n");
277 return;
280 azCounts[ei] = static_cast<uint>(std::round(360.0f / step));
282 else if(azCount != 1)
284 fprintf(stdout, "Incompatible layout (non-singular poles).\n");
285 return;
287 else
289 azCounts[ei] = 1;
293 for(uint ei{0u};ei < evStart;ei++)
294 azCounts[ei] = azCounts[evCount - ei - 1];
297 fprintf(stdout, "Compatible Layout:\n\ndistance = %.3f", fds[0].mDistance);
299 for(uint fi{1u};fi < fdCount;fi++)
300 fprintf(stdout, ", %.3f", fds[fi].mDistance);
302 fprintf(stdout, "\nazimuths = ");
303 for(uint fi{0u};fi < fdCount;fi++)
305 for(uint ei{0u};ei < fds[fi].mEvCount;ei++)
306 fprintf(stdout, "%d%s", fds[fi].mAzCounts[ei],
307 (ei < (fds[fi].mEvCount - 1)) ? ", " :
308 (fi < (fdCount - 1)) ? ";\n " : "\n");
312 // Load and inspect the given SOFA file.
313 static void SofaInfo(const char *filename)
315 int err;
316 MySofaHrtfPtr sofa{mysofa_load(filename, &err)};
317 if(!sofa)
319 fprintf(stdout, "Error: Could not load source file '%s'.\n", filename);
320 return;
323 /* NOTE: Some valid SOFA files are failing this check. */
324 err = mysofa_check(sofa.get());
325 if(err != MYSOFA_OK)
326 fprintf(stdout, "Warning: Supposedly malformed source file '%s' (%s).\n", filename,
327 SofaErrorStr(err));
329 mysofa_tocartesian(sofa.get());
331 PrintSofaAttributes("Info", sofa->attributes);
333 fprintf(stdout, "Measurements: %u\n", sofa->M);
334 fprintf(stdout, "Receivers: %u\n", sofa->R);
335 fprintf(stdout, "Emitters: %u\n", sofa->E);
336 fprintf(stdout, "Samples: %u\n", sofa->N);
338 PrintSofaArray("SampleRate", &sofa->DataSamplingRate);
339 PrintSofaArray("DataDelay", &sofa->DataDelay);
341 PrintCompatibleLayout(sofa->M, sofa->SourcePosition.values);
344 int main(int argc, char *argv[])
346 GET_UNICODE_ARGS(&argc, &argv);
348 if(argc != 2)
350 fprintf(stdout, "Usage: %s <sofa-file>\n", argv[0]);
351 return 0;
354 SofaInfo(argv[1]);
356 return 0;