aboutsummaryrefslogtreecommitdiffstats
path: root/utils
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2019-09-23 17:38:43 -0700
committerChris Robinson <[email protected]>2019-09-23 17:38:43 -0700
commit34a0fad6cd2513f640c58dcff16817d00cf00456 (patch)
tree3863752b371d9c1e905aacd2fe000a305126a019 /utils
parent4a111f7671c19f3cc0306e91ebe117d6bf7046ec (diff)
Use a unique_ptr to auto-free MYSOFA_HRTF
Diffstat (limited to 'utils')
-rw-r--r--utils/sofa-info.cpp79
1 files changed, 32 insertions, 47 deletions
diff --git a/utils/sofa-info.cpp b/utils/sofa-info.cpp
index 87531b37..32bef938 100644
--- a/utils/sofa-info.cpp
+++ b/utils/sofa-info.cpp
@@ -22,17 +22,23 @@
*/
#include <stdio.h>
-#include <stdlib.h>
#include <cmath>
+#include <memory>
#include <vector>
#include <mysofa.h>
#include "win_main_utf8.h"
+
using uint = unsigned int;
+struct MySofaDeleter {
+ void operator()(MYSOFA_HRTF *sofa) { mysofa_free(sofa); }
+};
+using MySofaHrtfPtr = std::unique_ptr<MYSOFA_HRTF,MySofaDeleter>;
+
// Per-field measurement info.
struct HrirFdT {
float mDistance{0.0f};
@@ -45,20 +51,13 @@ static const char *SofaErrorStr(int err)
{
switch(err)
{
- case MYSOFA_OK:
- return "OK";
- case MYSOFA_INVALID_FORMAT:
- return "Invalid format";
- case MYSOFA_UNSUPPORTED_FORMAT:
- return "Unsupported format";
- case MYSOFA_INTERNAL_ERROR:
- return "Internal error";
- case MYSOFA_NO_MEMORY:
- return "Out of memory";
- case MYSOFA_READ_ERROR:
- return "Read error";
+ case MYSOFA_OK: return "OK";
+ case MYSOFA_INVALID_FORMAT: return "Invalid format";
+ case MYSOFA_UNSUPPORTED_FORMAT: return "Unsupported format";
+ case MYSOFA_INTERNAL_ERROR: return "Internal error";
+ case MYSOFA_NO_MEMORY: return "Out of memory";
+ case MYSOFA_READ_ERROR: return "Read error";
}
-
return "Unknown";
}
@@ -237,10 +236,11 @@ static void PrintCompatibleLayout(const uint m, const float *xyzs)
{
float ev{90.0f + elems[ei]};
float eif{std::round(ev / step)};
+ const uint ev_start{static_cast<uint>(eif)};
- if(std::fabs(eif - static_cast<uint>(eif)) < (0.1f / step))
+ if(std::fabs(eif - static_cast<float>(ev_start)) < (0.1f/step))
{
- evStart = static_cast<uint>(eif);
+ evStart = ev_start;
break;
}
}
@@ -259,7 +259,7 @@ static void PrintCompatibleLayout(const uint m, const float *xyzs)
for(uint ei{evStart};ei < evCount;ei++)
{
- float ev{-90.0f + ei * 180.0f / (evCount - 1)};
+ float ev{-90.0f + static_cast<float>(ei)*180.0f/static_cast<float>(evCount - 1)};
uint azCount{GetUniquelySortedElems(m, aers.data(), 0, { nullptr, &ev, &dist }, { 0.1f, 0.1f, 0.001f }, elems.data())};
if(azCount > (m / 3))
@@ -312,48 +312,33 @@ static void PrintCompatibleLayout(const uint m, const float *xyzs)
// Load and inspect the given SOFA file.
static void SofaInfo(const char *filename)
{
- struct MYSOFA_EASY sofa;
-
- sofa.lookup = nullptr;
- sofa.neighborhood = nullptr;
-
int err;
- sofa.hrtf = mysofa_load(filename, &err);
-
- if(!sofa.hrtf)
+ MySofaHrtfPtr sofa{mysofa_load(filename, &err)};
+ if(!sofa)
{
- mysofa_close(&sofa);
fprintf(stdout, "Error: Could not load source file '%s'.\n", filename);
return;
}
- err = mysofa_check(sofa.hrtf);
+ /* NOTE: Some valid SOFA files are failing this check. */
+ err = mysofa_check(sofa.get());
if(err != MYSOFA_OK)
-/* NOTE: Some valid SOFA files are failing this check.
- {
- mysofa_close(&sofa);
- fprintf(stdout, "Error: Malformed source file '%s' (%s).\n", filename, SofaErrorStr(err));
-
- return;
- }
-*/
- fprintf(stdout, "Warning: Supposedly malformed source file '%s' (%s).\n", filename, SofaErrorStr(err));
-
- mysofa_tocartesian(sofa.hrtf);
+ fprintf(stdout, "Warning: Supposedly malformed source file '%s' (%s).\n", filename,
+ SofaErrorStr(err));
- PrintSofaAttributes("Info", sofa.hrtf->attributes);
+ mysofa_tocartesian(sofa.get());
- fprintf(stdout, "Measurements: %u\n", sofa.hrtf->M);
- fprintf(stdout, "Receivers: %u\n", sofa.hrtf->R);
- fprintf(stdout, "Emitters: %u\n", sofa.hrtf->E);
- fprintf(stdout, "Samples: %u\n", sofa.hrtf->N);
+ PrintSofaAttributes("Info", sofa->attributes);
- PrintSofaArray("SampleRate", &sofa.hrtf->DataSamplingRate);
- PrintSofaArray("DataDelay", &sofa.hrtf->DataDelay);
+ 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);
- PrintCompatibleLayout(sofa.hrtf->M, sofa.hrtf->SourcePosition.values);
+ PrintSofaArray("SampleRate", &sofa->DataSamplingRate);
+ PrintSofaArray("DataDelay", &sofa->DataDelay);
- mysofa_free(sofa.hrtf);
+ PrintCompatibleLayout(sofa->M, sofa->SourcePosition.values);
}
int main(int argc, char *argv[])