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