Allow storing multiple buffers in a ALbufferlistitem
[openal-soft.git] / utils / makehrtf.c
blob461df597824e8d24954be9e69bb5e5e5c4dbdeb6
1 /*
2 * HRTF utility for producing and demonstrating the process of creating an
3 * OpenAL Soft compatible HRIR data set.
5 * Copyright (C) 2011-2017 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
23 * --------------------------------------------------------------------------
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.
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:
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.
37 * 3. Take the diffuse-field average and limit its magnitude range.
38 * 4. Equalize the responses by using the inverse of the diffuse-field
39 * average.
40 * 5. Reconstruct the minimum-phase responses.
41 * 5. Zero the DC component.
42 * 6. IFFT the result and truncate to the desired-length minimum-phase FIR.
44 * The spherical head algorithm for calculating propagation delay was adapted
45 * from the paper:
47 * Modeling Interaural Time Difference Assuming a Spherical Head
48 * Joel David Miller
49 * Music 150, Musical Acoustics, Stanford University
50 * December 2, 2001
52 * The formulae for calculating the Kaiser window metrics are from the
53 * the textbook:
55 * Discrete-Time Signal Processing
56 * Alan V. Oppenheim and Ronald W. Schafer
57 * Prentice-Hall Signal Processing Series
58 * 1999
61 #include "config.h"
63 #define _UNICODE
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <stdarg.h>
67 #include <stddef.h>
68 #include <string.h>
69 #include <limits.h>
70 #include <ctype.h>
71 #include <math.h>
72 #ifdef HAVE_STRINGS_H
73 #include <strings.h>
74 #endif
75 #ifdef HAVE_GETOPT
76 #include <unistd.h>
77 #else
78 #include "getopt.h"
79 #endif
81 #include "win_main_utf8.h"
83 /* Define int64_t and uint64_t types */
84 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
85 #include <inttypes.h>
86 #elif defined(_WIN32) && defined(__GNUC__)
87 #include <stdint.h>
88 #elif defined(_WIN32)
89 typedef __int64 int64_t;
90 typedef unsigned __int64 uint64_t;
91 #else
92 /* Fallback if nothing above works */
93 #include <inttypes.h>
94 #endif
96 #ifndef M_PI
97 #define M_PI (3.14159265358979323846)
98 #endif
100 #ifndef HUGE_VAL
101 #define HUGE_VAL (1.0 / 0.0)
102 #endif
105 // The epsilon used to maintain signal stability.
106 #define EPSILON (1e-9)
108 // Constants for accessing the token reader's ring buffer.
109 #define TR_RING_BITS (16)
110 #define TR_RING_SIZE (1 << TR_RING_BITS)
111 #define TR_RING_MASK (TR_RING_SIZE - 1)
113 // The token reader's load interval in bytes.
114 #define TR_LOAD_SIZE (TR_RING_SIZE >> 2)
116 // The maximum identifier length used when processing the data set
117 // definition.
118 #define MAX_IDENT_LEN (16)
120 // The maximum path length used when processing filenames.
121 #define MAX_PATH_LEN (256)
123 // The limits for the sample 'rate' metric in the data set definition and for
124 // resampling.
125 #define MIN_RATE (32000)
126 #define MAX_RATE (96000)
128 // The limits for the HRIR 'points' metric in the data set definition.
129 #define MIN_POINTS (16)
130 #define MAX_POINTS (8192)
132 // The limit to the number of 'distances' listed in the data set definition.
133 #define MAX_FD_COUNT (16)
135 // The limits to the number of 'azimuths' listed in the data set definition.
136 #define MIN_EV_COUNT (5)
137 #define MAX_EV_COUNT (128)
139 // The limits for each of the 'azimuths' listed in the data set definition.
140 #define MIN_AZ_COUNT (1)
141 #define MAX_AZ_COUNT (128)
143 // The limits for the listener's head 'radius' in the data set definition.
144 #define MIN_RADIUS (0.05)
145 #define MAX_RADIUS (0.15)
147 // The limits for the 'distance' from source to listener for each field in
148 // the definition file.
149 #define MIN_DISTANCE (0.05)
150 #define MAX_DISTANCE (2.50)
152 // The maximum number of channels that can be addressed for a WAVE file
153 // source listed in the data set definition.
154 #define MAX_WAVE_CHANNELS (65535)
156 // The limits to the byte size for a binary source listed in the definition
157 // file.
158 #define MIN_BIN_SIZE (2)
159 #define MAX_BIN_SIZE (4)
161 // The minimum number of significant bits for binary sources listed in the
162 // data set definition. The maximum is calculated from the byte size.
163 #define MIN_BIN_BITS (16)
165 // The limits to the number of significant bits for an ASCII source listed in
166 // the data set definition.
167 #define MIN_ASCII_BITS (16)
168 #define MAX_ASCII_BITS (32)
170 // The limits to the FFT window size override on the command line.
171 #define MIN_FFTSIZE (65536)
172 #define MAX_FFTSIZE (131072)
174 // The limits to the equalization range limit on the command line.
175 #define MIN_LIMIT (2.0)
176 #define MAX_LIMIT (120.0)
178 // The limits to the truncation window size on the command line.
179 #define MIN_TRUNCSIZE (16)
180 #define MAX_TRUNCSIZE (512)
182 // The limits to the custom head radius on the command line.
183 #define MIN_CUSTOM_RADIUS (0.05)
184 #define MAX_CUSTOM_RADIUS (0.15)
186 // The truncation window size must be a multiple of the below value to allow
187 // for vectorized convolution.
188 #define MOD_TRUNCSIZE (8)
190 // The defaults for the command line options.
191 #define DEFAULT_FFTSIZE (65536)
192 #define DEFAULT_EQUALIZE (1)
193 #define DEFAULT_SURFACE (1)
194 #define DEFAULT_LIMIT (24.0)
195 #define DEFAULT_TRUNCSIZE (32)
196 #define DEFAULT_HEAD_MODEL (HM_DATASET)
197 #define DEFAULT_CUSTOM_RADIUS (0.0)
199 // The four-character-codes for RIFF/RIFX WAVE file chunks.
200 #define FOURCC_RIFF (0x46464952) // 'RIFF'
201 #define FOURCC_RIFX (0x58464952) // 'RIFX'
202 #define FOURCC_WAVE (0x45564157) // 'WAVE'
203 #define FOURCC_FMT (0x20746D66) // 'fmt '
204 #define FOURCC_DATA (0x61746164) // 'data'
205 #define FOURCC_LIST (0x5453494C) // 'LIST'
206 #define FOURCC_WAVL (0x6C766177) // 'wavl'
207 #define FOURCC_SLNT (0x746E6C73) // 'slnt'
209 // The supported wave formats.
210 #define WAVE_FORMAT_PCM (0x0001)
211 #define WAVE_FORMAT_IEEE_FLOAT (0x0003)
212 #define WAVE_FORMAT_EXTENSIBLE (0xFFFE)
214 // The maximum propagation delay value supported by OpenAL Soft.
215 #define MAX_HRTD (63.0)
217 // The OpenAL Soft HRTF format marker. It stands for minimum-phase head
218 // response protocol 02.
219 #define MHR_FORMAT ("MinPHR02")
221 // Sample and channel type enum values.
222 typedef enum SampleTypeT {
223 ST_S16 = 0,
224 ST_S24 = 1
225 } SampleTypeT;
227 // Certain iterations rely on these integer enum values.
228 typedef enum ChannelTypeT {
229 CT_NONE = -1,
230 CT_MONO = 0,
231 CT_STEREO = 1
232 } ChannelTypeT;
234 // Byte order for the serialization routines.
235 typedef enum ByteOrderT {
236 BO_NONE,
237 BO_LITTLE,
238 BO_BIG
239 } ByteOrderT;
241 // Source format for the references listed in the data set definition.
242 typedef enum SourceFormatT {
243 SF_NONE,
244 SF_WAVE, // RIFF/RIFX WAVE file.
245 SF_BIN_LE, // Little-endian binary file.
246 SF_BIN_BE, // Big-endian binary file.
247 SF_ASCII // ASCII text file.
248 } SourceFormatT;
250 // Element types for the references listed in the data set definition.
251 typedef enum ElementTypeT {
252 ET_NONE,
253 ET_INT, // Integer elements.
254 ET_FP // Floating-point elements.
255 } ElementTypeT;
257 // Head model used for calculating the impulse delays.
258 typedef enum HeadModelT {
259 HM_NONE,
260 HM_DATASET, // Measure the onset from the dataset.
261 HM_SPHERE // Calculate the onset using a spherical head model.
262 } HeadModelT;
264 // Unsigned integer type.
265 typedef unsigned int uint;
267 // Serialization types. The trailing digit indicates the number of bits.
268 typedef unsigned char uint8;
269 typedef int int32;
270 typedef unsigned int uint32;
271 typedef uint64_t uint64;
273 // Token reader state for parsing the data set definition.
274 typedef struct TokenReaderT {
275 FILE *mFile;
276 const char *mName;
277 uint mLine;
278 uint mColumn;
279 char mRing[TR_RING_SIZE];
280 size_t mIn;
281 size_t mOut;
282 } TokenReaderT;
284 // Source reference state used when loading sources.
285 typedef struct SourceRefT {
286 SourceFormatT mFormat;
287 ElementTypeT mType;
288 uint mSize;
289 int mBits;
290 uint mChannel;
291 uint mSkip;
292 uint mOffset;
293 char mPath[MAX_PATH_LEN+1];
294 } SourceRefT;
296 // Structured HRIR storage for stereo azimuth pairs, elevations, and fields.
297 typedef struct HrirAzT {
298 double mAzimuth;
299 uint mIndex;
300 double mDelays[2];
301 double *mIrs[2];
302 } HrirAzT;
304 typedef struct HrirEvT {
305 double mElevation;
306 uint mIrCount;
307 uint mAzCount;
308 HrirAzT *mAzs;
309 } HrirEvT;
311 typedef struct HrirFdT {
312 double mDistance;
313 uint mIrCount;
314 uint mEvCount;
315 uint mEvStart;
316 HrirEvT *mEvs;
317 } HrirFdT;
319 // The HRIR metrics and data set used when loading, processing, and storing
320 // the resulting HRTF.
321 typedef struct HrirDataT {
322 uint mIrRate;
323 SampleTypeT mSampleType;
324 ChannelTypeT mChannelType;
325 uint mIrPoints;
326 uint mFftSize;
327 uint mIrSize;
328 double mRadius;
329 uint mIrCount;
330 uint mFdCount;
331 HrirFdT *mFds;
332 } HrirDataT;
334 // The resampler metrics and FIR filter.
335 typedef struct ResamplerT {
336 uint mP, mQ, mM, mL;
337 double *mF;
338 } ResamplerT;
341 /****************************************
342 *** Complex number type and routines ***
343 ****************************************/
345 typedef struct {
346 double Real, Imag;
347 } Complex;
349 static Complex MakeComplex(double r, double i)
351 Complex c = { r, i };
352 return c;
355 static Complex c_add(Complex a, Complex b)
357 Complex r;
358 r.Real = a.Real + b.Real;
359 r.Imag = a.Imag + b.Imag;
360 return r;
363 static Complex c_sub(Complex a, Complex b)
365 Complex r;
366 r.Real = a.Real - b.Real;
367 r.Imag = a.Imag - b.Imag;
368 return r;
371 static Complex c_mul(Complex a, Complex b)
373 Complex r;
374 r.Real = a.Real*b.Real - a.Imag*b.Imag;
375 r.Imag = a.Imag*b.Real + a.Real*b.Imag;
376 return r;
379 static Complex c_muls(Complex a, double s)
381 Complex r;
382 r.Real = a.Real * s;
383 r.Imag = a.Imag * s;
384 return r;
387 static double c_abs(Complex a)
389 return sqrt(a.Real*a.Real + a.Imag*a.Imag);
392 static Complex c_exp(Complex a)
394 Complex r;
395 double e = exp(a.Real);
396 r.Real = e * cos(a.Imag);
397 r.Imag = e * sin(a.Imag);
398 return r;
401 /*****************************
402 *** Token reader routines ***
403 *****************************/
405 /* Whitespace is not significant. It can process tokens as identifiers, numbers
406 * (integer and floating-point), strings, and operators. Strings must be
407 * encapsulated by double-quotes and cannot span multiple lines.
410 // Setup the reader on the given file. The filename can be NULL if no error
411 // output is desired.
412 static void TrSetup(FILE *fp, const char *filename, TokenReaderT *tr)
414 const char *name = NULL;
416 if(filename)
418 const char *slash = strrchr(filename, '/');
419 if(slash)
421 const char *bslash = strrchr(slash+1, '\\');
422 if(bslash) name = bslash+1;
423 else name = slash+1;
425 else
427 const char *bslash = strrchr(filename, '\\');
428 if(bslash) name = bslash+1;
429 else name = filename;
433 tr->mFile = fp;
434 tr->mName = name;
435 tr->mLine = 1;
436 tr->mColumn = 1;
437 tr->mIn = 0;
438 tr->mOut = 0;
441 // Prime the reader's ring buffer, and return a result indicating that there
442 // is text to process.
443 static int TrLoad(TokenReaderT *tr)
445 size_t toLoad, in, count;
447 toLoad = TR_RING_SIZE - (tr->mIn - tr->mOut);
448 if(toLoad >= TR_LOAD_SIZE && !feof(tr->mFile))
450 // Load TR_LOAD_SIZE (or less if at the end of the file) per read.
451 toLoad = TR_LOAD_SIZE;
452 in = tr->mIn&TR_RING_MASK;
453 count = TR_RING_SIZE - in;
454 if(count < toLoad)
456 tr->mIn += fread(&tr->mRing[in], 1, count, tr->mFile);
457 tr->mIn += fread(&tr->mRing[0], 1, toLoad-count, tr->mFile);
459 else
460 tr->mIn += fread(&tr->mRing[in], 1, toLoad, tr->mFile);
462 if(tr->mOut >= TR_RING_SIZE)
464 tr->mOut -= TR_RING_SIZE;
465 tr->mIn -= TR_RING_SIZE;
468 if(tr->mIn > tr->mOut)
469 return 1;
470 return 0;
473 // Error display routine. Only displays when the base name is not NULL.
474 static void TrErrorVA(const TokenReaderT *tr, uint line, uint column, const char *format, va_list argPtr)
476 if(!tr->mName)
477 return;
478 fprintf(stderr, "Error (%s:%u:%u): ", tr->mName, line, column);
479 vfprintf(stderr, format, argPtr);
482 // Used to display an error at a saved line/column.
483 static void TrErrorAt(const TokenReaderT *tr, uint line, uint column, const char *format, ...)
485 va_list argPtr;
487 va_start(argPtr, format);
488 TrErrorVA(tr, line, column, format, argPtr);
489 va_end(argPtr);
492 // Used to display an error at the current line/column.
493 static void TrError(const TokenReaderT *tr, const char *format, ...)
495 va_list argPtr;
497 va_start(argPtr, format);
498 TrErrorVA(tr, tr->mLine, tr->mColumn, format, argPtr);
499 va_end(argPtr);
502 // Skips to the next line.
503 static void TrSkipLine(TokenReaderT *tr)
505 char ch;
507 while(TrLoad(tr))
509 ch = tr->mRing[tr->mOut&TR_RING_MASK];
510 tr->mOut++;
511 if(ch == '\n')
513 tr->mLine++;
514 tr->mColumn = 1;
515 break;
517 tr->mColumn ++;
521 // Skips to the next token.
522 static int TrSkipWhitespace(TokenReaderT *tr)
524 char ch;
526 while(TrLoad(tr))
528 ch = tr->mRing[tr->mOut&TR_RING_MASK];
529 if(isspace(ch))
531 tr->mOut++;
532 if(ch == '\n')
534 tr->mLine++;
535 tr->mColumn = 1;
537 else
538 tr->mColumn++;
540 else if(ch == '#')
541 TrSkipLine(tr);
542 else
543 return 1;
545 return 0;
548 // Get the line and/or column of the next token (or the end of input).
549 static void TrIndication(TokenReaderT *tr, uint *line, uint *column)
551 TrSkipWhitespace(tr);
552 if(line) *line = tr->mLine;
553 if(column) *column = tr->mColumn;
556 // Checks to see if a token is (likely to be) an identifier. It does not
557 // display any errors and will not proceed to the next token.
558 static int TrIsIdent(TokenReaderT *tr)
560 char ch;
562 if(!TrSkipWhitespace(tr))
563 return 0;
564 ch = tr->mRing[tr->mOut&TR_RING_MASK];
565 return ch == '_' || isalpha(ch);
569 // Checks to see if a token is the given operator. It does not display any
570 // errors and will not proceed to the next token.
571 static int TrIsOperator(TokenReaderT *tr, const char *op)
573 size_t out, len;
574 char ch;
576 if(!TrSkipWhitespace(tr))
577 return 0;
578 out = tr->mOut;
579 len = 0;
580 while(op[len] != '\0' && out < tr->mIn)
582 ch = tr->mRing[out&TR_RING_MASK];
583 if(ch != op[len]) break;
584 len++;
585 out++;
587 if(op[len] == '\0')
588 return 1;
589 return 0;
592 /* The TrRead*() routines obtain the value of a matching token type. They
593 * display type, form, and boundary errors and will proceed to the next
594 * token.
597 // Reads and validates an identifier token.
598 static int TrReadIdent(TokenReaderT *tr, const uint maxLen, char *ident)
600 uint col, len;
601 char ch;
603 col = tr->mColumn;
604 if(TrSkipWhitespace(tr))
606 col = tr->mColumn;
607 ch = tr->mRing[tr->mOut&TR_RING_MASK];
608 if(ch == '_' || isalpha(ch))
610 len = 0;
611 do {
612 if(len < maxLen)
613 ident[len] = ch;
614 len++;
615 tr->mOut++;
616 if(!TrLoad(tr))
617 break;
618 ch = tr->mRing[tr->mOut&TR_RING_MASK];
619 } while(ch == '_' || isdigit(ch) || isalpha(ch));
621 tr->mColumn += len;
622 if(len < maxLen)
624 ident[len] = '\0';
625 return 1;
627 TrErrorAt(tr, tr->mLine, col, "Identifier is too long.\n");
628 return 0;
631 TrErrorAt(tr, tr->mLine, col, "Expected an identifier.\n");
632 return 0;
635 // Reads and validates (including bounds) an integer token.
636 static int TrReadInt(TokenReaderT *tr, const int loBound, const int hiBound, int *value)
638 uint col, digis, len;
639 char ch, temp[64+1];
641 col = tr->mColumn;
642 if(TrSkipWhitespace(tr))
644 col = tr->mColumn;
645 len = 0;
646 ch = tr->mRing[tr->mOut&TR_RING_MASK];
647 if(ch == '+' || ch == '-')
649 temp[len] = ch;
650 len++;
651 tr->mOut++;
653 digis = 0;
654 while(TrLoad(tr))
656 ch = tr->mRing[tr->mOut&TR_RING_MASK];
657 if(!isdigit(ch)) break;
658 if(len < 64)
659 temp[len] = ch;
660 len++;
661 digis++;
662 tr->mOut++;
664 tr->mColumn += len;
665 if(digis > 0 && ch != '.' && !isalpha(ch))
667 if(len > 64)
669 TrErrorAt(tr, tr->mLine, col, "Integer is too long.");
670 return 0;
672 temp[len] = '\0';
673 *value = strtol(temp, NULL, 10);
674 if(*value < loBound || *value > hiBound)
676 TrErrorAt(tr, tr->mLine, col, "Expected a value from %d to %d.\n", loBound, hiBound);
677 return 0;
679 return 1;
682 TrErrorAt(tr, tr->mLine, col, "Expected an integer.\n");
683 return 0;
686 // Reads and validates (including bounds) a float token.
687 static int TrReadFloat(TokenReaderT *tr, const double loBound, const double hiBound, double *value)
689 uint col, digis, len;
690 char ch, temp[64+1];
692 col = tr->mColumn;
693 if(TrSkipWhitespace(tr))
695 col = tr->mColumn;
696 len = 0;
697 ch = tr->mRing[tr->mOut&TR_RING_MASK];
698 if(ch == '+' || ch == '-')
700 temp[len] = ch;
701 len++;
702 tr->mOut++;
705 digis = 0;
706 while(TrLoad(tr))
708 ch = tr->mRing[tr->mOut&TR_RING_MASK];
709 if(!isdigit(ch)) break;
710 if(len < 64)
711 temp[len] = ch;
712 len++;
713 digis++;
714 tr->mOut++;
716 if(ch == '.')
718 if(len < 64)
719 temp[len] = ch;
720 len++;
721 tr->mOut++;
723 while(TrLoad(tr))
725 ch = tr->mRing[tr->mOut&TR_RING_MASK];
726 if(!isdigit(ch)) break;
727 if(len < 64)
728 temp[len] = ch;
729 len++;
730 digis++;
731 tr->mOut++;
733 if(digis > 0)
735 if(ch == 'E' || ch == 'e')
737 if(len < 64)
738 temp[len] = ch;
739 len++;
740 digis = 0;
741 tr->mOut++;
742 if(ch == '+' || ch == '-')
744 if(len < 64)
745 temp[len] = ch;
746 len++;
747 tr->mOut++;
749 while(TrLoad(tr))
751 ch = tr->mRing[tr->mOut&TR_RING_MASK];
752 if(!isdigit(ch)) break;
753 if(len < 64)
754 temp[len] = ch;
755 len++;
756 digis++;
757 tr->mOut++;
760 tr->mColumn += len;
761 if(digis > 0 && ch != '.' && !isalpha(ch))
763 if(len > 64)
765 TrErrorAt(tr, tr->mLine, col, "Float is too long.");
766 return 0;
768 temp[len] = '\0';
769 *value = strtod(temp, NULL);
770 if(*value < loBound || *value > hiBound)
772 TrErrorAt(tr, tr->mLine, col, "Expected a value from %f to %f.\n", loBound, hiBound);
773 return 0;
775 return 1;
778 else
779 tr->mColumn += len;
781 TrErrorAt(tr, tr->mLine, col, "Expected a float.\n");
782 return 0;
785 // Reads and validates a string token.
786 static int TrReadString(TokenReaderT *tr, const uint maxLen, char *text)
788 uint col, len;
789 char ch;
791 col = tr->mColumn;
792 if(TrSkipWhitespace(tr))
794 col = tr->mColumn;
795 ch = tr->mRing[tr->mOut&TR_RING_MASK];
796 if(ch == '\"')
798 tr->mOut++;
799 len = 0;
800 while(TrLoad(tr))
802 ch = tr->mRing[tr->mOut&TR_RING_MASK];
803 tr->mOut++;
804 if(ch == '\"')
805 break;
806 if(ch == '\n')
808 TrErrorAt(tr, tr->mLine, col, "Unterminated string at end of line.\n");
809 return 0;
811 if(len < maxLen)
812 text[len] = ch;
813 len++;
815 if(ch != '\"')
817 tr->mColumn += 1 + len;
818 TrErrorAt(tr, tr->mLine, col, "Unterminated string at end of input.\n");
819 return 0;
821 tr->mColumn += 2 + len;
822 if(len > maxLen)
824 TrErrorAt(tr, tr->mLine, col, "String is too long.\n");
825 return 0;
827 text[len] = '\0';
828 return 1;
831 TrErrorAt(tr, tr->mLine, col, "Expected a string.\n");
832 return 0;
835 // Reads and validates the given operator.
836 static int TrReadOperator(TokenReaderT *tr, const char *op)
838 uint col, len;
839 char ch;
841 col = tr->mColumn;
842 if(TrSkipWhitespace(tr))
844 col = tr->mColumn;
845 len = 0;
846 while(op[len] != '\0' && TrLoad(tr))
848 ch = tr->mRing[tr->mOut&TR_RING_MASK];
849 if(ch != op[len]) break;
850 len++;
851 tr->mOut++;
853 tr->mColumn += len;
854 if(op[len] == '\0')
855 return 1;
857 TrErrorAt(tr, tr->mLine, col, "Expected '%s' operator.\n", op);
858 return 0;
861 /* Performs a string substitution. Any case-insensitive occurrences of the
862 * pattern string are replaced with the replacement string. The result is
863 * truncated if necessary.
865 static int StrSubst(const char *in, const char *pat, const char *rep, const size_t maxLen, char *out)
867 size_t inLen, patLen, repLen;
868 size_t si, di;
869 int truncated;
871 inLen = strlen(in);
872 patLen = strlen(pat);
873 repLen = strlen(rep);
874 si = 0;
875 di = 0;
876 truncated = 0;
877 while(si < inLen && di < maxLen)
879 if(patLen <= inLen-si)
881 if(strncasecmp(&in[si], pat, patLen) == 0)
883 if(repLen > maxLen-di)
885 repLen = maxLen - di;
886 truncated = 1;
888 strncpy(&out[di], rep, repLen);
889 si += patLen;
890 di += repLen;
893 out[di] = in[si];
894 si++;
895 di++;
897 if(si < inLen)
898 truncated = 1;
899 out[di] = '\0';
900 return !truncated;
904 /*********************
905 *** Math routines ***
906 *********************/
908 // Provide missing math routines for MSVC versions < 1800 (Visual Studio 2013).
909 #if defined(_MSC_VER) && _MSC_VER < 1800
910 static double round(double val)
912 if(val < 0.0)
913 return ceil(val-0.5);
914 return floor(val+0.5);
917 static double fmin(double a, double b)
919 return (a<b) ? a : b;
922 static double fmax(double a, double b)
924 return (a>b) ? a : b;
926 #endif
928 // Simple clamp routine.
929 static double Clamp(const double val, const double lower, const double upper)
931 return fmin(fmax(val, lower), upper);
934 // Performs linear interpolation.
935 static double Lerp(const double a, const double b, const double f)
937 return a + f * (b - a);
940 static inline uint dither_rng(uint *seed)
942 *seed = *seed * 96314165 + 907633515;
943 return *seed;
946 // Performs a triangular probability density function dither. The input samples
947 // should be normalized (-1 to +1).
948 static void TpdfDither(double *restrict out, const double *restrict in, const double scale,
949 const int count, const int step, uint *seed)
951 static const double PRNG_SCALE = 1.0 / UINT_MAX;
952 uint prn0, prn1;
953 int i;
955 for(i = 0;i < count;i++)
957 prn0 = dither_rng(seed);
958 prn1 = dither_rng(seed);
959 out[i*step] = round(in[i]*scale + (prn0*PRNG_SCALE - prn1*PRNG_SCALE));
963 // Allocates an array of doubles.
964 static double *CreateDoubles(size_t n)
966 double *a;
968 if(n == 0)
969 n = 1;
970 a = calloc(n, sizeof(*a));
971 if(a == NULL)
973 fprintf(stderr, "Error: Out of memory.\n");
974 exit(-1);
976 return a;
979 // Allocates an array of complex numbers.
980 static Complex *CreateComplexes(size_t n)
982 Complex *a;
984 if(n == 0)
985 n = 1;
986 a = calloc(n, sizeof(*a));
987 if(a == NULL)
989 fprintf(stderr, "Error: Out of memory.\n");
990 exit(-1);
992 return a;
995 /* Fast Fourier transform routines. The number of points must be a power of
996 * two. In-place operation is possible only if both the real and imaginary
997 * parts are in-place together.
1000 // Performs bit-reversal ordering.
1001 static void FftArrange(const uint n, const Complex *in, Complex *out)
1003 uint rk, k, m;
1005 if(in == out)
1007 // Handle in-place arrangement.
1008 rk = 0;
1009 for(k = 0;k < n;k++)
1011 if(rk > k)
1013 Complex temp = in[rk];
1014 out[rk] = in[k];
1015 out[k] = temp;
1017 m = n;
1018 while(rk&(m >>= 1))
1019 rk &= ~m;
1020 rk |= m;
1023 else
1025 // Handle copy arrangement.
1026 rk = 0;
1027 for(k = 0;k < n;k++)
1029 out[rk] = in[k];
1030 m = n;
1031 while(rk&(m >>= 1))
1032 rk &= ~m;
1033 rk |= m;
1038 // Performs the summation.
1039 static void FftSummation(const int n, const double s, Complex *cplx)
1041 double pi;
1042 int m, m2;
1043 int i, k, mk;
1045 pi = s * M_PI;
1046 for(m = 1, m2 = 2;m < n; m <<= 1, m2 <<= 1)
1048 // v = Complex (-2.0 * sin (0.5 * pi / m) * sin (0.5 * pi / m), -sin (pi / m))
1049 double sm = sin(0.5 * pi / m);
1050 Complex v = MakeComplex(-2.0*sm*sm, -sin(pi / m));
1051 Complex w = MakeComplex(1.0, 0.0);
1052 for(i = 0;i < m;i++)
1054 for(k = i;k < n;k += m2)
1056 Complex t;
1057 mk = k + m;
1058 t = c_mul(w, cplx[mk]);
1059 cplx[mk] = c_sub(cplx[k], t);
1060 cplx[k] = c_add(cplx[k], t);
1062 w = c_add(w, c_mul(v, w));
1067 // Performs a forward FFT.
1068 static void FftForward(const uint n, const Complex *in, Complex *out)
1070 FftArrange(n, in, out);
1071 FftSummation(n, 1.0, out);
1074 // Performs an inverse FFT.
1075 static void FftInverse(const uint n, const Complex *in, Complex *out)
1077 double f;
1078 uint i;
1080 FftArrange(n, in, out);
1081 FftSummation(n, -1.0, out);
1082 f = 1.0 / n;
1083 for(i = 0;i < n;i++)
1084 out[i] = c_muls(out[i], f);
1087 /* Calculate the complex helical sequence (or discrete-time analytical signal)
1088 * of the given input using the Hilbert transform. Given the natural logarithm
1089 * of a signal's magnitude response, the imaginary components can be used as
1090 * the angles for minimum-phase reconstruction.
1092 static void Hilbert(const uint n, const Complex *in, Complex *out)
1094 uint i;
1096 if(in == out)
1098 // Handle in-place operation.
1099 for(i = 0;i < n;i++)
1100 out[i].Imag = 0.0;
1102 else
1104 // Handle copy operation.
1105 for(i = 0;i < n;i++)
1106 out[i] = MakeComplex(in[i].Real, 0.0);
1108 FftInverse(n, out, out);
1109 for(i = 1;i < (n+1)/2;i++)
1110 out[i] = c_muls(out[i], 2.0);
1111 /* Increment i if n is even. */
1112 i += (n&1)^1;
1113 for(;i < n;i++)
1114 out[i] = MakeComplex(0.0, 0.0);
1115 FftForward(n, out, out);
1118 /* Calculate the magnitude response of the given input. This is used in
1119 * place of phase decomposition, since the phase residuals are discarded for
1120 * minimum phase reconstruction. The mirrored half of the response is also
1121 * discarded.
1123 static void MagnitudeResponse(const uint n, const Complex *in, double *out)
1125 const uint m = 1 + (n / 2);
1126 uint i;
1127 for(i = 0;i < m;i++)
1128 out[i] = fmax(c_abs(in[i]), EPSILON);
1131 /* Apply a range limit (in dB) to the given magnitude response. This is used
1132 * to adjust the effects of the diffuse-field average on the equalization
1133 * process.
1135 static void LimitMagnitudeResponse(const uint n, const uint m, const double limit, const double *in, double *out)
1137 double halfLim;
1138 uint i, lower, upper;
1139 double ave;
1141 halfLim = limit / 2.0;
1142 // Convert the response to dB.
1143 for(i = 0;i < m;i++)
1144 out[i] = 20.0 * log10(in[i]);
1145 // Use six octaves to calculate the average magnitude of the signal.
1146 lower = ((uint)ceil(n / pow(2.0, 8.0))) - 1;
1147 upper = ((uint)floor(n / pow(2.0, 2.0))) - 1;
1148 ave = 0.0;
1149 for(i = lower;i <= upper;i++)
1150 ave += out[i];
1151 ave /= upper - lower + 1;
1152 // Keep the response within range of the average magnitude.
1153 for(i = 0;i < m;i++)
1154 out[i] = Clamp(out[i], ave - halfLim, ave + halfLim);
1155 // Convert the response back to linear magnitude.
1156 for(i = 0;i < m;i++)
1157 out[i] = pow(10.0, out[i] / 20.0);
1160 /* Reconstructs the minimum-phase component for the given magnitude response
1161 * of a signal. This is equivalent to phase recomposition, sans the missing
1162 * residuals (which were discarded). The mirrored half of the response is
1163 * reconstructed.
1165 static void MinimumPhase(const uint n, const double *in, Complex *out)
1167 const uint m = 1 + (n / 2);
1168 double *mags;
1169 uint i;
1171 mags = CreateDoubles(n);
1172 for(i = 0;i < m;i++)
1174 mags[i] = fmax(EPSILON, in[i]);
1175 out[i] = MakeComplex(log(mags[i]), 0.0);
1177 for(;i < n;i++)
1179 mags[i] = mags[n - i];
1180 out[i] = out[n - i];
1182 Hilbert(n, out, out);
1183 // Remove any DC offset the filter has.
1184 mags[0] = EPSILON;
1185 for(i = 0;i < n;i++)
1187 Complex a = c_exp(MakeComplex(0.0, out[i].Imag));
1188 out[i] = c_mul(MakeComplex(mags[i], 0.0), a);
1190 free(mags);
1194 /***************************
1195 *** Resampler functions ***
1196 ***************************/
1198 /* This is the normalized cardinal sine (sinc) function.
1200 * sinc(x) = { 1, x = 0
1201 * { sin(pi x) / (pi x), otherwise.
1203 static double Sinc(const double x)
1205 if(fabs(x) < EPSILON)
1206 return 1.0;
1207 return sin(M_PI * x) / (M_PI * x);
1210 /* The zero-order modified Bessel function of the first kind, used for the
1211 * Kaiser window.
1213 * I_0(x) = sum_{k=0}^inf (1 / k!)^2 (x / 2)^(2 k)
1214 * = sum_{k=0}^inf ((x / 2)^k / k!)^2
1216 static double BesselI_0(const double x)
1218 double term, sum, x2, y, last_sum;
1219 int k;
1221 // Start at k=1 since k=0 is trivial.
1222 term = 1.0;
1223 sum = 1.0;
1224 x2 = x/2.0;
1225 k = 1;
1227 // Let the integration converge until the term of the sum is no longer
1228 // significant.
1229 do {
1230 y = x2 / k;
1231 k++;
1232 last_sum = sum;
1233 term *= y * y;
1234 sum += term;
1235 } while(sum != last_sum);
1236 return sum;
1239 /* Calculate a Kaiser window from the given beta value and a normalized k
1240 * [-1, 1].
1242 * w(k) = { I_0(B sqrt(1 - k^2)) / I_0(B), -1 <= k <= 1
1243 * { 0, elsewhere.
1245 * Where k can be calculated as:
1247 * k = i / l, where -l <= i <= l.
1249 * or:
1251 * k = 2 i / M - 1, where 0 <= i <= M.
1253 static double Kaiser(const double b, const double k)
1255 if(!(k >= -1.0 && k <= 1.0))
1256 return 0.0;
1257 return BesselI_0(b * sqrt(1.0 - k*k)) / BesselI_0(b);
1260 // Calculates the greatest common divisor of a and b.
1261 static uint Gcd(uint x, uint y)
1263 while(y > 0)
1265 uint z = y;
1266 y = x % y;
1267 x = z;
1269 return x;
1272 /* Calculates the size (order) of the Kaiser window. Rejection is in dB and
1273 * the transition width is normalized frequency (0.5 is nyquist).
1275 * M = { ceil((r - 7.95) / (2.285 2 pi f_t)), r > 21
1276 * { ceil(5.79 / 2 pi f_t), r <= 21.
1279 static uint CalcKaiserOrder(const double rejection, const double transition)
1281 double w_t = 2.0 * M_PI * transition;
1282 if(rejection > 21.0)
1283 return (uint)ceil((rejection - 7.95) / (2.285 * w_t));
1284 return (uint)ceil(5.79 / w_t);
1287 // Calculates the beta value of the Kaiser window. Rejection is in dB.
1288 static double CalcKaiserBeta(const double rejection)
1290 if(rejection > 50.0)
1291 return 0.1102 * (rejection - 8.7);
1292 if(rejection >= 21.0)
1293 return (0.5842 * pow(rejection - 21.0, 0.4)) +
1294 (0.07886 * (rejection - 21.0));
1295 return 0.0;
1298 /* Calculates a point on the Kaiser-windowed sinc filter for the given half-
1299 * width, beta, gain, and cutoff. The point is specified in non-normalized
1300 * samples, from 0 to M, where M = (2 l + 1).
1302 * w(k) 2 p f_t sinc(2 f_t x)
1304 * x -- centered sample index (i - l)
1305 * k -- normalized and centered window index (x / l)
1306 * w(k) -- window function (Kaiser)
1307 * p -- gain compensation factor when sampling
1308 * f_t -- normalized center frequency (or cutoff; 0.5 is nyquist)
1310 static double SincFilter(const int l, const double b, const double gain, const double cutoff, const int i)
1312 return Kaiser(b, (double)(i - l) / l) * 2.0 * gain * cutoff * Sinc(2.0 * cutoff * (i - l));
1315 /* This is a polyphase sinc-filtered resampler.
1317 * Upsample Downsample
1319 * p/q = 3/2 p/q = 3/5
1321 * M-+-+-+-> M-+-+-+->
1322 * -------------------+ ---------------------+
1323 * p s * f f f f|f| | p s * f f f f f |
1324 * | 0 * 0 0 0|0|0 | | 0 * 0 0 0 0|0| |
1325 * v 0 * 0 0|0|0 0 | v 0 * 0 0 0|0|0 |
1326 * s * f|f|f f f | s * f f|f|f f |
1327 * 0 * |0|0 0 0 0 | 0 * 0|0|0 0 0 |
1328 * --------+=+--------+ 0 * |0|0 0 0 0 |
1329 * d . d .|d|. d . d ----------+=+--------+
1330 * d . . . .|d|. . . .
1331 * q->
1332 * q-+-+-+->
1334 * P_f(i,j) = q i mod p + pj
1335 * P_s(i,j) = floor(q i / p) - j
1336 * d[i=0..N-1] = sum_{j=0}^{floor((M - 1) / p)} {
1337 * { f[P_f(i,j)] s[P_s(i,j)], P_f(i,j) < M
1338 * { 0, P_f(i,j) >= M. }
1341 // Calculate the resampling metrics and build the Kaiser-windowed sinc filter
1342 // that's used to cut frequencies above the destination nyquist.
1343 static void ResamplerSetup(ResamplerT *rs, const uint srcRate, const uint dstRate)
1345 double cutoff, width, beta;
1346 uint gcd, l;
1347 int i;
1349 gcd = Gcd(srcRate, dstRate);
1350 rs->mP = dstRate / gcd;
1351 rs->mQ = srcRate / gcd;
1352 /* The cutoff is adjusted by half the transition width, so the transition
1353 * ends before the nyquist (0.5). Both are scaled by the downsampling
1354 * factor.
1356 if(rs->mP > rs->mQ)
1358 cutoff = 0.475 / rs->mP;
1359 width = 0.05 / rs->mP;
1361 else
1363 cutoff = 0.475 / rs->mQ;
1364 width = 0.05 / rs->mQ;
1366 // A rejection of -180 dB is used for the stop band. Round up when
1367 // calculating the left offset to avoid increasing the transition width.
1368 l = (CalcKaiserOrder(180.0, width)+1) / 2;
1369 beta = CalcKaiserBeta(180.0);
1370 rs->mM = l*2 + 1;
1371 rs->mL = l;
1372 rs->mF = CreateDoubles(rs->mM);
1373 for(i = 0;i < ((int)rs->mM);i++)
1374 rs->mF[i] = SincFilter((int)l, beta, rs->mP, cutoff, i);
1377 // Clean up after the resampler.
1378 static void ResamplerClear(ResamplerT *rs)
1380 free(rs->mF);
1381 rs->mF = NULL;
1384 // Perform the upsample-filter-downsample resampling operation using a
1385 // polyphase filter implementation.
1386 static void ResamplerRun(ResamplerT *rs, const uint inN, const double *in, const uint outN, double *out)
1388 const uint p = rs->mP, q = rs->mQ, m = rs->mM, l = rs->mL;
1389 const double *f = rs->mF;
1390 uint j_f, j_s;
1391 double *work;
1392 uint i;
1394 if(outN == 0)
1395 return;
1397 // Handle in-place operation.
1398 if(in == out)
1399 work = CreateDoubles(outN);
1400 else
1401 work = out;
1402 // Resample the input.
1403 for(i = 0;i < outN;i++)
1405 double r = 0.0;
1406 // Input starts at l to compensate for the filter delay. This will
1407 // drop any build-up from the first half of the filter.
1408 j_f = (l + (q * i)) % p;
1409 j_s = (l + (q * i)) / p;
1410 while(j_f < m)
1412 // Only take input when 0 <= j_s < inN. This single unsigned
1413 // comparison catches both cases.
1414 if(j_s < inN)
1415 r += f[j_f] * in[j_s];
1416 j_f += p;
1417 j_s--;
1419 work[i] = r;
1421 // Clean up after in-place operation.
1422 if(work != out)
1424 for(i = 0;i < outN;i++)
1425 out[i] = work[i];
1426 free(work);
1430 /*************************
1431 *** File source input ***
1432 *************************/
1434 // Read a binary value of the specified byte order and byte size from a file,
1435 // storing it as a 32-bit unsigned integer.
1436 static int ReadBin4(FILE *fp, const char *filename, const ByteOrderT order, const uint bytes, uint32 *out)
1438 uint8 in[4];
1439 uint32 accum;
1440 uint i;
1442 if(fread(in, 1, bytes, fp) != bytes)
1444 fprintf(stderr, "Error: Bad read from file '%s'.\n", filename);
1445 return 0;
1447 accum = 0;
1448 switch(order)
1450 case BO_LITTLE:
1451 for(i = 0;i < bytes;i++)
1452 accum = (accum<<8) | in[bytes - i - 1];
1453 break;
1454 case BO_BIG:
1455 for(i = 0;i < bytes;i++)
1456 accum = (accum<<8) | in[i];
1457 break;
1458 default:
1459 break;
1461 *out = accum;
1462 return 1;
1465 // Read a binary value of the specified byte order from a file, storing it as
1466 // a 64-bit unsigned integer.
1467 static int ReadBin8(FILE *fp, const char *filename, const ByteOrderT order, uint64 *out)
1469 uint8 in [8];
1470 uint64 accum;
1471 uint i;
1473 if(fread(in, 1, 8, fp) != 8)
1475 fprintf(stderr, "Error: Bad read from file '%s'.\n", filename);
1476 return 0;
1478 accum = 0ULL;
1479 switch(order)
1481 case BO_LITTLE:
1482 for(i = 0;i < 8;i++)
1483 accum = (accum<<8) | in[8 - i - 1];
1484 break;
1485 case BO_BIG:
1486 for(i = 0;i < 8;i++)
1487 accum = (accum<<8) | in[i];
1488 break;
1489 default:
1490 break;
1492 *out = accum;
1493 return 1;
1496 /* Read a binary value of the specified type, byte order, and byte size from
1497 * a file, converting it to a double. For integer types, the significant
1498 * bits are used to normalize the result. The sign of bits determines
1499 * whether they are padded toward the MSB (negative) or LSB (positive).
1500 * Floating-point types are not normalized.
1502 static int ReadBinAsDouble(FILE *fp, const char *filename, const ByteOrderT order, const ElementTypeT type, const uint bytes, const int bits, double *out)
1504 union {
1505 uint32 ui;
1506 int32 i;
1507 float f;
1508 } v4;
1509 union {
1510 uint64 ui;
1511 double f;
1512 } v8;
1514 *out = 0.0;
1515 if(bytes > 4)
1517 if(!ReadBin8(fp, filename, order, &v8.ui))
1518 return 0;
1519 if(type == ET_FP)
1520 *out = v8.f;
1522 else
1524 if(!ReadBin4(fp, filename, order, bytes, &v4.ui))
1525 return 0;
1526 if(type == ET_FP)
1527 *out = v4.f;
1528 else
1530 if(bits > 0)
1531 v4.ui >>= (8*bytes) - ((uint)bits);
1532 else
1533 v4.ui &= (0xFFFFFFFF >> (32+bits));
1535 if(v4.ui&(uint)(1<<(abs(bits)-1)))
1536 v4.ui |= (0xFFFFFFFF << abs (bits));
1537 *out = v4.i / (double)(1<<(abs(bits)-1));
1540 return 1;
1543 /* Read an ascii value of the specified type from a file, converting it to a
1544 * double. For integer types, the significant bits are used to normalize the
1545 * result. The sign of the bits should always be positive. This also skips
1546 * up to one separator character before the element itself.
1548 static int ReadAsciiAsDouble(TokenReaderT *tr, const char *filename, const ElementTypeT type, const uint bits, double *out)
1550 if(TrIsOperator(tr, ","))
1551 TrReadOperator(tr, ",");
1552 else if(TrIsOperator(tr, ":"))
1553 TrReadOperator(tr, ":");
1554 else if(TrIsOperator(tr, ";"))
1555 TrReadOperator(tr, ";");
1556 else if(TrIsOperator(tr, "|"))
1557 TrReadOperator(tr, "|");
1559 if(type == ET_FP)
1561 if(!TrReadFloat(tr, -HUGE_VAL, HUGE_VAL, out))
1563 fprintf(stderr, "Error: Bad read from file '%s'.\n", filename);
1564 return 0;
1567 else
1569 int v;
1570 if(!TrReadInt(tr, -(1<<(bits-1)), (1<<(bits-1))-1, &v))
1572 fprintf(stderr, "Error: Bad read from file '%s'.\n", filename);
1573 return 0;
1575 *out = v / (double)((1<<(bits-1))-1);
1577 return 1;
1580 // Read the RIFF/RIFX WAVE format chunk from a file, validating it against
1581 // the source parameters and data set metrics.
1582 static int ReadWaveFormat(FILE *fp, const ByteOrderT order, const uint hrirRate, SourceRefT *src)
1584 uint32 fourCC, chunkSize;
1585 uint32 format, channels, rate, dummy, block, size, bits;
1587 chunkSize = 0;
1588 do {
1589 if(chunkSize > 0)
1590 fseek (fp, (long) chunkSize, SEEK_CUR);
1591 if(!ReadBin4(fp, src->mPath, BO_LITTLE, 4, &fourCC) ||
1592 !ReadBin4(fp, src->mPath, order, 4, &chunkSize))
1593 return 0;
1594 } while(fourCC != FOURCC_FMT);
1595 if(!ReadBin4(fp, src->mPath, order, 2, &format) ||
1596 !ReadBin4(fp, src->mPath, order, 2, &channels) ||
1597 !ReadBin4(fp, src->mPath, order, 4, &rate) ||
1598 !ReadBin4(fp, src->mPath, order, 4, &dummy) ||
1599 !ReadBin4(fp, src->mPath, order, 2, &block))
1600 return 0;
1601 block /= channels;
1602 if(chunkSize > 14)
1604 if(!ReadBin4(fp, src->mPath, order, 2, &size))
1605 return 0;
1606 size /= 8;
1607 if(block > size)
1608 size = block;
1610 else
1611 size = block;
1612 if(format == WAVE_FORMAT_EXTENSIBLE)
1614 fseek(fp, 2, SEEK_CUR);
1615 if(!ReadBin4(fp, src->mPath, order, 2, &bits))
1616 return 0;
1617 if(bits == 0)
1618 bits = 8 * size;
1619 fseek(fp, 4, SEEK_CUR);
1620 if(!ReadBin4(fp, src->mPath, order, 2, &format))
1621 return 0;
1622 fseek(fp, (long)(chunkSize - 26), SEEK_CUR);
1624 else
1626 bits = 8 * size;
1627 if(chunkSize > 14)
1628 fseek(fp, (long)(chunkSize - 16), SEEK_CUR);
1629 else
1630 fseek(fp, (long)(chunkSize - 14), SEEK_CUR);
1632 if(format != WAVE_FORMAT_PCM && format != WAVE_FORMAT_IEEE_FLOAT)
1634 fprintf(stderr, "Error: Unsupported WAVE format in file '%s'.\n", src->mPath);
1635 return 0;
1637 if(src->mChannel >= channels)
1639 fprintf(stderr, "Error: Missing source channel in WAVE file '%s'.\n", src->mPath);
1640 return 0;
1642 if(rate != hrirRate)
1644 fprintf(stderr, "Error: Mismatched source sample rate in WAVE file '%s'.\n", src->mPath);
1645 return 0;
1647 if(format == WAVE_FORMAT_PCM)
1649 if(size < 2 || size > 4)
1651 fprintf(stderr, "Error: Unsupported sample size in WAVE file '%s'.\n", src->mPath);
1652 return 0;
1654 if(bits < 16 || bits > (8*size))
1656 fprintf (stderr, "Error: Bad significant bits in WAVE file '%s'.\n", src->mPath);
1657 return 0;
1659 src->mType = ET_INT;
1661 else
1663 if(size != 4 && size != 8)
1665 fprintf(stderr, "Error: Unsupported sample size in WAVE file '%s'.\n", src->mPath);
1666 return 0;
1668 src->mType = ET_FP;
1670 src->mSize = size;
1671 src->mBits = (int)bits;
1672 src->mSkip = channels;
1673 return 1;
1676 // Read a RIFF/RIFX WAVE data chunk, converting all elements to doubles.
1677 static int ReadWaveData(FILE *fp, const SourceRefT *src, const ByteOrderT order, const uint n, double *hrir)
1679 int pre, post, skip;
1680 uint i;
1682 pre = (int)(src->mSize * src->mChannel);
1683 post = (int)(src->mSize * (src->mSkip - src->mChannel - 1));
1684 skip = 0;
1685 for(i = 0;i < n;i++)
1687 skip += pre;
1688 if(skip > 0)
1689 fseek(fp, skip, SEEK_CUR);
1690 if(!ReadBinAsDouble(fp, src->mPath, order, src->mType, src->mSize, src->mBits, &hrir[i]))
1691 return 0;
1692 skip = post;
1694 if(skip > 0)
1695 fseek(fp, skip, SEEK_CUR);
1696 return 1;
1699 // Read the RIFF/RIFX WAVE list or data chunk, converting all elements to
1700 // doubles.
1701 static int ReadWaveList(FILE *fp, const SourceRefT *src, const ByteOrderT order, const uint n, double *hrir)
1703 uint32 fourCC, chunkSize, listSize, count;
1704 uint block, skip, offset, i;
1705 double lastSample;
1707 for(;;)
1709 if(!ReadBin4(fp, src->mPath, BO_LITTLE, 4, &fourCC) ||
1710 !ReadBin4(fp, src->mPath, order, 4, &chunkSize))
1711 return 0;
1713 if(fourCC == FOURCC_DATA)
1715 block = src->mSize * src->mSkip;
1716 count = chunkSize / block;
1717 if(count < (src->mOffset + n))
1719 fprintf(stderr, "Error: Bad read from file '%s'.\n", src->mPath);
1720 return 0;
1722 fseek(fp, (long)(src->mOffset * block), SEEK_CUR);
1723 if(!ReadWaveData(fp, src, order, n, &hrir[0]))
1724 return 0;
1725 return 1;
1727 else if(fourCC == FOURCC_LIST)
1729 if(!ReadBin4(fp, src->mPath, BO_LITTLE, 4, &fourCC))
1730 return 0;
1731 chunkSize -= 4;
1732 if(fourCC == FOURCC_WAVL)
1733 break;
1735 if(chunkSize > 0)
1736 fseek(fp, (long)chunkSize, SEEK_CUR);
1738 listSize = chunkSize;
1739 block = src->mSize * src->mSkip;
1740 skip = src->mOffset;
1741 offset = 0;
1742 lastSample = 0.0;
1743 while(offset < n && listSize > 8)
1745 if(!ReadBin4(fp, src->mPath, BO_LITTLE, 4, &fourCC) ||
1746 !ReadBin4(fp, src->mPath, order, 4, &chunkSize))
1747 return 0;
1748 listSize -= 8 + chunkSize;
1749 if(fourCC == FOURCC_DATA)
1751 count = chunkSize / block;
1752 if(count > skip)
1754 fseek(fp, (long)(skip * block), SEEK_CUR);
1755 chunkSize -= skip * block;
1756 count -= skip;
1757 skip = 0;
1758 if(count > (n - offset))
1759 count = n - offset;
1760 if(!ReadWaveData(fp, src, order, count, &hrir[offset]))
1761 return 0;
1762 chunkSize -= count * block;
1763 offset += count;
1764 lastSample = hrir [offset - 1];
1766 else
1768 skip -= count;
1769 count = 0;
1772 else if(fourCC == FOURCC_SLNT)
1774 if(!ReadBin4(fp, src->mPath, order, 4, &count))
1775 return 0;
1776 chunkSize -= 4;
1777 if(count > skip)
1779 count -= skip;
1780 skip = 0;
1781 if(count > (n - offset))
1782 count = n - offset;
1783 for(i = 0; i < count; i ++)
1784 hrir[offset + i] = lastSample;
1785 offset += count;
1787 else
1789 skip -= count;
1790 count = 0;
1793 if(chunkSize > 0)
1794 fseek(fp, (long)chunkSize, SEEK_CUR);
1796 if(offset < n)
1798 fprintf(stderr, "Error: Bad read from file '%s'.\n", src->mPath);
1799 return 0;
1801 return 1;
1804 // Load a source HRIR from a RIFF/RIFX WAVE file.
1805 static int LoadWaveSource(FILE *fp, SourceRefT *src, const uint hrirRate, const uint n, double *hrir)
1807 uint32 fourCC, dummy;
1808 ByteOrderT order;
1810 if(!ReadBin4(fp, src->mPath, BO_LITTLE, 4, &fourCC) ||
1811 !ReadBin4(fp, src->mPath, BO_LITTLE, 4, &dummy))
1812 return 0;
1813 if(fourCC == FOURCC_RIFF)
1814 order = BO_LITTLE;
1815 else if(fourCC == FOURCC_RIFX)
1816 order = BO_BIG;
1817 else
1819 fprintf(stderr, "Error: No RIFF/RIFX chunk in file '%s'.\n", src->mPath);
1820 return 0;
1823 if(!ReadBin4(fp, src->mPath, BO_LITTLE, 4, &fourCC))
1824 return 0;
1825 if(fourCC != FOURCC_WAVE)
1827 fprintf(stderr, "Error: Not a RIFF/RIFX WAVE file '%s'.\n", src->mPath);
1828 return 0;
1830 if(!ReadWaveFormat(fp, order, hrirRate, src))
1831 return 0;
1832 if(!ReadWaveList(fp, src, order, n, hrir))
1833 return 0;
1834 return 1;
1837 // Load a source HRIR from a binary file.
1838 static int LoadBinarySource(FILE *fp, const SourceRefT *src, const ByteOrderT order, const uint n, double *hrir)
1840 uint i;
1842 fseek(fp, (long)src->mOffset, SEEK_SET);
1843 for(i = 0;i < n;i++)
1845 if(!ReadBinAsDouble(fp, src->mPath, order, src->mType, src->mSize, src->mBits, &hrir[i]))
1846 return 0;
1847 if(src->mSkip > 0)
1848 fseek(fp, (long)src->mSkip, SEEK_CUR);
1850 return 1;
1853 // Load a source HRIR from an ASCII text file containing a list of elements
1854 // separated by whitespace or common list operators (',', ';', ':', '|').
1855 static int LoadAsciiSource(FILE *fp, const SourceRefT *src, const uint n, double *hrir)
1857 TokenReaderT tr;
1858 uint i, j;
1859 double dummy;
1861 TrSetup(fp, NULL, &tr);
1862 for(i = 0;i < src->mOffset;i++)
1864 if(!ReadAsciiAsDouble(&tr, src->mPath, src->mType, (uint)src->mBits, &dummy))
1865 return 0;
1867 for(i = 0;i < n;i++)
1869 if(!ReadAsciiAsDouble(&tr, src->mPath, src->mType, (uint)src->mBits, &hrir[i]))
1870 return 0;
1871 for(j = 0;j < src->mSkip;j++)
1873 if(!ReadAsciiAsDouble(&tr, src->mPath, src->mType, (uint)src->mBits, &dummy))
1874 return 0;
1877 return 1;
1880 // Load a source HRIR from a supported file type.
1881 static int LoadSource(SourceRefT *src, const uint hrirRate, const uint n, double *hrir)
1883 int result;
1884 FILE *fp;
1886 if(src->mFormat == SF_ASCII)
1887 fp = fopen(src->mPath, "r");
1888 else
1889 fp = fopen(src->mPath, "rb");
1890 if(fp == NULL)
1892 fprintf(stderr, "Error: Could not open source file '%s'.\n", src->mPath);
1893 return 0;
1895 if(src->mFormat == SF_WAVE)
1896 result = LoadWaveSource(fp, src, hrirRate, n, hrir);
1897 else if(src->mFormat == SF_BIN_LE)
1898 result = LoadBinarySource(fp, src, BO_LITTLE, n, hrir);
1899 else if(src->mFormat == SF_BIN_BE)
1900 result = LoadBinarySource(fp, src, BO_BIG, n, hrir);
1901 else
1902 result = LoadAsciiSource(fp, src, n, hrir);
1903 fclose(fp);
1904 return result;
1908 /***************************
1909 *** File storage output ***
1910 ***************************/
1912 // Write an ASCII string to a file.
1913 static int WriteAscii(const char *out, FILE *fp, const char *filename)
1915 size_t len;
1917 len = strlen(out);
1918 if(fwrite(out, 1, len, fp) != len)
1920 fclose(fp);
1921 fprintf(stderr, "Error: Bad write to file '%s'.\n", filename);
1922 return 0;
1924 return 1;
1927 // Write a binary value of the given byte order and byte size to a file,
1928 // loading it from a 32-bit unsigned integer.
1929 static int WriteBin4(const ByteOrderT order, const uint bytes, const uint32 in, FILE *fp, const char *filename)
1931 uint8 out[4];
1932 uint i;
1934 switch(order)
1936 case BO_LITTLE:
1937 for(i = 0;i < bytes;i++)
1938 out[i] = (in>>(i*8)) & 0x000000FF;
1939 break;
1940 case BO_BIG:
1941 for(i = 0;i < bytes;i++)
1942 out[bytes - i - 1] = (in>>(i*8)) & 0x000000FF;
1943 break;
1944 default:
1945 break;
1947 if(fwrite(out, 1, bytes, fp) != bytes)
1949 fprintf(stderr, "Error: Bad write to file '%s'.\n", filename);
1950 return 0;
1952 return 1;
1955 // Store the OpenAL Soft HRTF data set.
1956 static int StoreMhr(const HrirDataT *hData, const char *filename)
1958 uint channels = (hData->mChannelType == CT_STEREO) ? 2 : 1;
1959 uint n = hData->mIrPoints;
1960 FILE *fp;
1961 uint fi, ei, ai, i;
1962 uint dither_seed = 22222;
1964 if((fp=fopen(filename, "wb")) == NULL)
1966 fprintf(stderr, "Error: Could not open MHR file '%s'.\n", filename);
1967 return 0;
1969 if(!WriteAscii(MHR_FORMAT, fp, filename))
1970 return 0;
1971 if(!WriteBin4(BO_LITTLE, 4, (uint32)hData->mIrRate, fp, filename))
1972 return 0;
1973 if(!WriteBin4(BO_LITTLE, 1, (uint32)hData->mSampleType, fp, filename))
1974 return 0;
1975 if(!WriteBin4(BO_LITTLE, 1, (uint32)hData->mChannelType, fp, filename))
1976 return 0;
1977 if(!WriteBin4(BO_LITTLE, 1, (uint32)hData->mIrPoints, fp, filename))
1978 return 0;
1979 if(!WriteBin4(BO_LITTLE, 1, (uint32)hData->mFdCount, fp, filename))
1980 return 0;
1981 for(fi = 0;fi < hData->mFdCount;fi++)
1983 if(!WriteBin4(BO_LITTLE, 2, (uint32)(1000.0 * hData->mFds[fi].mDistance), fp, filename))
1984 return 0;
1985 if(!WriteBin4(BO_LITTLE, 1, (uint32)hData->mFds[fi].mEvCount, fp, filename))
1986 return 0;
1987 for(ei = 0;ei < hData->mFds[fi].mEvCount;ei++)
1989 if(!WriteBin4(BO_LITTLE, 1, (uint32)hData->mFds[fi].mEvs[ei].mAzCount, fp, filename))
1990 return 0;
1994 for(fi = 0;fi < hData->mFdCount;fi++)
1996 const double scale = (hData->mSampleType == ST_S16) ? 32767.0 :
1997 ((hData->mSampleType == ST_S24) ? 8388607.0 : 0.0);
1998 const int bps = (hData->mSampleType == ST_S16) ? 2 :
1999 ((hData->mSampleType == ST_S24) ? 3 : 0);
2001 for(ei = 0;ei < hData->mFds[fi].mEvCount;ei++)
2003 for(ai = 0;ai < hData->mFds[fi].mEvs[ei].mAzCount;ai++)
2005 HrirAzT *azd = &hData->mFds[fi].mEvs[ei].mAzs[ai];
2006 double out[2 * MAX_TRUNCSIZE];
2008 TpdfDither(out, azd->mIrs[0], scale, n, channels, &dither_seed);
2009 if(hData->mChannelType == CT_STEREO)
2010 TpdfDither(out+1, azd->mIrs[1], scale, n, channels, &dither_seed);
2011 for(i = 0;i < (channels * n);i++)
2013 int v = (int)Clamp(out[i], -scale-1.0, scale);
2014 if(!WriteBin4(BO_LITTLE, bps, (uint32)v, fp, filename))
2015 return 0;
2020 for(fi = 0;fi < hData->mFdCount;fi++)
2022 for(ei = 0;ei < hData->mFds[fi].mEvCount;ei++)
2024 for(ai = 0;ai < hData->mFds[fi].mEvs[ei].mAzCount;ai++)
2026 HrirAzT *azd = &hData->mFds[fi].mEvs[ei].mAzs[ai];
2027 int v = (int)fmin(round(hData->mIrRate * azd->mDelays[0]), MAX_HRTD);
2029 if(!WriteBin4(BO_LITTLE, 1, (uint32)v, fp, filename))
2030 return 0;
2031 if(hData->mChannelType == CT_STEREO)
2033 v = (int)fmin(round(hData->mIrRate * azd->mDelays[1]), MAX_HRTD);
2035 if(!WriteBin4(BO_LITTLE, 1, (uint32)v, fp, filename))
2036 return 0;
2041 fclose(fp);
2042 return 1;
2046 /***********************
2047 *** HRTF processing ***
2048 ***********************/
2050 // Calculate the onset time of an HRIR and average it with any existing
2051 // timing for its field, elevation, azimuth, and ear.
2052 static double AverageHrirOnset(const uint rate, const uint n, const double *hrir, const double f, const double onset)
2054 double mag = 0.0;
2055 uint i;
2057 for(i = 0;i < n;i++)
2058 mag = fmax(fabs(hrir[i]), mag);
2059 mag *= 0.15;
2060 for(i = 0;i < n;i++)
2062 if(fabs(hrir[i]) >= mag)
2063 break;
2065 return Lerp(onset, (double)i / rate, f);
2068 // Calculate the magnitude response of an HRIR and average it with any
2069 // existing responses for its field, elevation, azimuth, and ear.
2070 static void AverageHrirMagnitude(const uint points, const uint n, const double *hrir, const double f, double *mag)
2072 uint m = 1 + (n / 2), i;
2073 Complex *h = CreateComplexes(n);
2074 double *r = CreateDoubles(n);
2076 for(i = 0;i < points;i++)
2077 h[i] = MakeComplex(hrir[i], 0.0);
2078 for(;i < n;i++)
2079 h[i] = MakeComplex(0.0, 0.0);
2080 FftForward(n, h, h);
2081 MagnitudeResponse(n, h, r);
2082 for(i = 0;i < m;i++)
2083 mag[i] = Lerp(mag[i], r[i], f);
2084 free(r);
2085 free(h);
2088 /* Calculate the contribution of each HRIR to the diffuse-field average based
2089 * on the area of its surface patch. All patches are centered at the HRIR
2090 * coordinates on the unit sphere and are measured by solid angle.
2092 static void CalculateDfWeights(const HrirDataT *hData, double *weights)
2094 double sum, evs, ev, upperEv, lowerEv, solidAngle;
2095 uint fi, ei;
2097 sum = 0.0;
2098 for(fi = 0;fi < hData->mFdCount;fi++)
2100 evs = M_PI / 2.0 / (hData->mFds[fi].mEvCount - 1);
2101 for(ei = hData->mFds[fi].mEvStart;ei < hData->mFds[fi].mEvCount;ei++)
2103 // For each elevation, calculate the upper and lower limits of
2104 // the patch band.
2105 ev = hData->mFds[fi].mEvs[ei].mElevation;
2106 lowerEv = fmax(-M_PI / 2.0, ev - evs);
2107 upperEv = fmin(M_PI / 2.0, ev + evs);
2108 // Calculate the area of the patch band.
2109 solidAngle = 2.0 * M_PI * (sin(upperEv) - sin(lowerEv));
2110 // Each weight is the area of one patch.
2111 weights[(fi * MAX_EV_COUNT) + ei] = solidAngle / hData->mFds[fi].mEvs[ei].mAzCount;
2112 // Sum the total surface area covered by the HRIRs of all fields.
2113 sum += solidAngle;
2116 /* TODO: It may be interesting to experiment with how a volume-based
2117 weighting performs compared to the existing distance-indepenent
2118 surface patches.
2120 for(fi = 0;fi < hData->mFdCount;fi++)
2122 // Normalize the weights given the total surface coverage for all
2123 // fields.
2124 for(ei = hData->mFds[fi].mEvStart;ei < hData->mFds[fi].mEvCount;ei++)
2125 weights[(fi * MAX_EV_COUNT) + ei] /= sum;
2129 /* Calculate the diffuse-field average from the given magnitude responses of
2130 * the HRIR set. Weighting can be applied to compensate for the varying
2131 * surface area covered by each HRIR. The final average can then be limited
2132 * by the specified magnitude range (in positive dB; 0.0 to skip).
2134 static void CalculateDiffuseFieldAverage(const HrirDataT *hData, const uint channels, const uint m, const int weighted, const double limit, double *dfa)
2136 double *weights = CreateDoubles(hData->mFdCount * MAX_EV_COUNT);
2137 uint count, ti, fi, ei, i, ai;
2139 if(weighted)
2141 // Use coverage weighting to calculate the average.
2142 CalculateDfWeights(hData, weights);
2144 else
2146 double weight;
2148 // If coverage weighting is not used, the weights still need to be
2149 // averaged by the number of existing HRIRs.
2150 count = hData->mIrCount;
2151 for(fi = 0;fi < hData->mFdCount;fi++)
2153 for(ei = 0;ei < hData->mFds[fi].mEvStart;ei++)
2154 count -= hData->mFds[fi].mEvs[ei].mAzCount;
2156 weight = 1.0 / count;
2158 for(fi = 0;fi < hData->mFdCount;fi++)
2160 for(ei = hData->mFds[fi].mEvStart;ei < hData->mFds[fi].mEvCount;ei++)
2161 weights[(fi * MAX_EV_COUNT) + ei] = weight;
2164 for(ti = 0;ti < channels;ti++)
2166 for(i = 0;i < m;i++)
2167 dfa[(ti * m) + i] = 0.0;
2168 for(fi = 0;fi < hData->mFdCount;fi++)
2170 for(ei = hData->mFds[fi].mEvStart;ei < hData->mFds[fi].mEvCount;ei++)
2172 for(ai = 0;ai < hData->mFds[fi].mEvs[ei].mAzCount;ai++)
2174 HrirAzT *azd = &hData->mFds[fi].mEvs[ei].mAzs[ai];
2175 // Get the weight for this HRIR's contribution.
2176 double weight = weights[(fi * MAX_EV_COUNT) + ei];
2178 // Add this HRIR's weighted power average to the total.
2179 for(i = 0;i < m;i++)
2180 dfa[(ti * m) + i] += weight * azd->mIrs[ti][i] * azd->mIrs[ti][i];
2184 // Finish the average calculation and keep it from being too small.
2185 for(i = 0;i < m;i++)
2186 dfa[(ti * m) + i] = fmax(sqrt(dfa[(ti * m) + i]), EPSILON);
2187 // Apply a limit to the magnitude range of the diffuse-field average
2188 // if desired.
2189 if(limit > 0.0)
2190 LimitMagnitudeResponse(hData->mFftSize, m, limit, &dfa[ti * m], &dfa[ti * m]);
2192 free(weights);
2195 // Perform diffuse-field equalization on the magnitude responses of the HRIR
2196 // set using the given average response.
2197 static void DiffuseFieldEqualize(const uint channels, const uint m, const double *dfa, const HrirDataT *hData)
2199 uint ti, fi, ei, ai, i;
2201 for(ti = 0;ti < channels;ti++)
2203 for(fi = 0;fi < hData->mFdCount;fi++)
2205 for(ei = hData->mFds[fi].mEvStart;ei < hData->mFds[fi].mEvCount;ei++)
2207 for(ai = 0;ai < hData->mFds[fi].mEvs[ei].mAzCount;ai++)
2209 HrirAzT *azd = &hData->mFds[fi].mEvs[ei].mAzs[ai];
2211 for(i = 0;i < m;i++)
2212 azd->mIrs[ti][i] /= dfa[(ti * m) + i];
2219 // Perform minimum-phase reconstruction using the magnitude responses of the
2220 // HRIR set.
2221 static void ReconstructHrirs(const HrirDataT *hData)
2223 uint channels = (hData->mChannelType == CT_STEREO) ? 2 : 1;
2224 uint n = hData->mFftSize;
2225 uint ti, fi, ei, ai, i;
2226 Complex *h = CreateComplexes(n);
2227 uint total, count, pcdone, lastpc;
2229 total = hData->mIrCount;
2230 for(fi = 0;fi < hData->mFdCount;fi++)
2232 for(ei = 0;ei < hData->mFds[fi].mEvStart;ei++)
2233 total -= hData->mFds[fi].mEvs[ei].mAzCount;
2235 total *= channels;
2236 count = pcdone = lastpc = 0;
2237 printf("%3d%% done.", pcdone);
2238 fflush(stdout);
2239 for(ti = 0;ti < channels;ti++)
2241 for(fi = 0;fi < hData->mFdCount;fi++)
2243 for(ei = hData->mFds[fi].mEvStart;ei < hData->mFds[fi].mEvCount;ei++)
2245 for(ai = 0;ai < hData->mFds[fi].mEvs[ei].mAzCount;ai++)
2247 HrirAzT *azd = &hData->mFds[fi].mEvs[ei].mAzs[ai];
2249 MinimumPhase(n, azd->mIrs[ti], h);
2250 FftInverse(n, h, h);
2251 for(i = 0;i < hData->mIrPoints;i++)
2252 azd->mIrs[ti][i] = h[i].Real;
2253 pcdone = ++count * 100 / total;
2254 if(pcdone != lastpc)
2256 lastpc = pcdone;
2257 printf("\r%3d%% done.", pcdone);
2258 fflush(stdout);
2264 printf("\n");
2265 free(h);
2268 // Resamples the HRIRs for use at the given sampling rate.
2269 static void ResampleHrirs(const uint rate, HrirDataT *hData)
2271 uint channels = (hData->mChannelType == CT_STEREO) ? 2 : 1;
2272 uint n = hData->mIrPoints;
2273 uint ti, fi, ei, ai;
2274 ResamplerT rs;
2276 ResamplerSetup(&rs, hData->mIrRate, rate);
2277 for(ti = 0;ti < channels;ti++)
2279 for(fi = 0;fi < hData->mFdCount;fi++)
2281 for(ei = hData->mFds[fi].mEvStart;ei < hData->mFds[fi].mEvCount;ei++)
2283 for(ai = 0;ai < hData->mFds[fi].mEvs[ei].mAzCount;ai++)
2285 HrirAzT *azd = &hData->mFds[fi].mEvs[ei].mAzs[ai];
2287 ResamplerRun(&rs, n, azd->mIrs[ti], n, azd->mIrs[ti]);
2292 hData->mIrRate = rate;
2293 ResamplerClear(&rs);
2296 /* Given field and elevation indices and an azimuth, calculate the indices of
2297 * the two HRIRs that bound the coordinate along with a factor for
2298 * calculating the continuous HRIR using interpolation.
2300 static void CalcAzIndices(const HrirDataT *hData, const uint fi, const uint ei, const double az, uint *a0, uint *a1, double *af)
2302 double f = (2.0*M_PI + az) * hData->mFds[fi].mEvs[ei].mAzCount / (2.0*M_PI);
2303 uint i = (uint)f % hData->mFds[fi].mEvs[ei].mAzCount;
2305 f -= floor(f);
2306 *a0 = i;
2307 *a1 = (i + 1) % hData->mFds[fi].mEvs[ei].mAzCount;
2308 *af = f;
2311 // Synthesize any missing onset timings at the bottom elevations of each
2312 // field. This just blends between slightly exaggerated known onsets (not
2313 // an accurate model).
2314 static void SynthesizeOnsets(HrirDataT *hData)
2316 uint channels = (hData->mChannelType == CT_STEREO) ? 2 : 1;
2317 uint ti, fi, oi, ai, ei, a0, a1;
2318 double t, of, af;
2320 for(ti = 0;ti < channels;ti++)
2322 for(fi = 0;fi < hData->mFdCount;fi++)
2324 if(hData->mFds[fi].mEvStart <= 0)
2325 continue;
2326 oi = hData->mFds[fi].mEvStart;
2327 t = 0.0;
2328 for(ai = 0;ai < hData->mFds[fi].mEvs[oi].mAzCount;ai++)
2329 t += hData->mFds[fi].mEvs[oi].mAzs[ai].mDelays[ti];
2330 hData->mFds[fi].mEvs[0].mAzs[0].mDelays[ti] = 1.32e-4 + (t / hData->mFds[fi].mEvs[oi].mAzCount);
2331 for(ei = 1;ei < hData->mFds[fi].mEvStart;ei++)
2333 of = (double)ei / hData->mFds[fi].mEvStart;
2334 for(ai = 0;ai < hData->mFds[fi].mEvs[ei].mAzCount;ai++)
2336 CalcAzIndices(hData, fi, oi, hData->mFds[fi].mEvs[ei].mAzs[ai].mAzimuth, &a0, &a1, &af);
2337 hData->mFds[fi].mEvs[ei].mAzs[ai].mDelays[ti] = Lerp(hData->mFds[fi].mEvs[0].mAzs[0].mDelays[ti], Lerp(hData->mFds[fi].mEvs[oi].mAzs[a0].mDelays[ti], hData->mFds[fi].mEvs[oi].mAzs[a1].mDelays[ti], af), of);
2344 /* Attempt to synthesize any missing HRIRs at the bottom elevations of each
2345 * field. Right now this just blends the lowest elevation HRIRs together and
2346 * applies some attenuation and high frequency damping. It is a simple, if
2347 * inaccurate model.
2349 static void SynthesizeHrirs(HrirDataT *hData)
2351 uint channels = (hData->mChannelType == CT_STEREO) ? 2 : 1;
2352 uint n = hData->mIrPoints;
2353 uint ti, fi, oi, ai, ei, i;
2354 double lp[4], s0, s1;
2355 double of, b;
2356 uint a0, a1;
2357 double af;
2359 for(ti = 0;ti < channels;ti++)
2361 for(fi = 0;fi < hData->mFdCount;fi++)
2363 if(hData->mFds[fi].mEvStart <= 0)
2364 continue;
2365 oi = hData->mFds[fi].mEvStart;
2366 for(i = 0;i < n;i++)
2367 hData->mFds[fi].mEvs[0].mAzs[0].mIrs[ti][i] = 0.0;
2368 for(ai = 0;ai < hData->mFds[fi].mEvs[oi].mAzCount;ai++)
2370 for(i = 0;i < n;i++)
2371 hData->mFds[fi].mEvs[0].mAzs[0].mIrs[ti][i] += hData->mFds[fi].mEvs[oi].mAzs[ai].mIrs[ti][i] / hData->mFds[fi].mEvs[oi].mAzCount;
2373 for(ei = 1;ei < hData->mFds[fi].mEvStart;ei++)
2375 of = (double)ei / hData->mFds[fi].mEvStart;
2376 b = (1.0 - of) * (3.5e-6 * hData->mIrRate);
2377 for(ai = 0;ai < hData->mFds[fi].mEvs[ei].mAzCount;ai++)
2379 CalcAzIndices(hData, fi, oi, hData->mFds[fi].mEvs[ei].mAzs[ai].mAzimuth, &a0, &a1, &af);
2380 lp[0] = 0.0;
2381 lp[1] = 0.0;
2382 lp[2] = 0.0;
2383 lp[3] = 0.0;
2384 for(i = 0;i < n;i++)
2386 s0 = hData->mFds[fi].mEvs[0].mAzs[0].mIrs[ti][i];
2387 s1 = Lerp(hData->mFds[fi].mEvs[oi].mAzs[a0].mIrs[ti][i], hData->mFds[fi].mEvs[oi].mAzs[a1].mIrs[ti][i], af);
2388 s0 = Lerp(s0, s1, of);
2389 lp[0] = Lerp(s0, lp[0], b);
2390 lp[1] = Lerp(lp[0], lp[1], b);
2391 lp[2] = Lerp(lp[1], lp[2], b);
2392 lp[3] = Lerp(lp[2], lp[3], b);
2393 hData->mFds[fi].mEvs[ei].mAzs[ai].mIrs[ti][i] = lp[3];
2397 b = 3.5e-6 * hData->mIrRate;
2398 lp[0] = 0.0;
2399 lp[1] = 0.0;
2400 lp[2] = 0.0;
2401 lp[3] = 0.0;
2402 for(i = 0;i < n;i++)
2404 s0 = hData->mFds[fi].mEvs[0].mAzs[0].mIrs[ti][i];
2405 lp[0] = Lerp(s0, lp[0], b);
2406 lp[1] = Lerp(lp[0], lp[1], b);
2407 lp[2] = Lerp(lp[1], lp[2], b);
2408 lp[3] = Lerp(lp[2], lp[3], b);
2409 hData->mFds[fi].mEvs[0].mAzs[0].mIrs[ti][i] = lp[3];
2411 hData->mFds[fi].mEvStart = 0;
2416 // The following routines assume a full set of HRIRs for all elevations.
2418 // Normalize the HRIR set and slightly attenuate the result.
2419 static void NormalizeHrirs(const HrirDataT *hData)
2421 uint channels = (hData->mChannelType == CT_STEREO) ? 2 : 1;
2422 uint n = hData->mIrPoints;
2423 uint ti, fi, ei, ai, i;
2424 double maxLevel = 0.0;
2426 for(ti = 0;ti < channels;ti++)
2428 for(fi = 0;fi < hData->mFdCount;fi++)
2430 for(ei = 0;ei < hData->mFds[fi].mEvCount;ei++)
2432 for(ai = 0;ai < hData->mFds[fi].mEvs[ei].mAzCount;ai++)
2434 HrirAzT *azd = &hData->mFds[fi].mEvs[ei].mAzs[ai];
2436 for(i = 0;i < n;i++)
2437 maxLevel = fmax(fabs(azd->mIrs[ti][i]), maxLevel);
2442 maxLevel = 1.01 * maxLevel;
2443 for(ti = 0;ti < channels;ti++)
2445 for(fi = 0;fi < hData->mFdCount;fi++)
2447 for(ei = 0;ei < hData->mFds[fi].mEvCount;ei++)
2449 for(ai = 0;ai < hData->mFds[fi].mEvs[ei].mAzCount;ai++)
2451 HrirAzT *azd = &hData->mFds[fi].mEvs[ei].mAzs[ai];
2453 for(i = 0;i < n;i++)
2454 azd->mIrs[ti][i] /= maxLevel;
2461 // Calculate the left-ear time delay using a spherical head model.
2462 static double CalcLTD(const double ev, const double az, const double rad, const double dist)
2464 double azp, dlp, l, al;
2466 azp = asin(cos(ev) * sin(az));
2467 dlp = sqrt((dist*dist) + (rad*rad) + (2.0*dist*rad*sin(azp)));
2468 l = sqrt((dist*dist) - (rad*rad));
2469 al = (0.5 * M_PI) + azp;
2470 if(dlp > l)
2471 dlp = l + (rad * (al - acos(rad / dist)));
2472 return dlp / 343.3;
2475 // Calculate the effective head-related time delays for each minimum-phase
2476 // HRIR.
2477 static void CalculateHrtds(const HeadModelT model, const double radius, HrirDataT *hData)
2479 uint channels = (hData->mChannelType == CT_STEREO) ? 2 : 1;
2480 double minHrtd = INFINITY, maxHrtd = -INFINITY;
2481 uint ti, fi, ei, ai;
2482 double t;
2484 if(model == HM_DATASET)
2486 for(ti = 0;ti < channels;ti++)
2488 for(fi = 0;fi < hData->mFdCount;fi++)
2490 for(ei = 0;ei < hData->mFds[fi].mEvCount;ei++)
2492 for(ai = 0;ai < hData->mFds[fi].mEvs[ei].mAzCount;ai++)
2494 HrirAzT *azd = &hData->mFds[fi].mEvs[ei].mAzs[ai];
2496 t = azd->mDelays[ti] * radius / hData->mRadius;
2497 azd->mDelays[ti] = t;
2498 maxHrtd = fmax(t, maxHrtd);
2499 minHrtd = fmin(t, minHrtd);
2505 else
2507 for(ti = 0;ti < channels;ti++)
2509 for(fi = 0;fi < hData->mFdCount;fi++)
2511 for(ei = 0;ei < hData->mFds[fi].mEvCount;ei++)
2513 HrirEvT *evd = &hData->mFds[fi].mEvs[ei];
2515 for(ai = 0;ai < evd->mAzCount;ai++)
2517 HrirAzT *azd = &evd->mAzs[ai];
2519 t = CalcLTD(evd->mElevation, azd->mAzimuth, radius, hData->mFds[fi].mDistance);
2520 azd->mDelays[ti] = t;
2521 maxHrtd = fmax(t, maxHrtd);
2522 minHrtd = fmin(t, minHrtd);
2528 for(ti = 0;ti < channels;ti++)
2530 for(fi = 0;fi < hData->mFdCount;fi++)
2532 for(ei = 0;ei < hData->mFds[fi].mEvCount;ei++)
2534 for(ai = 0;ai < hData->mFds[fi].mEvs[ei].mAzCount;ai++)
2535 hData->mFds[fi].mEvs[ei].mAzs[ai].mDelays[ti] -= minHrtd;
2541 // Clear the initial HRIR data state.
2542 static void ResetHrirData(HrirDataT *hData)
2544 hData->mIrRate = 0;
2545 hData->mSampleType = ST_S24;
2546 hData->mChannelType = CT_NONE;
2547 hData->mIrPoints = 0;
2548 hData->mFftSize = 0;
2549 hData->mIrSize = 0;
2550 hData->mRadius = 0.0;
2551 hData->mIrCount = 0;
2552 hData->mFdCount = 0;
2553 hData->mFds = NULL;
2556 // Allocate and configure dynamic HRIR structures.
2557 static int PrepareHrirData(const uint fdCount, const double distances[MAX_FD_COUNT], const uint evCounts[MAX_FD_COUNT], const uint azCounts[MAX_FD_COUNT * MAX_EV_COUNT], HrirDataT *hData)
2559 uint evTotal = 0, azTotal = 0, fi, ei, ai;
2561 for(fi = 0;fi < fdCount;fi++)
2563 evTotal += evCounts[fi];
2564 for(ei = 0;ei < evCounts[fi];ei++)
2565 azTotal += azCounts[(fi * MAX_EV_COUNT) + ei];
2567 hData->mFds = calloc(fdCount, sizeof(*hData->mFds));
2568 if(hData->mFds == NULL)
2569 return 0;
2570 hData->mFds[0].mEvs = calloc(evTotal, sizeof(*hData->mFds[0].mEvs));
2571 if(hData->mFds[0].mEvs == NULL)
2572 return 0;
2573 hData->mFds[0].mEvs[0].mAzs = calloc(azTotal, sizeof(*hData->mFds[0].mEvs[0].mAzs));
2574 if(hData->mFds[0].mEvs[0].mAzs == NULL)
2575 return 0;
2576 hData->mIrCount = azTotal;
2577 hData->mFdCount = fdCount;
2578 evTotal = 0;
2579 azTotal = 0;
2580 for(fi = 0;fi < fdCount;fi++)
2582 hData->mFds[fi].mDistance = distances[fi];
2583 hData->mFds[fi].mEvCount = evCounts[fi];
2584 hData->mFds[fi].mEvStart = 0;
2585 hData->mFds[fi].mEvs = &hData->mFds[0].mEvs[evTotal];
2586 evTotal += evCounts[fi];
2587 for(ei = 0;ei < evCounts[fi];ei++)
2589 uint azCount = azCounts[(fi * MAX_EV_COUNT) + ei];
2591 hData->mFds[fi].mIrCount += azCount;
2592 hData->mFds[fi].mEvs[ei].mElevation = -M_PI / 2.0 + M_PI * ei / (evCounts[fi] - 1);
2593 hData->mFds[fi].mEvs[ei].mIrCount += azCount;
2594 hData->mFds[fi].mEvs[ei].mAzCount = azCount;
2595 hData->mFds[fi].mEvs[ei].mAzs = &hData->mFds[0].mEvs[0].mAzs[azTotal];
2596 for(ai = 0;ai < azCount;ai++)
2598 hData->mFds[fi].mEvs[ei].mAzs[ai].mAzimuth = 2.0 * M_PI * ai / azCount;
2599 hData->mFds[fi].mEvs[ei].mAzs[ai].mIndex = azTotal + ai;
2600 hData->mFds[fi].mEvs[ei].mAzs[ai].mDelays[0] = 0.0;
2601 hData->mFds[fi].mEvs[ei].mAzs[ai].mDelays[1] = 0.0;
2602 hData->mFds[fi].mEvs[ei].mAzs[ai].mIrs[0] = NULL;
2603 hData->mFds[fi].mEvs[ei].mAzs[ai].mIrs[1] = NULL;
2605 azTotal += azCount;
2608 return 1;
2611 // Clean up HRIR data.
2612 static void FreeHrirData(HrirDataT *hData)
2614 if(hData->mFds != NULL)
2616 if(hData->mFds[0].mEvs != NULL)
2618 if(hData->mFds[0].mEvs[0].mAzs)
2620 if(hData->mFds[0].mEvs[0].mAzs[0].mIrs[0] != NULL)
2621 free(hData->mFds[0].mEvs[0].mAzs[0].mIrs[0]);
2622 free(hData->mFds[0].mEvs[0].mAzs);
2624 free(hData->mFds[0].mEvs);
2626 free(hData->mFds);
2627 hData->mFds = NULL;
2631 // Match the channel type from a given identifier.
2632 static ChannelTypeT MatchChannelType(const char *ident)
2634 if(strcasecmp(ident, "mono") == 0)
2635 return CT_MONO;
2636 if(strcasecmp(ident, "stereo") == 0)
2637 return CT_STEREO;
2638 return CT_NONE;
2641 // Process the data set definition to read and validate the data set metrics.
2642 static int ProcessMetrics(TokenReaderT *tr, const uint fftSize, const uint truncSize, HrirDataT *hData)
2644 int hasRate = 0, hasType = 0, hasPoints = 0, hasRadius = 0;
2645 int hasDistance = 0, hasAzimuths = 0;
2646 char ident[MAX_IDENT_LEN+1];
2647 uint line, col;
2648 double fpVal;
2649 uint points;
2650 int intVal;
2651 double distances[MAX_FD_COUNT];
2652 uint fdCount = 0;
2653 uint evCounts[MAX_FD_COUNT];
2654 uint *azCounts = calloc(MAX_FD_COUNT * MAX_EV_COUNT, sizeof(*azCounts));
2656 if(azCounts == NULL)
2658 fprintf(stderr, "Error: Out of memory.\n");
2659 exit(-1);
2661 TrIndication(tr, &line, &col);
2662 while(TrIsIdent(tr))
2664 TrIndication(tr, &line, &col);
2665 if(!TrReadIdent(tr, MAX_IDENT_LEN, ident))
2666 goto error;
2667 if(strcasecmp(ident, "rate") == 0)
2669 if(hasRate)
2671 TrErrorAt(tr, line, col, "Redefinition of 'rate'.\n");
2672 goto error;
2674 if(!TrReadOperator(tr, "="))
2675 goto error;
2676 if(!TrReadInt(tr, MIN_RATE, MAX_RATE, &intVal))
2677 goto error;
2678 hData->mIrRate = (uint)intVal;
2679 hasRate = 1;
2681 else if(strcasecmp(ident, "type") == 0)
2683 char type[MAX_IDENT_LEN+1];
2685 if(hasType)
2687 TrErrorAt(tr, line, col, "Redefinition of 'type'.\n");
2688 goto error;
2690 if(!TrReadOperator(tr, "="))
2691 goto error;
2693 if(!TrReadIdent(tr, MAX_IDENT_LEN, type))
2694 goto error;
2695 hData->mChannelType = MatchChannelType(type);
2696 if(hData->mChannelType == CT_NONE)
2698 TrErrorAt(tr, line, col, "Expected a channel type.\n");
2699 goto error;
2701 hasType = 1;
2703 else if(strcasecmp(ident, "points") == 0)
2705 if(hasPoints)
2707 TrErrorAt(tr, line, col, "Redefinition of 'points'.\n");
2708 goto error;
2710 if(!TrReadOperator(tr, "="))
2711 goto error;
2712 TrIndication(tr, &line, &col);
2713 if(!TrReadInt(tr, MIN_POINTS, MAX_POINTS, &intVal))
2714 goto error;
2715 points = (uint)intVal;
2716 if(fftSize > 0 && points > fftSize)
2718 TrErrorAt(tr, line, col, "Value exceeds the overridden FFT size.\n");
2719 goto error;
2721 if(points < truncSize)
2723 TrErrorAt(tr, line, col, "Value is below the truncation size.\n");
2724 goto error;
2726 hData->mIrPoints = points;
2727 if(fftSize <= 0)
2729 hData->mFftSize = DEFAULT_FFTSIZE;
2730 hData->mIrSize = 1 + (DEFAULT_FFTSIZE / 2);
2732 else
2734 hData->mFftSize = fftSize;
2735 hData->mIrSize = 1 + (fftSize / 2);
2736 if(points > hData->mIrSize)
2737 hData->mIrSize = points;
2739 hasPoints = 1;
2741 else if(strcasecmp(ident, "radius") == 0)
2743 if(hasRadius)
2745 TrErrorAt(tr, line, col, "Redefinition of 'radius'.\n");
2746 goto error;
2748 if(!TrReadOperator(tr, "="))
2749 goto error;
2750 if(!TrReadFloat(tr, MIN_RADIUS, MAX_RADIUS, &fpVal))
2751 goto error;
2752 hData->mRadius = fpVal;
2753 hasRadius = 1;
2755 else if(strcasecmp(ident, "distance") == 0)
2757 uint count = 0;
2759 if(hasDistance)
2761 TrErrorAt(tr, line, col, "Redefinition of 'distance'.\n");
2762 goto error;
2764 if(!TrReadOperator(tr, "="))
2765 goto error;
2767 for(;;)
2769 if(!TrReadFloat(tr, MIN_DISTANCE, MAX_DISTANCE, &fpVal))
2770 goto error;
2771 if(count > 0 && fpVal <= distances[count - 1])
2773 TrError(tr, "Distances are not ascending.\n");
2774 goto error;
2776 distances[count++] = fpVal;
2777 if(!TrIsOperator(tr, ","))
2778 break;
2779 if(count >= MAX_FD_COUNT)
2781 TrError(tr, "Exceeded the maximum of %d fields.\n", MAX_FD_COUNT);
2782 goto error;
2784 TrReadOperator(tr, ",");
2786 if(fdCount != 0 && count != fdCount)
2788 TrError(tr, "Did not match the specified number of %d fields.\n", fdCount);
2789 goto error;
2791 fdCount = count;
2792 hasDistance = 1;
2794 else if(strcasecmp(ident, "azimuths") == 0)
2796 uint count = 0;
2798 if(hasAzimuths)
2800 TrErrorAt(tr, line, col, "Redefinition of 'azimuths'.\n");
2801 goto error;
2803 if(!TrReadOperator(tr, "="))
2804 goto error;
2806 evCounts[0] = 0;
2807 for(;;)
2809 if(!TrReadInt(tr, MIN_AZ_COUNT, MAX_AZ_COUNT, &intVal))
2810 goto error;
2811 azCounts[(count * MAX_EV_COUNT) + evCounts[count]++] = (uint)intVal;
2812 if(TrIsOperator(tr, ","))
2814 if(evCounts[count] >= MAX_EV_COUNT)
2816 TrError(tr, "Exceeded the maximum of %d elevations.\n", MAX_EV_COUNT);
2817 goto error;
2819 TrReadOperator(tr, ",");
2821 else
2823 if(evCounts[count] < MIN_EV_COUNT)
2825 TrErrorAt(tr, line, col, "Did not reach the minimum of %d azimuth counts.\n", MIN_EV_COUNT);
2826 goto error;
2828 if(azCounts[count * MAX_EV_COUNT] != 1 || azCounts[(count * MAX_EV_COUNT) + evCounts[count] - 1] != 1)
2830 TrError(tr, "Poles are not singular for field %d.\n", count - 1);
2831 goto error;
2833 count++;
2834 if(TrIsOperator(tr, ";"))
2836 if(count >= MAX_FD_COUNT)
2838 TrError(tr, "Exceeded the maximum number of %d fields.\n", MAX_FD_COUNT);
2839 goto error;
2841 evCounts[count] = 0;
2842 TrReadOperator(tr, ";");
2844 else
2846 break;
2850 if(fdCount != 0 && count != fdCount)
2852 TrError(tr, "Did not match the specified number of %d fields.\n", fdCount);
2853 goto error;
2855 fdCount = count;
2856 hasAzimuths = 1;
2858 else
2860 TrErrorAt(tr, line, col, "Expected a metric name.\n");
2861 goto error;
2863 TrSkipWhitespace(tr);
2865 if(!(hasRate && hasPoints && hasRadius && hasDistance && hasAzimuths))
2867 TrErrorAt(tr, line, col, "Expected a metric name.\n");
2868 goto error;
2870 if(distances[0] < hData->mRadius)
2872 TrError(tr, "Distance cannot start below head radius.\n");
2873 goto error;
2875 if(hData->mChannelType == CT_NONE)
2876 hData->mChannelType = CT_MONO;
2877 if(!PrepareHrirData(fdCount, distances, evCounts, azCounts, hData))
2879 fprintf(stderr, "Error: Out of memory.\n");
2880 exit(-1);
2882 free(azCounts);
2883 return 1;
2885 error:
2886 free(azCounts);
2887 return 0;
2890 // Parse an index triplet from the data set definition.
2891 static int ReadIndexTriplet(TokenReaderT *tr, const HrirDataT *hData, uint *fi, uint *ei, uint *ai)
2893 int intVal;
2895 if(hData->mFdCount > 1)
2897 if(!TrReadInt(tr, 0, (int)hData->mFdCount - 1, &intVal))
2898 return 0;
2899 *fi = (uint)intVal;
2900 if(!TrReadOperator(tr, ","))
2901 return 0;
2903 else
2905 *fi = 0;
2907 if(!TrReadInt(tr, 0, (int)hData->mFds[*fi].mEvCount - 1, &intVal))
2908 return 0;
2909 *ei = (uint)intVal;
2910 if(!TrReadOperator(tr, ","))
2911 return 0;
2912 if(!TrReadInt(tr, 0, (int)hData->mFds[*fi].mEvs[*ei].mAzCount - 1, &intVal))
2913 return 0;
2914 *ai = (uint)intVal;
2915 return 1;
2918 // Match the source format from a given identifier.
2919 static SourceFormatT MatchSourceFormat(const char *ident)
2921 if(strcasecmp(ident, "wave") == 0)
2922 return SF_WAVE;
2923 if(strcasecmp(ident, "bin_le") == 0)
2924 return SF_BIN_LE;
2925 if(strcasecmp(ident, "bin_be") == 0)
2926 return SF_BIN_BE;
2927 if(strcasecmp(ident, "ascii") == 0)
2928 return SF_ASCII;
2929 return SF_NONE;
2932 // Match the source element type from a given identifier.
2933 static ElementTypeT MatchElementType(const char *ident)
2935 if(strcasecmp(ident, "int") == 0)
2936 return ET_INT;
2937 if(strcasecmp(ident, "fp") == 0)
2938 return ET_FP;
2939 return ET_NONE;
2942 // Parse and validate a source reference from the data set definition.
2943 static int ReadSourceRef(TokenReaderT *tr, SourceRefT *src)
2945 char ident[MAX_IDENT_LEN+1];
2946 uint line, col;
2947 int intVal;
2949 TrIndication(tr, &line, &col);
2950 if(!TrReadIdent(tr, MAX_IDENT_LEN, ident))
2951 return 0;
2952 src->mFormat = MatchSourceFormat(ident);
2953 if(src->mFormat == SF_NONE)
2955 TrErrorAt(tr, line, col, "Expected a source format.\n");
2956 return 0;
2958 if(!TrReadOperator(tr, "("))
2959 return 0;
2960 if(src->mFormat == SF_WAVE)
2962 if(!TrReadInt(tr, 0, MAX_WAVE_CHANNELS, &intVal))
2963 return 0;
2964 src->mType = ET_NONE;
2965 src->mSize = 0;
2966 src->mBits = 0;
2967 src->mChannel = (uint)intVal;
2968 src->mSkip = 0;
2970 else
2972 TrIndication(tr, &line, &col);
2973 if(!TrReadIdent(tr, MAX_IDENT_LEN, ident))
2974 return 0;
2975 src->mType = MatchElementType(ident);
2976 if(src->mType == ET_NONE)
2978 TrErrorAt(tr, line, col, "Expected a source element type.\n");
2979 return 0;
2981 if(src->mFormat == SF_BIN_LE || src->mFormat == SF_BIN_BE)
2983 if(!TrReadOperator(tr, ","))
2984 return 0;
2985 if(src->mType == ET_INT)
2987 if(!TrReadInt(tr, MIN_BIN_SIZE, MAX_BIN_SIZE, &intVal))
2988 return 0;
2989 src->mSize = (uint)intVal;
2990 if(!TrIsOperator(tr, ","))
2991 src->mBits = (int)(8*src->mSize);
2992 else
2994 TrReadOperator(tr, ",");
2995 TrIndication(tr, &line, &col);
2996 if(!TrReadInt(tr, -2147483647-1, 2147483647, &intVal))
2997 return 0;
2998 if(abs(intVal) < MIN_BIN_BITS || (uint)abs(intVal) > (8*src->mSize))
3000 TrErrorAt(tr, line, col, "Expected a value of (+/-) %d to %d.\n", MIN_BIN_BITS, 8*src->mSize);
3001 return 0;
3003 src->mBits = intVal;
3006 else
3008 TrIndication(tr, &line, &col);
3009 if(!TrReadInt(tr, -2147483647-1, 2147483647, &intVal))
3010 return 0;
3011 if(intVal != 4 && intVal != 8)
3013 TrErrorAt(tr, line, col, "Expected a value of 4 or 8.\n");
3014 return 0;
3016 src->mSize = (uint)intVal;
3017 src->mBits = 0;
3020 else if(src->mFormat == SF_ASCII && src->mType == ET_INT)
3022 if(!TrReadOperator(tr, ","))
3023 return 0;
3024 if(!TrReadInt(tr, MIN_ASCII_BITS, MAX_ASCII_BITS, &intVal))
3025 return 0;
3026 src->mSize = 0;
3027 src->mBits = intVal;
3029 else
3031 src->mSize = 0;
3032 src->mBits = 0;
3035 if(!TrIsOperator(tr, ";"))
3036 src->mSkip = 0;
3037 else
3039 TrReadOperator(tr, ";");
3040 if(!TrReadInt(tr, 0, 0x7FFFFFFF, &intVal))
3041 return 0;
3042 src->mSkip = (uint)intVal;
3045 if(!TrReadOperator(tr, ")"))
3046 return 0;
3047 if(TrIsOperator(tr, "@"))
3049 TrReadOperator(tr, "@");
3050 if(!TrReadInt(tr, 0, 0x7FFFFFFF, &intVal))
3051 return 0;
3052 src->mOffset = (uint)intVal;
3054 else
3055 src->mOffset = 0;
3056 if(!TrReadOperator(tr, ":"))
3057 return 0;
3058 if(!TrReadString(tr, MAX_PATH_LEN, src->mPath))
3059 return 0;
3060 return 1;
3063 // Match the target ear (index) from a given identifier.
3064 static int MatchTargetEar(const char *ident)
3066 if(strcasecmp(ident, "left") == 0)
3067 return 0;
3068 if(strcasecmp(ident, "right") == 0)
3069 return 1;
3070 return -1;
3073 // Process the list of sources in the data set definition.
3074 static int ProcessSources(const HeadModelT model, TokenReaderT *tr, HrirDataT *hData)
3076 uint channels = (hData->mChannelType == CT_STEREO) ? 2 : 1;
3077 double *hrirs = CreateDoubles(channels * hData->mIrCount * hData->mIrSize);
3078 double *hrir = CreateDoubles(hData->mIrPoints);
3079 uint line, col, fi, ei, ai, ti;
3080 int count;
3082 printf("Loading sources...");
3083 fflush(stdout);
3084 count = 0;
3085 while(TrIsOperator(tr, "["))
3087 double factor[2] = { 1.0, 1.0 };
3089 TrIndication(tr, &line, &col);
3090 TrReadOperator(tr, "[");
3091 if(!ReadIndexTriplet(tr, hData, &fi, &ei, &ai))
3092 goto error;
3093 if(!TrReadOperator(tr, "]"))
3094 goto error;
3095 HrirAzT *azd = &hData->mFds[fi].mEvs[ei].mAzs[ai];
3097 if(azd->mIrs[0] != NULL)
3099 TrErrorAt(tr, line, col, "Redefinition of source.\n");
3100 goto error;
3102 if(!TrReadOperator(tr, "="))
3103 goto error;
3105 for(;;)
3107 SourceRefT src;
3108 uint ti = 0;
3110 if(!ReadSourceRef(tr, &src))
3111 goto error;
3113 // TODO: Would be nice to display 'x of y files', but that would
3114 // require preparing the source refs first to get a total count
3115 // before loading them.
3116 ++count;
3117 printf("\rLoading sources... %d file%s", count, (count==1)?"":"s");
3118 fflush(stdout);
3120 if(!LoadSource(&src, hData->mIrRate, hData->mIrPoints, hrir))
3121 goto error;
3123 if(hData->mChannelType == CT_STEREO)
3125 char ident[MAX_IDENT_LEN+1];
3127 if(!TrReadIdent(tr, MAX_IDENT_LEN, ident))
3128 goto error;
3129 ti = MatchTargetEar(ident);
3130 if((int)ti < 0)
3132 TrErrorAt(tr, line, col, "Expected a target ear.\n");
3133 goto error;
3136 azd->mIrs[ti] = &hrirs[hData->mIrSize * (ti * hData->mIrCount + azd->mIndex)];
3137 if(model == HM_DATASET)
3138 azd->mDelays[ti] = AverageHrirOnset(hData->mIrRate, hData->mIrPoints, hrir, 1.0 / factor[ti], azd->mDelays[ti]);
3139 AverageHrirMagnitude(hData->mIrPoints, hData->mFftSize, hrir, 1.0 / factor[ti], azd->mIrs[ti]);
3140 factor[ti] += 1.0;
3141 if(!TrIsOperator(tr, "+"))
3142 break;
3143 TrReadOperator(tr, "+");
3145 if(hData->mChannelType == CT_STEREO)
3147 if(azd->mIrs[0] == NULL)
3149 TrErrorAt(tr, line, col, "Missing left ear source reference(s).\n");
3150 goto error;
3152 else if(azd->mIrs[1] == NULL)
3154 TrErrorAt(tr, line, col, "Missing right ear source reference(s).\n");
3155 goto error;
3159 printf("\n");
3160 for(fi = 0;fi < hData->mFdCount;fi++)
3162 for(ei = 0;ei < hData->mFds[fi].mEvCount;ei++)
3164 for(ai = 0;ai < hData->mFds[fi].mEvs[ei].mAzCount;ai++)
3166 HrirAzT *azd = &hData->mFds[fi].mEvs[ei].mAzs[ai];
3168 if(azd->mIrs[0] != NULL)
3169 break;
3171 if(ai < hData->mFds[fi].mEvs[ei].mAzCount)
3172 break;
3174 if(ei >= hData->mFds[fi].mEvCount)
3176 TrError(tr, "Missing source references [ %d, *, * ].\n", fi);
3177 goto error;
3179 hData->mFds[fi].mEvStart = ei;
3180 for(;ei < hData->mFds[fi].mEvCount;ei++)
3182 for(ai = 0;ai < hData->mFds[fi].mEvs[ei].mAzCount;ai++)
3184 HrirAzT *azd = &hData->mFds[fi].mEvs[ei].mAzs[ai];
3186 if(azd->mIrs[0] == NULL)
3188 TrError(tr, "Missing source reference [ %d, %d, %d ].\n", fi, ei, ai);
3189 goto error;
3194 for(ti = 0;ti < channels;ti++)
3196 for(fi = 0;fi < hData->mFdCount;fi++)
3198 for(ei = 0;ei < hData->mFds[fi].mEvCount;ei++)
3200 for(ai = 0;ai < hData->mFds[fi].mEvs[ei].mAzCount;ai++)
3202 HrirAzT *azd = &hData->mFds[fi].mEvs[ei].mAzs[ai];
3204 azd->mIrs[ti] = &hrirs[hData->mIrSize * (ti * hData->mIrCount + azd->mIndex)];
3209 if(!TrLoad(tr))
3211 free(hrir);
3212 return 1;
3214 TrError(tr, "Errant data at end of source list.\n");
3216 error:
3217 free(hrir);
3218 return 0;
3221 /* Parse the data set definition and process the source data, storing the
3222 * resulting data set as desired. If the input name is NULL it will read
3223 * from standard input.
3225 static int ProcessDefinition(const char *inName, const uint outRate, const uint fftSize, const int equalize, const int surface, const double limit, const uint truncSize, const HeadModelT model, const double radius, const char *outName)
3227 char rateStr[8+1], expName[MAX_PATH_LEN];
3228 TokenReaderT tr;
3229 HrirDataT hData;
3230 FILE *fp;
3231 int ret;
3233 ResetHrirData(&hData);
3234 fprintf(stdout, "Reading HRIR definition from %s...\n", inName?inName:"stdin");
3235 if(inName != NULL)
3237 fp = fopen(inName, "r");
3238 if(fp == NULL)
3240 fprintf(stderr, "Error: Could not open definition file '%s'\n", inName);
3241 return 0;
3243 TrSetup(fp, inName, &tr);
3245 else
3247 fp = stdin;
3248 TrSetup(fp, "<stdin>", &tr);
3250 if(!ProcessMetrics(&tr, fftSize, truncSize, &hData))
3252 if(inName != NULL)
3253 fclose(fp);
3254 return 0;
3256 if(!ProcessSources(model, &tr, &hData))
3258 FreeHrirData(&hData);
3259 if(inName != NULL)
3260 fclose(fp);
3261 return 0;
3263 if(fp != stdin)
3264 fclose(fp);
3265 if(equalize)
3267 uint c = (hData.mChannelType == CT_STEREO) ? 2 : 1;
3268 uint m = 1 + hData.mFftSize / 2;
3269 double *dfa = CreateDoubles(c * m);
3271 fprintf(stdout, "Calculating diffuse-field average...\n");
3272 CalculateDiffuseFieldAverage(&hData, c, m, surface, limit, dfa);
3273 fprintf(stdout, "Performing diffuse-field equalization...\n");
3274 DiffuseFieldEqualize(c, m, dfa, &hData);
3275 free(dfa);
3277 fprintf(stdout, "Performing minimum phase reconstruction...\n");
3278 ReconstructHrirs(&hData);
3279 if(outRate != 0 && outRate != hData.mIrRate)
3281 fprintf(stdout, "Resampling HRIRs...\n");
3282 ResampleHrirs(outRate, &hData);
3284 fprintf(stdout, "Truncating minimum-phase HRIRs...\n");
3285 hData.mIrPoints = truncSize;
3286 fprintf(stdout, "Synthesizing missing elevations...\n");
3287 if(model == HM_DATASET)
3288 SynthesizeOnsets(&hData);
3289 SynthesizeHrirs(&hData);
3290 fprintf(stdout, "Normalizing final HRIRs...\n");
3291 NormalizeHrirs(&hData);
3292 fprintf(stdout, "Calculating impulse delays...\n");
3293 CalculateHrtds(model, (radius > DEFAULT_CUSTOM_RADIUS) ? radius : hData.mRadius, &hData);
3294 snprintf(rateStr, 8, "%u", hData.mIrRate);
3295 StrSubst(outName, "%r", rateStr, MAX_PATH_LEN, expName);
3296 fprintf(stdout, "Creating MHR data set %s...\n", expName);
3297 ret = StoreMhr(&hData, expName);
3299 FreeHrirData(&hData);
3300 return ret;
3303 static void PrintHelp(const char *argv0, FILE *ofile)
3305 fprintf(ofile, "Usage: %s [<option>...]\n\n", argv0);
3306 fprintf(ofile, "Options:\n");
3307 fprintf(ofile, " -m Ignored for compatibility.\n");
3308 fprintf(ofile, " -r <rate> Change the data set sample rate to the specified value and\n");
3309 fprintf(ofile, " resample the HRIRs accordingly.\n");
3310 fprintf(ofile, " -f <points> Override the FFT window size (default: %u).\n", DEFAULT_FFTSIZE);
3311 fprintf(ofile, " -e {on|off} Toggle diffuse-field equalization (default: %s).\n", (DEFAULT_EQUALIZE ? "on" : "off"));
3312 fprintf(ofile, " -s {on|off} Toggle surface-weighted diffuse-field average (default: %s).\n", (DEFAULT_SURFACE ? "on" : "off"));
3313 fprintf(ofile, " -l {<dB>|none} Specify a limit to the magnitude range of the diffuse-field\n");
3314 fprintf(ofile, " average (default: %.2f).\n", DEFAULT_LIMIT);
3315 fprintf(ofile, " -w <points> Specify the size of the truncation window that's applied\n");
3316 fprintf(ofile, " after minimum-phase reconstruction (default: %u).\n", DEFAULT_TRUNCSIZE);
3317 fprintf(ofile, " -d {dataset| Specify the model used for calculating the head-delay timing\n");
3318 fprintf(ofile, " sphere} values (default: %s).\n", ((DEFAULT_HEAD_MODEL == HM_DATASET) ? "dataset" : "sphere"));
3319 fprintf(ofile, " -c <size> Use a customized head radius measured ear-to-ear in meters.\n");
3320 fprintf(ofile, " -i <filename> Specify an HRIR definition file to use (defaults to stdin).\n");
3321 fprintf(ofile, " -o <filename> Specify an output file. Use of '%%r' will be substituted with\n");
3322 fprintf(ofile, " the data set sample rate.\n");
3325 // Standard command line dispatch.
3326 int main(int argc, char *argv[])
3328 const char *inName = NULL, *outName = NULL;
3329 uint outRate, fftSize;
3330 int equalize, surface;
3331 char *end = NULL;
3332 HeadModelT model;
3333 uint truncSize;
3334 double radius;
3335 double limit;
3336 int opt;
3338 GET_UNICODE_ARGS(&argc, &argv);
3340 if(argc < 2)
3342 fprintf(stdout, "HRTF Processing and Composition Utility\n\n");
3343 PrintHelp(argv[0], stdout);
3344 exit(EXIT_SUCCESS);
3347 outName = "./oalsoft_hrtf_%r.mhr";
3348 outRate = 0;
3349 fftSize = 0;
3350 equalize = DEFAULT_EQUALIZE;
3351 surface = DEFAULT_SURFACE;
3352 limit = DEFAULT_LIMIT;
3353 truncSize = DEFAULT_TRUNCSIZE;
3354 model = DEFAULT_HEAD_MODEL;
3355 radius = DEFAULT_CUSTOM_RADIUS;
3357 while((opt=getopt(argc, argv, "mr:f:e:s:l:w:d:c:e:i:o:h")) != -1)
3359 switch(opt)
3361 case 'm':
3362 fprintf(stderr, "Ignoring unused command '-m'.\n");
3363 break;
3365 case 'r':
3366 outRate = strtoul(optarg, &end, 10);
3367 if(end[0] != '\0' || outRate < MIN_RATE || outRate > MAX_RATE)
3369 fprintf(stderr, "Error: Got unexpected value \"%s\" for option -%c, expected between %u to %u.\n", optarg, opt, MIN_RATE, MAX_RATE);
3370 exit(EXIT_FAILURE);
3372 break;
3374 case 'f':
3375 fftSize = strtoul(optarg, &end, 10);
3376 if(end[0] != '\0' || (fftSize&(fftSize-1)) || fftSize < MIN_FFTSIZE || fftSize > MAX_FFTSIZE)
3378 fprintf(stderr, "Error: Got unexpected value \"%s\" for option -%c, expected a power-of-two between %u to %u.\n", optarg, opt, MIN_FFTSIZE, MAX_FFTSIZE);
3379 exit(EXIT_FAILURE);
3381 break;
3383 case 'e':
3384 if(strcmp(optarg, "on") == 0)
3385 equalize = 1;
3386 else if(strcmp(optarg, "off") == 0)
3387 equalize = 0;
3388 else
3390 fprintf(stderr, "Error: Got unexpected value \"%s\" for option -%c, expected on or off.\n", optarg, opt);
3391 exit(EXIT_FAILURE);
3393 break;
3395 case 's':
3396 if(strcmp(optarg, "on") == 0)
3397 surface = 1;
3398 else if(strcmp(optarg, "off") == 0)
3399 surface = 0;
3400 else
3402 fprintf(stderr, "Error: Got unexpected value \"%s\" for option -%c, expected on or off.\n", optarg, opt);
3403 exit(EXIT_FAILURE);
3405 break;
3407 case 'l':
3408 if(strcmp(optarg, "none") == 0)
3409 limit = 0.0;
3410 else
3412 limit = strtod(optarg, &end);
3413 if(end[0] != '\0' || limit < MIN_LIMIT || limit > MAX_LIMIT)
3415 fprintf(stderr, "Error: Got unexpected value \"%s\" for option -%c, expected between %.0f to %.0f.\n", optarg, opt, MIN_LIMIT, MAX_LIMIT);
3416 exit(EXIT_FAILURE);
3419 break;
3421 case 'w':
3422 truncSize = strtoul(optarg, &end, 10);
3423 if(end[0] != '\0' || truncSize < MIN_TRUNCSIZE || truncSize > MAX_TRUNCSIZE || (truncSize%MOD_TRUNCSIZE))
3425 fprintf(stderr, "Error: Got unexpected value \"%s\" for option -%c, expected multiple of %u between %u to %u.\n", optarg, opt, MOD_TRUNCSIZE, MIN_TRUNCSIZE, MAX_TRUNCSIZE);
3426 exit(EXIT_FAILURE);
3428 break;
3430 case 'd':
3431 if(strcmp(optarg, "dataset") == 0)
3432 model = HM_DATASET;
3433 else if(strcmp(optarg, "sphere") == 0)
3434 model = HM_SPHERE;
3435 else
3437 fprintf(stderr, "Error: Got unexpected value \"%s\" for option -%c, expected dataset or sphere.\n", optarg, opt);
3438 exit(EXIT_FAILURE);
3440 break;
3442 case 'c':
3443 radius = strtod(optarg, &end);
3444 if(end[0] != '\0' || radius < MIN_CUSTOM_RADIUS || radius > MAX_CUSTOM_RADIUS)
3446 fprintf(stderr, "Error: Got unexpected value \"%s\" for option -%c, expected between %.2f to %.2f.\n", optarg, opt, MIN_CUSTOM_RADIUS, MAX_CUSTOM_RADIUS);
3447 exit(EXIT_FAILURE);
3449 break;
3451 case 'i':
3452 inName = optarg;
3453 break;
3455 case 'o':
3456 outName = optarg;
3457 break;
3459 case 'h':
3460 PrintHelp(argv[0], stdout);
3461 exit(EXIT_SUCCESS);
3463 default: /* '?' */
3464 PrintHelp(argv[0], stderr);
3465 exit(EXIT_FAILURE);
3469 if(!ProcessDefinition(inName, outRate, fftSize, equalize, surface, limit,
3470 truncSize, model, radius, outName))
3471 return -1;
3472 fprintf(stdout, "Operation completed.\n");
3474 return EXIT_SUCCESS;