sofa-info.cpp 4.9 KB

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