makemhr.cpp 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512
  1. /*
  2. * HRTF utility for producing and demonstrating the process of creating an
  3. * OpenAL Soft compatible HRIR data set.
  4. *
  5. * Copyright (C) 2011-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. * --------------------------------------------------------------------------
  24. *
  25. * A big thanks goes out to all those whose work done in the field of
  26. * binaural sound synthesis using measured HRTFs makes this utility and the
  27. * OpenAL Soft implementation possible.
  28. *
  29. * The algorithm for diffuse-field equalization was adapted from the work
  30. * done by Rio Emmanuel and Larcher Veronique of IRCAM and Bill Gardner of
  31. * MIT Media Laboratory. It operates as follows:
  32. *
  33. * 1. Take the FFT of each HRIR and only keep the magnitude responses.
  34. * 2. Calculate the diffuse-field power-average of all HRIRs weighted by
  35. * their contribution to the total surface area covered by their
  36. * measurement. This has since been modified to use coverage volume for
  37. * multi-field HRIR data sets.
  38. * 3. Take the diffuse-field average and limit its magnitude range.
  39. * 4. Equalize the responses by using the inverse of the diffuse-field
  40. * average.
  41. * 5. Reconstruct the minimum-phase responses.
  42. * 5. Zero the DC component.
  43. * 6. IFFT the result and truncate to the desired-length minimum-phase FIR.
  44. *
  45. * The spherical head algorithm for calculating propagation delay was adapted
  46. * from the paper:
  47. *
  48. * Modeling Interaural Time Difference Assuming a Spherical Head
  49. * Joel David Miller
  50. * Music 150, Musical Acoustics, Stanford University
  51. * December 2, 2001
  52. *
  53. * The formulae for calculating the Kaiser window metrics are from the
  54. * the textbook:
  55. *
  56. * Discrete-Time Signal Processing
  57. * Alan V. Oppenheim and Ronald W. Schafer
  58. * Prentice-Hall Signal Processing Series
  59. * 1999
  60. */
  61. #define _UNICODE /* NOLINT(bugprone-reserved-identifier) */
  62. #include "config.h"
  63. #include "makemhr.h"
  64. #include <algorithm>
  65. #include <atomic>
  66. #include <chrono>
  67. #include <cmath>
  68. #include <complex>
  69. #include <cstdint>
  70. #include <cstdio>
  71. #include <cstdlib>
  72. #include <cstring>
  73. #include <filesystem>
  74. #include <fstream>
  75. #include <functional>
  76. #include <iostream>
  77. #include <limits>
  78. #include <memory>
  79. #include <numeric>
  80. #include <string_view>
  81. #include <thread>
  82. #include <utility>
  83. #include <vector>
  84. #include "alcomplex.h"
  85. #include "alnumbers.h"
  86. #include "alnumeric.h"
  87. #include "alspan.h"
  88. #include "alstring.h"
  89. #include "loaddef.h"
  90. #include "loadsofa.h"
  91. #include "win_main_utf8.h"
  92. HrirDataT::~HrirDataT() = default;
  93. namespace {
  94. using namespace std::string_view_literals;
  95. struct FileDeleter {
  96. void operator()(gsl::owner<FILE*> f) { fclose(f); }
  97. };
  98. using FilePtr = std::unique_ptr<FILE,FileDeleter>;
  99. // The epsilon used to maintain signal stability.
  100. constexpr double Epsilon{1e-9};
  101. // The limits to the FFT window size override on the command line.
  102. constexpr uint MinFftSize{65536};
  103. constexpr uint MaxFftSize{131072};
  104. // The limits to the equalization range limit on the command line.
  105. constexpr double MinLimit{2.0};
  106. constexpr double MaxLimit{120.0};
  107. // The limits to the truncation window size on the command line.
  108. constexpr uint MinTruncSize{16};
  109. constexpr uint MaxTruncSize{128};
  110. // The limits to the custom head radius on the command line.
  111. constexpr double MinCustomRadius{0.05};
  112. constexpr double MaxCustomRadius{0.15};
  113. // The maximum propagation delay value supported by OpenAL Soft.
  114. constexpr double MaxHrtd{63.0};
  115. // The OpenAL Soft HRTF format marker. It stands for minimum-phase head
  116. // response protocol 03.
  117. constexpr auto GetMHRMarker() noexcept { return "MinPHR03"sv; }
  118. // Head model used for calculating the impulse delays.
  119. enum HeadModelT {
  120. HM_None,
  121. HM_Dataset, // Measure the onset from the dataset.
  122. HM_Sphere, // Calculate the onset using a spherical head model.
  123. HM_Default = HM_Dataset
  124. };
  125. // The defaults for the command line options.
  126. constexpr uint DefaultFftSize{65536};
  127. constexpr bool DefaultEqualize{true};
  128. constexpr bool DefaultSurface{true};
  129. constexpr double DefaultLimit{24.0};
  130. constexpr uint DefaultTruncSize{64};
  131. constexpr double DefaultCustomRadius{0.0};
  132. /* Channel index enums. Mono uses LeftChannel only. */
  133. enum ChannelIndex : uint {
  134. LeftChannel = 0u,
  135. RightChannel = 1u
  136. };
  137. /* Performs a string substitution. Any case-insensitive occurrences of the
  138. * pattern string are replaced with the replacement string. The result is
  139. * truncated if necessary.
  140. */
  141. auto StrSubst(std::string_view in, const std::string_view pat, const std::string_view rep) -> std::string
  142. {
  143. std::string ret;
  144. ret.reserve(in.size() + pat.size());
  145. while(in.size() >= pat.size())
  146. {
  147. if(al::starts_with(in, pat))
  148. {
  149. in = in.substr(pat.size());
  150. ret += rep;
  151. }
  152. else
  153. {
  154. size_t endpos{1};
  155. while(endpos < in.size() && std::toupper(in[endpos]) != std::toupper(pat.front()))
  156. ++endpos;
  157. ret += in.substr(0, endpos);
  158. in = in.substr(endpos);
  159. }
  160. }
  161. ret += in;
  162. return ret;
  163. }
  164. /*********************
  165. *** Math routines ***
  166. *********************/
  167. // Simple clamp routine.
  168. double Clamp(const double val, const double lower, const double upper)
  169. {
  170. return std::min(std::max(val, lower), upper);
  171. }
  172. inline uint dither_rng(uint *seed)
  173. {
  174. *seed = *seed * 96314165 + 907633515;
  175. return *seed;
  176. }
  177. // Performs a triangular probability density function dither. The input samples
  178. // should be normalized (-1 to +1).
  179. void TpdfDither(const al::span<double> out, const al::span<const double> in, const double scale,
  180. const size_t channel, const size_t step, uint *seed)
  181. {
  182. static constexpr double PRNG_SCALE = 1.0 / std::numeric_limits<uint>::max();
  183. assert(channel < step);
  184. for(size_t i{0};i < in.size();++i)
  185. {
  186. uint prn0{dither_rng(seed)};
  187. uint prn1{dither_rng(seed)};
  188. out[i*step + channel] = std::round(in[i]*scale + (prn0*PRNG_SCALE - prn1*PRNG_SCALE));
  189. }
  190. }
  191. /* Apply a range limit (in dB) to the given magnitude response. This is used
  192. * to adjust the effects of the diffuse-field average on the equalization
  193. * process.
  194. */
  195. void LimitMagnitudeResponse(const uint n, const uint m, const double limit,
  196. const al::span<double> inout)
  197. {
  198. const double halfLim{limit / 2.0};
  199. // Convert the response to dB.
  200. for(uint i{0};i < m;++i)
  201. inout[i] = 20.0 * std::log10(inout[i]);
  202. // Use six octaves to calculate the average magnitude of the signal.
  203. const auto lower = (static_cast<uint>(std::ceil(n / std::pow(2.0, 8.0)))) - 1;
  204. const auto upper = (static_cast<uint>(std::floor(n / std::pow(2.0, 2.0)))) - 1;
  205. double ave{0.0};
  206. for(uint i{lower};i <= upper;++i)
  207. ave += inout[i];
  208. ave /= upper - lower + 1;
  209. // Keep the response within range of the average magnitude.
  210. for(uint i{0};i < m;++i)
  211. inout[i] = Clamp(inout[i], ave - halfLim, ave + halfLim);
  212. // Convert the response back to linear magnitude.
  213. for(uint i{0};i < m;++i)
  214. inout[i] = std::pow(10.0, inout[i] / 20.0);
  215. }
  216. /* Reconstructs the minimum-phase component for the given magnitude response
  217. * of a signal. This is equivalent to phase recomposition, sans the missing
  218. * residuals (which were discarded). The mirrored half of the response is
  219. * reconstructed.
  220. */
  221. void MinimumPhase(const al::span<double> mags, const al::span<complex_d> out)
  222. {
  223. assert(mags.size() == out.size());
  224. const size_t m{(mags.size()/2) + 1};
  225. size_t i;
  226. for(i = 0;i < m;i++)
  227. out[i] = std::log(mags[i]);
  228. for(;i < mags.size();++i)
  229. {
  230. mags[i] = mags[mags.size() - i];
  231. out[i] = out[mags.size() - i];
  232. }
  233. complex_hilbert(out);
  234. // Remove any DC offset the filter has.
  235. mags[0] = Epsilon;
  236. for(i = 0;i < mags.size();++i)
  237. out[i] = std::polar(mags[i], out[i].imag());
  238. }
  239. /***************************
  240. *** File storage output ***
  241. ***************************/
  242. // Write an ASCII string to a file.
  243. auto WriteAscii(const std::string_view out, std::ostream &ostream, const std::string_view filename) -> int
  244. {
  245. if(!ostream.write(out.data(), std::streamsize(out.size())) || ostream.bad())
  246. {
  247. fprintf(stderr, "\nError: Bad write to file '%.*s'.\n", al::sizei(filename),
  248. filename.data());
  249. return 0;
  250. }
  251. return 1;
  252. }
  253. // Write a binary value of the given byte order and byte size to a file,
  254. // loading it from a 32-bit unsigned integer.
  255. auto WriteBin4(const uint bytes, const uint32_t in, std::ostream &ostream,
  256. const std::string_view filename) -> int
  257. {
  258. std::array<char,4> out{};
  259. for(uint i{0};i < bytes;i++)
  260. out[i] = static_cast<char>((in>>(i*8)) & 0x000000FF);
  261. if(!ostream.write(out.data(), std::streamsize(bytes)) || ostream.bad())
  262. {
  263. fprintf(stderr, "\nError: Bad write to file '%.*s'.\n", al::sizei(filename),
  264. filename.data());
  265. return 0;
  266. }
  267. return 1;
  268. }
  269. // Store the OpenAL Soft HRTF data set.
  270. auto StoreMhr(const HrirDataT *hData, const std::string_view filename) -> bool
  271. {
  272. const uint channels{(hData->mChannelType == CT_STEREO) ? 2u : 1u};
  273. const uint n{hData->mIrPoints};
  274. uint dither_seed{22222};
  275. std::ofstream ostream{std::filesystem::u8path(filename)};
  276. if(!ostream.is_open())
  277. {
  278. fprintf(stderr, "\nError: Could not open MHR file '%.*s'.\n", al::sizei(filename),
  279. filename.data());
  280. return false;
  281. }
  282. if(!WriteAscii(GetMHRMarker(), ostream, filename))
  283. return false;
  284. if(!WriteBin4(4, hData->mIrRate, ostream, filename))
  285. return false;
  286. if(!WriteBin4(1, static_cast<uint32_t>(hData->mChannelType), ostream, filename))
  287. return false;
  288. if(!WriteBin4(1, hData->mIrPoints, ostream, filename))
  289. return false;
  290. if(!WriteBin4(1, static_cast<uint>(hData->mFds.size()), ostream, filename))
  291. return false;
  292. for(size_t fi{hData->mFds.size()-1};fi < hData->mFds.size();--fi)
  293. {
  294. auto fdist = static_cast<uint32_t>(std::round(1000.0 * hData->mFds[fi].mDistance));
  295. if(!WriteBin4(2, fdist, ostream, filename))
  296. return false;
  297. if(!WriteBin4(1, static_cast<uint32_t>(hData->mFds[fi].mEvs.size()), ostream, filename))
  298. return false;
  299. for(size_t ei{0};ei < hData->mFds[fi].mEvs.size();++ei)
  300. {
  301. const auto &elev = hData->mFds[fi].mEvs[ei];
  302. if(!WriteBin4(1, static_cast<uint32_t>(elev.mAzs.size()), ostream, filename))
  303. return false;
  304. }
  305. }
  306. for(size_t fi{hData->mFds.size()-1};fi < hData->mFds.size();--fi)
  307. {
  308. static constexpr double scale{8388607.0};
  309. static constexpr uint bps{3u};
  310. for(const auto &evd : hData->mFds[fi].mEvs)
  311. {
  312. for(const auto &azd : evd.mAzs)
  313. {
  314. std::array<double,MaxTruncSize*2_uz> out{};
  315. TpdfDither(out, azd.mIrs[0].first(n), scale, 0, channels, &dither_seed);
  316. if(hData->mChannelType == CT_STEREO)
  317. TpdfDither(out, azd.mIrs[1].first(n), scale, 1, channels, &dither_seed);
  318. const size_t numsamples{size_t{channels} * n};
  319. for(size_t i{0};i < numsamples;i++)
  320. {
  321. const auto v = static_cast<int>(Clamp(out[i], -scale-1.0, scale));
  322. if(!WriteBin4(bps, static_cast<uint32_t>(v), ostream, filename))
  323. return false;
  324. }
  325. }
  326. }
  327. }
  328. for(size_t fi{hData->mFds.size()-1};fi < hData->mFds.size();--fi)
  329. {
  330. /* Delay storage has 2 bits of extra precision. */
  331. static constexpr double DelayPrecScale{4.0};
  332. for(const auto &evd : hData->mFds[fi].mEvs)
  333. {
  334. for(const auto &azd : evd.mAzs)
  335. {
  336. auto v = static_cast<uint>(std::round(azd.mDelays[0]*DelayPrecScale));
  337. if(!WriteBin4(1, v, ostream, filename)) return false;
  338. if(hData->mChannelType == CT_STEREO)
  339. {
  340. v = static_cast<uint>(std::round(azd.mDelays[1]*DelayPrecScale));
  341. if(!WriteBin4(1, v, ostream, filename)) return false;
  342. }
  343. }
  344. }
  345. }
  346. return true;
  347. }
  348. /***********************
  349. *** HRTF processing ***
  350. ***********************/
  351. /* Balances the maximum HRIR magnitudes of multi-field data sets by
  352. * independently normalizing each field in relation to the overall maximum.
  353. * This is done to ignore distance attenuation.
  354. */
  355. void BalanceFieldMagnitudes(const HrirDataT *hData, const uint channels, const uint m)
  356. {
  357. std::array<double,MAX_FD_COUNT> maxMags{};
  358. double maxMag{0.0};
  359. for(size_t fi{0};fi < hData->mFds.size();++fi)
  360. {
  361. for(size_t ei{hData->mFds[fi].mEvStart};ei < hData->mFds[fi].mEvs.size();++ei)
  362. {
  363. for(const auto &azd : hData->mFds[fi].mEvs[ei].mAzs)
  364. {
  365. for(size_t ti{0};ti < channels;++ti)
  366. {
  367. for(size_t i{0};i < m;++i)
  368. maxMags[fi] = std::max(azd.mIrs[ti][i], maxMags[fi]);
  369. }
  370. }
  371. }
  372. maxMag = std::max(maxMags[fi], maxMag);
  373. }
  374. for(size_t fi{0};fi < hData->mFds.size();++fi)
  375. {
  376. const double magFactor{maxMag / maxMags[fi]};
  377. for(size_t ei{hData->mFds[fi].mEvStart};ei < hData->mFds[fi].mEvs.size();++ei)
  378. {
  379. for(const auto &azd : hData->mFds[fi].mEvs[ei].mAzs)
  380. {
  381. for(size_t ti{0};ti < channels;++ti)
  382. {
  383. for(size_t i{0};i < m;++i)
  384. azd.mIrs[ti][i] *= magFactor;
  385. }
  386. }
  387. }
  388. }
  389. }
  390. /* Calculate the contribution of each HRIR to the diffuse-field average based
  391. * on its coverage volume. All volumes are centered at the spherical HRIR
  392. * coordinates and measured by extruded solid angle.
  393. */
  394. void CalculateDfWeights(const HrirDataT *hData, const al::span<double> weights)
  395. {
  396. double sum, innerRa, outerRa, evs, ev, upperEv, lowerEv;
  397. double solidAngle, solidVolume;
  398. uint fi, ei;
  399. sum = 0.0;
  400. // The head radius acts as the limit for the inner radius.
  401. innerRa = hData->mRadius;
  402. for(fi = 0;fi < hData->mFds.size();fi++)
  403. {
  404. // Each volume ends half way between progressive field measurements.
  405. if((fi + 1) < hData->mFds.size())
  406. outerRa = 0.5f * (hData->mFds[fi].mDistance + hData->mFds[fi + 1].mDistance);
  407. // The final volume has its limit extended to some practical value.
  408. // This is done to emphasize the far-field responses in the average.
  409. else
  410. outerRa = 10.0f;
  411. const double raPowDiff{std::pow(outerRa, 3.0) - std::pow(innerRa, 3.0)};
  412. evs = al::numbers::pi / 2.0 / static_cast<double>(hData->mFds[fi].mEvs.size() - 1);
  413. for(ei = hData->mFds[fi].mEvStart;ei < hData->mFds[fi].mEvs.size();ei++)
  414. {
  415. const auto &elev = hData->mFds[fi].mEvs[ei];
  416. // For each elevation, calculate the upper and lower limits of
  417. // the patch band.
  418. ev = elev.mElevation;
  419. lowerEv = std::max(-al::numbers::pi / 2.0, ev - evs);
  420. upperEv = std::min(al::numbers::pi / 2.0, ev + evs);
  421. // Calculate the surface area of the patch band.
  422. solidAngle = 2.0 * al::numbers::pi * (std::sin(upperEv) - std::sin(lowerEv));
  423. // Then the volume of the extruded patch band.
  424. solidVolume = solidAngle * raPowDiff / 3.0;
  425. // Each weight is the volume of one extruded patch.
  426. weights[(fi*MAX_EV_COUNT) + ei] = solidVolume / static_cast<double>(elev.mAzs.size());
  427. // Sum the total coverage volume of the HRIRs for all fields.
  428. sum += solidAngle;
  429. }
  430. innerRa = outerRa;
  431. }
  432. for(fi = 0;fi < hData->mFds.size();fi++)
  433. {
  434. // Normalize the weights given the total surface coverage for all
  435. // fields.
  436. for(ei = hData->mFds[fi].mEvStart;ei < hData->mFds[fi].mEvs.size();ei++)
  437. weights[(fi * MAX_EV_COUNT) + ei] /= sum;
  438. }
  439. }
  440. /* Calculate the diffuse-field average from the given magnitude responses of
  441. * the HRIR set. Weighting can be applied to compensate for the varying
  442. * coverage of each HRIR. The final average can then be limited by the
  443. * specified magnitude range (in positive dB; 0.0 to skip).
  444. */
  445. void CalculateDiffuseFieldAverage(const HrirDataT *hData, const uint channels, const uint m,
  446. const bool weighted, const double limit, const al::span<double> dfa)
  447. {
  448. std::vector<double> weights(hData->mFds.size() * MAX_EV_COUNT);
  449. uint count;
  450. if(weighted)
  451. {
  452. // Use coverage weighting to calculate the average.
  453. CalculateDfWeights(hData, weights);
  454. }
  455. else
  456. {
  457. double weight;
  458. // If coverage weighting is not used, the weights still need to be
  459. // averaged by the number of existing HRIRs.
  460. count = hData->mIrCount;
  461. for(size_t fi{0};fi < hData->mFds.size();++fi)
  462. {
  463. for(size_t ei{0};ei < hData->mFds[fi].mEvStart;++ei)
  464. count -= static_cast<uint>(hData->mFds[fi].mEvs[ei].mAzs.size());
  465. }
  466. weight = 1.0 / count;
  467. for(size_t fi{0};fi < hData->mFds.size();++fi)
  468. {
  469. for(size_t ei{hData->mFds[fi].mEvStart};ei < hData->mFds[fi].mEvs.size();++ei)
  470. weights[(fi * MAX_EV_COUNT) + ei] = weight;
  471. }
  472. }
  473. for(size_t ti{0};ti < channels;++ti)
  474. {
  475. for(size_t i{0};i < m;++i)
  476. dfa[(ti * m) + i] = 0.0;
  477. for(size_t fi{0};fi < hData->mFds.size();++fi)
  478. {
  479. for(size_t ei{hData->mFds[fi].mEvStart};ei < hData->mFds[fi].mEvs.size();++ei)
  480. {
  481. for(size_t ai{0};ai < hData->mFds[fi].mEvs[ei].mAzs.size();++ai)
  482. {
  483. HrirAzT *azd = &hData->mFds[fi].mEvs[ei].mAzs[ai];
  484. // Get the weight for this HRIR's contribution.
  485. double weight = weights[(fi * MAX_EV_COUNT) + ei];
  486. // Add this HRIR's weighted power average to the total.
  487. for(size_t i{0};i < m;++i)
  488. dfa[(ti * m) + i] += weight * azd->mIrs[ti][i] * azd->mIrs[ti][i];
  489. }
  490. }
  491. }
  492. // Finish the average calculation and keep it from being too small.
  493. for(size_t i{0};i < m;++i)
  494. dfa[(ti * m) + i] = std::max(sqrt(dfa[(ti * m) + i]), Epsilon);
  495. // Apply a limit to the magnitude range of the diffuse-field average
  496. // if desired.
  497. if(limit > 0.0)
  498. LimitMagnitudeResponse(hData->mFftSize, m, limit, dfa.subspan(ti * m));
  499. }
  500. }
  501. // Perform diffuse-field equalization on the magnitude responses of the HRIR
  502. // set using the given average response.
  503. void DiffuseFieldEqualize(const uint channels, const uint m, const al::span<const double> dfa,
  504. const HrirDataT *hData)
  505. {
  506. for(size_t fi{0};fi < hData->mFds.size();++fi)
  507. {
  508. for(size_t ei{hData->mFds[fi].mEvStart};ei < hData->mFds[fi].mEvs.size();++ei)
  509. {
  510. for(auto &azd : hData->mFds[fi].mEvs[ei].mAzs)
  511. {
  512. for(size_t ti{0};ti < channels;++ti)
  513. {
  514. for(size_t i{0};i < m;++i)
  515. azd.mIrs[ti][i] /= dfa[(ti * m) + i];
  516. }
  517. }
  518. }
  519. }
  520. }
  521. /* Given field and elevation indices and an azimuth, calculate the indices of
  522. * the two HRIRs that bound the coordinate along with a factor for
  523. * calculating the continuous HRIR using interpolation.
  524. */
  525. void CalcAzIndices(const HrirFdT &field, const uint ei, const double az, uint *a0, uint *a1, double *af)
  526. {
  527. double f{(2.0*al::numbers::pi + az) * static_cast<double>(field.mEvs[ei].mAzs.size()) /
  528. (2.0*al::numbers::pi)};
  529. const uint i{static_cast<uint>(f) % static_cast<uint>(field.mEvs[ei].mAzs.size())};
  530. f -= std::floor(f);
  531. *a0 = i;
  532. *a1 = (i + 1) % static_cast<uint>(field.mEvs[ei].mAzs.size());
  533. *af = f;
  534. }
  535. /* Synthesize any missing onset timings at the bottom elevations of each field.
  536. * This just mirrors some top elevations for the bottom, and blends the
  537. * remaining elevations (not an accurate model).
  538. */
  539. void SynthesizeOnsets(HrirDataT *hData)
  540. {
  541. const uint channels{(hData->mChannelType == CT_STEREO) ? 2u : 1u};
  542. auto proc_field = [channels](HrirFdT &field) -> void
  543. {
  544. /* Get the starting elevation from the measurements, and use it as the
  545. * upper elevation limit for what needs to be calculated.
  546. */
  547. const uint upperElevReal{field.mEvStart};
  548. if(upperElevReal <= 0) return;
  549. /* Get the lowest half of the missing elevations' delays by mirroring
  550. * the top elevation delays. The responses are on a spherical grid
  551. * centered between the ears, so these should align.
  552. */
  553. uint ei{};
  554. if(channels > 1)
  555. {
  556. /* Take the polar opposite position of the desired measurement and
  557. * swap the ears.
  558. */
  559. field.mEvs[0].mAzs[0].mDelays[0] = field.mEvs[field.mEvs.size()-1].mAzs[0].mDelays[1];
  560. field.mEvs[0].mAzs[0].mDelays[1] = field.mEvs[field.mEvs.size()-1].mAzs[0].mDelays[0];
  561. for(ei = 1u;ei < (upperElevReal+1)/2;++ei)
  562. {
  563. const uint topElev{static_cast<uint>(field.mEvs.size()-ei-1)};
  564. for(uint ai{0u};ai < field.mEvs[ei].mAzs.size();ai++)
  565. {
  566. uint a0, a1;
  567. double af;
  568. /* Rotate this current azimuth by a half-circle, and lookup
  569. * the mirrored elevation to find the indices for the polar
  570. * opposite position (may need blending).
  571. */
  572. const double az{field.mEvs[ei].mAzs[ai].mAzimuth + al::numbers::pi};
  573. CalcAzIndices(field, topElev, az, &a0, &a1, &af);
  574. /* Blend the delays, and again, swap the ears. */
  575. field.mEvs[ei].mAzs[ai].mDelays[0] = Lerp(
  576. field.mEvs[topElev].mAzs[a0].mDelays[1],
  577. field.mEvs[topElev].mAzs[a1].mDelays[1], af);
  578. field.mEvs[ei].mAzs[ai].mDelays[1] = Lerp(
  579. field.mEvs[topElev].mAzs[a0].mDelays[0],
  580. field.mEvs[topElev].mAzs[a1].mDelays[0], af);
  581. }
  582. }
  583. }
  584. else
  585. {
  586. field.mEvs[0].mAzs[0].mDelays[0] = field.mEvs[field.mEvs.size()-1].mAzs[0].mDelays[0];
  587. for(ei = 1u;ei < (upperElevReal+1)/2;++ei)
  588. {
  589. const uint topElev{static_cast<uint>(field.mEvs.size()-ei-1)};
  590. for(uint ai{0u};ai < field.mEvs[ei].mAzs.size();ai++)
  591. {
  592. uint a0, a1;
  593. double af;
  594. /* For mono data sets, mirror the azimuth front<->back
  595. * since the other ear is a mirror of what we have (e.g.
  596. * the left ear's back-left is simulated with the right
  597. * ear's front-right, which uses the left ear's front-left
  598. * measurement).
  599. */
  600. double az{field.mEvs[ei].mAzs[ai].mAzimuth};
  601. if(az <= al::numbers::pi) az = al::numbers::pi - az;
  602. else az = (al::numbers::pi*2.0)-az + al::numbers::pi;
  603. CalcAzIndices(field, topElev, az, &a0, &a1, &af);
  604. field.mEvs[ei].mAzs[ai].mDelays[0] = Lerp(
  605. field.mEvs[topElev].mAzs[a0].mDelays[0],
  606. field.mEvs[topElev].mAzs[a1].mDelays[0], af);
  607. }
  608. }
  609. }
  610. /* Record the lowest elevation filled in with the mirrored top. */
  611. const uint lowerElevFake{ei-1u};
  612. /* Fill in the remaining delays using bilinear interpolation. This
  613. * helps smooth the transition back to the real delays.
  614. */
  615. for(;ei < upperElevReal;++ei)
  616. {
  617. const double ef{(field.mEvs[upperElevReal].mElevation - field.mEvs[ei].mElevation) /
  618. (field.mEvs[upperElevReal].mElevation - field.mEvs[lowerElevFake].mElevation)};
  619. for(uint ai{0u};ai < field.mEvs[ei].mAzs.size();ai++)
  620. {
  621. uint a0, a1, a2, a3;
  622. double af0, af1;
  623. double az{field.mEvs[ei].mAzs[ai].mAzimuth};
  624. CalcAzIndices(field, upperElevReal, az, &a0, &a1, &af0);
  625. CalcAzIndices(field, lowerElevFake, az, &a2, &a3, &af1);
  626. std::array<double,4> blend{{
  627. (1.0-ef) * (1.0-af0),
  628. (1.0-ef) * ( af0),
  629. ( ef) * (1.0-af1),
  630. ( ef) * ( af1)
  631. }};
  632. for(uint ti{0u};ti < channels;ti++)
  633. {
  634. field.mEvs[ei].mAzs[ai].mDelays[ti] =
  635. field.mEvs[upperElevReal].mAzs[a0].mDelays[ti]*blend[0] +
  636. field.mEvs[upperElevReal].mAzs[a1].mDelays[ti]*blend[1] +
  637. field.mEvs[lowerElevFake].mAzs[a2].mDelays[ti]*blend[2] +
  638. field.mEvs[lowerElevFake].mAzs[a3].mDelays[ti]*blend[3];
  639. }
  640. }
  641. }
  642. };
  643. std::for_each(hData->mFds.begin(), hData->mFds.end(), proc_field);
  644. }
  645. /* Attempt to synthesize any missing HRIRs at the bottom elevations of each
  646. * field. Right now this just blends the lowest elevation HRIRs together and
  647. * applies a low-pass filter to simulate body occlusion. It is a simple, if
  648. * inaccurate model.
  649. */
  650. void SynthesizeHrirs(HrirDataT *hData)
  651. {
  652. const uint channels{(hData->mChannelType == CT_STEREO) ? 2u : 1u};
  653. auto htemp = std::vector<complex_d>(hData->mFftSize);
  654. const uint m{hData->mFftSize/2u + 1u};
  655. auto filter = std::vector<double>(m);
  656. const double beta{3.5e-6 * hData->mIrRate};
  657. auto proc_field = [channels,m,beta,&htemp,&filter](HrirFdT &field) -> void
  658. {
  659. const uint oi{field.mEvStart};
  660. if(oi <= 0) return;
  661. for(uint ti{0u};ti < channels;ti++)
  662. {
  663. uint a0, a1;
  664. double af;
  665. /* Use the lowest immediate-left response for the left ear and
  666. * lowest immediate-right response for the right ear. Given no comb
  667. * effects as a result of the left response reaching the right ear
  668. * and vice-versa, this produces a decent phantom-center response
  669. * underneath the head.
  670. */
  671. CalcAzIndices(field, oi, al::numbers::pi / ((ti==0) ? -2.0 : 2.0), &a0, &a1, &af);
  672. for(uint i{0u};i < m;i++)
  673. {
  674. field.mEvs[0].mAzs[0].mIrs[ti][i] = Lerp(field.mEvs[oi].mAzs[a0].mIrs[ti][i],
  675. field.mEvs[oi].mAzs[a1].mIrs[ti][i], af);
  676. }
  677. }
  678. for(uint ei{1u};ei < field.mEvStart;ei++)
  679. {
  680. const double of{static_cast<double>(ei) / field.mEvStart};
  681. const double b{(1.0 - of) * beta};
  682. std::array<double,4> lp{};
  683. /* Calculate a low-pass filter to simulate body occlusion. */
  684. lp[0] = Lerp(1.0, lp[0], b);
  685. lp[1] = Lerp(lp[0], lp[1], b);
  686. lp[2] = Lerp(lp[1], lp[2], b);
  687. lp[3] = Lerp(lp[2], lp[3], b);
  688. htemp[0] = lp[3];
  689. for(size_t i{1u};i < htemp.size();i++)
  690. {
  691. lp[0] = Lerp(0.0, lp[0], b);
  692. lp[1] = Lerp(lp[0], lp[1], b);
  693. lp[2] = Lerp(lp[1], lp[2], b);
  694. lp[3] = Lerp(lp[2], lp[3], b);
  695. htemp[i] = lp[3];
  696. }
  697. /* Get the filter's frequency-domain response and extract the
  698. * frequency magnitudes (phase will be reconstructed later)).
  699. */
  700. FftForward(static_cast<uint>(htemp.size()), htemp.data());
  701. std::transform(htemp.cbegin(), htemp.cbegin()+m, filter.begin(),
  702. [](const complex_d c) -> double { return std::abs(c); });
  703. for(uint ai{0u};ai < field.mEvs[ei].mAzs.size();ai++)
  704. {
  705. uint a0, a1;
  706. double af;
  707. CalcAzIndices(field, oi, field.mEvs[ei].mAzs[ai].mAzimuth, &a0, &a1, &af);
  708. for(uint ti{0u};ti < channels;ti++)
  709. {
  710. for(uint i{0u};i < m;i++)
  711. {
  712. /* Blend the two defined HRIRs closest to this azimuth,
  713. * then blend that with the synthesized -90 elevation.
  714. */
  715. const double s1{Lerp(field.mEvs[oi].mAzs[a0].mIrs[ti][i],
  716. field.mEvs[oi].mAzs[a1].mIrs[ti][i], af)};
  717. const double s{Lerp(field.mEvs[0].mAzs[0].mIrs[ti][i], s1, of)};
  718. field.mEvs[ei].mAzs[ai].mIrs[ti][i] = s * filter[i];
  719. }
  720. }
  721. }
  722. }
  723. const double b{beta};
  724. std::array<double,4> lp{};
  725. lp[0] = Lerp(1.0, lp[0], b);
  726. lp[1] = Lerp(lp[0], lp[1], b);
  727. lp[2] = Lerp(lp[1], lp[2], b);
  728. lp[3] = Lerp(lp[2], lp[3], b);
  729. htemp[0] = lp[3];
  730. for(size_t i{1u};i < htemp.size();i++)
  731. {
  732. lp[0] = Lerp(0.0, lp[0], b);
  733. lp[1] = Lerp(lp[0], lp[1], b);
  734. lp[2] = Lerp(lp[1], lp[2], b);
  735. lp[3] = Lerp(lp[2], lp[3], b);
  736. htemp[i] = lp[3];
  737. }
  738. FftForward(static_cast<uint>(htemp.size()), htemp.data());
  739. std::transform(htemp.cbegin(), htemp.cbegin()+m, filter.begin(),
  740. [](const complex_d c) -> double { return std::abs(c); });
  741. for(uint ti{0u};ti < channels;ti++)
  742. {
  743. for(uint i{0u};i < m;i++)
  744. field.mEvs[0].mAzs[0].mIrs[ti][i] *= filter[i];
  745. }
  746. };
  747. std::for_each(hData->mFds.begin(), hData->mFds.end(), proc_field);
  748. }
  749. // The following routines assume a full set of HRIRs for all elevations.
  750. /* Perform minimum-phase reconstruction using the magnitude responses of the
  751. * HRIR set. Work is delegated to this struct, which runs asynchronously on one
  752. * or more threads (sharing the same reconstructor object).
  753. */
  754. struct HrirReconstructor {
  755. std::vector<al::span<double>> mIrs;
  756. std::atomic<size_t> mCurrent{};
  757. std::atomic<size_t> mDone{};
  758. uint mFftSize{};
  759. uint mIrPoints{};
  760. void Worker()
  761. {
  762. auto h = std::vector<complex_d>(mFftSize);
  763. auto mags = std::vector<double>(mFftSize);
  764. size_t m{(mFftSize/2) + 1};
  765. while(true)
  766. {
  767. /* Load the current index to process. */
  768. size_t idx{mCurrent.load()};
  769. do {
  770. /* If the index is at the end, we're done. */
  771. if(idx >= mIrs.size())
  772. return;
  773. /* Otherwise, increment the current index atomically so other
  774. * threads know to go to the next one. If this call fails, the
  775. * current index was just changed by another thread and the new
  776. * value is loaded into idx, which we'll recheck.
  777. */
  778. } while(!mCurrent.compare_exchange_weak(idx, idx+1, std::memory_order_relaxed));
  779. /* Now do the reconstruction, and apply the inverse FFT to get the
  780. * time-domain response.
  781. */
  782. for(size_t i{0};i < m;++i)
  783. mags[i] = std::max(mIrs[idx][i], Epsilon);
  784. MinimumPhase(mags, h);
  785. FftInverse(mFftSize, h.data());
  786. for(uint i{0u};i < mIrPoints;++i)
  787. mIrs[idx][i] = h[i].real();
  788. /* Increment the number of IRs done. */
  789. mDone.fetch_add(1);
  790. }
  791. }
  792. };
  793. void ReconstructHrirs(const HrirDataT *hData, const uint numThreads)
  794. {
  795. const uint channels{(hData->mChannelType == CT_STEREO) ? 2u : 1u};
  796. /* Set up the reconstructor with the needed size info and pointers to the
  797. * IRs to process.
  798. */
  799. HrirReconstructor reconstructor;
  800. reconstructor.mCurrent.store(0, std::memory_order_relaxed);
  801. reconstructor.mDone.store(0, std::memory_order_relaxed);
  802. reconstructor.mFftSize = hData->mFftSize;
  803. reconstructor.mIrPoints = hData->mIrPoints;
  804. for(const auto &field : hData->mFds)
  805. {
  806. for(auto &elev : field.mEvs)
  807. {
  808. for(const auto &azd : elev.mAzs)
  809. {
  810. for(uint ti{0u};ti < channels;ti++)
  811. reconstructor.mIrs.push_back(azd.mIrs[ti]);
  812. }
  813. }
  814. }
  815. /* Launch threads to work on reconstruction. */
  816. std::vector<std::thread> thrds;
  817. thrds.reserve(numThreads);
  818. for(size_t i{0};i < numThreads;++i)
  819. thrds.emplace_back(std::mem_fn(&HrirReconstructor::Worker), &reconstructor);
  820. /* Keep track of the number of IRs done, periodically reporting it. */
  821. size_t count;
  822. do {
  823. std::this_thread::sleep_for(std::chrono::milliseconds{50});
  824. count = reconstructor.mDone.load();
  825. size_t pcdone{count * 100 / reconstructor.mIrs.size()};
  826. printf("\r%3zu%% done (%zu of %zu)", pcdone, count, reconstructor.mIrs.size());
  827. fflush(stdout);
  828. } while(count < reconstructor.mIrs.size());
  829. fputc('\n', stdout);
  830. for(auto &thrd : thrds)
  831. {
  832. if(thrd.joinable())
  833. thrd.join();
  834. }
  835. }
  836. // Normalize the HRIR set and slightly attenuate the result.
  837. void NormalizeHrirs(HrirDataT *hData)
  838. {
  839. const uint channels{(hData->mChannelType == CT_STEREO) ? 2u : 1u};
  840. const uint irSize{hData->mIrPoints};
  841. /* Find the maximum amplitude and RMS out of all the IRs. */
  842. struct LevelPair { double amp, rms; };
  843. auto mesasure_channel = [irSize](const LevelPair levels, al::span<const double> ir)
  844. {
  845. /* Calculate the peak amplitude and RMS of this IR. */
  846. ir = ir.first(irSize);
  847. auto current = std::accumulate(ir.cbegin(), ir.cend(), LevelPair{0.0, 0.0},
  848. [](const LevelPair cur, const double impulse)
  849. {
  850. return LevelPair{std::max(std::abs(impulse), cur.amp), cur.rms + impulse*impulse};
  851. });
  852. current.rms = std::sqrt(current.rms / irSize);
  853. /* Accumulate levels by taking the maximum amplitude and RMS. */
  854. return LevelPair{std::max(current.amp, levels.amp), std::max(current.rms, levels.rms)};
  855. };
  856. auto measure_azi = [channels,mesasure_channel](const LevelPair levels, const HrirAzT &azi)
  857. { return std::accumulate(azi.mIrs.begin(), azi.mIrs.begin()+channels, levels, mesasure_channel); };
  858. auto measure_elev = [measure_azi](const LevelPair levels, const HrirEvT &elev)
  859. { return std::accumulate(elev.mAzs.cbegin(), elev.mAzs.cend(), levels, measure_azi); };
  860. auto measure_field = [measure_elev](const LevelPair levels, const HrirFdT &field)
  861. { return std::accumulate(field.mEvs.cbegin(), field.mEvs.cend(), levels, measure_elev); };
  862. const auto maxlev = std::accumulate(hData->mFds.begin(), hData->mFds.end(),
  863. LevelPair{0.0, 0.0}, measure_field);
  864. /* Normalize using the maximum RMS of the HRIRs. The RMS measure for the
  865. * non-filtered signal is of an impulse with equal length (to the filter):
  866. *
  867. * rms_impulse = sqrt(sum([ 1^2, 0^2, 0^2, ... ]) / n)
  868. * = sqrt(1 / n)
  869. *
  870. * This helps keep a more consistent volume between the non-filtered signal
  871. * and various data sets.
  872. */
  873. double factor{std::sqrt(1.0 / irSize) / maxlev.rms};
  874. /* Also ensure the samples themselves won't clip. */
  875. factor = std::min(factor, 0.99/maxlev.amp);
  876. /* Now scale all IRs by the given factor. */
  877. auto proc_channel = [irSize,factor](al::span<double> ir)
  878. {
  879. ir = ir.first(irSize);
  880. std::transform(ir.cbegin(), ir.cend(), ir.begin(),
  881. [factor](double s) { return s * factor; });
  882. };
  883. auto proc_azi = [channels,proc_channel](HrirAzT &azi)
  884. { std::for_each(azi.mIrs.begin(), azi.mIrs.begin()+channels, proc_channel); };
  885. auto proc_elev = [proc_azi](HrirEvT &elev)
  886. { std::for_each(elev.mAzs.begin(), elev.mAzs.end(), proc_azi); };
  887. auto proc1_field = [proc_elev](HrirFdT &field)
  888. { std::for_each(field.mEvs.begin(), field.mEvs.end(), proc_elev); };
  889. std::for_each(hData->mFds.begin(), hData->mFds.end(), proc1_field);
  890. }
  891. // Calculate the left-ear time delay using a spherical head model.
  892. double CalcLTD(const double ev, const double az, const double rad, const double dist)
  893. {
  894. double azp, dlp, l, al;
  895. azp = std::asin(std::cos(ev) * std::sin(az));
  896. dlp = std::sqrt((dist*dist) + (rad*rad) + (2.0*dist*rad*sin(azp)));
  897. l = std::sqrt((dist*dist) - (rad*rad));
  898. al = (0.5 * al::numbers::pi) + azp;
  899. if(dlp > l)
  900. dlp = l + (rad * (al - std::acos(rad / dist)));
  901. return dlp / 343.3;
  902. }
  903. // Calculate the effective head-related time delays for each minimum-phase
  904. // HRIR. This is done per-field since distance delay is ignored.
  905. void CalculateHrtds(const HeadModelT model, const double radius, HrirDataT *hData)
  906. {
  907. uint channels = (hData->mChannelType == CT_STEREO) ? 2 : 1;
  908. double customRatio{radius / hData->mRadius};
  909. uint ti;
  910. if(model == HM_Sphere)
  911. {
  912. for(auto &field : hData->mFds)
  913. {
  914. for(auto &elev : field.mEvs)
  915. {
  916. for(auto &azd : elev.mAzs)
  917. {
  918. for(ti = 0;ti < channels;ti++)
  919. azd.mDelays[ti] = CalcLTD(elev.mElevation, azd.mAzimuth, radius, field.mDistance);
  920. }
  921. }
  922. }
  923. }
  924. else if(customRatio != 1.0)
  925. {
  926. for(auto &field : hData->mFds)
  927. {
  928. for(auto &elev : field.mEvs)
  929. {
  930. for(auto &azd : elev.mAzs)
  931. {
  932. for(ti = 0;ti < channels;ti++)
  933. azd.mDelays[ti] *= customRatio;
  934. }
  935. }
  936. }
  937. }
  938. double maxHrtd{0.0};
  939. for(auto &field : hData->mFds)
  940. {
  941. double minHrtd{std::numeric_limits<double>::infinity()};
  942. for(auto &elev : field.mEvs)
  943. {
  944. for(auto &azd : elev.mAzs)
  945. {
  946. for(ti = 0;ti < channels;ti++)
  947. minHrtd = std::min(azd.mDelays[ti], minHrtd);
  948. }
  949. }
  950. for(auto &elev : field.mEvs)
  951. {
  952. for(auto &azd : elev.mAzs)
  953. {
  954. for(ti = 0;ti < channels;ti++)
  955. {
  956. azd.mDelays[ti] = (azd.mDelays[ti]-minHrtd) * hData->mIrRate;
  957. maxHrtd = std::max(maxHrtd, azd.mDelays[ti]);
  958. }
  959. }
  960. }
  961. }
  962. if(maxHrtd > MaxHrtd)
  963. {
  964. fprintf(stdout, " Scaling for max delay of %f samples to %f\n...\n", maxHrtd, MaxHrtd);
  965. const double scale{MaxHrtd / maxHrtd};
  966. for(auto &field : hData->mFds)
  967. {
  968. for(auto &elev : field.mEvs)
  969. {
  970. for(auto &azd : elev.mAzs)
  971. {
  972. for(ti = 0;ti < channels;ti++)
  973. azd.mDelays[ti] *= scale;
  974. }
  975. }
  976. }
  977. }
  978. }
  979. } // namespace
  980. // Allocate and configure dynamic HRIR structures.
  981. bool PrepareHrirData(const al::span<const double> distances,
  982. const al::span<const uint,MAX_FD_COUNT> evCounts,
  983. const al::span<const std::array<uint,MAX_EV_COUNT>,MAX_FD_COUNT> azCounts, HrirDataT *hData)
  984. {
  985. uint evTotal{0}, azTotal{0};
  986. for(size_t fi{0};fi < distances.size();++fi)
  987. {
  988. evTotal += evCounts[fi];
  989. for(size_t ei{0};ei < evCounts[fi];++ei)
  990. azTotal += azCounts[fi][ei];
  991. }
  992. if(!evTotal || !azTotal)
  993. return false;
  994. hData->mEvsBase.resize(evTotal);
  995. hData->mAzsBase.resize(azTotal);
  996. hData->mFds.resize(distances.size());
  997. hData->mIrCount = azTotal;
  998. evTotal = 0;
  999. azTotal = 0;
  1000. for(size_t fi{0};fi < distances.size();++fi)
  1001. {
  1002. hData->mFds[fi].mDistance = distances[fi];
  1003. hData->mFds[fi].mEvStart = 0;
  1004. hData->mFds[fi].mEvs = al::span{hData->mEvsBase}.subspan(evTotal, evCounts[fi]);
  1005. evTotal += evCounts[fi];
  1006. for(uint ei{0};ei < evCounts[fi];++ei)
  1007. {
  1008. uint azCount = azCounts[fi][ei];
  1009. hData->mFds[fi].mEvs[ei].mElevation = -al::numbers::pi / 2.0 + al::numbers::pi * ei /
  1010. (evCounts[fi] - 1);
  1011. hData->mFds[fi].mEvs[ei].mAzs = al::span{hData->mAzsBase}.subspan(azTotal, azCount);
  1012. for(uint ai{0};ai < azCount;ai++)
  1013. {
  1014. hData->mFds[fi].mEvs[ei].mAzs[ai].mAzimuth = 2.0 * al::numbers::pi * ai / azCount;
  1015. hData->mFds[fi].mEvs[ei].mAzs[ai].mIndex = azTotal + ai;
  1016. hData->mFds[fi].mEvs[ei].mAzs[ai].mDelays[0] = 0.0;
  1017. hData->mFds[fi].mEvs[ei].mAzs[ai].mDelays[1] = 0.0;
  1018. hData->mFds[fi].mEvs[ei].mAzs[ai].mIrs[0] = {};
  1019. hData->mFds[fi].mEvs[ei].mAzs[ai].mIrs[1] = {};
  1020. }
  1021. azTotal += azCount;
  1022. }
  1023. }
  1024. return true;
  1025. }
  1026. namespace {
  1027. /* Parse the data set definition and process the source data, storing the
  1028. * resulting data set as desired. If the input name is NULL it will read
  1029. * from standard input.
  1030. */
  1031. bool ProcessDefinition(std::string_view inName, const uint outRate, const ChannelModeT chanMode,
  1032. const bool farfield, const uint numThreads, const uint fftSize, const bool equalize,
  1033. const bool surface, const double limit, const uint truncSize, const HeadModelT model,
  1034. const double radius, const std::string_view outName)
  1035. {
  1036. HrirDataT hData;
  1037. fprintf(stdout, "Using %u thread%s.\n", numThreads, (numThreads==1)?"":"s");
  1038. if(inName.empty() || inName == "-"sv)
  1039. {
  1040. inName = "stdin"sv;
  1041. fprintf(stdout, "Reading HRIR definition from %.*s...\n", al::sizei(inName),
  1042. inName.data());
  1043. if(!LoadDefInput(std::cin, {}, inName, fftSize, truncSize, outRate, chanMode, &hData))
  1044. return false;
  1045. }
  1046. else
  1047. {
  1048. auto input = std::make_unique<std::ifstream>(std::filesystem::u8path(inName));
  1049. if(!input->is_open())
  1050. {
  1051. fprintf(stderr, "Error: Could not open input file '%.*s'\n", al::sizei(inName),
  1052. inName.data());
  1053. return false;
  1054. }
  1055. std::array<char,4> startbytes{};
  1056. input->read(startbytes.data(), startbytes.size());
  1057. if(input->gcount() != startbytes.size() || !input->good())
  1058. {
  1059. fprintf(stderr, "Error: Could not read input file '%.*s'\n", al::sizei(inName),
  1060. inName.data());
  1061. return false;
  1062. }
  1063. if(startbytes[0] == '\x89' && startbytes[1] == 'H' && startbytes[2] == 'D'
  1064. && startbytes[3] == 'F')
  1065. {
  1066. input = nullptr;
  1067. fprintf(stdout, "Reading HRTF data from %.*s...\n", al::sizei(inName),
  1068. inName.data());
  1069. if(!LoadSofaFile(inName, numThreads, fftSize, truncSize, outRate, chanMode, &hData))
  1070. return false;
  1071. }
  1072. else
  1073. {
  1074. fprintf(stdout, "Reading HRIR definition from %.*s...\n", al::sizei(inName),
  1075. inName.data());
  1076. if(!LoadDefInput(*input, startbytes, inName, fftSize, truncSize, outRate, chanMode,
  1077. &hData))
  1078. return false;
  1079. }
  1080. }
  1081. if(equalize)
  1082. {
  1083. uint c{(hData.mChannelType == CT_STEREO) ? 2u : 1u};
  1084. uint m{hData.mFftSize/2u + 1u};
  1085. auto dfa = std::vector<double>(size_t{c} * m);
  1086. if(hData.mFds.size() > 1)
  1087. {
  1088. fprintf(stdout, "Balancing field magnitudes...\n");
  1089. BalanceFieldMagnitudes(&hData, c, m);
  1090. }
  1091. fprintf(stdout, "Calculating diffuse-field average...\n");
  1092. CalculateDiffuseFieldAverage(&hData, c, m, surface, limit, dfa);
  1093. fprintf(stdout, "Performing diffuse-field equalization...\n");
  1094. DiffuseFieldEqualize(c, m, dfa, &hData);
  1095. }
  1096. if(hData.mFds.size() > 1)
  1097. {
  1098. fprintf(stdout, "Sorting %zu fields...\n", hData.mFds.size());
  1099. std::sort(hData.mFds.begin(), hData.mFds.end(),
  1100. [](const HrirFdT &lhs, const HrirFdT &rhs) noexcept
  1101. { return lhs.mDistance < rhs.mDistance; });
  1102. if(farfield)
  1103. {
  1104. fprintf(stdout, "Clearing %zu near field%s...\n", hData.mFds.size()-1,
  1105. (hData.mFds.size()-1 != 1) ? "s" : "");
  1106. hData.mFds.erase(hData.mFds.cbegin(), hData.mFds.cend()-1);
  1107. }
  1108. }
  1109. fprintf(stdout, "Synthesizing missing elevations...\n");
  1110. if(model == HM_Dataset)
  1111. SynthesizeOnsets(&hData);
  1112. SynthesizeHrirs(&hData);
  1113. fprintf(stdout, "Performing minimum phase reconstruction...\n");
  1114. ReconstructHrirs(&hData, numThreads);
  1115. fprintf(stdout, "Truncating minimum-phase HRIRs...\n");
  1116. hData.mIrPoints = truncSize;
  1117. fprintf(stdout, "Normalizing final HRIRs...\n");
  1118. NormalizeHrirs(&hData);
  1119. fprintf(stdout, "Calculating impulse delays...\n");
  1120. CalculateHrtds(model, (radius > DefaultCustomRadius) ? radius : hData.mRadius, &hData);
  1121. const auto rateStr = std::to_string(hData.mIrRate);
  1122. const auto expName = StrSubst(outName, "%r"sv, rateStr);
  1123. fprintf(stdout, "Creating MHR data set %s...\n", expName.c_str());
  1124. return StoreMhr(&hData, expName);
  1125. }
  1126. void PrintHelp(const std::string_view argv0, FILE *ofile)
  1127. {
  1128. fprintf(ofile, "Usage: %.*s [<option>...]\n\n", al::sizei(argv0), argv0.data());
  1129. fprintf(ofile, "Options:\n");
  1130. fprintf(ofile, " -r <rate> Change the data set sample rate to the specified value and\n");
  1131. fprintf(ofile, " resample the HRIRs accordingly.\n");
  1132. fprintf(ofile, " -m Change the data set to mono, mirroring the left ear for the\n");
  1133. fprintf(ofile, " right ear.\n");
  1134. fprintf(ofile, " -a Change the data set to single field, using the farthest field.\n");
  1135. fprintf(ofile, " -j <threads> Number of threads used to process HRIRs (default: 2).\n");
  1136. fprintf(ofile, " -f <points> Override the FFT window size (default: %u).\n", DefaultFftSize);
  1137. fprintf(ofile, " -e {on|off} Toggle diffuse-field equalization (default: %s).\n", (DefaultEqualize ? "on" : "off"));
  1138. fprintf(ofile, " -s {on|off} Toggle surface-weighted diffuse-field average (default: %s).\n", (DefaultSurface ? "on" : "off"));
  1139. fprintf(ofile, " -l {<dB>|none} Specify a limit to the magnitude range of the diffuse-field\n");
  1140. fprintf(ofile, " average (default: %.2f).\n", DefaultLimit);
  1141. fprintf(ofile, " -w <points> Specify the size of the truncation window that's applied\n");
  1142. fprintf(ofile, " after minimum-phase reconstruction (default: %u).\n", DefaultTruncSize);
  1143. fprintf(ofile, " -d {dataset| Specify the model used for calculating the head-delay timing\n");
  1144. fprintf(ofile, " sphere} values (default: %s).\n", ((HM_Default == HM_Dataset) ? "dataset" : "sphere"));
  1145. fprintf(ofile, " -c <radius> Use a customized head radius measured to-ear in meters.\n");
  1146. fprintf(ofile, " -i <filename> Specify an HRIR definition file to use (defaults to stdin).\n");
  1147. fprintf(ofile, " -o <filename> Specify an output file. Use of '%%r' will be substituted with\n");
  1148. fprintf(ofile, " the data set sample rate.\n");
  1149. }
  1150. // Standard command line dispatch.
  1151. int main(al::span<std::string_view> args)
  1152. {
  1153. if(args.size() < 2)
  1154. {
  1155. fprintf(stdout, "HRTF Processing and Composition Utility\n\n");
  1156. PrintHelp(args[0], stdout);
  1157. exit(EXIT_SUCCESS);
  1158. }
  1159. std::string_view outName{"./oalsoft_hrtf_%r.mhr"sv};
  1160. uint outRate{0};
  1161. ChannelModeT chanMode{CM_AllowStereo};
  1162. uint fftSize{DefaultFftSize};
  1163. bool equalize{DefaultEqualize};
  1164. bool surface{DefaultSurface};
  1165. double limit{DefaultLimit};
  1166. uint numThreads{2};
  1167. uint truncSize{DefaultTruncSize};
  1168. HeadModelT model{HM_Default};
  1169. double radius{DefaultCustomRadius};
  1170. bool farfield{false};
  1171. std::string_view inName;
  1172. const std::string_view optlist{"r:maj:f:e:s:l:w:d:c:e:i:o:h"sv};
  1173. const auto arg0 = args[0];
  1174. args = args.subspan(1);
  1175. std::string_view optarg;
  1176. size_t argplace{0};
  1177. auto getarg = [&args,&argplace,&optarg,optlist]
  1178. {
  1179. while(!args.empty() && argplace >= args[0].size())
  1180. {
  1181. argplace = 0;
  1182. args = args.subspan(1);
  1183. }
  1184. if(args.empty())
  1185. return 0;
  1186. if(argplace == 0)
  1187. {
  1188. if(args[0] == "--"sv)
  1189. return 0;
  1190. if(args[0][0] != '-' || args[0].size() == 1)
  1191. {
  1192. fprintf(stderr, "Invalid argument: %.*s\n", al::sizei(args[0]), args[0].data());
  1193. return -1;
  1194. }
  1195. ++argplace;
  1196. }
  1197. const char nextopt{args[0][argplace]};
  1198. const auto listidx = optlist.find(nextopt);
  1199. if(listidx >= optlist.size())
  1200. {
  1201. fprintf(stderr, "Unknown argument: -%c\n", nextopt);
  1202. return -1;
  1203. }
  1204. const bool needsarg{listidx+1 < optlist.size() && optlist[listidx+1] == ':'};
  1205. if(needsarg && (argplace+1 < args[0].size() || args.size() < 2))
  1206. {
  1207. fprintf(stderr, "Missing parameter for argument: -%c\n", nextopt);
  1208. return -1;
  1209. }
  1210. if(++argplace == args[0].size())
  1211. {
  1212. if(needsarg)
  1213. optarg = args[1];
  1214. argplace = 0;
  1215. args = args.subspan(1u + needsarg);
  1216. }
  1217. return int{nextopt};
  1218. };
  1219. while(auto opt = getarg())
  1220. {
  1221. std::size_t endpos{};
  1222. switch(opt)
  1223. {
  1224. case 'r':
  1225. outRate = static_cast<uint>(std::stoul(std::string{optarg}, &endpos, 10));
  1226. if(endpos != optarg.size() || outRate < MIN_RATE || outRate > MAX_RATE)
  1227. {
  1228. fprintf(stderr, "\nError: Got unexpected value \"%.*s\" for option -%c, expected between %u to %u.\n",
  1229. al::sizei(optarg), optarg.data(), opt, MIN_RATE, MAX_RATE);
  1230. exit(EXIT_FAILURE);
  1231. }
  1232. break;
  1233. case 'm':
  1234. chanMode = CM_ForceMono;
  1235. break;
  1236. case 'a':
  1237. farfield = true;
  1238. break;
  1239. case 'j':
  1240. numThreads = static_cast<uint>(std::stoul(std::string{optarg}, &endpos, 10));
  1241. if(endpos != optarg.size() || numThreads > 64)
  1242. {
  1243. fprintf(stderr, "\nError: Got unexpected value \"%.*s\" for option -%c, expected between %u to %u.\n",
  1244. al::sizei(optarg), optarg.data(), opt, 0, 64);
  1245. exit(EXIT_FAILURE);
  1246. }
  1247. if(numThreads == 0)
  1248. numThreads = std::thread::hardware_concurrency();
  1249. break;
  1250. case 'f':
  1251. fftSize = static_cast<uint>(std::stoul(std::string{optarg}, &endpos, 10));
  1252. if(endpos != optarg.size() || (fftSize&(fftSize-1)) || fftSize < MinFftSize
  1253. || fftSize > MaxFftSize)
  1254. {
  1255. fprintf(stderr, "\nError: Got unexpected value \"%.*s\" for option -%c, expected a power-of-two between %u to %u.\n",
  1256. al::sizei(optarg), optarg.data(), opt, MinFftSize, MaxFftSize);
  1257. exit(EXIT_FAILURE);
  1258. }
  1259. break;
  1260. case 'e':
  1261. if(optarg == "on"sv)
  1262. equalize = true;
  1263. else if(optarg == "off"sv)
  1264. equalize = false;
  1265. else
  1266. {
  1267. fprintf(stderr, "\nError: Got unexpected value \"%.*s\" for option -%c, expected on or off.\n",
  1268. al::sizei(optarg), optarg.data(), opt);
  1269. exit(EXIT_FAILURE);
  1270. }
  1271. break;
  1272. case 's':
  1273. if(optarg == "on"sv)
  1274. surface = true;
  1275. else if(optarg == "off"sv)
  1276. surface = false;
  1277. else
  1278. {
  1279. fprintf(stderr, "\nError: Got unexpected value \"%.*s\" for option -%c, expected on or off.\n",
  1280. al::sizei(optarg), optarg.data(), opt);
  1281. exit(EXIT_FAILURE);
  1282. }
  1283. break;
  1284. case 'l':
  1285. if(optarg == "none"sv)
  1286. limit = 0.0;
  1287. else
  1288. {
  1289. limit = std::stod(std::string{optarg}, &endpos);
  1290. if(endpos != optarg.size() || limit < MinLimit || limit > MaxLimit)
  1291. {
  1292. fprintf(stderr, "\nError: Got unexpected value \"%.*s\" for option -%c, expected between %.0f to %.0f.\n",
  1293. al::sizei(optarg), optarg.data(), opt, MinLimit, MaxLimit);
  1294. exit(EXIT_FAILURE);
  1295. }
  1296. }
  1297. break;
  1298. case 'w':
  1299. truncSize = static_cast<uint>(std::stoul(std::string{optarg}, &endpos, 10));
  1300. if(endpos != optarg.size() || truncSize < MinTruncSize || truncSize > MaxTruncSize)
  1301. {
  1302. fprintf(stderr, "\nError: Got unexpected value \"%.*s\" for option -%c, expected between %u to %u.\n",
  1303. al::sizei(optarg), optarg.data(), opt, MinTruncSize, MaxTruncSize);
  1304. exit(EXIT_FAILURE);
  1305. }
  1306. break;
  1307. case 'd':
  1308. if(optarg == "dataset"sv)
  1309. model = HM_Dataset;
  1310. else if(optarg == "sphere"sv)
  1311. model = HM_Sphere;
  1312. else
  1313. {
  1314. fprintf(stderr, "\nError: Got unexpected value \"%.*s\" for option -%c, expected dataset or sphere.\n",
  1315. al::sizei(optarg), optarg.data(), opt);
  1316. exit(EXIT_FAILURE);
  1317. }
  1318. break;
  1319. case 'c':
  1320. radius = std::stod(std::string{optarg}, &endpos);
  1321. if(endpos != optarg.size() || radius < MinCustomRadius || radius > MaxCustomRadius)
  1322. {
  1323. fprintf(stderr, "\nError: Got unexpected value \"%.*s\" for option -%c, expected between %.2f to %.2f.\n",
  1324. al::sizei(optarg), optarg.data(), opt, MinCustomRadius, MaxCustomRadius);
  1325. exit(EXIT_FAILURE);
  1326. }
  1327. break;
  1328. case 'i':
  1329. inName = optarg;
  1330. break;
  1331. case 'o':
  1332. outName = optarg;
  1333. break;
  1334. case 'h':
  1335. PrintHelp(arg0, stdout);
  1336. exit(EXIT_SUCCESS);
  1337. default: /* '?' */
  1338. PrintHelp(arg0, stderr);
  1339. exit(EXIT_FAILURE);
  1340. }
  1341. }
  1342. const int ret{ProcessDefinition(inName, outRate, chanMode, farfield, numThreads, fftSize,
  1343. equalize, surface, limit, truncSize, model, radius, outName)};
  1344. if(!ret) return -1;
  1345. fprintf(stdout, "Operation completed.\n");
  1346. return EXIT_SUCCESS;
  1347. }
  1348. } /* namespace */
  1349. int main(int argc, char **argv)
  1350. {
  1351. assert(argc >= 0);
  1352. auto args = std::vector<std::string_view>(static_cast<unsigned int>(argc));
  1353. std::copy_n(argv, args.size(), args.begin());
  1354. return main(al::span{args});
  1355. }