28 unsigned mWindowSize = 2 * framesPerBlock;
29 mNumFFTBlocks = hrirLength / framesPerBlock;
30 unsigned hrtfLength = framesPerBlock + 1;
33 for (
unsigned i = 0; i < mNumFFTBlocks; i++) {
37 for (
unsigned i = 0; i < mNumFFTBlocks; i++) {
48 unsigned mWindowSize = 2 * framesPerBlock;
49 mNumFFTBlocks = hrirLength / framesPerBlock;
50 unsigned hrtfLength = framesPerBlock + 1;
54 for (
unsigned i = 0; i < mNumFFTBlocks; i++) {
58 for (
unsigned i = 0; i < mNumFFTBlocks; i++) {
62 Buffer mInBuf(1, mWindowSize), mOutBuf(1, mWindowSize);
63 mInBuf.allocateBuffers();
65 char *cursor = strstr(filename,
"_T");
67 logMsg(
kLogError,
"Cannot parse HRIR file name for theta: \"%s\"\n", filename);
68 throw IOError(
"Cannot parse HRIR filename");
70 theta = strtof((cursor + 2), (
char **)NULL);
72 cursor = strstr(filename,
"_P");
74 logMsg(
kLogError,
"Cannot parse HRIR file namefor phi: \"%s\"\n", filename);
75 throw IOError(
"Cannot parse HRIR filename");
77 phi = strtof((cursor + 2), (
char **)NULL);
81 SoundFile hrirFile(filename);
82 hrirFile.openForRead();
84 if ( ! hrirFile.isValid()) {
85 logMsg(
"Cannot open sound file \"%s\"", filename);
86 throw IOError(
"Sound file open error");
88 if ((hrirFile.channels() != 2) || (hrirFile.duration() <
HRIR_SIZE)) {
89 logMsg(
"Sound file \"%s\" is wrong format: %d ch, %d frames",
90 filename, hrirFile.channels(), hrirFile.duration());
91 throw IOError(
"Sound file open error");
94 for (
unsigned i = 0; i < mNumFFTBlocks; i++) {
97 (hrirFile.mWavetable.monoBuffer(0)) + (i * framesPerBlock),
98 (framesPerBlock *
sizeof(
sample)));
101 fft.nextBuffer(mInBuf, mOutBuf);
103 mInBuf.zeroBuffers();
105 (hrirFile.mWavetable.monoBuffer(1)) + (i * framesPerBlock),
106 (framesPerBlock *
sizeof(
sample)));
109 fft.nextBuffer(mInBuf, mOutBuf);
117 for (
unsigned i = 0; i < mNumFFTBlocks; i++) {
128 logMsg(
"HRTF with Azimuth: %f \t Elevation: %f",
153 if (mDatabase == NULL) {
201 if (strcasestr(folder,
".dat") != NULL) {
209 }
else if (folder[strlen(folder) - 1] ==
'/') {
212 if (folder[0] ==
'~') {
213 strcpy(realFolder, getenv(
"HOME"));
214 if (strlen(folder) > 1)
215 strcat(realFolder, &folder[1]);
217 strcpy(realFolder, folder);
220 this->loadFromFolder(realFolder);
230 loadFromFile(folder);
245 strcpy(filename, folder);
247 this->loadFromFile(filename);
259 listFile = fopen(listname,
"r");
261 logMsg(
"Cannot open list file %s for reading.\n", listname);
264 strcpy(dirname, listname);
265 char * ext = rindex(dirname,
'/');
268 while ( ! feof(listFile)) {
269 fgets(lineBuffer, 1024, listFile);
271 int len = strlen(lineBuffer) - 1;
274 if (lineBuffer[len] ==
'\n')
275 lineBuffer[len] =
'\0';
276 strcpy(filename, dirname);
277 strcat(filename, lineBuffer);
280 HRTF * tHRTF =
new HRTF(filename, fft);
281 mHRTFVector.push_back(tHRTF);
292 #ifdef IPHONE // byte-swap floats
293 void swapBuffer(
float * buf,
unsigned numFloats) {
295 for (
unsigned b = 0; b < numFloats; b++) {
296 unsigned int val = (
unsigned int) *ptr;
297 val = CFSwapInt32LittleToHost(val);
298 *ptr++ = (float) val;
309 int nHRTFs, nBlocks, hrirLen, fftLen;
314 dbFile = fopen(dbName,
"r");
316 logMsg(
"Cannot open DB file %s for reading.\n", dbName);
319 fgets(lineBuffer, 1024, dbFile);
320 if (strncmp(lineBuffer,
"HRTF", 4) != 0) {
321 logMsg(
"DB file %s wrong format.\n", dbName);
325 int numFound = sscanf(lineBuffer,
"%s\t%s\t%d\t%d\t%d\t%d",
326 tok, nam, & nHRTFs, & hrirLen, & fftLen, & nBlocks);
328 logMsg(
"Line format: %s\n", lineBuffer);
332 mWindowSize = 2 * fftLen;
333 mHRTFLength = fftLen;
334 mHRIRLength = hrirLen;
335 if (nBlocks != numBlocks()) {
336 logMsg(
"Line format: %s\n", lineBuffer);
341 while(strlen(lineBuffer) > 1) {
343 if(sscanf(lineBuffer,
"%d\t%d", & azim, & elev) != 2) {
344 logMsg(
"Line format: %s\n", lineBuffer);
350 hrtfPoints.push_back(cpt);
355 if (hrtfPoints.size() != nHRTFs) {
356 logMsg(
"Found %d positions ( ! = %d)", hrtfPoints.size(), nHRTFs);
361 int hdrSize = (int) ftell(dbFile);
362 for (
unsigned i = 0; i < nHRTFs; i++) {
365 hrtfPoints[i]->theta(),
366 hrtfPoints[i]->ele());
367 for (
unsigned b = 0; b < nBlocks; b++) {
370 if (nRead != mHRTFLength) {
372 perror(
"Error reading HRTF ");
373 throw IOError(
"Error reading HRTF");
376 if (nRead != mHRTFLength) {
378 perror(
"Error reading HRTF ");
379 throw IOError(
"Error reading HRTF");
382 mHRTFVector.push_back(hrtf);
384 int dataSize = (int) ftell(dbFile) - hdrSize;
385 fgets(lineBuffer, 1024, dbFile);
386 if (strncmp(lineBuffer,
"HRTF", 4) != 0)
387 logMsg(
"Error: Trailer format: %s\n", lineBuffer);
407 logMsg(
"HRTF: %d files = %4.1f MB (w: %d i: %d f: %d b: %d)",
408 numHRTFs(), ((
float) size() / 1000000.0f),
409 windowSize(), hrirLength(), hrtfLength(), numBlocks());
411 for (
unsigned i = 0; i < numHRTFs(); i++)
412 mHRTFVector[i]->dump();
419 for (
unsigned i = 0; i < this->numHRTFs(); i++)
420 siz += mHRTFVector[i]->size();
427 unsigned numHRTFs = this->numHRTFs();
432 return mHRTFVector[index];
439 double lowVal = 10000;
440 unsigned localHRTF = 0;
441 unsigned numHRTFs = this->numHRTFs();
448 for (
unsigned j = 0; j < numHRTFs - 1; j++) {
449 tmpVal = sPt.
distance2(mHRTFVector[j]->mPosition);
450 if (tmpVal < lowVal) {
461 return mHRTFVector.size();
485 return mHRIRLength / (windowSize() / 2);
499 unsigned numBlocks = hrirLength() / (windowSize() / 2);
500 unsigned num2Store = numHRTFs();
502 FILE * store = fopen(filename,
"w");
504 printf(
"Error saving HRTF DB - cannot open file\n");
507 fprintf(store,
"HRTF %s\t%d\t%d\t%d\t%d\n",
508 sname, num2Store, hrirLength(), hrtfLength(), numBlocks);
510 for (
unsigned j = 0; j < num2Store; j++) {
513 fprintf(store,
"%g\t%g\n",
517 fprintf(store,
"\n");
518 int hdrSize = (int) ftell(store);
520 for (
unsigned i = 0; i < num2Store; i++) {
523 HRTF * hrtf = mHRTFVector[i];
524 for (
unsigned b = 0; b < numBlocks; b++) {
532 int dataSize = (int) ftell(store) - hdrSize;
533 fprintf(store,
"HRTF\n");
535 printf(
"Saved HRTF DB %s with %d HRTFs %d blocks of %d-len arrays;\n\thdr = %d B, data = %5.2f MB\n\n",
536 sname, num2Store, numBlocks, hrtfLength(), hdrSize, ((
float) dataSize / 1000000.0f));
547 listFile = fopen(listname,
"r");
549 logMsg(
"Cannot open list file %s for reading.\n", listname);
552 while ( ! feof(listFile)) {
553 fgets(lineBuffer, 1024, listFile);
554 int len = strlen(lineBuffer) - 1;
555 if (lineBuffer[len] ==
'\n')
556 lineBuffer[len] =
'\0';
557 logMsg(
"Read HRTF folder %s", lineBuffer);
558 sprintf(filename,
"%sIRCAM_HRTF/%s/",
562 logMsg(
"Loaded %d HRTF files = %4.1f MB",
566 char * cpos = strstr(filename,
"IRC_");
568 int index = atoi(cpos + 4);
569 sprintf(filename,
"%sIRCAM_HRTF/HRTF_%d.dat",
571 sprintf(tok,
"%d", index);