sofa-info.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 <cstdio>
  24. #include <memory>
  25. #include <string>
  26. #include <string_view>
  27. #include <vector>
  28. #include "alnumeric.h"
  29. #include "alspan.h"
  30. #include "alstring.h"
  31. #include "sofa-support.h"
  32. #include "mysofa.h"
  33. #include "win_main_utf8.h"
  34. namespace {
  35. using uint = unsigned int;
  36. void PrintSofaAttributes(const char *prefix, MYSOFA_ATTRIBUTE *attribute)
  37. {
  38. while(attribute)
  39. {
  40. fprintf(stdout, "%s.%s: %s\n", prefix, attribute->name, attribute->value);
  41. attribute = attribute->next;
  42. }
  43. }
  44. void PrintSofaArray(const char *prefix, MYSOFA_ARRAY *array, bool showValues=true)
  45. {
  46. PrintSofaAttributes(prefix, array->attributes);
  47. if(showValues)
  48. {
  49. const auto values = al::span{array->values, array->elements};
  50. for(size_t i{0u};i < values.size();++i)
  51. fprintf(stdout, "%s[%zu]: %.6f\n", prefix, i, values[i]);
  52. }
  53. else
  54. fprintf(stdout, "%s[...]: <%u values suppressed>\n", prefix, array->elements);
  55. }
  56. /* Attempts to produce a compatible layout. Most data sets tend to be
  57. * uniform and have the same major axis as used by OpenAL Soft's HRTF model.
  58. * This will remove outliers and produce a maximally dense layout when
  59. * possible. Those sets that contain purely random measurements or use
  60. * different major axes will fail.
  61. */
  62. void PrintCompatibleLayout(const al::span<const float> xyzs)
  63. {
  64. fputc('\n', stdout);
  65. auto fds = GetCompatibleLayout(xyzs);
  66. if(fds.empty())
  67. {
  68. fprintf(stdout, "No compatible field layouts in SOFA file.\n");
  69. return;
  70. }
  71. uint used_elems{0};
  72. for(size_t fi{0u};fi < fds.size();++fi)
  73. {
  74. for(uint ei{fds[fi].mEvStart};ei < fds[fi].mEvCount;++ei)
  75. used_elems += fds[fi].mAzCounts[ei];
  76. }
  77. fprintf(stdout, "Compatible Layout (%u of %zu measurements):\n\ndistance = %.3f", used_elems,
  78. xyzs.size()/3, fds[0].mDistance);
  79. for(size_t fi{1u};fi < fds.size();fi++)
  80. fprintf(stdout, ", %.3f", fds[fi].mDistance);
  81. fprintf(stdout, "\nazimuths = ");
  82. for(size_t fi{0u};fi < fds.size();++fi)
  83. {
  84. for(uint ei{0u};ei < fds[fi].mEvStart;++ei)
  85. fprintf(stdout, "%d%s", fds[fi].mAzCounts[fds[fi].mEvCount - 1 - ei], ", ");
  86. for(uint ei{fds[fi].mEvStart};ei < fds[fi].mEvCount;++ei)
  87. fprintf(stdout, "%d%s", fds[fi].mAzCounts[ei],
  88. (ei < (fds[fi].mEvCount - 1)) ? ", " :
  89. (fi < (fds.size() - 1)) ? ";\n " : "\n");
  90. }
  91. }
  92. // Load and inspect the given SOFA file.
  93. void SofaInfo(const std::string &filename)
  94. {
  95. int err;
  96. MySofaHrtfPtr sofa{mysofa_load(filename.c_str(), &err)};
  97. if(!sofa)
  98. {
  99. fprintf(stdout, "Error: Could not load source file '%s' (%s).\n", filename.c_str(),
  100. SofaErrorStr(err));
  101. return;
  102. }
  103. /* NOTE: Some valid SOFA files are failing this check. */
  104. err = mysofa_check(sofa.get());
  105. if(err != MYSOFA_OK)
  106. fprintf(stdout, "Warning: Supposedly malformed source file '%s' (%s).\n", filename.c_str(),
  107. SofaErrorStr(err));
  108. mysofa_tocartesian(sofa.get());
  109. PrintSofaAttributes("Info", sofa->attributes);
  110. fprintf(stdout, "Measurements: %u\n", sofa->M);
  111. fprintf(stdout, "Receivers: %u\n", sofa->R);
  112. fprintf(stdout, "Emitters: %u\n", sofa->E);
  113. fprintf(stdout, "Samples: %u\n", sofa->N);
  114. PrintSofaArray("SampleRate", &sofa->DataSamplingRate);
  115. PrintSofaArray("DataDelay", &sofa->DataDelay);
  116. PrintSofaArray("SourcePosition", &sofa->SourcePosition, false);
  117. PrintCompatibleLayout(al::span{sofa->SourcePosition.values, sofa->M*3_uz});
  118. }
  119. int main(al::span<std::string_view> args)
  120. {
  121. if(args.size() != 2)
  122. {
  123. fprintf(stdout, "Usage: %.*s <sofa-file>\n", al::sizei(args[0]), args[0].data());
  124. return 0;
  125. }
  126. SofaInfo(std::string{args[1]});
  127. return 0;
  128. }
  129. } /* namespace */
  130. int main(int argc, char **argv)
  131. {
  132. assert(argc >= 0);
  133. auto args = std::vector<std::string_view>(static_cast<unsigned int>(argc));
  134. std::copy_n(argv, args.size(), args.begin());
  135. return main(al::span{args});
  136. }