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
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.
46 double mDistance
{0.0};
49 std::vector
<uint
> mAzCounts
;
52 static const char *SofaErrorStr(int 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";
66 static void PrintSofaAttributes(const char *prefix
, struct MYSOFA_ATTRIBUTE
*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
)
92 for(uint i
{0u};i
< m
;++i
)
94 const double elem
{aers
[i
][axis
]};
99 if(filters
[j
] && std::fabs(aers
[i
][j
] - *filters
[j
]) > epsilons
[j
])
105 for(j
= 0;j
< count
;j
++)
107 const double delta
{elem
- elems
[j
]};
109 if(delta
> epsilons
[axis
])
112 if(delta
>= -epsilons
[axis
])
115 for(uint k
{count
};k
> j
;k
--)
116 elems
[k
] = elems
[k
- 1];
124 elems
[count
++] = elem
;
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);
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
]};
148 for(j
= 0;j
< count
;j
++)
150 if(std::fabs(step
- steps
[j
]) < epsilon
)
165 for(uint i
{1u};i
< count
;i
++)
167 if(counts
[i
] > counts
[0])
170 counts
[0] = counts
[i
];
183 while(counts
[0]/i
> 255 && (counts
[0]%i
) != 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]};
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");
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");
239 double step
{GetUniformStepSize(0.1, evCount
, elems
.data())};
242 fprintf(stdout
, "Incompatible layout (non-uniform elevations).\n");
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
))
260 evCount
= static_cast<uint
>(std::round(180.0 / step
)) + 1;
263 fprintf(stdout
, "Incompatible layout (too few uniform elevations).\n");
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");
284 if(ei
> 0 && ei
< (evCount
- 1))
286 step
= GetUniformStepSize(0.1, azCount
, elems
.data());
289 fprintf(stdout
, "Incompatible layout (non-uniform azimuths).\n");
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");
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
)
329 MySofaHrtfPtr sofa
{mysofa_load(filename
, &err
)};
332 fprintf(stdout
, "Error: Could not load source file '%s'.\n", filename
);
336 /* NOTE: Some valid SOFA files are failing this check. */
337 err
= mysofa_check(sofa
.get());
339 fprintf(stdout
, "Warning: Supposedly malformed source file '%s' (%s).\n", filename
,
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
);
363 fprintf(stdout
, "Usage: %s <sofa-file>\n", argv
[0]);