2 * SOFA utility methods for inspecting SOFA file metrics and determining HRTF
3 * utility compatible layouts.
5 * Copyright (C) 2018-2019 Christopher Fitzgerald
6 * Copyright (C) 2019 Christopher Robinson
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 * Or visit: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
25 #include "sofa-support.h"
40 using uint
= unsigned int;
41 using double3
= std::array
<double,3>;
44 /* Produces a sorted array of unique elements from a particular axis of the
45 * triplets array. The filters are used to focus on particular coordinates
46 * of other axes as necessary. The epsilons are used to constrain the
47 * equality of unique elements.
49 std::vector
<double> GetUniquelySortedElems(const std::vector
<double3
> &aers
, const uint axis
,
50 const std::array
<const double*,3> &filters
, const std::array
<double,3> &epsilons
)
52 std::vector
<double> elems
;
53 for(const double3
&aer
: aers
)
55 const double elem
{aer
[axis
]};
60 if(filters
[j
] && std::abs(aer
[j
] - *filters
[j
]) > epsilons
[j
])
66 auto iter
= elems
.begin();
67 for(;iter
!= elems
.end();++iter
)
69 const double delta
{elem
- *iter
};
70 if(delta
> epsilons
[axis
]) continue;
71 if(delta
>= -epsilons
[axis
]) break;
73 iter
= elems
.emplace(iter
, elem
);
76 if(iter
== elems
.end())
77 elems
.emplace_back(elem
);
82 /* Given a list of azimuths, this will produce the smallest step size that can
83 * uniformly cover the list. Ideally this will be over half, but in degenerate
84 * cases this can fall to a minimum of 5 (the lower limit).
86 double GetUniformAzimStep(const double epsilon
, const std::vector
<double> &elems
)
88 if(elems
.size() < 5) return 0.0;
90 /* Get the maximum count possible, given the first two elements. It would
91 * be impossible to have more than this since the first element must be
94 uint count
{static_cast<uint
>(std::ceil(360.0 / (elems
[1]-elems
[0])))};
95 count
= std::min(count
, 255u);
97 for(;count
>= 5;--count
)
99 /* Given the stepping value for this number of elements, check each
100 * multiple to ensure there's a matching element.
102 const double step
{360.0 / count
};
105 for(uint mult
{1u};mult
< count
&& good
;++mult
)
107 const double target
{step
*mult
+ elems
[0]};
108 while(idx
< elems
.size() && target
-elems
[idx
] > epsilon
)
110 good
&= (idx
< elems
.size()) && !(std::abs(target
-elems
[idx
++]) > epsilon
);
118 /* Given a list of elevations, this will produce the smallest step size that
119 * can uniformly cover the list. Ideally this will be over half, but in
120 * degenerate cases this can fall to a minimum of 5 (the lower limit).
122 double GetUniformElevStep(const double epsilon
, std::vector
<double> &elems
)
124 if(elems
.size() < 5) return 0.0;
126 /* Reverse the elevations so it increments starting with -90 (flipped from
127 * +90). This makes it easier to work out a proper stepping value.
129 std::reverse(elems
.begin(), elems
.end());
130 for(auto &v
: elems
) v
*= -1.0;
132 uint count
{static_cast<uint
>(std::ceil(180.0 / (elems
[1]-elems
[0])))};
133 count
= std::min(count
, 255u);
136 for(;count
>= 5;--count
)
138 const double step
{180.0 / count
};
141 /* Elevations don't need to match all multiples if there's not enough
142 * elements to check. Missing elevations can be synthesized.
144 for(uint mult
{1u};mult
<= count
&& idx
< elems
.size() && good
;++mult
)
146 const double target
{step
*mult
+ elems
[0]};
147 while(idx
< elems
.size() && target
-elems
[idx
] > epsilon
)
149 good
&= !(idx
< elems
.size()) || !(std::abs(target
-elems
[idx
++]) > epsilon
);
157 /* Re-reverse the elevations to restore the correct order. */
158 for(auto &v
: elems
) v
*= -1.0;
159 std::reverse(elems
.begin(), elems
.end());
167 const char *SofaErrorStr(int err
)
171 case MYSOFA_OK
: return "OK";
172 case MYSOFA_INVALID_FORMAT
: return "Invalid format";
173 case MYSOFA_UNSUPPORTED_FORMAT
: return "Unsupported format";
174 case MYSOFA_INTERNAL_ERROR
: return "Internal error";
175 case MYSOFA_NO_MEMORY
: return "Out of memory";
176 case MYSOFA_READ_ERROR
: return "Read error";
181 auto GetCompatibleLayout(const al::span
<const float> xyzs
) -> std::vector
<SofaField
>
183 auto aers
= std::vector
<double3
>(xyzs
.size()/3, double3
{});
184 for(size_t i
{0u};i
< aers
.size();++i
)
186 std::array vals
{xyzs
[i
*3], xyzs
[i
*3 + 1], xyzs
[i
*3 + 2]};
187 mysofa_c2s(vals
.data());
188 aers
[i
] = {vals
[0], vals
[1], vals
[2]};
191 auto radii
= GetUniquelySortedElems(aers
, 2, {}, {0.1, 0.1, 0.001});
192 std::vector
<SofaField
> fds
;
193 fds
.reserve(radii
.size());
195 for(const double dist
: radii
)
197 auto elevs
= GetUniquelySortedElems(aers
, 1, {nullptr, nullptr, &dist
}, {0.1, 0.1, 0.001});
199 /* Remove elevations that don't have a valid set of azimuths. */
200 auto invalid_elev
= [&dist
,&aers
](const double ev
) -> bool
202 auto azims
= GetUniquelySortedElems(aers
, 0, {nullptr, &ev
, &dist
}, {0.1, 0.1, 0.001});
204 if(std::abs(ev
) > 89.999)
205 return azims
.size() != 1;
206 if(azims
.empty() || !(std::abs(azims
[0]) < 0.1))
208 return GetUniformAzimStep(0.1, azims
) <= 0.0;
210 elevs
.erase(std::remove_if(elevs
.begin(), elevs
.end(), invalid_elev
), elevs
.end());
212 double step
{GetUniformElevStep(0.1, elevs
)};
216 printf("No usable elevations on field distance %f.\n", dist
);
219 printf("Non-uniform elevations on field distance %.3f.\nGot: %+.2f", dist
,
221 for(size_t ei
{1u};ei
< elevs
.size();++ei
)
222 printf(", %+.2f", elevs
[ei
]);
229 for(uint ei
{0u};ei
< elevs
.size();ei
++)
231 if(!(elevs
[ei
] < 0.0))
233 printf("Too many missing elevations on field distance %f.\n", dist
);
237 double eif
{(90.0+elevs
[ei
]) / step
};
238 const double ev_start
{std::round(eif
)};
240 if(std::abs(eif
- ev_start
) < (0.1/step
))
242 evStart
= static_cast<uint
>(ev_start
);
247 const auto evCount
= static_cast<uint
>(std::round(180.0 / step
)) + 1;
250 printf("Too few uniform elevations on field distance %f.\n", dist
);
255 field
.mDistance
= dist
;
256 field
.mEvCount
= evCount
;
257 field
.mEvStart
= evStart
;
258 field
.mAzCounts
.resize(evCount
, 0u);
259 auto &azCounts
= field
.mAzCounts
;
261 for(uint ei
{evStart
};ei
< evCount
;ei
++)
263 double ev
{-90.0 + ei
*180.0/(evCount
- 1)};
264 auto azims
= GetUniquelySortedElems(aers
, 0, {nullptr, &ev
, &dist
}, {0.1, 0.1, 0.001});
266 if(ei
== 0 || ei
== (evCount
-1))
268 if(azims
.size() != 1)
270 printf("Non-singular poles on field distance %f.\n", dist
);
277 step
= GetUniformAzimStep(0.1, azims
);
280 printf("Non-uniform azimuths on elevation %f, field distance %f.\n", ev
, dist
);
283 azCounts
[ei
] = static_cast<uint
>(std::round(360.0f
/ step
));
287 fds
.emplace_back(std::move(field
));