123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- /*
- * SOFA info utility for inspecting SOFA file metrics and determining HRTF
- * utility compatible layouts.
- *
- * Copyright (C) 2018-2019 Christopher Fitzgerald
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Or visit: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
- */
- #include <cstdio>
- #include <memory>
- #include <string>
- #include <string_view>
- #include <vector>
- #include "alnumeric.h"
- #include "alspan.h"
- #include "alstring.h"
- #include "sofa-support.h"
- #include "mysofa.h"
- #include "win_main_utf8.h"
- namespace {
- using uint = unsigned int;
- void PrintSofaAttributes(const char *prefix, MYSOFA_ATTRIBUTE *attribute)
- {
- while(attribute)
- {
- fprintf(stdout, "%s.%s: %s\n", prefix, attribute->name, attribute->value);
- attribute = attribute->next;
- }
- }
- void PrintSofaArray(const char *prefix, MYSOFA_ARRAY *array, bool showValues=true)
- {
- PrintSofaAttributes(prefix, array->attributes);
- if(showValues)
- {
- const auto values = al::span{array->values, array->elements};
- for(size_t i{0u};i < values.size();++i)
- fprintf(stdout, "%s[%zu]: %.6f\n", prefix, i, values[i]);
- }
- else
- fprintf(stdout, "%s[...]: <%u values suppressed>\n", prefix, array->elements);
- }
- /* Attempts to produce a compatible layout. Most data sets tend to be
- * uniform and have the same major axis as used by OpenAL Soft's HRTF model.
- * This will remove outliers and produce a maximally dense layout when
- * possible. Those sets that contain purely random measurements or use
- * different major axes will fail.
- */
- void PrintCompatibleLayout(const al::span<const float> xyzs)
- {
- fputc('\n', stdout);
- auto fds = GetCompatibleLayout(xyzs);
- if(fds.empty())
- {
- fprintf(stdout, "No compatible field layouts in SOFA file.\n");
- return;
- }
- uint used_elems{0};
- for(size_t fi{0u};fi < fds.size();++fi)
- {
- for(uint ei{fds[fi].mEvStart};ei < fds[fi].mEvCount;++ei)
- used_elems += fds[fi].mAzCounts[ei];
- }
- fprintf(stdout, "Compatible Layout (%u of %zu measurements):\n\ndistance = %.3f", used_elems,
- xyzs.size()/3, fds[0].mDistance);
- for(size_t fi{1u};fi < fds.size();fi++)
- fprintf(stdout, ", %.3f", fds[fi].mDistance);
- fprintf(stdout, "\nazimuths = ");
- for(size_t fi{0u};fi < fds.size();++fi)
- {
- for(uint ei{0u};ei < fds[fi].mEvStart;++ei)
- fprintf(stdout, "%d%s", fds[fi].mAzCounts[fds[fi].mEvCount - 1 - ei], ", ");
- for(uint ei{fds[fi].mEvStart};ei < fds[fi].mEvCount;++ei)
- fprintf(stdout, "%d%s", fds[fi].mAzCounts[ei],
- (ei < (fds[fi].mEvCount - 1)) ? ", " :
- (fi < (fds.size() - 1)) ? ";\n " : "\n");
- }
- }
- // Load and inspect the given SOFA file.
- void SofaInfo(const std::string &filename)
- {
- int err;
- MySofaHrtfPtr sofa{mysofa_load(filename.c_str(), &err)};
- if(!sofa)
- {
- fprintf(stdout, "Error: Could not load source file '%s' (%s).\n", filename.c_str(),
- SofaErrorStr(err));
- return;
- }
- /* NOTE: Some valid SOFA files are failing this check. */
- err = mysofa_check(sofa.get());
- if(err != MYSOFA_OK)
- fprintf(stdout, "Warning: Supposedly malformed source file '%s' (%s).\n", filename.c_str(),
- SofaErrorStr(err));
- mysofa_tocartesian(sofa.get());
- PrintSofaAttributes("Info", sofa->attributes);
- fprintf(stdout, "Measurements: %u\n", sofa->M);
- fprintf(stdout, "Receivers: %u\n", sofa->R);
- fprintf(stdout, "Emitters: %u\n", sofa->E);
- fprintf(stdout, "Samples: %u\n", sofa->N);
- PrintSofaArray("SampleRate", &sofa->DataSamplingRate);
- PrintSofaArray("DataDelay", &sofa->DataDelay);
- PrintSofaArray("SourcePosition", &sofa->SourcePosition, false);
- PrintCompatibleLayout(al::span{sofa->SourcePosition.values, sofa->M*3_uz});
- }
- int main(al::span<std::string_view> args)
- {
- if(args.size() != 2)
- {
- fprintf(stdout, "Usage: %.*s <sofa-file>\n", al::sizei(args[0]), args[0].data());
- return 0;
- }
- SofaInfo(std::string{args[1]});
- return 0;
- }
- } /* namespace */
- int main(int argc, char **argv)
- {
- assert(argc >= 0);
- auto args = std::vector<std::string_view>(static_cast<unsigned int>(argc));
- std::copy_n(argv, args.size(), args.begin());
- return main(al::span{args});
- }
|