🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

383 lines
10 KiB

  1. /*
  2. * SOFA info utility for inspecting SOFA file metrics and determining HRTF
  3. * utility compatible layouts.
  4. *
  5. * Copyright (C) 2018-2019 Christopher Fitzgerald
  6. *
  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.
  11. *
  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.
  16. *
  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.
  20. *
  21. * Or visit: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <cmath>
  26. #include <vector>
  27. #include <mysofa.h>
  28. #include "win_main_utf8.h"
  29. using uint = unsigned int;
  30. // Per-field measurement info.
  31. struct HrirFdT {
  32. float mDistance{0.0f};
  33. uint mEvCount{0u};
  34. uint mEvStart{0u};
  35. std::vector<uint> mAzCounts;
  36. };
  37. static const char *SofaErrorStr(int err)
  38. {
  39. switch(err)
  40. {
  41. case MYSOFA_OK:
  42. return "OK";
  43. case MYSOFA_INVALID_FORMAT:
  44. return "Invalid format";
  45. case MYSOFA_UNSUPPORTED_FORMAT:
  46. return "Unsupported format";
  47. case MYSOFA_INTERNAL_ERROR:
  48. return "Internal error";
  49. case MYSOFA_NO_MEMORY:
  50. return "Out of memory";
  51. case MYSOFA_READ_ERROR:
  52. return "Read error";
  53. }
  54. return "Unknown";
  55. }
  56. static void PrintSofaAttributes(const char *prefix, struct MYSOFA_ATTRIBUTE *attribute)
  57. {
  58. while(attribute)
  59. {
  60. fprintf(stdout, "%s.%s: %s\n", prefix, attribute->name, attribute->value);
  61. attribute = attribute->next;
  62. }
  63. }
  64. static void PrintSofaArray(const char *prefix, struct MYSOFA_ARRAY *array)
  65. {
  66. PrintSofaAttributes(prefix, array->attributes);
  67. for(uint i{0u};i < array->elements;i++)
  68. fprintf(stdout, "%s[%u]: %.6f\n", prefix, i, array->values[i]);
  69. }
  70. /* Produces a sorted array of unique elements from a particular axis of the
  71. * triplets array. The filters are used to focus on particular coordinates
  72. * of other axes as necessary. The epsilons are used to constrain the
  73. * equality of unique elements.
  74. */
  75. static uint GetUniquelySortedElems(const uint m, const float *triplets, const int axis,
  76. const float *const (&filters)[3], const float (&epsilons)[3],
  77. float *elems)
  78. {
  79. uint count{0u};
  80. for(uint i{0u};i < 3*m;i += 3)
  81. {
  82. float elem = triplets[i + axis];
  83. uint j;
  84. for(j = 0;j < 3;j++)
  85. {
  86. if(filters[j] && std::fabs(triplets[i + j] - *filters[j]) > epsilons[j])
  87. break;
  88. }
  89. if(j < 3)
  90. continue;
  91. for(j = 0;j < count;j++)
  92. {
  93. const float delta{elem - elems[j]};
  94. if(delta > epsilons[axis])
  95. continue;
  96. if(delta >= -epsilons[axis])
  97. break;
  98. for(uint k{count};k > j;k--)
  99. elems[k] = elems[k - 1];
  100. elems[j] = elem;
  101. count++;
  102. break;
  103. }
  104. if(j >= count)
  105. elems[count++] = elem;
  106. }
  107. return count;
  108. }
  109. /* Given a list of elements, this will produce the smallest step size that
  110. * can uniformly cover a fair portion of the list. Ideally this will be over
  111. * half, but in degenerate cases this can fall to a minimum of 5 (the lower
  112. * limit on elevations necessary to build a layout).
  113. */
  114. static float GetUniformStepSize(const float epsilon, const uint m, const float *elems)
  115. {
  116. std::vector<float> steps(m, 0.0f);
  117. std::vector<uint> counts(m, 0u);
  118. float step{0.0f};
  119. uint count{0u};
  120. for(uint stride{1u};stride < m/2;stride++)
  121. {
  122. for(uint i{0u};i < m-stride;i++)
  123. {
  124. const float step{elems[i + stride] - elems[i]};
  125. uint j;
  126. for(j = 0;j < count;j++)
  127. {
  128. if(std::fabs(step - steps[j]) < epsilon)
  129. {
  130. counts[j]++;
  131. break;
  132. }
  133. }
  134. if(j >= count)
  135. {
  136. steps[j] = step;
  137. counts[j] = 1;
  138. count++;
  139. }
  140. }
  141. for(uint i{1u};i < count;i++)
  142. {
  143. if(counts[i] > counts[0])
  144. {
  145. steps[0] = steps[i];
  146. counts[0] = counts[i];
  147. }
  148. }
  149. count = 1;
  150. if(counts[0] > m/2)
  151. {
  152. step = steps[0];
  153. return step;
  154. }
  155. }
  156. if(counts[0] > 5)
  157. step = steps[0];
  158. return step;
  159. }
  160. /* Attempts to produce a compatible layout. Most data sets tend to be
  161. * uniform and have the same major axis as used by OpenAL Soft's HRTF model.
  162. * This will remove outliers and produce a maximally dense layout when
  163. * possible. Those sets that contain purely random measurements or use
  164. * different major axes will fail.
  165. */
  166. static void PrintCompatibleLayout(const uint m, const float *xyzs)
  167. {
  168. std::vector<float> aers(3*m, 0.0f);
  169. std::vector<float> elems(m, 0.0f);
  170. fprintf(stdout, "\n");
  171. for(uint i{0u};i < 3*m;i += 3)
  172. {
  173. aers[i] = xyzs[i];
  174. aers[i + 1] = xyzs[i + 1];
  175. aers[i + 2] = xyzs[i + 2];
  176. mysofa_c2s(&aers[i]);
  177. }
  178. uint fdCount{GetUniquelySortedElems(m, aers.data(), 2,
  179. (const float*[3]){ nullptr, nullptr, nullptr }, (const float[3]){ 0.1f, 0.1f, 0.001f },
  180. elems.data())};
  181. if(fdCount > (m / 3))
  182. {
  183. fprintf(stdout, "Incompatible layout (inumerable radii).\n");
  184. return;
  185. }
  186. std::vector<HrirFdT> fds(fdCount);
  187. for(uint fi{0u};fi < fdCount;fi++)
  188. fds[fi].mDistance = elems[fi];
  189. for(uint fi{0u};fi < fdCount;fi++)
  190. {
  191. float dist{fds[fi].mDistance};
  192. uint evCount{GetUniquelySortedElems(m, aers.data(), 1,
  193. (const float*[3]){ nullptr, nullptr, &dist }, (const float[3]){ 0.1f, 0.1f, 0.001f },
  194. elems.data())};
  195. if(evCount > (m / 3))
  196. {
  197. fprintf(stdout, "Incompatible layout (innumerable elevations).\n");
  198. return;
  199. }
  200. float step{GetUniformStepSize(0.1f, evCount, elems.data())};
  201. if(step <= 0.0f)
  202. {
  203. fprintf(stdout, "Incompatible layout (non-uniform elevations).\n");
  204. return;
  205. }
  206. uint evStart{0u};
  207. for(uint ei{0u};ei < evCount;ei++)
  208. {
  209. float ev{90.0f + elems[ei]};
  210. float eif{std::round(ev / step)};
  211. if(std::fabs(eif - (uint)eif) < (0.1f / step))
  212. {
  213. evStart = static_cast<uint>(eif);
  214. break;
  215. }
  216. }
  217. evCount = static_cast<uint>(std::round(180.0f / step)) + 1;
  218. if(evCount < 5)
  219. {
  220. fprintf(stdout, "Incompatible layout (too few uniform elevations).\n");
  221. return;
  222. }
  223. fds[fi].mEvCount = evCount;
  224. fds[fi].mEvStart = evStart;
  225. fds[fi].mAzCounts.resize(evCount);
  226. auto &azCounts = fds[fi].mAzCounts;
  227. for(uint ei{evStart};ei < evCount;ei++)
  228. {
  229. float ev{-90.0f + ei * 180.0f / (evCount - 1)};
  230. uint azCount{GetUniquelySortedElems(m, aers.data(), 0,
  231. (const float*[3]){ nullptr, &ev, &dist }, (const float[3]){ 0.1f, 0.1f, 0.001f },
  232. elems.data())};
  233. if(azCount > (m / 3))
  234. {
  235. fprintf(stdout, "Incompatible layout (innumerable azimuths).\n");
  236. return;
  237. }
  238. if(ei > 0 && ei < (evCount - 1))
  239. {
  240. step = GetUniformStepSize(0.1f, azCount, elems.data());
  241. if(step <= 0.0f)
  242. {
  243. fprintf(stdout, "Incompatible layout (non-uniform azimuths).\n");
  244. return;
  245. }
  246. azCounts[ei] = static_cast<uint>(std::round(360.0f / step));
  247. }
  248. else if(azCount != 1)
  249. {
  250. fprintf(stdout, "Incompatible layout (non-singular poles).\n");
  251. return;
  252. }
  253. else
  254. {
  255. azCounts[ei] = 1;
  256. }
  257. }
  258. for(uint ei{0u};ei < evStart;ei++)
  259. azCounts[ei] = azCounts[evCount - ei - 1];
  260. }
  261. fprintf(stdout, "Compatible Layout:\n\ndistance = %.3f", fds[0].mDistance);
  262. for(uint fi{1u};fi < fdCount;fi++)
  263. fprintf(stdout, ", %.3f", fds[fi].mDistance);
  264. fprintf(stdout, "\nazimuths = ");
  265. for(uint fi{0u};fi < fdCount;fi++)
  266. {
  267. for(uint ei{0u};ei < fds[fi].mEvCount;ei++)
  268. fprintf(stdout, "%d%s", fds[fi].mAzCounts[ei],
  269. (ei < (fds[fi].mEvCount - 1)) ? ", " :
  270. (fi < (fdCount - 1)) ? ";\n " : "\n");
  271. }
  272. }
  273. // Load and inspect the given SOFA file.
  274. static void SofaInfo(const char *filename)
  275. {
  276. struct MYSOFA_EASY sofa;
  277. sofa.lookup = nullptr;
  278. sofa.neighborhood = nullptr;
  279. int err;
  280. sofa.hrtf = mysofa_load(filename, &err);
  281. if(!sofa.hrtf)
  282. {
  283. mysofa_close(&sofa);
  284. fprintf(stdout, "Error: Could not load source file '%s'.\n", filename);
  285. return;
  286. }
  287. err = mysofa_check(sofa.hrtf);
  288. if(err != MYSOFA_OK)
  289. /* NOTE: Some valid SOFA files are failing this check.
  290. {
  291. mysofa_close(&sofa);
  292. fprintf(stdout, "Error: Malformed source file '%s' (%s).\n", filename, SofaErrorStr(err));
  293. return;
  294. }
  295. */
  296. fprintf(stdout, "Warning: Supposedly malformed source file '%s' (%s).\n", filename, SofaErrorStr(err));
  297. mysofa_tocartesian(sofa.hrtf);
  298. PrintSofaAttributes("Info", sofa.hrtf->attributes);
  299. fprintf(stdout, "Measurements: %u\n", sofa.hrtf->M);
  300. fprintf(stdout, "Receivers: %u\n", sofa.hrtf->R);
  301. fprintf(stdout, "Emitters: %u\n", sofa.hrtf->E);
  302. fprintf(stdout, "Samples: %u\n", sofa.hrtf->N);
  303. PrintSofaArray("SampleRate", &sofa.hrtf->DataSamplingRate);
  304. PrintSofaArray("DataDelay", &sofa.hrtf->DataDelay);
  305. PrintCompatibleLayout(sofa.hrtf->M, sofa.hrtf->SourcePosition.values);
  306. mysofa_free(sofa.hrtf);
  307. }
  308. int main(int argc, char *argv[])
  309. {
  310. GET_UNICODE_ARGS(&argc, &argv);
  311. if(argc != 2)
  312. {
  313. fprintf(stdout, "Usage: %s <sofa-file>\n", argv[0]);
  314. return 0;
  315. }
  316. SofaInfo(argv[1]);
  317. return 0;
  318. }