Detect SOFA files for makemhr input

This commit is contained in:
Chris Robinson 2019-03-25 13:01:44 -07:00
parent a3687db015
commit cb02bb00be
3 changed files with 38 additions and 12 deletions

View File

@ -137,7 +137,7 @@ struct SourceRefT {
// Setup the reader on the given file. The filename can be NULL if no error
// output is desired.
static void TrSetup(FILE *fp, const char *filename, TokenReaderT *tr)
static void TrSetup(FILE *fp, const char *startbytes, size_t startbytecount, const char *filename, TokenReaderT *tr)
{
const char *name = nullptr;
@ -164,6 +164,12 @@ static void TrSetup(FILE *fp, const char *filename, TokenReaderT *tr)
tr->mColumn = 1;
tr->mIn = 0;
tr->mOut = 0;
if(startbytecount > 0)
{
memcpy(tr->mRing, startbytes, startbytecount);
tr->mIn += startbytecount;
}
}
// Prime the reader's ring buffer, and return a result indicating that there
@ -966,7 +972,7 @@ static int LoadAsciiSource(FILE *fp, const SourceRefT *src, const uint n, double
uint i, j;
double dummy;
TrSetup(fp, nullptr, &tr);
TrSetup(fp, nullptr, 0, nullptr, &tr);
for(i = 0;i < src->mOffset;i++)
{
if(!ReadAsciiAsDouble(&tr, src->mPath, src->mType, static_cast<uint>(src->mBits), &dummy))
@ -1995,11 +2001,12 @@ static int ProcessSources(TokenReaderT *tr, HrirDataT *hData)
}
bool LoadDefInput(FILE *fp, const char *filename, const uint fftSize, const uint truncSize, const ChannelModeT chanMode, HrirDataT *hData)
bool LoadDefInput(FILE *fp, const char *startbytes, size_t startbytecount, const char *filename,
const uint fftSize, const uint truncSize, const ChannelModeT chanMode, HrirDataT *hData)
{
TokenReaderT tr;
TrSetup(fp, filename, &tr);
TrSetup(fp, startbytes, startbytecount, filename, &tr);
if(!ProcessMetrics(&tr, fftSize, truncSize, chanMode, hData))
{
if(fp != stdin)

View File

@ -6,7 +6,7 @@
#include "makemhr.h"
bool LoadDefInput(FILE *fp, const char *filename, const uint fftSize, const uint truncSize,
const ChannelModeT chanMode, HrirDataT *hData);
bool LoadDefInput(FILE *fp, const char *startbytes, size_t startbytecount, const char *filename,
const uint fftSize, const uint truncSize, const ChannelModeT chanMode, HrirDataT *hData);
#endif /* LOADDEF_H */

View File

@ -1534,6 +1534,8 @@ int PrepareHrirData(const uint fdCount, const double distances[MAX_FD_COUNT], co
static int ProcessDefinition(const char *inName, const uint outRate, const ChannelModeT chanMode, const uint fftSize, const int equalize, const int surface, const double limit, const uint truncSize, const HeadModelT model, const double radius, const char *outName)
{
char rateStr[8+1], expName[MAX_PATH_LEN];
char startbytes[4]{};
size_t startbytecount{0u};
HrirDataT hData;
FILE *fp;
int ret;
@ -1551,16 +1553,33 @@ static int ProcessDefinition(const char *inName, const uint outRate, const Chann
fprintf(stderr, "Error: Could not open input file '%s'\n", inName);
return 0;
}
startbytecount = fread(startbytes, 1, sizeof(startbytes), fp);
if(startbytecount != sizeof(startbytes))
{
fclose(fp);
fprintf(stderr, "Error: Could not read input file '%s'\n", inName);
return 0;
}
if(startbytes[0] == '\x89' && startbytes[1] == 'H' && startbytes[2] == 'D' &&
startbytes[3] == 'F')
{
fclose(fp);
fprintf(stderr, "Error: Direct SOFA input not yet supported\n");
return 0;
}
}
fprintf(stdout, "Reading HRIR definition from %s...\n", inName);
if(!LoadDefInput(fp, inName, fftSize, truncSize, chanMode, &hData))
if(fp != nullptr)
{
fprintf(stdout, "Reading HRIR definition from %s...\n", inName);
const bool success{LoadDefInput(fp, startbytes, startbytecount, inName, fftSize, truncSize,
chanMode, &hData)};
if(fp != stdin)
fclose(fp);
return 0;
if(!success)
return 0;
}
if(fp != stdin)
fclose(fp);
if(equalize)
{
@ -1660,7 +1679,7 @@ int main(int argc, char *argv[])
model = DEFAULT_HEAD_MODEL;
radius = DEFAULT_CUSTOM_RADIUS;
while((opt=getopt(argc, argv, "r:m:f:e:s:l:w:d:c:e:i:o:h")) != -1)
while((opt=getopt(argc, argv, "r:mf:e:s:l:w:d:c:e:i:o:h")) != -1)
{
switch(opt)
{