Don't explicitly search for avrt
[openal-soft.git] / utils / makemhr / loaddef.cpp
blob3fcb968c1dd43e17d84c9ce5f145ab455c5ef1ba
1 /*
2 * HRTF utility for producing and demonstrating the process of creating an
3 * OpenAL Soft compatible HRIR data set.
5 * Copyright (C) 2011-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 "loaddef.h"
26 #include <algorithm>
27 #include <cctype>
28 #include <cmath>
29 #include <cstdarg>
30 #include <cstdio>
31 #include <cstdlib>
32 #include <cstring>
33 #include <filesystem>
34 #include <fstream>
35 #include <iterator>
36 #include <limits>
37 #include <memory>
38 #include <optional>
39 #include <string>
40 #include <string_view>
41 #include <vector>
43 #include "albit.h"
44 #include "almalloc.h"
45 #include "alnumeric.h"
46 #include "alspan.h"
47 #include "alstring.h"
48 #include "makemhr.h"
49 #include "polyphase_resampler.h"
50 #include "sofa-support.h"
52 #include "mysofa.h"
54 namespace {
56 // Constants for accessing the token reader's ring buffer.
57 constexpr uint TRRingBits{16};
58 constexpr uint TRRingSize{1 << TRRingBits};
59 constexpr uint TRRingMask{TRRingSize - 1};
61 // The token reader's load interval in bytes.
62 constexpr uint TRLoadSize{TRRingSize >> 2};
64 // Token reader state for parsing the data set definition.
65 struct TokenReaderT {
66 std::istream &mIStream;
67 std::string mName{};
68 uint mLine{};
69 uint mColumn{};
70 std::array<char,TRRingSize> mRing{};
71 std::streamsize mIn{};
72 std::streamsize mOut{};
74 TokenReaderT(std::istream &istream) noexcept : mIStream{istream} { }
75 TokenReaderT(const TokenReaderT&) = default;
79 // The maximum identifier length used when processing the data set
80 // definition.
81 constexpr uint MaxIdentLen{16};
83 // The limits for the listener's head 'radius' in the data set definition.
84 constexpr double MinRadius{0.05};
85 constexpr double MaxRadius{0.15};
87 // The maximum number of channels that can be addressed for a WAVE file
88 // source listed in the data set definition.
89 constexpr uint MaxWaveChannels{65535};
91 // The limits to the byte size for a binary source listed in the definition
92 // file.
93 enum : uint {
94 MinBinSize = 2,
95 MaxBinSize = 4
98 // The limits to the number of significant bits for an ASCII source listed in
99 // the data set definition.
100 enum : uint {
101 MinASCIIBits = 16,
102 MaxASCIIBits = 32
105 // The four-character-codes for RIFF/RIFX WAVE file chunks.
106 enum : uint {
107 FOURCC_RIFF = 0x46464952, // 'RIFF'
108 FOURCC_RIFX = 0x58464952, // 'RIFX'
109 FOURCC_WAVE = 0x45564157, // 'WAVE'
110 FOURCC_FMT = 0x20746D66, // 'fmt '
111 FOURCC_DATA = 0x61746164, // 'data'
112 FOURCC_LIST = 0x5453494C, // 'LIST'
113 FOURCC_WAVL = 0x6C766177, // 'wavl'
114 FOURCC_SLNT = 0x746E6C73, // 'slnt'
117 // The supported wave formats.
118 enum : uint {
119 WAVE_FORMAT_PCM = 0x0001,
120 WAVE_FORMAT_IEEE_FLOAT = 0x0003,
121 WAVE_FORMAT_EXTENSIBLE = 0xFFFE,
125 enum ByteOrderT {
126 BO_NONE,
127 BO_LITTLE,
128 BO_BIG
131 // Source format for the references listed in the data set definition.
132 enum SourceFormatT {
133 SF_NONE,
134 SF_ASCII, // ASCII text file.
135 SF_BIN_LE, // Little-endian binary file.
136 SF_BIN_BE, // Big-endian binary file.
137 SF_WAVE, // RIFF/RIFX WAVE file.
138 SF_SOFA // Spatially Oriented Format for Accoustics (SOFA) file.
141 // Element types for the references listed in the data set definition.
142 enum ElementTypeT {
143 ET_NONE,
144 ET_INT, // Integer elements.
145 ET_FP // Floating-point elements.
148 // Source reference state used when loading sources.
149 struct SourceRefT {
150 SourceFormatT mFormat;
151 ElementTypeT mType;
152 uint mSize;
153 int mBits;
154 uint mChannel;
155 double mAzimuth;
156 double mElevation;
157 double mRadius;
158 uint mSkip;
159 uint mOffset;
160 std::array<char,MAX_PATH_LEN+1> mPath;
164 /* Whitespace is not significant. It can process tokens as identifiers, numbers
165 * (integer and floating-point), strings, and operators. Strings must be
166 * encapsulated by double-quotes and cannot span multiple lines.
169 // Setup the reader on the given file. The filename can be NULL if no error
170 // output is desired.
171 void TrSetup(const al::span<const char> startbytes, const std::string_view filename,
172 TokenReaderT *tr)
174 std::string_view namepart;
176 if(!filename.empty())
178 const auto fslashpos = filename.rfind('/');
179 const auto bslashpos = filename.rfind('\\');
180 const auto slashpos = (bslashpos >= filename.size()) ? fslashpos :
181 (fslashpos >= filename.size()) ? bslashpos :
182 std::max(fslashpos, bslashpos);
183 if(slashpos < filename.size())
184 namepart = filename.substr(slashpos+1);
187 tr->mName = namepart;
188 tr->mLine = 1;
189 tr->mColumn = 1;
190 tr->mIn = 0;
191 tr->mOut = 0;
193 if(!startbytes.empty())
195 assert(startbytes.size() <= tr->mRing.size());
196 std::copy(startbytes.cbegin(), startbytes.cend(), tr->mRing.begin());
197 tr->mIn += std::streamsize(startbytes.size());
201 // Prime the reader's ring buffer, and return a result indicating that there
202 // is text to process.
203 auto TrLoad(TokenReaderT *tr) -> int
205 std::istream &istream = tr->mIStream;
207 std::streamsize toLoad{TRRingSize - static_cast<std::streamsize>(tr->mIn - tr->mOut)};
208 if(toLoad >= TRLoadSize && istream.good())
210 // Load TRLoadSize (or less if at the end of the file) per read.
211 toLoad = TRLoadSize;
213 const auto in = tr->mIn&TRRingMask;
214 std::streamsize count{TRRingSize - in};
215 if(count < toLoad)
217 istream.read(al::to_address(tr->mRing.begin() + in), count);
218 tr->mIn += istream.gcount();
219 istream.read(tr->mRing.data(), toLoad-count);
220 tr->mIn += istream.gcount();
222 else
224 istream.read(al::to_address(tr->mRing.begin() + in), toLoad);
225 tr->mIn += istream.gcount();
228 if(tr->mOut >= TRRingSize)
230 tr->mOut -= TRRingSize;
231 tr->mIn -= TRRingSize;
234 if(tr->mIn > tr->mOut)
235 return 1;
236 return 0;
239 // Error display routine. Only displays when the base name is not NULL.
240 void TrErrorVA(const TokenReaderT *tr, uint line, uint column, const char *format, va_list argPtr)
242 if(tr->mName.empty())
243 return;
244 fprintf(stderr, "\nError (%s:%u:%u): ", tr->mName.c_str(), line, column);
245 vfprintf(stderr, format, argPtr);
248 // Used to display an error at a saved line/column.
249 void TrErrorAt(const TokenReaderT *tr, uint line, uint column, const char *format, ...)
251 /* NOLINTBEGIN(*-array-to-pointer-decay) */
252 va_list argPtr;
253 va_start(argPtr, format);
254 TrErrorVA(tr, line, column, format, argPtr);
255 va_end(argPtr);
256 /* NOLINTEND(*-array-to-pointer-decay) */
259 // Used to display an error at the current line/column.
260 void TrError(const TokenReaderT *tr, const char *format, ...)
262 /* NOLINTBEGIN(*-array-to-pointer-decay) */
263 va_list argPtr;
264 va_start(argPtr, format);
265 TrErrorVA(tr, tr->mLine, tr->mColumn, format, argPtr);
266 va_end(argPtr);
267 /* NOLINTEND(*-array-to-pointer-decay) */
270 // Skips to the next line.
271 void TrSkipLine(TokenReaderT *tr)
273 char ch;
275 while(TrLoad(tr))
277 ch = tr->mRing[tr->mOut&TRRingMask];
278 tr->mOut++;
279 if(ch == '\n')
281 tr->mLine++;
282 tr->mColumn = 1;
283 break;
285 tr->mColumn ++;
289 // Skips to the next token.
290 auto TrSkipWhitespace(TokenReaderT *tr) -> int
292 while(TrLoad(tr))
294 char ch{tr->mRing[tr->mOut&TRRingMask]};
295 if(isspace(ch))
297 tr->mOut++;
298 if(ch == '\n')
300 tr->mLine++;
301 tr->mColumn = 1;
303 else
304 tr->mColumn++;
306 else if(ch == '#')
307 TrSkipLine(tr);
308 else
309 return 1;
311 return 0;
314 // Get the line and/or column of the next token (or the end of input).
315 void TrIndication(TokenReaderT *tr, uint *line, uint *column)
317 TrSkipWhitespace(tr);
318 if(line) *line = tr->mLine;
319 if(column) *column = tr->mColumn;
322 // Checks to see if a token is (likely to be) an identifier. It does not
323 // display any errors and will not proceed to the next token.
324 auto TrIsIdent(TokenReaderT *tr) -> int
326 if(!TrSkipWhitespace(tr))
327 return 0;
328 char ch{tr->mRing[tr->mOut&TRRingMask]};
329 return ch == '_' || isalpha(ch);
333 // Checks to see if a token is the given operator. It does not display any
334 // errors and will not proceed to the next token.
335 auto TrIsOperator(TokenReaderT *tr, const std::string_view op) -> int
337 if(!TrSkipWhitespace(tr))
338 return 0;
339 auto out = tr->mOut;
340 size_t len{0};
341 while(len < op.size() && out < tr->mIn)
343 if(tr->mRing[out&TRRingMask] != op[len])
344 break;
345 ++len;
346 ++out;
348 if(len == op.size())
349 return 1;
350 return 0;
353 /* The TrRead*() routines obtain the value of a matching token type. They
354 * display type, form, and boundary errors and will proceed to the next
355 * token.
358 // Reads and validates an identifier token.
359 auto TrReadIdent(TokenReaderT *tr, const al::span<char> ident) -> int
361 assert(!ident.empty());
362 const size_t maxLen{ident.size()-1};
363 uint col{tr->mColumn};
364 if(TrSkipWhitespace(tr))
366 col = tr->mColumn;
367 char ch{tr->mRing[tr->mOut&TRRingMask]};
368 if(ch == '_' || isalpha(ch))
370 size_t len{0};
371 do {
372 if(len < maxLen)
373 ident[len] = ch;
374 ++len;
375 tr->mOut++;
376 if(!TrLoad(tr))
377 break;
378 ch = tr->mRing[tr->mOut&TRRingMask];
379 } while(ch == '_' || isdigit(ch) || isalpha(ch));
381 tr->mColumn += static_cast<uint>(len);
382 if(len < maxLen)
384 ident[len] = '\0';
385 return 1;
387 TrErrorAt(tr, tr->mLine, col, "Identifier is too long.\n");
388 return 0;
391 TrErrorAt(tr, tr->mLine, col, "Expected an identifier.\n");
392 return 0;
395 // Reads and validates (including bounds) an integer token.
396 auto TrReadInt(TokenReaderT *tr, const int loBound, const int hiBound, int *value) -> int
398 uint col{tr->mColumn};
399 if(TrSkipWhitespace(tr))
401 col = tr->mColumn;
402 uint len{0};
403 std::array<char,64+1> temp{};
404 char ch{tr->mRing[tr->mOut&TRRingMask]};
405 if(ch == '+' || ch == '-')
407 temp[len] = ch;
408 len++;
409 tr->mOut++;
411 uint digis{0};
412 while(TrLoad(tr))
414 ch = tr->mRing[tr->mOut&TRRingMask];
415 if(!isdigit(ch)) break;
416 if(len < 64)
417 temp[len] = ch;
418 len++;
419 digis++;
420 tr->mOut++;
422 tr->mColumn += len;
423 if(digis > 0 && ch != '.' && !isalpha(ch))
425 if(len > 64)
427 TrErrorAt(tr, tr->mLine, col, "Integer is too long.");
428 return 0;
430 temp[len] = '\0';
431 *value = static_cast<int>(strtol(temp.data(), nullptr, 10));
432 if(*value < loBound || *value > hiBound)
434 TrErrorAt(tr, tr->mLine, col, "Expected a value from %d to %d.\n", loBound, hiBound);
435 return 0;
437 return 1;
440 TrErrorAt(tr, tr->mLine, col, "Expected an integer.\n");
441 return 0;
444 // Reads and validates (including bounds) a float token.
445 auto TrReadFloat(TokenReaderT *tr, const double loBound, const double hiBound, double *value) -> int
447 uint col{tr->mColumn};
448 if(TrSkipWhitespace(tr))
450 col = tr->mColumn;
451 std::array<char,64+1> temp{};
452 uint len{0};
453 char ch{tr->mRing[tr->mOut&TRRingMask]};
454 if(ch == '+' || ch == '-')
456 temp[len] = ch;
457 len++;
458 tr->mOut++;
461 uint digis{0};
462 while(TrLoad(tr))
464 ch = tr->mRing[tr->mOut&TRRingMask];
465 if(!isdigit(ch)) break;
466 if(len < 64)
467 temp[len] = ch;
468 len++;
469 digis++;
470 tr->mOut++;
472 if(ch == '.')
474 if(len < 64)
475 temp[len] = ch;
476 len++;
477 tr->mOut++;
479 while(TrLoad(tr))
481 ch = tr->mRing[tr->mOut&TRRingMask];
482 if(!isdigit(ch)) break;
483 if(len < 64)
484 temp[len] = ch;
485 len++;
486 digis++;
487 tr->mOut++;
489 if(digis > 0)
491 if(ch == 'E' || ch == 'e')
493 if(len < 64)
494 temp[len] = ch;
495 len++;
496 digis = 0;
497 tr->mOut++;
498 if(ch == '+' || ch == '-')
500 if(len < 64)
501 temp[len] = ch;
502 len++;
503 tr->mOut++;
505 while(TrLoad(tr))
507 ch = tr->mRing[tr->mOut&TRRingMask];
508 if(!isdigit(ch)) break;
509 if(len < 64)
510 temp[len] = ch;
511 len++;
512 digis++;
513 tr->mOut++;
516 tr->mColumn += len;
517 if(digis > 0 && ch != '.' && !isalpha(ch))
519 if(len > 64)
521 TrErrorAt(tr, tr->mLine, col, "Float is too long.");
522 return 0;
524 temp[len] = '\0';
525 *value = strtod(temp.data(), nullptr);
526 if(*value < loBound || *value > hiBound)
528 TrErrorAt(tr, tr->mLine, col, "Expected a value from %f to %f.\n", loBound, hiBound);
529 return 0;
531 return 1;
534 else
535 tr->mColumn += len;
537 TrErrorAt(tr, tr->mLine, col, "Expected a float.\n");
538 return 0;
541 // Reads and validates a string token.
542 auto TrReadString(TokenReaderT *tr, const al::span<char> text) -> int
544 assert(!text.empty());
545 const size_t maxLen{text.size()-1};
547 uint col{tr->mColumn};
548 if(TrSkipWhitespace(tr))
550 col = tr->mColumn;
551 if(char ch{tr->mRing[tr->mOut&TRRingMask]}; ch == '\"')
553 tr->mOut++;
554 size_t len{0};
555 while(TrLoad(tr))
557 ch = tr->mRing[tr->mOut&TRRingMask];
558 tr->mOut++;
559 if(ch == '\"')
560 break;
561 if(ch == '\n')
563 TrErrorAt(tr, tr->mLine, col, "Unterminated string at end of line.\n");
564 return 0;
566 if(len < maxLen)
567 text[len] = ch;
568 len++;
570 if(ch != '\"')
572 tr->mColumn += static_cast<uint>(1 + len);
573 TrErrorAt(tr, tr->mLine, col, "Unterminated string at end of input.\n");
574 return 0;
576 tr->mColumn += static_cast<uint>(2 + len);
577 if(len > maxLen)
579 TrErrorAt(tr, tr->mLine, col, "String is too long.\n");
580 return 0;
582 text[len] = '\0';
583 return 1;
586 TrErrorAt(tr, tr->mLine, col, "Expected a string.\n");
587 return 0;
590 // Reads and validates the given operator.
591 auto TrReadOperator(TokenReaderT *tr, const std::string_view op) -> int
593 uint col{tr->mColumn};
594 if(TrSkipWhitespace(tr))
596 col = tr->mColumn;
597 size_t len{0};
598 while(len < op.size() && TrLoad(tr))
600 if(tr->mRing[tr->mOut&TRRingMask] != op[len])
601 break;
602 ++len;
603 tr->mOut += 1;
605 tr->mColumn += static_cast<uint>(len);
606 if(len == op.size())
607 return 1;
609 TrErrorAt(tr, tr->mLine, col, "Expected '%s' operator.\n", op);
610 return 0;
614 /*************************
615 *** File source input ***
616 *************************/
618 // Read a binary value of the specified byte order and byte size from a file,
619 // storing it as a 32-bit unsigned integer.
620 auto ReadBin4(std::istream &istream, const char *filename, const ByteOrderT order,
621 const uint bytes, uint32_t *out) -> int
623 std::array<uint8_t,4> in{};
624 istream.read(reinterpret_cast<char*>(in.data()), static_cast<int>(bytes));
625 if(istream.gcount() != bytes)
627 fprintf(stderr, "\nError: Bad read from file '%s'.\n", filename);
628 return 0;
630 uint32_t accum{0};
631 switch(order)
633 case BO_LITTLE:
634 for(uint i = 0;i < bytes;i++)
635 accum = (accum<<8) | in[bytes - i - 1];
636 break;
637 case BO_BIG:
638 for(uint i = 0;i < bytes;i++)
639 accum = (accum<<8) | in[i];
640 break;
641 default:
642 break;
644 *out = accum;
645 return 1;
648 // Read a binary value of the specified byte order from a file, storing it as
649 // a 64-bit unsigned integer.
650 auto ReadBin8(std::istream &istream, const char *filename, const ByteOrderT order, uint64_t *out) -> int
652 std::array<uint8_t,8> in{};
653 istream.read(reinterpret_cast<char*>(in.data()), 8);
654 if(istream.gcount() != 8)
656 fprintf(stderr, "\nError: Bad read from file '%s'.\n", filename);
657 return 0;
660 uint64_t accum{};
661 switch(order)
663 case BO_LITTLE:
664 for(uint i{0};i < 8;++i)
665 accum = (accum<<8) | in[8 - i - 1];
666 break;
667 case BO_BIG:
668 for(uint i{0};i < 8;++i)
669 accum = (accum<<8) | in[i];
670 break;
671 default:
672 break;
674 *out = accum;
675 return 1;
678 /* Read a binary value of the specified type, byte order, and byte size from
679 * a file, converting it to a double. For integer types, the significant
680 * bits are used to normalize the result. The sign of bits determines
681 * whether they are padded toward the MSB (negative) or LSB (positive).
682 * Floating-point types are not normalized.
684 auto ReadBinAsDouble(std::istream &istream, const char *filename, const ByteOrderT order,
685 const ElementTypeT type, const uint bytes, const int bits, double *out) -> int
687 *out = 0.0;
688 if(bytes > 4)
690 uint64_t val{};
691 if(!ReadBin8(istream, filename, order, &val))
692 return 0;
693 if(type == ET_FP)
694 *out = al::bit_cast<double>(val);
696 else
698 uint32_t val{};
699 if(!ReadBin4(istream, filename, order, bytes, &val))
700 return 0;
701 if(type == ET_FP)
702 *out = al::bit_cast<float>(val);
703 else
705 if(bits > 0)
706 val >>= (8*bytes) - (static_cast<uint>(bits));
707 else
708 val &= (0xFFFFFFFF >> (32+bits));
710 if(val&static_cast<uint>(1<<(std::abs(bits)-1)))
711 val |= (0xFFFFFFFF << std::abs(bits));
712 *out = static_cast<int32_t>(val) / static_cast<double>(1<<(std::abs(bits)-1));
715 return 1;
718 /* Read an ascii value of the specified type from a file, converting it to a
719 * double. For integer types, the significant bits are used to normalize the
720 * result. The sign of the bits should always be positive. This also skips
721 * up to one separator character before the element itself.
723 auto ReadAsciiAsDouble(TokenReaderT *tr, const char *filename, const ElementTypeT type,
724 const uint bits, double *out) -> int
726 if(TrIsOperator(tr, ","))
727 TrReadOperator(tr, ",");
728 else if(TrIsOperator(tr, ":"))
729 TrReadOperator(tr, ":");
730 else if(TrIsOperator(tr, ";"))
731 TrReadOperator(tr, ";");
732 else if(TrIsOperator(tr, "|"))
733 TrReadOperator(tr, "|");
735 if(type == ET_FP)
737 if(!TrReadFloat(tr, -std::numeric_limits<double>::infinity(),
738 std::numeric_limits<double>::infinity(), out))
740 fprintf(stderr, "\nError: Bad read from file '%s'.\n", filename);
741 return 0;
744 else
746 int v;
747 if(!TrReadInt(tr, -(1<<(bits-1)), (1<<(bits-1))-1, &v))
749 fprintf(stderr, "\nError: Bad read from file '%s'.\n", filename);
750 return 0;
752 *out = v / static_cast<double>((1<<(bits-1))-1);
754 return 1;
757 // Read the RIFF/RIFX WAVE format chunk from a file, validating it against
758 // the source parameters and data set metrics.
759 auto ReadWaveFormat(std::istream &istream, const ByteOrderT order, const uint hrirRate,
760 SourceRefT *src) -> int
762 uint32_t fourCC, chunkSize;
763 uint32_t format, channels, rate, dummy, block, size, bits;
765 chunkSize = 0;
766 do {
767 if(chunkSize > 0)
768 istream.seekg(static_cast<int>(chunkSize), std::ios::cur);
769 if(!ReadBin4(istream, src->mPath.data(), BO_LITTLE, 4, &fourCC)
770 || !ReadBin4(istream, src->mPath.data(), order, 4, &chunkSize))
771 return 0;
772 } while(fourCC != FOURCC_FMT);
773 if(!ReadBin4(istream, src->mPath.data(), order, 2, &format)
774 || !ReadBin4(istream, src->mPath.data(), order, 2, &channels)
775 || !ReadBin4(istream, src->mPath.data(), order, 4, &rate)
776 || !ReadBin4(istream, src->mPath.data(), order, 4, &dummy)
777 || !ReadBin4(istream, src->mPath.data(), order, 2, &block))
778 return 0;
779 block /= channels;
780 if(chunkSize > 14)
782 if(!ReadBin4(istream, src->mPath.data(), order, 2, &size))
783 return 0;
784 size /= 8;
785 if(block > size)
786 size = block;
788 else
789 size = block;
790 if(format == WAVE_FORMAT_EXTENSIBLE)
792 istream.seekg(2, std::ios::cur);
793 if(!ReadBin4(istream, src->mPath.data(), order, 2, &bits))
794 return 0;
795 if(bits == 0)
796 bits = 8 * size;
797 istream.seekg(4, std::ios::cur);
798 if(!ReadBin4(istream, src->mPath.data(), order, 2, &format))
799 return 0;
800 istream.seekg(static_cast<int>(chunkSize - 26), std::ios::cur);
802 else
804 bits = 8 * size;
805 if(chunkSize > 14)
806 istream.seekg(static_cast<int>(chunkSize - 16), std::ios::cur);
807 else
808 istream.seekg(static_cast<int>(chunkSize - 14), std::ios::cur);
810 if(format != WAVE_FORMAT_PCM && format != WAVE_FORMAT_IEEE_FLOAT)
812 fprintf(stderr, "\nError: Unsupported WAVE format in file '%s'.\n", src->mPath.data());
813 return 0;
815 if(src->mChannel >= channels)
817 fprintf(stderr, "\nError: Missing source channel in WAVE file '%s'.\n", src->mPath.data());
818 return 0;
820 if(rate != hrirRate)
822 fprintf(stderr, "\nError: Mismatched source sample rate in WAVE file '%s'.\n",
823 src->mPath.data());
824 return 0;
826 if(format == WAVE_FORMAT_PCM)
828 if(size < 2 || size > 4)
830 fprintf(stderr, "\nError: Unsupported sample size in WAVE file '%s'.\n",
831 src->mPath.data());
832 return 0;
834 if(bits < 16 || bits > (8*size))
836 fprintf(stderr, "\nError: Bad significant bits in WAVE file '%s'.\n",
837 src->mPath.data());
838 return 0;
840 src->mType = ET_INT;
842 else
844 if(size != 4 && size != 8)
846 fprintf(stderr, "\nError: Unsupported sample size in WAVE file '%s'.\n",
847 src->mPath.data());
848 return 0;
850 src->mType = ET_FP;
852 src->mSize = size;
853 src->mBits = static_cast<int>(bits);
854 src->mSkip = channels;
855 return 1;
858 // Read a RIFF/RIFX WAVE data chunk, converting all elements to doubles.
859 auto ReadWaveData(std::istream &istream, const SourceRefT *src, const ByteOrderT order,
860 const al::span<double> hrir) -> int
862 auto pre = static_cast<int>(src->mSize * src->mChannel);
863 auto post = static_cast<int>(src->mSize * (src->mSkip - src->mChannel - 1));
864 auto skip = int{0};
865 for(size_t i{0};i < hrir.size();++i)
867 skip += pre;
868 if(skip > 0)
869 istream.seekg(skip, std::ios::cur);
870 if(!ReadBinAsDouble(istream, src->mPath.data(), order, src->mType, src->mSize, src->mBits,
871 &hrir[i]))
872 return 0;
873 skip = post;
875 if(skip > 0)
876 istream.seekg(skip, std::ios::cur);
877 return 1;
880 // Read the RIFF/RIFX WAVE list or data chunk, converting all elements to
881 // doubles.
882 auto ReadWaveList(std::istream &istream, const SourceRefT *src, const ByteOrderT order,
883 const al::span<double> hrir) -> int
885 uint32_t fourCC, chunkSize, listSize, count;
886 uint block, skip, offset, i;
887 double lastSample;
889 for(;;)
891 if(!ReadBin4(istream, src->mPath.data(), BO_LITTLE, 4, &fourCC)
892 || !ReadBin4(istream, src->mPath.data(), order, 4, &chunkSize))
893 return 0;
895 if(fourCC == FOURCC_DATA)
897 block = src->mSize * src->mSkip;
898 count = chunkSize / block;
899 if(count < (src->mOffset + hrir.size()))
901 fprintf(stderr, "\nError: Bad read from file '%s'.\n", src->mPath.data());
902 return 0;
904 using off_type = std::istream::off_type;
905 istream.seekg(off_type(src->mOffset) * off_type(block), std::ios::cur);
906 if(!ReadWaveData(istream, src, order, hrir))
907 return 0;
908 return 1;
910 if(fourCC == FOURCC_LIST)
912 if(!ReadBin4(istream, src->mPath.data(), BO_LITTLE, 4, &fourCC))
913 return 0;
914 chunkSize -= 4;
915 if(fourCC == FOURCC_WAVL)
916 break;
918 if(chunkSize > 0)
919 istream.seekg(static_cast<long>(chunkSize), std::ios::cur);
921 listSize = chunkSize;
922 block = src->mSize * src->mSkip;
923 skip = src->mOffset;
924 offset = 0;
925 lastSample = 0.0;
926 while(offset < hrir.size() && listSize > 8)
928 if(!ReadBin4(istream, src->mPath.data(), BO_LITTLE, 4, &fourCC)
929 || !ReadBin4(istream, src->mPath.data(), order, 4, &chunkSize))
930 return 0;
931 listSize -= 8 + chunkSize;
932 if(fourCC == FOURCC_DATA)
934 count = chunkSize / block;
935 if(count > skip)
937 using off_type = std::istream::off_type;
938 istream.seekg(off_type(skip) * off_type(block), std::ios::cur);
939 chunkSize -= skip * block;
940 count -= skip;
941 skip = 0;
942 if(count > (hrir.size() - offset))
943 count = static_cast<uint>(hrir.size() - offset);
944 if(!ReadWaveData(istream, src, order, hrir.subspan(offset, count)))
945 return 0;
946 chunkSize -= count * block;
947 offset += count;
948 lastSample = hrir[offset - 1];
950 else
952 skip -= count;
953 count = 0;
956 else if(fourCC == FOURCC_SLNT)
958 if(!ReadBin4(istream, src->mPath.data(), order, 4, &count))
959 return 0;
960 chunkSize -= 4;
961 if(count > skip)
963 count -= skip;
964 skip = 0;
965 if(count > (hrir.size() - offset))
966 count = static_cast<uint>(hrir.size() - offset);
967 for(i = 0; i < count; i ++)
968 hrir[offset + i] = lastSample;
969 offset += count;
971 else
973 skip -= count;
974 count = 0;
977 if(chunkSize > 0)
978 istream.seekg(static_cast<long>(chunkSize), std::ios::cur);
980 if(offset < hrir.size())
982 fprintf(stderr, "\nError: Bad read from file '%s'.\n", src->mPath.data());
983 return 0;
985 return 1;
988 // Load a source HRIR from an ASCII text file containing a list of elements
989 // separated by whitespace or common list operators (',', ';', ':', '|').
990 auto LoadAsciiSource(std::istream &istream, const SourceRefT *src, const al::span<double> hrir) -> int
992 TokenReaderT tr{istream};
994 TrSetup({}, {}, &tr);
995 for(uint i{0};i < src->mOffset;++i)
997 double dummy{};
998 if(!ReadAsciiAsDouble(&tr, src->mPath.data(), src->mType, static_cast<uint>(src->mBits),
999 &dummy))
1000 return 0;
1002 for(size_t i{0};i < hrir.size();++i)
1004 if(!ReadAsciiAsDouble(&tr, src->mPath.data(), src->mType, static_cast<uint>(src->mBits),
1005 &hrir[i]))
1006 return 0;
1007 for(uint j{0};j < src->mSkip;++j)
1009 double dummy{};
1010 if(!ReadAsciiAsDouble(&tr, src->mPath.data(), src->mType,
1011 static_cast<uint>(src->mBits), &dummy))
1012 return 0;
1015 return 1;
1018 // Load a source HRIR from a binary file.
1019 auto LoadBinarySource(std::istream &istream, const SourceRefT *src, const ByteOrderT order,
1020 const al::span<double> hrir) -> int
1022 istream.seekg(static_cast<long>(src->mOffset), std::ios::beg);
1023 for(size_t i{0};i < hrir.size();++i)
1025 if(!ReadBinAsDouble(istream, src->mPath.data(), order, src->mType, src->mSize, src->mBits,
1026 &hrir[i]))
1027 return 0;
1028 if(src->mSkip > 0)
1029 istream.seekg(static_cast<long>(src->mSkip), std::ios::cur);
1031 return 1;
1034 // Load a source HRIR from a RIFF/RIFX WAVE file.
1035 auto LoadWaveSource(std::istream &istream, SourceRefT *src, const uint hrirRate,
1036 const al::span<double> hrir) -> int
1038 uint32_t fourCC, dummy;
1039 ByteOrderT order;
1041 if(!ReadBin4(istream, src->mPath.data(), BO_LITTLE, 4, &fourCC)
1042 || !ReadBin4(istream, src->mPath.data(), BO_LITTLE, 4, &dummy))
1043 return 0;
1044 if(fourCC == FOURCC_RIFF)
1045 order = BO_LITTLE;
1046 else if(fourCC == FOURCC_RIFX)
1047 order = BO_BIG;
1048 else
1050 fprintf(stderr, "\nError: No RIFF/RIFX chunk in file '%s'.\n", src->mPath.data());
1051 return 0;
1054 if(!ReadBin4(istream, src->mPath.data(), BO_LITTLE, 4, &fourCC))
1055 return 0;
1056 if(fourCC != FOURCC_WAVE)
1058 fprintf(stderr, "\nError: Not a RIFF/RIFX WAVE file '%s'.\n", src->mPath.data());
1059 return 0;
1061 if(!ReadWaveFormat(istream, order, hrirRate, src))
1062 return 0;
1063 if(!ReadWaveList(istream, src, order, hrir))
1064 return 0;
1065 return 1;
1069 struct SofaEasyDeleter {
1070 void operator()(gsl::owner<MYSOFA_EASY*> sofa)
1072 if(sofa->neighborhood) mysofa_neighborhood_free(sofa->neighborhood);
1073 if(sofa->lookup) mysofa_lookup_free(sofa->lookup);
1074 if(sofa->hrtf) mysofa_free(sofa->hrtf);
1075 delete sofa;
1078 using SofaEasyPtr = std::unique_ptr<MYSOFA_EASY,SofaEasyDeleter>;
1080 struct SofaCacheEntry {
1081 std::string mName;
1082 uint mSampleRate{};
1083 SofaEasyPtr mSofa;
1085 std::vector<SofaCacheEntry> gSofaCache;
1087 // Load a Spatially Oriented Format for Accoustics (SOFA) file.
1088 auto LoadSofaFile(SourceRefT *src, const uint hrirRate, const uint n) -> MYSOFA_EASY*
1090 const std::string_view srcname{src->mPath.data()};
1091 auto iter = std::find_if(gSofaCache.begin(), gSofaCache.end(),
1092 [srcname,hrirRate](SofaCacheEntry &entry) -> bool
1093 { return entry.mName == srcname && entry.mSampleRate == hrirRate; });
1094 if(iter != gSofaCache.end()) return iter->mSofa.get();
1096 SofaEasyPtr sofa{new(std::nothrow) MYSOFA_EASY{}};
1097 if(!sofa)
1099 fprintf(stderr, "\nError: Out of memory.\n");
1100 return nullptr;
1102 sofa->lookup = nullptr;
1103 sofa->neighborhood = nullptr;
1105 int err;
1106 sofa->hrtf = mysofa_load(src->mPath.data(), &err);
1107 if(!sofa->hrtf)
1109 fprintf(stderr, "\nError: Could not load source file '%s': %s (%d).\n",
1110 src->mPath.data(), SofaErrorStr(err), err);
1111 return nullptr;
1113 /* NOTE: Some valid SOFA files are failing this check. */
1114 err = mysofa_check(sofa->hrtf);
1115 if(err != MYSOFA_OK)
1116 fprintf(stderr, "\nWarning: Supposedly malformed source file '%s': %s (%d).\n",
1117 src->mPath.data(), SofaErrorStr(err), err);
1118 if((src->mOffset + n) > sofa->hrtf->N)
1120 fprintf(stderr, "\nError: Not enough samples in SOFA file '%s'.\n", src->mPath.data());
1121 return nullptr;
1123 if(src->mChannel >= sofa->hrtf->R)
1125 fprintf(stderr, "\nError: Missing source receiver in SOFA file '%s'.\n",src->mPath.data());
1126 return nullptr;
1128 mysofa_tocartesian(sofa->hrtf);
1129 sofa->lookup = mysofa_lookup_init(sofa->hrtf);
1130 if(sofa->lookup == nullptr)
1132 fprintf(stderr, "\nError: Out of memory.\n");
1133 return nullptr;
1135 gSofaCache.emplace_back(SofaCacheEntry{std::string{srcname}, hrirRate, std::move(sofa)});
1136 return gSofaCache.back().mSofa.get();
1139 // Copies the HRIR data from a particular SOFA measurement.
1140 void ExtractSofaHrir(const MYSOFA_HRTF *hrtf, const size_t index, const size_t channel,
1141 const size_t offset, const al::span<double> hrir)
1143 const auto irValues = al::span{hrtf->DataIR.values, hrtf->DataIR.elements}
1144 .subspan((index*hrtf->R + channel)*hrtf->N + offset);
1145 std::copy_n(irValues.cbegin(), hrir.size(), hrir.begin());
1148 // Load a source HRIR from a Spatially Oriented Format for Accoustics (SOFA)
1149 // file.
1150 auto LoadSofaSource(SourceRefT *src, const uint hrirRate, const al::span<double> hrir) -> int
1152 MYSOFA_EASY *sofa{LoadSofaFile(src, hrirRate, static_cast<uint>(hrir.size()))};
1153 if(sofa == nullptr) return 0;
1155 /* NOTE: At some point it may be beneficial or necessary to consider the
1156 various coordinate systems, listener/source orientations, and
1157 directional vectors defined in the SOFA file.
1159 std::array target{
1160 static_cast<float>(src->mAzimuth),
1161 static_cast<float>(src->mElevation),
1162 static_cast<float>(src->mRadius)
1164 mysofa_s2c(target.data());
1166 int nearest{mysofa_lookup(sofa->lookup, target.data())};
1167 if(nearest < 0)
1169 fprintf(stderr, "\nError: Lookup failed in source file '%s'.\n", src->mPath.data());
1170 return 0;
1173 al::span<float,3> coords = al::span{sofa->hrtf->SourcePosition.values, sofa->hrtf->M*3_uz}
1174 .subspan(static_cast<uint>(nearest)*3_uz).first<3>();
1175 if(std::abs(coords[0] - target[0]) > 0.001 || std::abs(coords[1] - target[1]) > 0.001
1176 || std::abs(coords[2] - target[2]) > 0.001)
1178 fprintf(stderr, "\nError: No impulse response at coordinates (%.3fr, %.1fev, %.1faz) in file '%s'.\n",
1179 src->mRadius, src->mElevation, src->mAzimuth, src->mPath.data());
1180 target[0] = coords[0];
1181 target[1] = coords[1];
1182 target[2] = coords[2];
1183 mysofa_c2s(target.data());
1184 fprintf(stderr, " Nearest candidate at (%.3fr, %.1fev, %.1faz).\n", target[2],
1185 target[1], target[0]);
1186 return 0;
1189 ExtractSofaHrir(sofa->hrtf, static_cast<uint>(nearest), src->mChannel, src->mOffset, hrir);
1191 return 1;
1194 // Load a source HRIR from a supported file type.
1195 auto LoadSource(SourceRefT *src, const uint hrirRate, const al::span<double> hrir) -> int
1197 std::unique_ptr<std::istream> istream;
1198 if(src->mFormat != SF_SOFA)
1200 if(src->mFormat == SF_ASCII)
1201 istream = std::make_unique<std::ifstream>(std::filesystem::u8path(src->mPath.data()));
1202 else
1203 istream = std::make_unique<std::ifstream>(std::filesystem::u8path(src->mPath.data()),
1204 std::ios::binary);
1205 if(!istream->good())
1207 fprintf(stderr, "\nError: Could not open source file '%s'.\n", src->mPath.data());
1208 return 0;
1212 switch(src->mFormat)
1214 case SF_ASCII: return LoadAsciiSource(*istream, src, hrir);
1215 case SF_BIN_LE: return LoadBinarySource(*istream, src, BO_LITTLE, hrir);
1216 case SF_BIN_BE: return LoadBinarySource(*istream, src, BO_BIG, hrir);
1217 case SF_WAVE: return LoadWaveSource(*istream, src, hrirRate, hrir);
1218 case SF_SOFA: return LoadSofaSource(src, hrirRate, hrir);
1219 case SF_NONE: break;
1221 return 0;
1225 // Match the channel type from a given identifier.
1226 auto MatchChannelType(const char *ident) -> ChannelTypeT
1228 if(al::strcasecmp(ident, "mono") == 0)
1229 return CT_MONO;
1230 if(al::strcasecmp(ident, "stereo") == 0)
1231 return CT_STEREO;
1232 return CT_NONE;
1236 // Process the data set definition to read and validate the data set metrics.
1237 auto ProcessMetrics(TokenReaderT *tr, const uint fftSize, const uint truncSize,
1238 const ChannelModeT chanMode, HrirDataT *hData) -> int
1240 int hasRate = 0, hasType = 0, hasPoints = 0, hasRadius = 0;
1241 int hasDistance = 0, hasAzimuths = 0;
1242 std::array<char,MaxIdentLen+1> ident{};
1243 uint line, col;
1244 double fpVal;
1245 uint points;
1246 int intVal;
1247 std::array<double,MAX_FD_COUNT> distances{};
1248 uint fdCount = 0;
1249 std::array<uint,MAX_FD_COUNT> evCounts{};
1250 auto azCounts = std::vector<std::array<uint,MAX_EV_COUNT>>(MAX_FD_COUNT);
1251 for(auto &azs : azCounts) azs.fill(0u);
1253 TrIndication(tr, &line, &col);
1254 while(TrIsIdent(tr))
1256 TrIndication(tr, &line, &col);
1257 if(!TrReadIdent(tr, ident))
1258 return 0;
1259 if(al::strcasecmp(ident.data(), "rate") == 0)
1261 if(hasRate)
1263 TrErrorAt(tr, line, col, "Redefinition of 'rate'.\n");
1264 return 0;
1266 if(!TrReadOperator(tr, "="))
1267 return 0;
1268 if(!TrReadInt(tr, MIN_RATE, MAX_RATE, &intVal))
1269 return 0;
1270 hData->mIrRate = static_cast<uint>(intVal);
1271 hasRate = 1;
1273 else if(al::strcasecmp(ident.data(), "type") == 0)
1275 std::array<char,MaxIdentLen+1> type{};
1277 if(hasType)
1279 TrErrorAt(tr, line, col, "Redefinition of 'type'.\n");
1280 return 0;
1282 if(!TrReadOperator(tr, "="))
1283 return 0;
1285 if(!TrReadIdent(tr, type))
1286 return 0;
1287 hData->mChannelType = MatchChannelType(type.data());
1288 if(hData->mChannelType == CT_NONE)
1290 TrErrorAt(tr, line, col, "Expected a channel type.\n");
1291 return 0;
1293 if(hData->mChannelType == CT_STEREO)
1295 if(chanMode == CM_ForceMono)
1296 hData->mChannelType = CT_MONO;
1298 hasType = 1;
1300 else if(al::strcasecmp(ident.data(), "points") == 0)
1302 if(hasPoints)
1304 TrErrorAt(tr, line, col, "Redefinition of 'points'.\n");
1305 return 0;
1307 if(!TrReadOperator(tr, "="))
1308 return 0;
1309 TrIndication(tr, &line, &col);
1310 if(!TrReadInt(tr, MIN_POINTS, MAX_POINTS, &intVal))
1311 return 0;
1312 points = static_cast<uint>(intVal);
1313 if(fftSize > 0 && points > fftSize)
1315 TrErrorAt(tr, line, col, "Value exceeds the overridden FFT size.\n");
1316 return 0;
1318 if(points < truncSize)
1320 TrErrorAt(tr, line, col, "Value is below the truncation size.\n");
1321 return 0;
1323 hData->mIrPoints = points;
1324 hData->mFftSize = fftSize;
1325 hData->mIrSize = 1 + (fftSize / 2);
1326 if(points > hData->mIrSize)
1327 hData->mIrSize = points;
1328 hasPoints = 1;
1330 else if(al::strcasecmp(ident.data(), "radius") == 0)
1332 if(hasRadius)
1334 TrErrorAt(tr, line, col, "Redefinition of 'radius'.\n");
1335 return 0;
1337 if(!TrReadOperator(tr, "="))
1338 return 0;
1339 if(!TrReadFloat(tr, MinRadius, MaxRadius, &fpVal))
1340 return 0;
1341 hData->mRadius = fpVal;
1342 hasRadius = 1;
1344 else if(al::strcasecmp(ident.data(), "distance") == 0)
1346 uint count = 0;
1348 if(hasDistance)
1350 TrErrorAt(tr, line, col, "Redefinition of 'distance'.\n");
1351 return 0;
1353 if(!TrReadOperator(tr, "="))
1354 return 0;
1356 for(;;)
1358 if(!TrReadFloat(tr, MIN_DISTANCE, MAX_DISTANCE, &fpVal))
1359 return 0;
1360 if(count > 0 && fpVal <= distances[count - 1])
1362 TrError(tr, "Distances are not ascending.\n");
1363 return 0;
1365 distances[count++] = fpVal;
1366 if(!TrIsOperator(tr, ","))
1367 break;
1368 if(count >= MAX_FD_COUNT)
1370 TrError(tr, "Exceeded the maximum of %d fields.\n", MAX_FD_COUNT);
1371 return 0;
1373 TrReadOperator(tr, ",");
1375 if(fdCount != 0 && count != fdCount)
1377 TrError(tr, "Did not match the specified number of %d fields.\n", fdCount);
1378 return 0;
1380 fdCount = count;
1381 hasDistance = 1;
1383 else if(al::strcasecmp(ident.data(), "azimuths") == 0)
1385 uint count = 0;
1387 if(hasAzimuths)
1389 TrErrorAt(tr, line, col, "Redefinition of 'azimuths'.\n");
1390 return 0;
1392 if(!TrReadOperator(tr, "="))
1393 return 0;
1395 evCounts[0] = 0;
1396 for(;;)
1398 if(!TrReadInt(tr, MIN_AZ_COUNT, MAX_AZ_COUNT, &intVal))
1399 return 0;
1400 azCounts[count][evCounts[count]++] = static_cast<uint>(intVal);
1401 if(TrIsOperator(tr, ","))
1403 if(evCounts[count] >= MAX_EV_COUNT)
1405 TrError(tr, "Exceeded the maximum of %d elevations.\n", MAX_EV_COUNT);
1406 return 0;
1408 TrReadOperator(tr, ",");
1410 else
1412 if(evCounts[count] < MIN_EV_COUNT)
1414 TrErrorAt(tr, line, col, "Did not reach the minimum of %d azimuth counts.\n", MIN_EV_COUNT);
1415 return 0;
1417 if(azCounts[count][0] != 1 || azCounts[count][evCounts[count] - 1] != 1)
1419 TrError(tr, "Poles are not singular for field %d.\n", count - 1);
1420 return 0;
1422 count++;
1423 if(!TrIsOperator(tr, ";"))
1424 break;
1426 if(count >= MAX_FD_COUNT)
1428 TrError(tr, "Exceeded the maximum number of %d fields.\n", MAX_FD_COUNT);
1429 return 0;
1431 evCounts[count] = 0;
1432 TrReadOperator(tr, ";");
1435 if(fdCount != 0 && count != fdCount)
1437 TrError(tr, "Did not match the specified number of %d fields.\n", fdCount);
1438 return 0;
1440 fdCount = count;
1441 hasAzimuths = 1;
1443 else
1445 TrErrorAt(tr, line, col, "Expected a metric name.\n");
1446 return 0;
1448 TrSkipWhitespace(tr);
1450 if(!(hasRate && hasPoints && hasRadius && hasDistance && hasAzimuths))
1452 TrErrorAt(tr, line, col, "Expected a metric name.\n");
1453 return 0;
1455 if(distances[0] < hData->mRadius)
1457 TrError(tr, "Distance cannot start below head radius.\n");
1458 return 0;
1460 if(hData->mChannelType == CT_NONE)
1461 hData->mChannelType = CT_MONO;
1462 const auto azs = al::span{azCounts}.first<MAX_FD_COUNT>();
1463 if(!PrepareHrirData(al::span{distances}.first(fdCount), evCounts, azs, hData))
1465 fprintf(stderr, "Error: Out of memory.\n");
1466 exit(-1);
1468 return 1;
1471 // Parse an index triplet from the data set definition.
1472 auto ReadIndexTriplet(TokenReaderT *tr, const HrirDataT *hData, uint *fi, uint *ei, uint *ai)->int
1474 int intVal;
1476 if(hData->mFds.size() > 1)
1478 if(!TrReadInt(tr, 0, static_cast<int>(hData->mFds.size()-1), &intVal))
1479 return 0;
1480 *fi = static_cast<uint>(intVal);
1481 if(!TrReadOperator(tr, ","))
1482 return 0;
1484 else
1486 *fi = 0;
1488 if(!TrReadInt(tr, 0, static_cast<int>(hData->mFds[*fi].mEvs.size()-1), &intVal))
1489 return 0;
1490 *ei = static_cast<uint>(intVal);
1491 if(!TrReadOperator(tr, ","))
1492 return 0;
1493 if(!TrReadInt(tr, 0, static_cast<int>(hData->mFds[*fi].mEvs[*ei].mAzs.size()-1), &intVal))
1494 return 0;
1495 *ai = static_cast<uint>(intVal);
1496 return 1;
1499 // Match the source format from a given identifier.
1500 auto MatchSourceFormat(const char *ident) -> SourceFormatT
1502 if(al::strcasecmp(ident, "ascii") == 0)
1503 return SF_ASCII;
1504 if(al::strcasecmp(ident, "bin_le") == 0)
1505 return SF_BIN_LE;
1506 if(al::strcasecmp(ident, "bin_be") == 0)
1507 return SF_BIN_BE;
1508 if(al::strcasecmp(ident, "wave") == 0)
1509 return SF_WAVE;
1510 if(al::strcasecmp(ident, "sofa") == 0)
1511 return SF_SOFA;
1512 return SF_NONE;
1515 // Match the source element type from a given identifier.
1516 auto MatchElementType(const char *ident) -> ElementTypeT
1518 if(al::strcasecmp(ident, "int") == 0)
1519 return ET_INT;
1520 if(al::strcasecmp(ident, "fp") == 0)
1521 return ET_FP;
1522 return ET_NONE;
1525 // Parse and validate a source reference from the data set definition.
1526 auto ReadSourceRef(TokenReaderT *tr, SourceRefT *src) -> int
1528 std::array<char,MaxIdentLen+1> ident{};
1529 uint line, col;
1530 double fpVal;
1531 int intVal;
1533 TrIndication(tr, &line, &col);
1534 if(!TrReadIdent(tr, ident))
1535 return 0;
1536 src->mFormat = MatchSourceFormat(ident.data());
1537 if(src->mFormat == SF_NONE)
1539 TrErrorAt(tr, line, col, "Expected a source format.\n");
1540 return 0;
1542 if(!TrReadOperator(tr, "("))
1543 return 0;
1544 if(src->mFormat == SF_SOFA)
1546 if(!TrReadFloat(tr, MIN_DISTANCE, MAX_DISTANCE, &fpVal))
1547 return 0;
1548 src->mRadius = fpVal;
1549 if(!TrReadOperator(tr, ","))
1550 return 0;
1551 if(!TrReadFloat(tr, -90.0, 90.0, &fpVal))
1552 return 0;
1553 src->mElevation = fpVal;
1554 if(!TrReadOperator(tr, ","))
1555 return 0;
1556 if(!TrReadFloat(tr, -360.0, 360.0, &fpVal))
1557 return 0;
1558 src->mAzimuth = fpVal;
1559 if(!TrReadOperator(tr, ":"))
1560 return 0;
1561 if(!TrReadInt(tr, 0, MaxWaveChannels, &intVal))
1562 return 0;
1563 src->mType = ET_NONE;
1564 src->mSize = 0;
1565 src->mBits = 0;
1566 src->mChannel = static_cast<uint>(intVal);
1567 src->mSkip = 0;
1569 else if(src->mFormat == SF_WAVE)
1571 if(!TrReadInt(tr, 0, MaxWaveChannels, &intVal))
1572 return 0;
1573 src->mType = ET_NONE;
1574 src->mSize = 0;
1575 src->mBits = 0;
1576 src->mChannel = static_cast<uint>(intVal);
1577 src->mSkip = 0;
1579 else
1581 TrIndication(tr, &line, &col);
1582 if(!TrReadIdent(tr, ident))
1583 return 0;
1584 src->mType = MatchElementType(ident.data());
1585 if(src->mType == ET_NONE)
1587 TrErrorAt(tr, line, col, "Expected a source element type.\n");
1588 return 0;
1590 if(src->mFormat == SF_BIN_LE || src->mFormat == SF_BIN_BE)
1592 if(!TrReadOperator(tr, ","))
1593 return 0;
1594 if(src->mType == ET_INT)
1596 if(!TrReadInt(tr, MinBinSize, MaxBinSize, &intVal))
1597 return 0;
1598 src->mSize = static_cast<uint>(intVal);
1599 if(!TrIsOperator(tr, ","))
1600 src->mBits = static_cast<int>(8*src->mSize);
1601 else
1603 TrReadOperator(tr, ",");
1604 TrIndication(tr, &line, &col);
1605 if(!TrReadInt(tr, -2147483647-1, 2147483647, &intVal))
1606 return 0;
1607 if(std::abs(intVal) < int{MinBinSize}*8 || static_cast<uint>(std::abs(intVal)) > (8*src->mSize))
1609 TrErrorAt(tr, line, col, "Expected a value of (+/-) %d to %d.\n", MinBinSize*8, 8*src->mSize);
1610 return 0;
1612 src->mBits = intVal;
1615 else
1617 TrIndication(tr, &line, &col);
1618 if(!TrReadInt(tr, -2147483647-1, 2147483647, &intVal))
1619 return 0;
1620 if(intVal != 4 && intVal != 8)
1622 TrErrorAt(tr, line, col, "Expected a value of 4 or 8.\n");
1623 return 0;
1625 src->mSize = static_cast<uint>(intVal);
1626 src->mBits = 0;
1629 else if(src->mFormat == SF_ASCII && src->mType == ET_INT)
1631 if(!TrReadOperator(tr, ","))
1632 return 0;
1633 if(!TrReadInt(tr, MinASCIIBits, MaxASCIIBits, &intVal))
1634 return 0;
1635 src->mSize = 0;
1636 src->mBits = intVal;
1638 else
1640 src->mSize = 0;
1641 src->mBits = 0;
1644 if(!TrIsOperator(tr, ";"))
1645 src->mSkip = 0;
1646 else
1648 TrReadOperator(tr, ";");
1649 if(!TrReadInt(tr, 0, 0x7FFFFFFF, &intVal))
1650 return 0;
1651 src->mSkip = static_cast<uint>(intVal);
1654 if(!TrReadOperator(tr, ")"))
1655 return 0;
1656 if(TrIsOperator(tr, "@"))
1658 TrReadOperator(tr, "@");
1659 if(!TrReadInt(tr, 0, 0x7FFFFFFF, &intVal))
1660 return 0;
1661 src->mOffset = static_cast<uint>(intVal);
1663 else
1664 src->mOffset = 0;
1665 if(!TrReadOperator(tr, ":"))
1666 return 0;
1667 if(!TrReadString(tr, src->mPath))
1668 return 0;
1669 return 1;
1672 // Parse and validate a SOFA source reference from the data set definition.
1673 auto ReadSofaRef(TokenReaderT *tr, SourceRefT *src) -> int
1675 std::array<char,MaxIdentLen+1> ident{};
1676 uint line, col;
1677 int intVal;
1679 TrIndication(tr, &line, &col);
1680 if(!TrReadIdent(tr, ident))
1681 return 0;
1682 src->mFormat = MatchSourceFormat(ident.data());
1683 if(src->mFormat != SF_SOFA)
1685 TrErrorAt(tr, line, col, "Expected the SOFA source format.\n");
1686 return 0;
1689 src->mType = ET_NONE;
1690 src->mSize = 0;
1691 src->mBits = 0;
1692 src->mChannel = 0;
1693 src->mSkip = 0;
1695 if(TrIsOperator(tr, "@"))
1697 TrReadOperator(tr, "@");
1698 if(!TrReadInt(tr, 0, 0x7FFFFFFF, &intVal))
1699 return 0;
1700 src->mOffset = static_cast<uint>(intVal);
1702 else
1703 src->mOffset = 0;
1704 if(!TrReadOperator(tr, ":"))
1705 return 0;
1706 if(!TrReadString(tr, src->mPath))
1707 return 0;
1708 return 1;
1711 // Match the target ear (index) from a given identifier.
1712 auto MatchTargetEar(const char *ident) -> int
1714 if(al::strcasecmp(ident, "left") == 0)
1715 return 0;
1716 if(al::strcasecmp(ident, "right") == 0)
1717 return 1;
1718 return -1;
1721 // Calculate the onset time of an HRIR and average it with any existing
1722 // timing for its field, elevation, azimuth, and ear.
1723 constexpr int OnsetRateMultiple{10};
1724 auto AverageHrirOnset(PPhaseResampler &rs, al::span<double> upsampled, const uint rate,
1725 const al::span<const double> hrir, const double f, const double onset) -> double
1727 rs.process(hrir, upsampled);
1729 auto abs_lt = [](const double lhs, const double rhs) -> bool
1730 { return std::abs(lhs) < std::abs(rhs); };
1731 auto iter = std::max_element(upsampled.cbegin(), upsampled.cend(), abs_lt);
1732 return Lerp(onset, static_cast<double>(std::distance(upsampled.cbegin(), iter))/(10*rate), f);
1735 // Calculate the magnitude response of an HRIR and average it with any
1736 // existing responses for its field, elevation, azimuth, and ear.
1737 void AverageHrirMagnitude(const uint fftSize, const al::span<const double> hrir, const double f,
1738 const al::span<double> mag)
1740 const uint m{1 + (fftSize/2)};
1741 std::vector<complex_d> h(fftSize);
1742 std::vector<double> r(m);
1744 auto hiter = std::copy(hrir.cbegin(), hrir.cend(), h.begin());
1745 std::fill(hiter, h.end(), 0.0);
1746 forward_fft(h);
1747 MagnitudeResponse(h, r);
1748 for(uint i{0};i < m;++i)
1749 mag[i] = Lerp(mag[i], r[i], f);
1752 // Process the list of sources in the data set definition.
1753 auto ProcessSources(TokenReaderT *tr, HrirDataT *hData, const uint outRate) -> int
1755 const uint channels{(hData->mChannelType == CT_STEREO) ? 2u : 1u};
1756 hData->mHrirsBase.resize(size_t{channels} * hData->mIrCount * hData->mIrSize);
1757 const auto hrirs = al::span<double>{hData->mHrirsBase};
1758 auto hrir = std::vector<double>(hData->mIrSize);
1759 uint line, col, fi, ei, ai;
1761 std::vector<double> onsetSamples(size_t{OnsetRateMultiple} * hData->mIrPoints);
1762 PPhaseResampler onsetResampler;
1763 onsetResampler.init(hData->mIrRate, OnsetRateMultiple*hData->mIrRate);
1765 std::optional<PPhaseResampler> resampler;
1766 if(outRate && outRate != hData->mIrRate)
1767 resampler.emplace().init(hData->mIrRate, outRate);
1768 const double rateScale{outRate ? static_cast<double>(outRate) / hData->mIrRate : 1.0};
1769 const uint irPoints{outRate
1770 ? std::min(static_cast<uint>(std::ceil(hData->mIrPoints*rateScale)), hData->mIrPoints)
1771 : hData->mIrPoints};
1773 printf("Loading sources...");
1774 fflush(stdout);
1775 int count{0};
1776 while(TrIsOperator(tr, "["))
1778 std::array factor{1.0, 1.0};
1780 TrIndication(tr, &line, &col);
1781 TrReadOperator(tr, "[");
1783 if(TrIsOperator(tr, "*"))
1785 TrReadOperator(tr, "*");
1786 if(!TrReadOperator(tr, "]") || !TrReadOperator(tr, "="))
1787 return 0;
1789 TrIndication(tr, &line, &col);
1790 SourceRefT src{};
1791 if(!ReadSofaRef(tr, &src))
1792 return 0;
1794 if(hData->mChannelType == CT_STEREO)
1796 std::array<char,MaxIdentLen+1> type{};
1798 if(!TrReadIdent(tr, type))
1799 return 0;
1801 const ChannelTypeT channelType{MatchChannelType(type.data())};
1802 switch(channelType)
1804 case CT_NONE:
1805 TrErrorAt(tr, line, col, "Expected a channel type.\n");
1806 return 0;
1807 case CT_MONO:
1808 src.mChannel = 0;
1809 break;
1810 case CT_STEREO:
1811 src.mChannel = 1;
1812 break;
1815 else
1817 std::array<char,MaxIdentLen+1> type{};
1818 if(!TrReadIdent(tr, type))
1819 return 0;
1821 ChannelTypeT channelType{MatchChannelType(type.data())};
1822 if(channelType != CT_MONO)
1824 TrErrorAt(tr, line, col, "Expected a mono channel type.\n");
1825 return 0;
1827 src.mChannel = 0;
1830 MYSOFA_EASY *sofa{LoadSofaFile(&src, hData->mIrRate, hData->mIrPoints)};
1831 if(!sofa) return 0;
1833 const auto srcPosValues = al::span{sofa->hrtf->SourcePosition.values,
1834 sofa->hrtf->M*3_uz};
1835 for(uint si{0};si < sofa->hrtf->M;++si)
1837 printf("\rLoading sources... %d of %d", si+1, sofa->hrtf->M);
1838 fflush(stdout);
1840 std::array aer{srcPosValues[3_uz*si], srcPosValues[3_uz*si + 1],
1841 srcPosValues[3_uz*si + 2]};
1842 mysofa_c2s(aer.data());
1844 if(std::fabs(aer[1]) >= 89.999f)
1845 aer[0] = 0.0f;
1846 else
1847 aer[0] = std::fmod(360.0f - aer[0], 360.0f);
1849 auto field = std::find_if(hData->mFds.cbegin(), hData->mFds.cend(),
1850 [&aer](const HrirFdT &fld) -> bool
1851 { return (std::abs(aer[2] - fld.mDistance) < 0.001); });
1852 if(field == hData->mFds.cend())
1853 continue;
1854 fi = static_cast<uint>(std::distance(hData->mFds.cbegin(), field));
1856 const double evscale{180.0 / static_cast<double>(field->mEvs.size()-1)};
1857 double ef{(90.0 + aer[1]) / evscale};
1858 ei = static_cast<uint>(std::round(ef));
1859 ef = (ef - ei) * evscale;
1860 if(std::abs(ef) >= 0.1)
1861 continue;
1863 const double azscale{360.0 / static_cast<double>(field->mEvs[ei].mAzs.size())};
1864 double af{aer[0] / azscale};
1865 ai = static_cast<uint>(std::round(af));
1866 af = (af - ai) * azscale;
1867 ai %= static_cast<uint>(field->mEvs[ei].mAzs.size());
1868 if(std::abs(af) >= 0.1)
1869 continue;
1871 HrirAzT *azd = &field->mEvs[ei].mAzs[ai];
1872 if(!azd->mIrs[0].empty())
1874 TrErrorAt(tr, line, col, "Redefinition of source [ %d, %d, %d ].\n", fi, ei, ai);
1875 return 0;
1878 const auto hrirPoints = al::span{hrir}.first(hData->mIrPoints);
1879 ExtractSofaHrir(sofa->hrtf, si, 0, src.mOffset, hrirPoints);
1880 azd->mIrs[0] = hrirs.subspan(size_t{hData->mIrSize}*azd->mIndex, hData->mIrSize);
1881 azd->mDelays[0] = AverageHrirOnset(onsetResampler, onsetSamples, hData->mIrRate,
1882 hrirPoints, 1.0, azd->mDelays[0]);
1883 if(resampler)
1884 resampler->process(hrirPoints, hrir);
1885 AverageHrirMagnitude(hData->mFftSize, al::span{hrir}.first(irPoints), 1.0,
1886 azd->mIrs[0]);
1888 if(src.mChannel == 1)
1890 ExtractSofaHrir(sofa->hrtf, si, 1, src.mOffset, hrirPoints);
1891 azd->mIrs[1] = hrirs.subspan(
1892 (size_t{hData->mIrCount}+azd->mIndex) * hData->mIrSize, hData->mIrSize);
1893 azd->mDelays[1] = AverageHrirOnset(onsetResampler, onsetSamples,
1894 hData->mIrRate, hrirPoints, 1.0, azd->mDelays[1]);
1895 if(resampler)
1896 resampler->process(hrirPoints, hrir);
1897 AverageHrirMagnitude(hData->mFftSize, al::span{hrir}.first(irPoints), 1.0,
1898 azd->mIrs[1]);
1901 // TODO: Since some SOFA files contain minimum phase HRIRs,
1902 // it would be beneficial to check for per-measurement delays
1903 // (when available) to reconstruct the HRTDs.
1906 continue;
1909 if(!ReadIndexTriplet(tr, hData, &fi, &ei, &ai))
1910 return 0;
1911 if(!TrReadOperator(tr, "]"))
1912 return 0;
1913 HrirAzT *azd = &hData->mFds[fi].mEvs[ei].mAzs[ai];
1915 if(!azd->mIrs[0].empty())
1917 TrErrorAt(tr, line, col, "Redefinition of source.\n");
1918 return 0;
1920 if(!TrReadOperator(tr, "="))
1921 return 0;
1923 while(true)
1925 SourceRefT src{};
1926 if(!ReadSourceRef(tr, &src))
1927 return 0;
1929 // TODO: Would be nice to display 'x of y files', but that would
1930 // require preparing the source refs first to get a total count
1931 // before loading them.
1932 ++count;
1933 printf("\rLoading sources... %d file%s", count, (count==1)?"":"s");
1934 fflush(stdout);
1936 if(!LoadSource(&src, hData->mIrRate, al::span{hrir}.first(hData->mIrPoints)))
1937 return 0;
1939 uint ti{0};
1940 if(hData->mChannelType == CT_STEREO)
1942 std::array<char,MaxIdentLen+1> ident{};
1943 if(!TrReadIdent(tr, ident))
1944 return 0;
1945 ti = static_cast<uint>(MatchTargetEar(ident.data()));
1946 if(static_cast<int>(ti) < 0)
1948 TrErrorAt(tr, line, col, "Expected a target ear.\n");
1949 return 0;
1952 const auto hrirPoints = al::span{hrir}.first(hData->mIrPoints);
1953 azd->mIrs[ti] = hrirs.subspan((ti*size_t{hData->mIrCount}+azd->mIndex)*hData->mIrSize,
1954 hData->mIrSize);
1955 azd->mDelays[ti] = AverageHrirOnset(onsetResampler, onsetSamples, hData->mIrRate,
1956 hrirPoints, 1.0/factor[ti], azd->mDelays[ti]);
1957 if(resampler)
1958 resampler->process(hrirPoints, hrir);
1959 AverageHrirMagnitude(hData->mFftSize, al::span{hrir}.first(irPoints), 1.0/factor[ti],
1960 azd->mIrs[ti]);
1961 factor[ti] += 1.0;
1962 if(!TrIsOperator(tr, "+"))
1963 break;
1964 TrReadOperator(tr, "+");
1966 if(hData->mChannelType == CT_STEREO)
1968 if(azd->mIrs[0].empty())
1970 TrErrorAt(tr, line, col, "Missing left ear source reference(s).\n");
1971 return 0;
1973 if(azd->mIrs[1].empty())
1975 TrErrorAt(tr, line, col, "Missing right ear source reference(s).\n");
1976 return 0;
1980 printf("\n");
1981 hrir.clear();
1982 if(resampler)
1984 hData->mIrRate = outRate;
1985 hData->mIrPoints = irPoints;
1986 resampler.reset();
1988 for(fi = 0;fi < hData->mFds.size();fi++)
1990 for(ei = 0;ei < hData->mFds[fi].mEvs.size();ei++)
1992 for(ai = 0;ai < hData->mFds[fi].mEvs[ei].mAzs.size();ai++)
1994 HrirAzT *azd = &hData->mFds[fi].mEvs[ei].mAzs[ai];
1995 if(!azd->mIrs[0].empty())
1996 break;
1998 if(ai < hData->mFds[fi].mEvs[ei].mAzs.size())
1999 break;
2001 if(ei >= hData->mFds[fi].mEvs.size())
2003 TrError(tr, "Missing source references [ %d, *, * ].\n", fi);
2004 return 0;
2006 hData->mFds[fi].mEvStart = ei;
2007 for(;ei < hData->mFds[fi].mEvs.size();ei++)
2009 for(ai = 0;ai < hData->mFds[fi].mEvs[ei].mAzs.size();ai++)
2011 HrirAzT *azd = &hData->mFds[fi].mEvs[ei].mAzs[ai];
2013 if(azd->mIrs[0].empty())
2015 TrError(tr, "Missing source reference [ %d, %d, %d ].\n", fi, ei, ai);
2016 return 0;
2021 for(uint ti{0};ti < channels;ti++)
2023 for(fi = 0;fi < hData->mFds.size();fi++)
2025 for(ei = 0;ei < hData->mFds[fi].mEvs.size();ei++)
2027 for(ai = 0;ai < hData->mFds[fi].mEvs[ei].mAzs.size();ai++)
2029 HrirAzT *azd = &hData->mFds[fi].mEvs[ei].mAzs[ai];
2030 azd->mIrs[ti] = hrirs.subspan(
2031 (ti*size_t{hData->mIrCount} + azd->mIndex) * hData->mIrSize,
2032 hData->mIrSize);
2037 if(!TrLoad(tr))
2039 gSofaCache.clear();
2040 return 1;
2043 TrError(tr, "Errant data at end of source list.\n");
2044 gSofaCache.clear();
2045 return 0;
2048 } /* namespace */
2050 bool LoadDefInput(std::istream &istream, const al::span<const char> startbytes,
2051 const std::string_view filename, const uint fftSize, const uint truncSize, const uint outRate,
2052 const ChannelModeT chanMode, HrirDataT *hData)
2054 TokenReaderT tr{istream};
2056 TrSetup(startbytes, filename, &tr);
2057 if(!ProcessMetrics(&tr, fftSize, truncSize, chanMode, hData)
2058 || !ProcessSources(&tr, hData, outRate))
2059 return false;
2061 return true;