2011-05-01 13:59:44 -07:00
|
|
|
/**
|
|
|
|
* OpenAL cross platform audio library
|
|
|
|
* Copyright (C) 2011 by Chris Robinson
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2014-08-18 14:11:03 +02:00
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2011-05-01 13:59:44 -07:00
|
|
|
* Or go to http://www.gnu.org/copyleft/lgpl.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2019-07-28 11:28:36 -07:00
|
|
|
#include "hrtf.h"
|
2011-09-18 10:09:32 -07:00
|
|
|
|
2019-07-28 11:28:36 -07:00
|
|
|
#include <algorithm>
|
2018-11-09 18:08:42 -08:00
|
|
|
#include <array>
|
2019-07-28 11:28:36 -07:00
|
|
|
#include <cassert>
|
|
|
|
#include <cctype>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <functional>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iterator>
|
2018-11-09 23:47:42 -08:00
|
|
|
#include <memory>
|
2019-07-28 11:28:36 -07:00
|
|
|
#include <mutex>
|
|
|
|
#include <new>
|
2019-01-22 10:27:04 -08:00
|
|
|
#include <numeric>
|
2019-09-22 12:23:41 -07:00
|
|
|
#include <type_traits>
|
2019-07-28 11:28:36 -07:00
|
|
|
#include <utility>
|
2018-11-09 18:08:42 -08:00
|
|
|
|
2011-05-01 13:59:44 -07:00
|
|
|
#include "AL/al.h"
|
2019-07-28 11:28:36 -07:00
|
|
|
|
2019-07-28 18:33:29 -07:00
|
|
|
#include "alcmain.h"
|
2018-01-11 07:56:54 -08:00
|
|
|
#include "alconfig.h"
|
2019-09-22 12:23:41 -07:00
|
|
|
#include "alfstream.h"
|
2016-07-07 10:31:43 -07:00
|
|
|
#include "almalloc.h"
|
2019-07-28 11:28:36 -07:00
|
|
|
#include "alnumeric.h"
|
|
|
|
#include "aloptional.h"
|
2019-05-23 08:17:05 -07:00
|
|
|
#include "alspan.h"
|
2019-07-28 11:28:36 -07:00
|
|
|
#include "filters/splitter.h"
|
|
|
|
#include "logging.h"
|
|
|
|
#include "math_defs.h"
|
|
|
|
#include "opthelpers.h"
|
2019-11-28 11:48:44 -08:00
|
|
|
#include "polyphase_resampler.h"
|
2015-10-06 04:01:53 -07:00
|
|
|
|
2012-09-12 03:45:26 -07:00
|
|
|
|
2018-11-09 18:08:42 -08:00
|
|
|
namespace {
|
|
|
|
|
2019-01-29 14:14:52 -08:00
|
|
|
using namespace std::placeholders;
|
|
|
|
|
2019-11-28 08:24:29 -08:00
|
|
|
struct HrtfEntry {
|
|
|
|
std::string mDispName;
|
|
|
|
std::string mFilename;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct LoadedHrtf {
|
|
|
|
std::string mFilename;
|
|
|
|
std::unique_ptr<HrtfStore> mEntry;
|
|
|
|
};
|
2018-12-09 03:04:18 -08:00
|
|
|
|
2019-02-16 22:57:38 -08:00
|
|
|
/* Data set limits must be the same as or more flexible than those defined in
|
2019-03-24 17:35:32 -07:00
|
|
|
* the makemhr utility.
|
2019-02-16 22:57:38 -08:00
|
|
|
*/
|
2012-09-11 01:59:42 -07:00
|
|
|
#define MIN_IR_SIZE (8)
|
2019-02-16 22:57:38 -08:00
|
|
|
#define MOD_IR_SIZE (2)
|
2011-07-16 00:22:01 -07:00
|
|
|
|
2017-10-22 15:36:42 -07:00
|
|
|
#define MIN_FD_COUNT (1)
|
|
|
|
#define MAX_FD_COUNT (16)
|
|
|
|
|
2019-10-27 15:50:59 -07:00
|
|
|
#define MIN_FD_DISTANCE (50)
|
|
|
|
#define MAX_FD_DISTANCE (2500)
|
2017-10-22 15:36:42 -07:00
|
|
|
|
2012-09-11 01:59:42 -07:00
|
|
|
#define MIN_EV_COUNT (5)
|
2019-10-21 11:30:39 -07:00
|
|
|
#define MAX_EV_COUNT (181)
|
2011-07-16 00:22:01 -07:00
|
|
|
|
2012-09-11 01:59:42 -07:00
|
|
|
#define MIN_AZ_COUNT (1)
|
2019-10-22 15:23:17 -07:00
|
|
|
#define MAX_AZ_COUNT (255)
|
2011-05-01 13:59:44 -07:00
|
|
|
|
2017-10-22 15:36:42 -07:00
|
|
|
#define MAX_HRIR_DELAY (HRTF_HISTORY_LENGTH-1)
|
|
|
|
|
2019-11-29 13:45:45 -08:00
|
|
|
#define HRIR_DELAY_FRACBITS 2
|
|
|
|
#define HRIR_DELAY_FRACONE (1<<HRIR_DELAY_FRACBITS)
|
2019-12-08 16:46:11 -08:00
|
|
|
#define HRIR_DELAY_FRACHALF (HRIR_DELAY_FRACONE>>1)
|
2019-11-29 13:45:45 -08:00
|
|
|
|
|
|
|
static_assert(MAX_HRIR_DELAY*HRIR_DELAY_FRACONE < 256, "MAX_HRIR_DELAY or DELAY_FRAC too large");
|
|
|
|
|
2018-11-09 18:08:42 -08:00
|
|
|
constexpr ALchar magicMarker00[8]{'M','i','n','P','H','R','0','0'};
|
|
|
|
constexpr ALchar magicMarker01[8]{'M','i','n','P','H','R','0','1'};
|
|
|
|
constexpr ALchar magicMarker02[8]{'M','i','n','P','H','R','0','2'};
|
2012-09-11 01:59:42 -07:00
|
|
|
|
2014-11-23 10:49:54 -08:00
|
|
|
/* First value for pass-through coefficients (remaining are 0), used for omni-
|
|
|
|
* directional sounds. */
|
2018-11-09 23:47:42 -08:00
|
|
|
constexpr ALfloat PassthruCoeff{0.707106781187f/*sqrt(0.5)*/};
|
2014-11-23 10:49:54 -08:00
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
std::mutex LoadedHrtfLock;
|
2019-11-28 08:24:29 -08:00
|
|
|
al::vector<LoadedHrtf> LoadedHrtfs;
|
|
|
|
|
|
|
|
std::mutex EnumeratedHrtfLock;
|
|
|
|
al::vector<HrtfEntry> EnumeratedHrtfs;
|
2018-11-09 23:47:42 -08:00
|
|
|
|
|
|
|
|
|
|
|
class databuf final : public std::streambuf {
|
|
|
|
int_type underflow() override
|
|
|
|
{ return traits_type::eof(); }
|
|
|
|
|
|
|
|
pos_type seekoff(off_type offset, std::ios_base::seekdir whence, std::ios_base::openmode mode) override
|
|
|
|
{
|
|
|
|
if((mode&std::ios_base::out) || !(mode&std::ios_base::in))
|
|
|
|
return traits_type::eof();
|
|
|
|
|
|
|
|
char_type *cur;
|
|
|
|
switch(whence)
|
|
|
|
{
|
|
|
|
case std::ios_base::beg:
|
|
|
|
if(offset < 0 || offset > egptr()-eback())
|
|
|
|
return traits_type::eof();
|
|
|
|
cur = eback() + offset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case std::ios_base::cur:
|
|
|
|
if((offset >= 0 && offset > egptr()-gptr()) ||
|
|
|
|
(offset < 0 && -offset > gptr()-eback()))
|
|
|
|
return traits_type::eof();
|
|
|
|
cur = gptr() + offset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case std::ios_base::end:
|
|
|
|
if(offset > 0 || -offset > egptr()-eback())
|
|
|
|
return traits_type::eof();
|
|
|
|
cur = egptr() + offset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return traits_type::eof();
|
|
|
|
}
|
|
|
|
|
|
|
|
setg(eback(), cur, egptr());
|
|
|
|
return cur - eback();
|
|
|
|
}
|
|
|
|
|
|
|
|
pos_type seekpos(pos_type pos, std::ios_base::openmode mode) override
|
|
|
|
{
|
|
|
|
// Simplified version of seekoff
|
|
|
|
if((mode&std::ios_base::out) || !(mode&std::ios_base::in))
|
|
|
|
return traits_type::eof();
|
|
|
|
|
|
|
|
if(pos < 0 || pos > egptr()-eback())
|
|
|
|
return traits_type::eof();
|
|
|
|
|
2018-12-12 21:58:41 -08:00
|
|
|
setg(eback(), eback() + static_cast<size_t>(pos), egptr());
|
2018-11-09 23:47:42 -08:00
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2019-09-18 17:02:33 -07:00
|
|
|
databuf(const char_type *start_, const char_type *end_) noexcept
|
2018-11-09 23:47:42 -08:00
|
|
|
{
|
2019-09-18 17:02:33 -07:00
|
|
|
setg(const_cast<char_type*>(start_), const_cast<char_type*>(start_),
|
|
|
|
const_cast<char_type*>(end_));
|
2018-11-09 23:47:42 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class idstream final : public std::istream {
|
|
|
|
databuf mStreamBuf;
|
|
|
|
|
|
|
|
public:
|
2019-09-18 17:02:33 -07:00
|
|
|
idstream(const char *start_, const char *end_)
|
|
|
|
: std::istream{nullptr}, mStreamBuf{start_, end_}
|
2018-11-09 23:47:42 -08:00
|
|
|
{ init(&mStreamBuf); }
|
|
|
|
};
|
2011-09-18 10:09:32 -07:00
|
|
|
|
2016-10-09 00:37:47 -07:00
|
|
|
|
2019-11-29 08:33:46 -08:00
|
|
|
struct IdxBlend { ALuint idx; float blend; };
|
2016-10-09 00:37:47 -07:00
|
|
|
/* Calculate the elevation index given the polar elevation in radians. This
|
2018-05-03 21:43:53 -07:00
|
|
|
* will return an index between 0 and (evcount - 1).
|
2012-09-11 01:59:42 -07:00
|
|
|
*/
|
2019-11-29 08:33:46 -08:00
|
|
|
IdxBlend CalcEvIndex(ALuint evcount, float ev)
|
2011-05-01 13:59:44 -07:00
|
|
|
{
|
2019-09-14 16:55:28 -07:00
|
|
|
ev = (al::MathDefs<float>::Pi()*0.5f + ev) * static_cast<float>(evcount-1) /
|
|
|
|
al::MathDefs<float>::Pi();
|
2019-11-29 08:33:46 -08:00
|
|
|
ALuint idx{float2uint(ev)};
|
2017-05-01 15:46:25 -07:00
|
|
|
|
2019-11-29 08:33:46 -08:00
|
|
|
return IdxBlend{minu(idx, evcount-1), ev-static_cast<float>(idx)};
|
2011-06-03 01:06:00 -07:00
|
|
|
}
|
|
|
|
|
2016-10-09 00:37:47 -07:00
|
|
|
/* Calculate the azimuth index given the polar azimuth in radians. This will
|
2018-05-03 21:43:53 -07:00
|
|
|
* return an index between 0 and (azcount - 1).
|
2012-09-11 01:59:42 -07:00
|
|
|
*/
|
2019-11-29 08:33:46 -08:00
|
|
|
IdxBlend CalcAzIndex(ALuint azcount, float az)
|
2011-06-03 01:06:00 -07:00
|
|
|
{
|
2019-09-14 16:55:28 -07:00
|
|
|
az = (al::MathDefs<float>::Tau()+az) * static_cast<float>(azcount) /
|
|
|
|
al::MathDefs<float>::Tau();
|
2019-11-29 08:33:46 -08:00
|
|
|
ALuint idx{float2uint(az)};
|
2017-05-01 15:46:25 -07:00
|
|
|
|
2019-09-14 16:55:28 -07:00
|
|
|
return IdxBlend{idx%azcount, az-static_cast<float>(idx)};
|
2011-05-01 13:59:44 -07:00
|
|
|
}
|
|
|
|
|
2018-11-09 18:08:42 -08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
2016-10-09 00:37:47 -07:00
|
|
|
/* Calculates static HRIR coefficients and delays for the given polar elevation
|
2017-03-11 18:04:06 -08:00
|
|
|
* and azimuth in radians. The coefficients are normalized.
|
2012-09-11 01:59:42 -07:00
|
|
|
*/
|
2019-11-29 08:33:46 -08:00
|
|
|
void GetHrtfCoeffs(const HrtfStore *Hrtf, float elevation, float azimuth, float distance,
|
|
|
|
float spread, HrirArray &coeffs, ALuint (&delays)[2])
|
2011-05-01 13:59:44 -07:00
|
|
|
{
|
2019-11-29 08:33:46 -08:00
|
|
|
const float dirfact{1.0f - (spread / al::MathDefs<float>::Tau())};
|
2016-04-24 21:42:59 -07:00
|
|
|
|
2019-01-28 22:07:03 -08:00
|
|
|
const auto *field = Hrtf->field;
|
|
|
|
const auto *field_end = field + Hrtf->fdCount-1;
|
2019-11-29 08:33:46 -08:00
|
|
|
size_t ebase{0};
|
2019-02-27 23:13:40 -08:00
|
|
|
while(distance < field->distance && field != field_end)
|
|
|
|
{
|
2019-05-22 10:58:18 -07:00
|
|
|
ebase += field->evCount;
|
2019-02-27 23:13:40 -08:00
|
|
|
++field;
|
|
|
|
}
|
2019-01-28 20:31:58 -08:00
|
|
|
|
2019-03-14 22:26:19 -07:00
|
|
|
/* Claculate the elevation indinces. */
|
2019-03-14 13:17:07 -07:00
|
|
|
const auto elev0 = CalcEvIndex(field->evCount, elevation);
|
2019-11-29 08:33:46 -08:00
|
|
|
const size_t elev1_idx{minu(elev0.idx+1, field->evCount-1)};
|
|
|
|
const size_t ir0offset{Hrtf->elev[ebase + elev0.idx].irOffset};
|
|
|
|
const size_t ir1offset{Hrtf->elev[ebase + elev1_idx].irOffset};
|
2011-06-03 01:06:00 -07:00
|
|
|
|
2019-03-14 22:26:19 -07:00
|
|
|
/* Calculate azimuth indices. */
|
2019-05-22 10:58:18 -07:00
|
|
|
const auto az0 = CalcAzIndex(Hrtf->elev[ebase + elev0.idx].azCount, azimuth);
|
|
|
|
const auto az1 = CalcAzIndex(Hrtf->elev[ebase + elev1_idx].azCount, azimuth);
|
2017-05-01 15:46:25 -07:00
|
|
|
|
2019-01-28 19:38:20 -08:00
|
|
|
/* Calculate the HRIR indices to blend. */
|
2019-11-29 08:33:46 -08:00
|
|
|
const size_t idx[4]{
|
2019-05-22 10:58:18 -07:00
|
|
|
ir0offset + az0.idx,
|
|
|
|
ir0offset + ((az0.idx+1) % Hrtf->elev[ebase + elev0.idx].azCount),
|
|
|
|
ir1offset + az1.idx,
|
|
|
|
ir1offset + ((az1.idx+1) % Hrtf->elev[ebase + elev1_idx].azCount)
|
2019-01-28 19:38:20 -08:00
|
|
|
};
|
|
|
|
|
2017-05-01 15:46:25 -07:00
|
|
|
/* Calculate bilinear blending weights, attenuated according to the
|
|
|
|
* directional panning factor.
|
|
|
|
*/
|
2019-11-29 08:33:46 -08:00
|
|
|
const float blend[4]{
|
2019-03-14 13:17:07 -07:00
|
|
|
(1.0f-elev0.blend) * (1.0f-az0.blend) * dirfact,
|
|
|
|
(1.0f-elev0.blend) * ( az0.blend) * dirfact,
|
|
|
|
( elev0.blend) * (1.0f-az1.blend) * dirfact,
|
|
|
|
( elev0.blend) * ( az1.blend) * dirfact
|
2018-11-09 23:47:42 -08:00
|
|
|
};
|
2017-05-01 15:46:25 -07:00
|
|
|
|
|
|
|
/* Calculate the blended HRIR delays. */
|
2019-11-29 13:45:45 -08:00
|
|
|
float d{Hrtf->delays[idx[0]][0]*blend[0] + Hrtf->delays[idx[1]][0]*blend[1] +
|
|
|
|
Hrtf->delays[idx[2]][0]*blend[2] + Hrtf->delays[idx[3]][0]*blend[3]};
|
|
|
|
delays[0] = fastf2u(d * float{1.0f/HRIR_DELAY_FRACONE});
|
|
|
|
d = Hrtf->delays[idx[0]][1]*blend[0] + Hrtf->delays[idx[1]][1]*blend[1] +
|
|
|
|
Hrtf->delays[idx[2]][1]*blend[2] + Hrtf->delays[idx[3]][1]*blend[1];
|
|
|
|
delays[1] = fastf2u(d * float{1.0f/HRIR_DELAY_FRACONE});
|
2016-10-09 00:37:47 -07:00
|
|
|
|
2019-09-13 09:38:35 -07:00
|
|
|
const ALuint irSize{Hrtf->irSize};
|
2018-12-21 21:07:42 -08:00
|
|
|
ASSUME(irSize >= MIN_IR_SIZE);
|
2017-05-01 15:46:25 -07:00
|
|
|
|
|
|
|
/* Calculate the blended HRIR coefficients. */
|
2019-11-29 08:33:46 -08:00
|
|
|
float *coeffout{al::assume_aligned<16>(&coeffs[0][0])};
|
2018-12-21 21:07:42 -08:00
|
|
|
coeffout[0] = PassthruCoeff * (1.0f-dirfact);
|
|
|
|
coeffout[1] = PassthruCoeff * (1.0f-dirfact);
|
2019-11-21 02:43:34 -08:00
|
|
|
std::fill(coeffout+2, coeffout + HRIR_LENGTH*2, 0.0f);
|
2018-11-09 23:47:42 -08:00
|
|
|
for(ALsizei c{0};c < 4;c++)
|
2017-05-01 15:46:25 -07:00
|
|
|
{
|
2019-11-28 09:46:16 -08:00
|
|
|
const float *srccoeffs{al::assume_aligned<16>(Hrtf->coeffs[idx[c]][0].data())};
|
|
|
|
const float mult{blend[c]};
|
2018-12-21 21:07:42 -08:00
|
|
|
auto blend_coeffs = [mult](const ALfloat src, const ALfloat coeff) noexcept -> ALfloat
|
|
|
|
{ return src*mult + coeff; };
|
2019-05-25 08:17:37 -07:00
|
|
|
std::transform(srccoeffs, srccoeffs + irSize*2, coeffout, coeffout, blend_coeffs);
|
2014-11-23 10:49:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-09 15:59:29 -08:00
|
|
|
|
2019-01-12 01:25:33 -08:00
|
|
|
std::unique_ptr<DirectHrtfState> DirectHrtfState::Create(size_t num_chans)
|
2019-01-08 23:44:08 -08:00
|
|
|
{
|
2019-09-11 03:59:53 -07:00
|
|
|
return std::unique_ptr<DirectHrtfState>{new (FamCount{num_chans}) DirectHrtfState{num_chans}};
|
2019-01-08 23:44:08 -08:00
|
|
|
}
|
|
|
|
|
2019-11-28 06:10:36 -08:00
|
|
|
void BuildBFormatHrtf(const HrtfStore *Hrtf, DirectHrtfState *state,
|
2019-09-25 02:11:37 -07:00
|
|
|
const al::span<const AngularPoint> AmbiPoints, const ALfloat (*AmbiMatrix)[MAX_AMBI_CHANNELS],
|
|
|
|
const ALfloat *AmbiOrderHFGain)
|
Decode directly from B-Format to HRTF instead of a cube
Last time this attempted to average the HRIRs according to their contribution
to a given B-Format channel as if they were loudspeakers, as well as averaging
the HRIR delays. The latter part resulted in the loss of the ITD (inter-aural
time delay), a key component of HRTF.
This time, the HRIRs are averaged similar to above, except instead of averaging
the delays, they're applied to the resulting coefficients (for example, a delay
of 8 would apply the HRIR starting at the 8th sample of the target HRIR). This
does roughly double the IR length, as the largest delay is about 35 samples
while the filter is normally 32 samples. However, this is still smaller the
original data set IR (which was 256 samples), it also only needs to be applied
to 4 channels for first-order ambisonics, rather than the 8-channel cube. So
it's doing twice as much work per sample, but only working on half the number
of samples.
Additionally, since the resulting HRIRs no longer rely on an extra delay line,
a more efficient HRTF mixing function can be made that doesn't use one. Such a
function can also avoid the per-sample stepping parameters the original uses.
2016-08-11 23:20:35 -07:00
|
|
|
{
|
2019-09-24 22:27:12 -07:00
|
|
|
using double2 = std::array<double,2>;
|
|
|
|
struct ImpulseResponse {
|
2019-10-01 22:50:28 -07:00
|
|
|
alignas(16) std::array<double2,HRIR_LENGTH> hrir;
|
2019-09-24 22:27:12 -07:00
|
|
|
ALuint ldelay, rdelay;
|
|
|
|
};
|
|
|
|
|
2019-10-25 01:43:23 -07:00
|
|
|
static const int OrderFromChan[MAX_AMBI_CHANNELS]{
|
2018-12-21 18:17:59 -08:00
|
|
|
0, 1,1,1, 2,2,2,2,2, 3,3,3,3,3,3,3,
|
|
|
|
};
|
2019-02-16 22:57:38 -08:00
|
|
|
/* Set this to true for dual-band HRTF processing. May require better
|
|
|
|
* calculation of the new IR length to deal with the head and tail
|
|
|
|
* generated by the HF scaling.
|
2019-01-06 00:23:15 -08:00
|
|
|
*/
|
|
|
|
static constexpr bool DualBand{true};
|
2018-12-21 18:17:59 -08:00
|
|
|
|
2019-12-08 16:46:11 -08:00
|
|
|
ALuint min_delay{HRTF_HISTORY_LENGTH*HRIR_DELAY_FRACONE};
|
2019-09-13 09:38:35 -07:00
|
|
|
ALuint max_delay{0};
|
2019-09-25 02:11:37 -07:00
|
|
|
al::vector<ImpulseResponse> impres; impres.reserve(AmbiPoints.size());
|
2019-09-24 22:27:12 -07:00
|
|
|
auto calc_res = [Hrtf,&max_delay,&min_delay](const AngularPoint &pt) -> ImpulseResponse
|
Decode directly from B-Format to HRTF instead of a cube
Last time this attempted to average the HRIRs according to their contribution
to a given B-Format channel as if they were loudspeakers, as well as averaging
the HRIR delays. The latter part resulted in the loss of the ITD (inter-aural
time delay), a key component of HRTF.
This time, the HRIRs are averaged similar to above, except instead of averaging
the delays, they're applied to the resulting coefficients (for example, a delay
of 8 would apply the HRIR starting at the 8th sample of the target HRIR). This
does roughly double the IR length, as the largest delay is about 35 samples
while the filter is normally 32 samples. However, this is still smaller the
original data set IR (which was 256 samples), it also only needs to be applied
to 4 channels for first-order ambisonics, rather than the 8-channel cube. So
it's doing twice as much work per sample, but only working on half the number
of samples.
Additionally, since the resulting HRIRs no longer rely on an extra delay line,
a more efficient HRTF mixing function can be made that doesn't use one. Such a
function can also avoid the per-sample stepping parameters the original uses.
2016-08-11 23:20:35 -07:00
|
|
|
{
|
2019-09-24 22:27:12 -07:00
|
|
|
ImpulseResponse res;
|
2016-08-17 05:34:09 -07:00
|
|
|
|
2019-09-24 22:27:12 -07:00
|
|
|
auto &field = Hrtf->field[0];
|
2016-08-17 05:34:09 -07:00
|
|
|
|
2019-09-24 22:27:12 -07:00
|
|
|
/* Calculate the elevation indices. */
|
2019-11-16 14:07:31 -08:00
|
|
|
const auto elev0 = CalcEvIndex(field.evCount, pt.Elev.value);
|
2019-11-29 08:33:46 -08:00
|
|
|
const size_t elev1_idx{minu(elev0.idx+1, field.evCount-1)};
|
|
|
|
const size_t ir0offset{Hrtf->elev[elev0.idx].irOffset};
|
|
|
|
const size_t ir1offset{Hrtf->elev[elev1_idx].irOffset};
|
2019-09-24 22:27:12 -07:00
|
|
|
|
|
|
|
/* Calculate azimuth indices. */
|
2019-11-16 14:07:31 -08:00
|
|
|
const auto az0 = CalcAzIndex(Hrtf->elev[elev0.idx].azCount, pt.Azim.value);
|
|
|
|
const auto az1 = CalcAzIndex(Hrtf->elev[elev1_idx].azCount, pt.Azim.value);
|
2019-09-24 22:27:12 -07:00
|
|
|
|
|
|
|
/* Calculate the HRIR indices to blend. */
|
2019-11-29 08:33:46 -08:00
|
|
|
const size_t idx[4]{
|
|
|
|
ir0offset + az0.idx,
|
|
|
|
ir0offset + ((az0.idx+1) % Hrtf->elev[elev0.idx].azCount),
|
|
|
|
ir1offset + az1.idx,
|
|
|
|
ir1offset + ((az1.idx+1) % Hrtf->elev[elev1_idx].azCount)};
|
2019-09-24 22:27:12 -07:00
|
|
|
|
|
|
|
/* Calculate bilinear blending weights. */
|
2019-12-06 22:31:30 -08:00
|
|
|
const double blend[4]{
|
|
|
|
(1.0-elev0.blend) * (1.0-az0.blend),
|
|
|
|
(1.0-elev0.blend) * ( az0.blend),
|
|
|
|
( elev0.blend) * (1.0-az1.blend),
|
|
|
|
( elev0.blend) * ( az1.blend)};
|
2019-09-24 22:27:12 -07:00
|
|
|
|
2019-12-08 16:46:11 -08:00
|
|
|
/* Calculate the blended HRIR delays (in fixed-point). */
|
2019-12-06 22:31:30 -08:00
|
|
|
double d{Hrtf->delays[idx[0]][0]*blend[0] + Hrtf->delays[idx[1]][0]*blend[1] +
|
2019-11-29 13:45:45 -08:00
|
|
|
Hrtf->delays[idx[2]][0]*blend[2] + Hrtf->delays[idx[3]][0]*blend[3]};
|
2019-12-08 16:46:11 -08:00
|
|
|
res.ldelay = fastf2u(static_cast<float>(d));
|
2019-11-29 13:45:45 -08:00
|
|
|
d = Hrtf->delays[idx[0]][1]*blend[0] + Hrtf->delays[idx[1]][1]*blend[1] +
|
|
|
|
Hrtf->delays[idx[2]][1]*blend[2] + Hrtf->delays[idx[3]][1]*blend[3];
|
2019-12-08 16:46:11 -08:00
|
|
|
res.rdelay = fastf2u(static_cast<float>(d));
|
2019-09-24 22:27:12 -07:00
|
|
|
|
|
|
|
/* Calculate the blended HRIR coefficients. */
|
|
|
|
double *coeffout{al::assume_aligned<16>(&res.hrir[0][0])};
|
2019-11-10 21:56:19 -08:00
|
|
|
std::fill(coeffout, coeffout + HRIR_LENGTH*2, 0.0);
|
2019-09-24 22:27:12 -07:00
|
|
|
for(ALsizei c{0};c < 4;c++)
|
|
|
|
{
|
2019-11-28 09:46:16 -08:00
|
|
|
const float *srccoeffs{al::assume_aligned<16>(Hrtf->coeffs[idx[c]][0].data())};
|
2019-12-06 22:31:30 -08:00
|
|
|
const double mult{blend[c]};
|
2019-09-24 22:27:12 -07:00
|
|
|
auto blend_coeffs = [mult](const float src, const double coeff) noexcept -> double
|
|
|
|
{ return src*mult + coeff; };
|
2019-11-21 23:55:10 -08:00
|
|
|
std::transform(srccoeffs, srccoeffs + HRIR_LENGTH*2, coeffout, coeffout, blend_coeffs);
|
2019-09-24 22:27:12 -07:00
|
|
|
}
|
2018-04-16 18:51:01 -07:00
|
|
|
|
2019-09-24 22:27:12 -07:00
|
|
|
min_delay = minu(min_delay, minu(res.ldelay, res.rdelay));
|
|
|
|
max_delay = maxu(max_delay, maxu(res.ldelay, res.rdelay));
|
Decode directly from B-Format to HRTF instead of a cube
Last time this attempted to average the HRIRs according to their contribution
to a given B-Format channel as if they were loudspeakers, as well as averaging
the HRIR delays. The latter part resulted in the loss of the ITD (inter-aural
time delay), a key component of HRTF.
This time, the HRIRs are averaged similar to above, except instead of averaging
the delays, they're applied to the resulting coefficients (for example, a delay
of 8 would apply the HRIR starting at the 8th sample of the target HRIR). This
does roughly double the IR length, as the largest delay is about 35 samples
while the filter is normally 32 samples. However, this is still smaller the
original data set IR (which was 256 samples), it also only needs to be applied
to 4 channels for first-order ambisonics, rather than the 8-channel cube. So
it's doing twice as much work per sample, but only working on half the number
of samples.
Additionally, since the resulting HRIRs no longer rely on an extra delay line,
a more efficient HRTF mixing function can be made that doesn't use one. Such a
function can also avoid the per-sample stepping parameters the original uses.
2016-08-11 23:20:35 -07:00
|
|
|
|
2019-09-24 22:27:12 -07:00
|
|
|
return res;
|
2018-12-21 18:17:59 -08:00
|
|
|
};
|
2019-09-25 02:11:37 -07:00
|
|
|
std::transform(AmbiPoints.begin(), AmbiPoints.end(), std::back_inserter(impres), calc_res);
|
2019-12-08 16:46:11 -08:00
|
|
|
auto hrir_delay_round = [](const ALuint d) noexcept -> ALuint
|
|
|
|
{ return (d+HRIR_DELAY_FRACHALF) >> HRIR_DELAY_FRACBITS; };
|
2018-04-17 21:27:47 -07:00
|
|
|
|
2019-06-04 20:27:32 -07:00
|
|
|
/* For dual-band processing, add a 16-sample delay to compensate for the HF
|
2019-02-16 22:57:38 -08:00
|
|
|
* scale on the minimum-phase response.
|
|
|
|
*/
|
2019-11-29 08:33:46 -08:00
|
|
|
static constexpr ALuint base_delay{DualBand ? 16 : 0};
|
|
|
|
const double xover_norm{400.0 / Hrtf->sampleRate};
|
2019-05-22 03:03:24 -07:00
|
|
|
BandSplitterR<double> splitter{xover_norm};
|
2019-01-06 00:23:15 -08:00
|
|
|
|
2019-09-25 02:11:37 -07:00
|
|
|
auto tmpres = al::vector<std::array<double2,HRIR_LENGTH>>(state->Coeffs.size());
|
2019-09-24 22:27:12 -07:00
|
|
|
auto tmpflt = al::vector<std::array<double,HRIR_LENGTH*4>>(3);
|
2019-09-25 02:11:37 -07:00
|
|
|
for(size_t c{0u};c < AmbiPoints.size();++c)
|
Decode directly from B-Format to HRTF instead of a cube
Last time this attempted to average the HRIRs according to their contribution
to a given B-Format channel as if they were loudspeakers, as well as averaging
the HRIR delays. The latter part resulted in the loss of the ITD (inter-aural
time delay), a key component of HRTF.
This time, the HRIRs are averaged similar to above, except instead of averaging
the delays, they're applied to the resulting coefficients (for example, a delay
of 8 would apply the HRIR starting at the 8th sample of the target HRIR). This
does roughly double the IR length, as the largest delay is about 35 samples
while the filter is normally 32 samples. However, this is still smaller the
original data set IR (which was 256 samples), it also only needs to be applied
to 4 channels for first-order ambisonics, rather than the 8-channel cube. So
it's doing twice as much work per sample, but only working on half the number
of samples.
Additionally, since the resulting HRIRs no longer rely on an extra delay line,
a more efficient HRTF mixing function can be made that doesn't use one. Such a
function can also avoid the per-sample stepping parameters the original uses.
2016-08-11 23:20:35 -07:00
|
|
|
{
|
2019-09-24 22:27:12 -07:00
|
|
|
const al::span<const double2,HRIR_LENGTH> hrir{impres[c].hrir};
|
2019-12-08 16:46:11 -08:00
|
|
|
const ALuint ldelay{hrir_delay_round(impres[c].ldelay-min_delay) + base_delay};
|
|
|
|
const ALuint rdelay{hrir_delay_round(impres[c].rdelay-min_delay) + base_delay};
|
2017-04-07 11:49:24 -07:00
|
|
|
|
2019-09-24 22:27:12 -07:00
|
|
|
if /*constexpr*/(!DualBand)
|
2016-08-30 22:33:33 -07:00
|
|
|
{
|
2019-01-07 21:10:32 -08:00
|
|
|
/* For single-band decoding, apply the HF scale to the response. */
|
2019-09-25 02:11:37 -07:00
|
|
|
for(size_t i{0u};i < state->Coeffs.size();++i)
|
2017-03-31 04:25:22 -07:00
|
|
|
{
|
2019-09-24 22:27:12 -07:00
|
|
|
const double mult{double{AmbiOrderHFGain[OrderFromChan[i]]} * AmbiMatrix[c][i]};
|
2019-11-21 23:55:10 -08:00
|
|
|
const ALuint numirs{HRIR_LENGTH - maxu(ldelay, rdelay)};
|
2019-09-13 09:38:35 -07:00
|
|
|
ALuint lidx{ldelay}, ridx{rdelay};
|
|
|
|
for(ALuint j{0};j < numirs;++j)
|
2017-04-07 11:49:24 -07:00
|
|
|
{
|
2019-09-24 22:27:12 -07:00
|
|
|
tmpres[i][lidx++][0] += hrir[j][0] * mult;
|
|
|
|
tmpres[i][ridx++][1] += hrir[j][1] * mult;
|
2017-04-07 11:49:24 -07:00
|
|
|
}
|
2017-03-31 04:25:22 -07:00
|
|
|
}
|
2019-01-07 21:10:32 -08:00
|
|
|
continue;
|
2016-08-30 22:33:33 -07:00
|
|
|
}
|
2019-01-07 21:10:32 -08:00
|
|
|
|
2019-02-16 22:57:38 -08:00
|
|
|
/* For dual-band processing, the HRIR needs to be split into low and
|
|
|
|
* high frequency responses. The band-splitter alone creates frequency-
|
|
|
|
* dependent phase-shifts, which is not ideal. To counteract it,
|
|
|
|
* combine it with a backwards phase-shift.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Load the (left) HRIR backwards, into a temp buffer with padding. */
|
2019-09-24 22:27:12 -07:00
|
|
|
std::fill(tmpflt[2].begin(), tmpflt[2].end(), 0.0);
|
2019-11-21 23:55:10 -08:00
|
|
|
std::transform(hrir.cbegin(), hrir.cend(), tmpflt[2].rbegin() + HRIR_LENGTH*3,
|
2019-09-24 22:27:12 -07:00
|
|
|
[](const double2 &ir) noexcept -> double { return ir[0]; });
|
2019-02-16 22:57:38 -08:00
|
|
|
|
|
|
|
/* Apply the all-pass on the reversed signal and reverse the resulting
|
|
|
|
* sample array. This produces the forward response with a backwards
|
|
|
|
* phase-shift (+n degrees becomes -n degrees).
|
|
|
|
*/
|
2019-09-24 22:27:12 -07:00
|
|
|
splitter.applyAllpass(tmpflt[2].data(), tmpflt[2].size());
|
|
|
|
std::reverse(tmpflt[2].begin(), tmpflt[2].end());
|
2019-02-16 22:57:38 -08:00
|
|
|
|
|
|
|
/* Now apply the band-splitter. This applies the normal phase-shift,
|
|
|
|
* which cancels out with the backwards phase-shift to get the original
|
|
|
|
* phase on the split signal.
|
|
|
|
*/
|
2019-01-07 21:10:32 -08:00
|
|
|
splitter.clear();
|
2019-09-24 22:27:12 -07:00
|
|
|
splitter.process(tmpflt[0].data(), tmpflt[1].data(), tmpflt[2].data(), tmpflt[2].size());
|
2019-01-07 21:10:32 -08:00
|
|
|
|
|
|
|
/* Apply left ear response with delay and HF scale. */
|
2019-09-25 02:11:37 -07:00
|
|
|
for(size_t i{0u};i < state->Coeffs.size();++i)
|
2016-08-30 22:33:33 -07:00
|
|
|
{
|
2019-01-07 21:10:32 -08:00
|
|
|
const ALdouble mult{AmbiMatrix[c][i]};
|
|
|
|
const ALdouble hfgain{AmbiOrderHFGain[OrderFromChan[i]]};
|
2019-09-13 09:38:35 -07:00
|
|
|
ALuint j{HRIR_LENGTH*3 - ldelay};
|
|
|
|
for(ALuint lidx{0};lidx < HRIR_LENGTH;++lidx,++j)
|
2019-09-24 22:27:12 -07:00
|
|
|
tmpres[i][lidx][0] += (tmpflt[0][j]*hfgain + tmpflt[1][j]) * mult;
|
2019-01-07 21:10:32 -08:00
|
|
|
}
|
2016-08-17 05:34:09 -07:00
|
|
|
|
2019-02-16 22:57:38 -08:00
|
|
|
/* Now run the same process on the right HRIR. */
|
2019-09-24 22:27:12 -07:00
|
|
|
std::fill(tmpflt[2].begin(), tmpflt[2].end(), 0.0);
|
2019-11-21 23:55:10 -08:00
|
|
|
std::transform(hrir.cbegin(), hrir.cend(), tmpflt[2].rbegin() + HRIR_LENGTH*3,
|
2019-09-24 22:27:12 -07:00
|
|
|
[](const double2 &ir) noexcept -> double { return ir[1]; });
|
2019-02-16 22:57:38 -08:00
|
|
|
|
2019-09-24 22:27:12 -07:00
|
|
|
splitter.applyAllpass(tmpflt[2].data(), tmpflt[2].size());
|
|
|
|
std::reverse(tmpflt[2].begin(), tmpflt[2].end());
|
2019-02-16 22:57:38 -08:00
|
|
|
|
2019-01-07 21:10:32 -08:00
|
|
|
splitter.clear();
|
2019-09-24 22:27:12 -07:00
|
|
|
splitter.process(tmpflt[0].data(), tmpflt[1].data(), tmpflt[2].data(), tmpflt[2].size());
|
2016-08-21 03:05:42 -07:00
|
|
|
|
2019-09-25 02:11:37 -07:00
|
|
|
for(size_t i{0u};i < state->Coeffs.size();++i)
|
2019-01-07 21:10:32 -08:00
|
|
|
{
|
|
|
|
const ALdouble mult{AmbiMatrix[c][i]};
|
|
|
|
const ALdouble hfgain{AmbiOrderHFGain[OrderFromChan[i]]};
|
2019-09-13 09:38:35 -07:00
|
|
|
ALuint j{HRIR_LENGTH*3 - rdelay};
|
|
|
|
for(ALuint ridx{0};ridx < HRIR_LENGTH;++ridx,++j)
|
2019-09-24 22:27:12 -07:00
|
|
|
tmpres[i][ridx][1] += (tmpflt[0][j]*hfgain + tmpflt[1][j]) * mult;
|
2016-08-17 05:34:09 -07:00
|
|
|
}
|
Decode directly from B-Format to HRTF instead of a cube
Last time this attempted to average the HRIRs according to their contribution
to a given B-Format channel as if they were loudspeakers, as well as averaging
the HRIR delays. The latter part resulted in the loss of the ITD (inter-aural
time delay), a key component of HRTF.
This time, the HRIRs are averaged similar to above, except instead of averaging
the delays, they're applied to the resulting coefficients (for example, a delay
of 8 would apply the HRIR starting at the 8th sample of the target HRIR). This
does roughly double the IR length, as the largest delay is about 35 samples
while the filter is normally 32 samples. However, this is still smaller the
original data set IR (which was 256 samples), it also only needs to be applied
to 4 channels for first-order ambisonics, rather than the 8-channel cube. So
it's doing twice as much work per sample, but only working on half the number
of samples.
Additionally, since the resulting HRIRs no longer rely on an extra delay line,
a more efficient HRTF mixing function can be made that doesn't use one. Such a
function can also avoid the per-sample stepping parameters the original uses.
2016-08-11 23:20:35 -07:00
|
|
|
}
|
2019-09-24 22:27:12 -07:00
|
|
|
tmpflt.clear();
|
|
|
|
impres.clear();
|
2016-08-17 05:34:09 -07:00
|
|
|
|
2019-09-25 02:11:37 -07:00
|
|
|
for(size_t i{0u};i < state->Coeffs.size();++i)
|
2018-04-17 21:27:47 -07:00
|
|
|
{
|
2019-09-24 22:27:12 -07:00
|
|
|
auto copy_arr = [](const double2 &in) noexcept -> float2
|
2019-07-31 11:05:53 -07:00
|
|
|
{ return float2{{static_cast<float>(in[0]), static_cast<float>(in[1])}}; };
|
2019-11-21 23:55:10 -08:00
|
|
|
std::transform(tmpres[i].cbegin(), tmpres[i].cend(), state->Coeffs[i].begin(),
|
2019-02-07 08:38:49 -08:00
|
|
|
copy_arr);
|
2018-04-17 21:27:47 -07:00
|
|
|
}
|
2018-11-09 18:08:42 -08:00
|
|
|
tmpres.clear();
|
2018-04-17 21:27:47 -07:00
|
|
|
|
2019-12-08 16:46:11 -08:00
|
|
|
max_delay -= min_delay;
|
2019-09-13 09:38:35 -07:00
|
|
|
ALuint max_length{HRIR_LENGTH};
|
2019-06-04 20:27:32 -07:00
|
|
|
/* Increase the IR size by double the base delay with dual-band processing
|
|
|
|
* to account for the head and tail from the HF response scale.
|
2019-02-07 08:38:49 -08:00
|
|
|
*/
|
2019-09-13 09:38:35 -07:00
|
|
|
const ALuint irsize{minu(Hrtf->irSize + base_delay*2, max_length)};
|
2019-12-08 16:46:11 -08:00
|
|
|
max_length = minu(hrir_delay_round(max_delay) + irsize, max_length);
|
2019-02-07 08:38:49 -08:00
|
|
|
|
2018-05-04 06:48:20 -07:00
|
|
|
/* Round up to the next IR size multiple. */
|
|
|
|
max_length += MOD_IR_SIZE-1;
|
|
|
|
max_length -= max_length%MOD_IR_SIZE;
|
|
|
|
|
2019-12-08 16:46:11 -08:00
|
|
|
TRACE("Skipped delay: %.2f, max delay: %.2f, new FIR length: %u\n",
|
|
|
|
min_delay/double{HRIR_DELAY_FRACONE}, max_delay/double{HRIR_DELAY_FRACONE},
|
2019-09-13 09:38:35 -07:00
|
|
|
max_length);
|
2017-07-31 01:20:42 -07:00
|
|
|
state->IrSize = max_length;
|
Decode directly from B-Format to HRTF instead of a cube
Last time this attempted to average the HRIRs according to their contribution
to a given B-Format channel as if they were loudspeakers, as well as averaging
the HRIR delays. The latter part resulted in the loss of the ITD (inter-aural
time delay), a key component of HRTF.
This time, the HRIRs are averaged similar to above, except instead of averaging
the delays, they're applied to the resulting coefficients (for example, a delay
of 8 would apply the HRIR starting at the 8th sample of the target HRIR). This
does roughly double the IR length, as the largest delay is about 35 samples
while the filter is normally 32 samples. However, this is still smaller the
original data set IR (which was 256 samples), it also only needs to be applied
to 4 channels for first-order ambisonics, rather than the 8-channel cube. So
it's doing twice as much work per sample, but only working on half the number
of samples.
Additionally, since the resulting HRIRs no longer rely on an extra delay line,
a more efficient HRTF mixing function can be made that doesn't use one. Such a
function can also avoid the per-sample stepping parameters the original uses.
2016-08-11 23:20:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
namespace {
|
|
|
|
|
2019-12-09 20:55:54 -08:00
|
|
|
using ubyte2 = std::array<ALubyte,2>;
|
|
|
|
|
2019-11-28 06:10:36 -08:00
|
|
|
std::unique_ptr<HrtfStore> CreateHrtfStore(ALuint rate, ALushort irSize, const ALuint fdCount,
|
2019-10-27 15:50:59 -07:00
|
|
|
const ALubyte *evCount, const ALushort *distance, const ALushort *azCount,
|
2019-12-09 20:55:54 -08:00
|
|
|
const ALushort *irOffset, ALushort irCount, const float2 *coeffs, const ubyte2 *delays,
|
|
|
|
const char *filename)
|
2017-03-31 03:45:26 -07:00
|
|
|
{
|
2019-11-28 06:10:36 -08:00
|
|
|
std::unique_ptr<HrtfStore> Hrtf;
|
2017-03-31 03:45:26 -07:00
|
|
|
|
2019-09-13 09:38:35 -07:00
|
|
|
ALuint evTotal{std::accumulate(evCount, evCount+fdCount, 0u)};
|
2019-11-28 06:10:36 -08:00
|
|
|
size_t total{sizeof(HrtfStore)};
|
|
|
|
total = RoundUp(total, alignof(HrtfStore::Field)); /* Align for field infos */
|
|
|
|
total += sizeof(HrtfStore::Field)*fdCount;
|
|
|
|
total = RoundUp(total, alignof(HrtfStore::Elevation)); /* Align for elevation infos */
|
2019-05-22 10:58:18 -07:00
|
|
|
total += sizeof(Hrtf->elev[0])*evTotal;
|
2017-04-07 08:46:50 -07:00
|
|
|
total = RoundUp(total, 16); /* Align for coefficients using SIMD */
|
2019-11-28 09:46:16 -08:00
|
|
|
total += sizeof(Hrtf->coeffs[0])*irCount;
|
2017-03-31 03:45:26 -07:00
|
|
|
total += sizeof(Hrtf->delays[0])*irCount;
|
|
|
|
|
2019-11-28 06:10:36 -08:00
|
|
|
Hrtf.reset(new (al_calloc(16, total)) HrtfStore{});
|
2019-02-01 20:59:31 -08:00
|
|
|
if(!Hrtf)
|
2017-04-05 12:27:30 -07:00
|
|
|
ERR("Out of memory allocating storage for %s.\n", filename);
|
2017-03-31 03:45:26 -07:00
|
|
|
else
|
|
|
|
{
|
2019-09-14 18:27:57 -07:00
|
|
|
InitRef(Hrtf->mRef, 1u);
|
2017-03-31 03:45:26 -07:00
|
|
|
Hrtf->sampleRate = rate;
|
|
|
|
Hrtf->irSize = irSize;
|
2019-01-28 20:31:58 -08:00
|
|
|
Hrtf->fdCount = fdCount;
|
2017-03-31 03:45:26 -07:00
|
|
|
|
|
|
|
/* Set up pointers to storage following the main HRTF struct. */
|
2019-02-01 20:59:31 -08:00
|
|
|
char *base = reinterpret_cast<char*>(Hrtf.get());
|
2019-11-28 06:10:36 -08:00
|
|
|
uintptr_t offset = sizeof(HrtfStore);
|
2019-02-01 20:59:31 -08:00
|
|
|
|
2019-11-28 06:10:36 -08:00
|
|
|
offset = RoundUp(offset, alignof(HrtfStore::Field)); /* Align for field infos */
|
|
|
|
auto field_ = reinterpret_cast<HrtfStore::Field*>(base + offset);
|
2019-01-28 20:31:58 -08:00
|
|
|
offset += sizeof(field_[0])*fdCount;
|
|
|
|
|
2019-11-28 06:10:36 -08:00
|
|
|
offset = RoundUp(offset, alignof(HrtfStore::Elevation)); /* Align for elevation infos */
|
|
|
|
auto elev_ = reinterpret_cast<HrtfStore::Elevation*>(base + offset);
|
2019-05-22 10:58:18 -07:00
|
|
|
offset += sizeof(elev_[0])*evTotal;
|
2017-03-31 03:45:26 -07:00
|
|
|
|
2017-04-07 08:46:50 -07:00
|
|
|
offset = RoundUp(offset, 16); /* Align for coefficients using SIMD */
|
2019-11-28 09:46:16 -08:00
|
|
|
auto coeffs_ = reinterpret_cast<HrirArray*>(base + offset);
|
|
|
|
offset += sizeof(coeffs_[0])*irCount;
|
2017-03-31 03:45:26 -07:00
|
|
|
|
2019-02-01 20:59:31 -08:00
|
|
|
auto delays_ = reinterpret_cast<ALubyte(*)[2]>(base + offset);
|
2019-01-28 20:31:58 -08:00
|
|
|
offset += sizeof(delays_[0])*irCount;
|
2017-03-31 03:45:26 -07:00
|
|
|
|
2018-01-15 08:38:25 -08:00
|
|
|
assert(offset == total);
|
|
|
|
|
2017-03-31 03:45:26 -07:00
|
|
|
/* Copy input data to storage. */
|
2019-09-13 09:38:35 -07:00
|
|
|
for(ALuint i{0};i < fdCount;i++)
|
2019-01-28 20:31:58 -08:00
|
|
|
{
|
2019-10-27 15:50:59 -07:00
|
|
|
field_[i].distance = distance[i] / 1000.0f;
|
2019-05-22 10:58:18 -07:00
|
|
|
field_[i].evCount = evCount[i];
|
|
|
|
}
|
2019-09-13 09:38:35 -07:00
|
|
|
for(ALuint i{0};i < evTotal;i++)
|
2019-05-22 10:58:18 -07:00
|
|
|
{
|
|
|
|
elev_[i].azCount = azCount[i];
|
|
|
|
elev_[i].irOffset = irOffset[i];
|
2019-01-28 20:31:58 -08:00
|
|
|
}
|
2019-11-21 02:43:34 -08:00
|
|
|
for(ALuint i{0};i < irCount;i++)
|
2017-04-07 08:46:50 -07:00
|
|
|
{
|
2019-11-21 02:43:34 -08:00
|
|
|
for(ALuint j{0};j < ALuint{irSize};j++)
|
|
|
|
{
|
2019-11-28 09:46:16 -08:00
|
|
|
coeffs_[i][j][0] = coeffs[i*irSize + j][0];
|
|
|
|
coeffs_[i][j][1] = coeffs[i*irSize + j][1];
|
2019-11-21 02:43:34 -08:00
|
|
|
}
|
2019-11-29 08:33:46 -08:00
|
|
|
std::fill(coeffs_[i].begin()+irSize, coeffs_[i].end(), float2{});
|
2017-04-07 08:46:50 -07:00
|
|
|
}
|
2019-09-13 09:38:35 -07:00
|
|
|
for(ALuint i{0};i < irCount;i++)
|
2017-04-07 08:46:50 -07:00
|
|
|
{
|
2019-01-28 20:31:58 -08:00
|
|
|
delays_[i][0] = delays[i][0];
|
|
|
|
delays_[i][1] = delays[i][1];
|
2017-04-07 08:46:50 -07:00
|
|
|
}
|
2017-03-31 03:45:26 -07:00
|
|
|
|
2018-01-15 08:38:25 -08:00
|
|
|
/* Finally, assign the storage pointers. */
|
2019-01-28 20:31:58 -08:00
|
|
|
Hrtf->field = field_;
|
2019-05-22 10:58:18 -07:00
|
|
|
Hrtf->elev = elev_;
|
2019-01-28 20:31:58 -08:00
|
|
|
Hrtf->coeffs = coeffs_;
|
|
|
|
Hrtf->delays = delays_;
|
2017-03-31 03:45:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return Hrtf;
|
|
|
|
}
|
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
ALubyte GetLE_ALubyte(std::istream &data)
|
2017-06-16 22:58:13 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
return static_cast<ALubyte>(data.get());
|
2017-06-16 22:58:13 -07:00
|
|
|
}
|
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
ALshort GetLE_ALshort(std::istream &data)
|
2017-06-16 22:58:13 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
int ret = data.get();
|
|
|
|
ret |= data.get() << 8;
|
|
|
|
return static_cast<ALshort>((ret^32768) - 32768);
|
2017-06-16 22:58:13 -07:00
|
|
|
}
|
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
ALushort GetLE_ALushort(std::istream &data)
|
2017-06-16 22:58:13 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
int ret = data.get();
|
|
|
|
ret |= data.get() << 8;
|
|
|
|
return static_cast<ALushort>(ret);
|
2017-06-16 22:58:13 -07:00
|
|
|
}
|
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
ALint GetLE_ALint24(std::istream &data)
|
2017-06-16 22:58:13 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
int ret = data.get();
|
|
|
|
ret |= data.get() << 8;
|
|
|
|
ret |= data.get() << 16;
|
|
|
|
return (ret^8388608) - 8388608;
|
2017-08-08 20:25:31 -07:00
|
|
|
}
|
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
ALuint GetLE_ALuint(std::istream &data)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
int ret = data.get();
|
|
|
|
ret |= data.get() << 8;
|
|
|
|
ret |= data.get() << 16;
|
|
|
|
ret |= data.get() << 24;
|
2019-09-13 09:38:35 -07:00
|
|
|
return static_cast<ALuint>(ret);
|
2017-06-16 22:58:13 -07:00
|
|
|
}
|
|
|
|
|
2019-11-28 06:10:36 -08:00
|
|
|
std::unique_ptr<HrtfStore> LoadHrtf00(std::istream &data, const char *filename)
|
2017-06-16 22:58:13 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
ALuint rate{GetLE_ALuint(data)};
|
|
|
|
ALushort irCount{GetLE_ALushort(data)};
|
|
|
|
ALushort irSize{GetLE_ALushort(data)};
|
|
|
|
ALubyte evCount{GetLE_ALubyte(data)};
|
|
|
|
if(!data || data.eof())
|
2016-08-31 08:16:49 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
ERR("Failed reading %s\n", filename);
|
|
|
|
return nullptr;
|
2016-08-31 08:16:49 -07:00
|
|
|
}
|
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
ALboolean failed{AL_FALSE};
|
2019-12-11 01:20:00 -08:00
|
|
|
if(irSize < MIN_IR_SIZE || irSize > HRIR_LENGTH)
|
2012-09-12 07:25:05 -07:00
|
|
|
{
|
2019-12-11 01:20:00 -08:00
|
|
|
ERR("Unsupported HRIR size, irSize=%d (%d to %d)\n", irSize, MIN_IR_SIZE, HRIR_LENGTH);
|
2012-09-12 07:25:05 -07:00
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
|
|
|
if(evCount < MIN_EV_COUNT || evCount > MAX_EV_COUNT)
|
|
|
|
{
|
|
|
|
ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
|
|
|
|
evCount, MIN_EV_COUNT, MAX_EV_COUNT);
|
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
|
|
|
if(failed)
|
2018-11-09 23:47:42 -08:00
|
|
|
return nullptr;
|
2012-09-12 07:25:05 -07:00
|
|
|
|
2019-09-14 16:55:28 -07:00
|
|
|
auto evOffset = al::vector<ALushort>(evCount);
|
2018-11-09 23:47:42 -08:00
|
|
|
for(auto &val : evOffset)
|
|
|
|
val = GetLE_ALushort(data);
|
|
|
|
if(!data || data.eof())
|
2016-08-31 08:16:49 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
ERR("Failed reading %s\n", filename);
|
|
|
|
return nullptr;
|
2016-08-31 08:16:49 -07:00
|
|
|
}
|
2019-09-13 09:38:35 -07:00
|
|
|
for(size_t i{1};i < evCount;i++)
|
2012-09-12 07:25:05 -07:00
|
|
|
{
|
2018-11-09 18:08:42 -08:00
|
|
|
if(evOffset[i] <= evOffset[i-1])
|
2012-09-12 07:25:05 -07:00
|
|
|
{
|
2019-09-13 09:38:35 -07:00
|
|
|
ERR("Invalid evOffset: evOffset[%zu]=%d (last=%d)\n", i, evOffset[i], evOffset[i-1]);
|
2012-09-12 07:25:05 -07:00
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
2018-11-09 23:47:42 -08:00
|
|
|
}
|
|
|
|
if(irCount <= evOffset.back())
|
|
|
|
{
|
2019-04-11 16:01:11 -07:00
|
|
|
ERR("Invalid evOffset: evOffset[%zu]=%d (irCount=%d)\n",
|
2018-11-09 23:47:42 -08:00
|
|
|
evOffset.size()-1, evOffset.back(), irCount);
|
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
|
|
|
if(failed)
|
|
|
|
return nullptr;
|
2012-09-12 07:25:05 -07:00
|
|
|
|
2019-09-14 16:55:28 -07:00
|
|
|
auto azCount = al::vector<ALushort>(evCount);
|
2019-09-13 09:38:35 -07:00
|
|
|
for(size_t i{1};i < evCount;i++)
|
2018-11-09 23:47:42 -08:00
|
|
|
{
|
2019-09-14 16:55:28 -07:00
|
|
|
azCount[i-1] = static_cast<ALushort>(evOffset[i] - evOffset[i-1]);
|
2012-09-12 07:25:05 -07:00
|
|
|
if(azCount[i-1] < MIN_AZ_COUNT || azCount[i-1] > MAX_AZ_COUNT)
|
|
|
|
{
|
2019-09-13 09:38:35 -07:00
|
|
|
ERR("Unsupported azimuth count: azCount[%zd]=%d (%d to %d)\n",
|
2012-09-12 07:25:05 -07:00
|
|
|
i-1, azCount[i-1], MIN_AZ_COUNT, MAX_AZ_COUNT);
|
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
|
|
|
}
|
2019-09-14 16:55:28 -07:00
|
|
|
azCount.back() = static_cast<ALushort>(irCount - evOffset.back());
|
2018-11-09 23:47:42 -08:00
|
|
|
if(azCount.back() < MIN_AZ_COUNT || azCount.back() > MAX_AZ_COUNT)
|
2018-11-09 18:08:42 -08:00
|
|
|
{
|
2019-04-11 16:01:11 -07:00
|
|
|
ERR("Unsupported azimuth count: azCount[%zu]=%d (%d to %d)\n",
|
2018-11-09 23:47:42 -08:00
|
|
|
azCount.size()-1, azCount.back(), MIN_AZ_COUNT, MAX_AZ_COUNT);
|
2018-11-09 18:08:42 -08:00
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
2018-11-09 23:47:42 -08:00
|
|
|
if(failed)
|
|
|
|
return nullptr;
|
|
|
|
|
2019-12-09 20:55:54 -08:00
|
|
|
auto coeffs = al::vector<float2>(irSize*irCount);
|
|
|
|
auto delays = al::vector<ubyte2>(irCount);
|
2018-11-09 23:47:42 -08:00
|
|
|
for(auto &val : coeffs)
|
|
|
|
val[0] = GetLE_ALshort(data) / 32768.0f;
|
|
|
|
for(auto &val : delays)
|
|
|
|
val[0] = GetLE_ALubyte(data);
|
|
|
|
if(!data || data.eof())
|
2012-09-12 07:25:05 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
ERR("Failed reading %s\n", filename);
|
|
|
|
return nullptr;
|
2012-09-12 07:25:05 -07:00
|
|
|
}
|
2019-09-13 09:38:35 -07:00
|
|
|
for(size_t i{0};i < irCount;i++)
|
2016-08-31 08:16:49 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
if(delays[i][0] > MAX_HRIR_DELAY)
|
2016-08-31 08:16:49 -07:00
|
|
|
{
|
2019-09-13 09:38:35 -07:00
|
|
|
ERR("Invalid delays[%zd]: %d (%d)\n", i, delays[i][0], MAX_HRIR_DELAY);
|
2016-08-31 08:16:49 -07:00
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
2019-11-29 13:45:45 -08:00
|
|
|
delays[i][0] <<= HRIR_DELAY_FRACBITS;
|
2016-08-31 08:16:49 -07:00
|
|
|
}
|
2018-11-09 23:47:42 -08:00
|
|
|
if(failed)
|
|
|
|
return nullptr;
|
2016-08-31 08:16:49 -07:00
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
/* Mirror the left ear responses to the right ear. */
|
2019-09-13 09:38:35 -07:00
|
|
|
for(size_t i{0};i < evCount;i++)
|
2017-04-07 08:46:50 -07:00
|
|
|
{
|
2019-05-24 06:47:24 -07:00
|
|
|
const ALushort evoffset{evOffset[i]};
|
|
|
|
const ALushort azcount{azCount[i]};
|
2019-09-13 09:38:35 -07:00
|
|
|
for(size_t j{0};j < azcount;j++)
|
2017-04-07 08:46:50 -07:00
|
|
|
{
|
2019-09-13 09:38:35 -07:00
|
|
|
const size_t lidx{evoffset + j};
|
|
|
|
const size_t ridx{evoffset + ((azcount-j) % azcount)};
|
2017-04-07 08:46:50 -07:00
|
|
|
|
2019-09-13 09:38:35 -07:00
|
|
|
for(size_t k{0};k < irSize;k++)
|
2018-11-09 23:47:42 -08:00
|
|
|
coeffs[ridx*irSize + k][1] = coeffs[lidx*irSize + k][0];
|
|
|
|
delays[ridx][1] = delays[lidx][0];
|
2017-04-07 08:46:50 -07:00
|
|
|
}
|
|
|
|
}
|
2012-09-12 07:25:05 -07:00
|
|
|
|
2019-10-27 15:50:59 -07:00
|
|
|
static const ALushort distance{0};
|
2019-02-27 23:13:40 -08:00
|
|
|
return CreateHrtfStore(rate, irSize, 1, &evCount, &distance, azCount.data(), evOffset.data(),
|
2019-12-09 20:55:54 -08:00
|
|
|
irCount, coeffs.data(), delays.data(), filename);
|
2012-09-12 07:25:05 -07:00
|
|
|
}
|
|
|
|
|
2019-11-28 06:10:36 -08:00
|
|
|
std::unique_ptr<HrtfStore> LoadHrtf01(std::istream &data, const char *filename)
|
2012-09-12 07:25:05 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
ALuint rate{GetLE_ALuint(data)};
|
|
|
|
ALushort irSize{GetLE_ALubyte(data)};
|
|
|
|
ALubyte evCount{GetLE_ALubyte(data)};
|
|
|
|
if(!data || data.eof())
|
2016-08-31 08:16:49 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
ERR("Failed reading %s\n", filename);
|
|
|
|
return nullptr;
|
2016-08-31 08:16:49 -07:00
|
|
|
}
|
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
ALboolean failed{AL_FALSE};
|
2019-12-11 01:20:00 -08:00
|
|
|
if(irSize < MIN_IR_SIZE || irSize > HRIR_LENGTH)
|
2012-09-12 07:25:05 -07:00
|
|
|
{
|
2019-12-11 01:20:00 -08:00
|
|
|
ERR("Unsupported HRIR size, irSize=%d (%d to %d)\n", irSize, MIN_IR_SIZE, HRIR_LENGTH);
|
2012-09-12 07:25:05 -07:00
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
|
|
|
if(evCount < MIN_EV_COUNT || evCount > MAX_EV_COUNT)
|
|
|
|
{
|
|
|
|
ERR("Unsupported elevation count: evCount=%d (%d to %d)\n",
|
|
|
|
evCount, MIN_EV_COUNT, MAX_EV_COUNT);
|
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
|
|
|
if(failed)
|
2018-11-09 23:47:42 -08:00
|
|
|
return nullptr;
|
2012-09-12 07:25:05 -07:00
|
|
|
|
2019-09-14 16:55:28 -07:00
|
|
|
auto azCount = al::vector<ALushort>(evCount);
|
2019-05-22 10:58:18 -07:00
|
|
|
std::generate(azCount.begin(), azCount.end(), std::bind(GetLE_ALubyte, std::ref(data)));
|
|
|
|
if(!data || data.eof())
|
2016-08-31 08:16:49 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
ERR("Failed reading %s\n", filename);
|
|
|
|
return nullptr;
|
2016-08-31 08:16:49 -07:00
|
|
|
}
|
2019-09-13 09:38:35 -07:00
|
|
|
for(size_t i{0};i < evCount;++i)
|
2012-09-12 07:25:05 -07:00
|
|
|
{
|
2018-11-09 18:08:42 -08:00
|
|
|
if(azCount[i] < MIN_AZ_COUNT || azCount[i] > MAX_AZ_COUNT)
|
2012-09-12 07:25:05 -07:00
|
|
|
{
|
2019-09-13 09:38:35 -07:00
|
|
|
ERR("Unsupported azimuth count: azCount[%zd]=%d (%d to %d)\n", i, azCount[i],
|
|
|
|
MIN_AZ_COUNT, MAX_AZ_COUNT);
|
2018-11-09 18:08:42 -08:00
|
|
|
failed = AL_TRUE;
|
2012-09-12 07:25:05 -07:00
|
|
|
}
|
|
|
|
}
|
2018-11-09 23:47:42 -08:00
|
|
|
if(failed)
|
|
|
|
return nullptr;
|
2012-09-12 07:25:05 -07:00
|
|
|
|
2019-09-13 09:38:35 -07:00
|
|
|
auto evOffset = al::vector<ALushort>(evCount);
|
2018-11-09 23:47:42 -08:00
|
|
|
evOffset[0] = 0;
|
|
|
|
ALushort irCount{azCount[0]};
|
2019-09-13 09:38:35 -07:00
|
|
|
for(size_t i{1};i < evCount;i++)
|
2012-09-12 07:25:05 -07:00
|
|
|
{
|
2019-09-14 16:55:28 -07:00
|
|
|
evOffset[i] = static_cast<ALushort>(evOffset[i-1] + azCount[i-1]);
|
2019-09-18 20:13:58 -07:00
|
|
|
irCount = static_cast<ALushort>(irCount + azCount[i]);
|
2016-08-31 08:16:49 -07:00
|
|
|
}
|
|
|
|
|
2019-12-09 20:55:54 -08:00
|
|
|
auto coeffs = al::vector<float2>(irSize*irCount);
|
|
|
|
auto delays = al::vector<ubyte2>(irCount);
|
2018-11-09 23:47:42 -08:00
|
|
|
for(auto &val : coeffs)
|
|
|
|
val[0] = GetLE_ALshort(data) / 32768.0f;
|
|
|
|
for(auto &val : delays)
|
|
|
|
val[0] = GetLE_ALubyte(data);
|
|
|
|
if(!data || data.eof())
|
2012-09-12 07:25:05 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
ERR("Failed reading %s\n", filename);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-09-13 09:38:35 -07:00
|
|
|
for(size_t i{0};i < irCount;i++)
|
2018-11-09 23:47:42 -08:00
|
|
|
{
|
|
|
|
if(delays[i][0] > MAX_HRIR_DELAY)
|
2012-09-12 07:25:05 -07:00
|
|
|
{
|
2019-09-13 09:38:35 -07:00
|
|
|
ERR("Invalid delays[%zd]: %d (%d)\n", i, delays[i][0], MAX_HRIR_DELAY);
|
2018-11-09 23:47:42 -08:00
|
|
|
failed = AL_TRUE;
|
2012-09-12 07:25:05 -07:00
|
|
|
}
|
2019-11-29 13:45:45 -08:00
|
|
|
delays[i][0] <<= HRIR_DELAY_FRACBITS;
|
2012-09-12 07:25:05 -07:00
|
|
|
}
|
2018-11-09 23:47:42 -08:00
|
|
|
if(failed)
|
|
|
|
return nullptr;
|
2012-09-12 07:25:05 -07:00
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
/* Mirror the left ear responses to the right ear. */
|
2019-09-13 09:38:35 -07:00
|
|
|
for(size_t i{0};i < evCount;i++)
|
2017-04-07 08:46:50 -07:00
|
|
|
{
|
2019-05-24 06:47:24 -07:00
|
|
|
const ALushort evoffset{evOffset[i]};
|
|
|
|
const ALushort azcount{azCount[i]};
|
2019-09-13 09:38:35 -07:00
|
|
|
for(size_t j{0};j < azcount;j++)
|
2017-04-07 08:46:50 -07:00
|
|
|
{
|
2019-09-13 09:38:35 -07:00
|
|
|
const size_t lidx{evoffset + j};
|
|
|
|
const size_t ridx{evoffset + ((azcount-j) % azcount)};
|
2017-04-07 08:46:50 -07:00
|
|
|
|
2019-09-13 09:38:35 -07:00
|
|
|
for(size_t k{0};k < irSize;k++)
|
2018-11-09 23:47:42 -08:00
|
|
|
coeffs[ridx*irSize + k][1] = coeffs[lidx*irSize + k][0];
|
|
|
|
delays[ridx][1] = delays[lidx][0];
|
2017-04-07 08:46:50 -07:00
|
|
|
}
|
|
|
|
}
|
2012-09-12 07:25:05 -07:00
|
|
|
|
2019-10-27 15:50:59 -07:00
|
|
|
static const ALushort distance{0};
|
2019-02-27 23:13:40 -08:00
|
|
|
return CreateHrtfStore(rate, irSize, 1, &evCount, &distance, azCount.data(), evOffset.data(),
|
2019-12-09 20:55:54 -08:00
|
|
|
irCount, coeffs.data(), delays.data(), filename);
|
2012-09-12 07:25:05 -07:00
|
|
|
}
|
|
|
|
|
2019-11-28 06:10:36 -08:00
|
|
|
std::unique_ptr<HrtfStore> LoadHrtf02(std::istream &data, const char *filename)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
2019-12-09 20:45:11 -08:00
|
|
|
constexpr ALubyte SampleType_S16{0};
|
|
|
|
constexpr ALubyte SampleType_S24{1};
|
|
|
|
constexpr ALubyte ChanType_LeftOnly{0};
|
|
|
|
constexpr ALubyte ChanType_LeftRight{1};
|
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
ALuint rate{GetLE_ALuint(data)};
|
|
|
|
ALubyte sampleType{GetLE_ALubyte(data)};
|
|
|
|
ALubyte channelType{GetLE_ALubyte(data)};
|
|
|
|
ALushort irSize{GetLE_ALubyte(data)};
|
|
|
|
ALubyte fdCount{GetLE_ALubyte(data)};
|
|
|
|
if(!data || data.eof())
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
ERR("Failed reading %s\n", filename);
|
|
|
|
return nullptr;
|
2017-08-08 20:25:31 -07:00
|
|
|
}
|
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
ALboolean failed{AL_FALSE};
|
2019-12-09 20:45:11 -08:00
|
|
|
if(sampleType > SampleType_S24)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
|
|
|
ERR("Unsupported sample type: %d\n", sampleType);
|
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
2019-12-09 20:45:11 -08:00
|
|
|
if(channelType > ChanType_LeftRight)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
|
|
|
ERR("Unsupported channel type: %d\n", channelType);
|
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
|
|
|
|
2019-12-11 01:20:00 -08:00
|
|
|
if(irSize < MIN_IR_SIZE || irSize > HRIR_LENGTH)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
2019-12-11 01:20:00 -08:00
|
|
|
ERR("Unsupported HRIR size, irSize=%d (%d to %d)\n", irSize, MIN_IR_SIZE, HRIR_LENGTH);
|
2017-08-08 20:25:31 -07:00
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
2019-01-22 10:27:04 -08:00
|
|
|
if(fdCount < 1 || fdCount > MAX_FD_COUNT)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
2017-10-22 15:36:42 -07:00
|
|
|
ERR("Multiple field-depths not supported: fdCount=%d (%d to %d)\n",
|
2018-11-09 23:47:42 -08:00
|
|
|
fdCount, MIN_FD_COUNT, MAX_FD_COUNT);
|
2017-08-08 20:25:31 -07:00
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
|
|
|
if(failed)
|
2018-11-09 23:47:42 -08:00
|
|
|
return nullptr;
|
2017-08-08 20:25:31 -07:00
|
|
|
|
2019-10-27 15:50:59 -07:00
|
|
|
auto distance = al::vector<ALushort>(fdCount);
|
2019-09-13 09:38:35 -07:00
|
|
|
auto evCount = al::vector<ALubyte>(fdCount);
|
|
|
|
auto azCount = al::vector<ALushort>{};
|
|
|
|
for(size_t f{0};f < fdCount;f++)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
2019-10-27 15:50:59 -07:00
|
|
|
distance[f] = GetLE_ALushort(data);
|
2019-01-22 10:27:04 -08:00
|
|
|
evCount[f] = GetLE_ALubyte(data);
|
2018-11-09 23:47:42 -08:00
|
|
|
if(!data || data.eof())
|
2017-10-22 15:36:42 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
ERR("Failed reading %s\n", filename);
|
|
|
|
return nullptr;
|
2017-10-22 15:36:42 -07:00
|
|
|
}
|
2017-08-08 20:25:31 -07:00
|
|
|
|
2019-01-22 10:27:04 -08:00
|
|
|
if(distance[f] < MIN_FD_DISTANCE || distance[f] > MAX_FD_DISTANCE)
|
2017-10-22 15:36:42 -07:00
|
|
|
{
|
2019-10-27 15:50:59 -07:00
|
|
|
ERR("Unsupported field distance[%zu]=%d (%d to %d millimeters)\n", f, distance[f],
|
2019-09-13 09:38:35 -07:00
|
|
|
MIN_FD_DISTANCE, MAX_FD_DISTANCE);
|
2017-10-22 15:36:42 -07:00
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
2019-01-22 21:36:40 -08:00
|
|
|
if(f > 0 && distance[f] <= distance[f-1])
|
|
|
|
{
|
2019-10-27 15:50:59 -07:00
|
|
|
ERR("Field distance[%zu] is not after previous (%d > %d)\n", f, distance[f],
|
2019-01-22 21:36:40 -08:00
|
|
|
distance[f-1]);
|
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
2019-01-22 10:27:04 -08:00
|
|
|
if(evCount[f] < MIN_EV_COUNT || evCount[f] > MAX_EV_COUNT)
|
2017-10-22 15:36:42 -07:00
|
|
|
{
|
2019-09-13 09:38:35 -07:00
|
|
|
ERR("Unsupported elevation count: evCount[%zu]=%d (%d to %d)\n", f, evCount[f],
|
|
|
|
MIN_EV_COUNT, MAX_EV_COUNT);
|
2017-10-22 15:36:42 -07:00
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
|
|
|
if(failed)
|
2018-11-09 23:47:42 -08:00
|
|
|
return nullptr;
|
2017-08-08 20:25:31 -07:00
|
|
|
|
2019-09-13 09:38:35 -07:00
|
|
|
const size_t ebase{azCount.size()};
|
2019-01-22 10:27:04 -08:00
|
|
|
azCount.resize(ebase + evCount[f]);
|
2019-09-13 09:38:35 -07:00
|
|
|
std::generate(azCount.begin()+static_cast<ptrdiff_t>(ebase), azCount.end(),
|
2019-05-22 10:58:18 -07:00
|
|
|
std::bind(GetLE_ALubyte, std::ref(data)));
|
|
|
|
if(!data || data.eof())
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
ERR("Failed reading %s\n", filename);
|
|
|
|
return nullptr;
|
2017-10-22 15:36:42 -07:00
|
|
|
}
|
|
|
|
|
2019-09-13 09:38:35 -07:00
|
|
|
for(size_t e{0};e < evCount[f];e++)
|
2017-10-22 15:36:42 -07:00
|
|
|
{
|
2019-01-22 10:27:04 -08:00
|
|
|
if(azCount[ebase+e] < MIN_AZ_COUNT || azCount[ebase+e] > MAX_AZ_COUNT)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
2019-09-13 09:38:35 -07:00
|
|
|
ERR("Unsupported azimuth count: azCount[%zu][%zu]=%d (%d to %d)\n", f, e,
|
2019-01-22 10:27:04 -08:00
|
|
|
azCount[ebase+e], MIN_AZ_COUNT, MAX_AZ_COUNT);
|
2017-08-08 20:25:31 -07:00
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
|
|
|
}
|
2018-11-09 23:47:42 -08:00
|
|
|
if(failed)
|
|
|
|
return nullptr;
|
2017-08-08 20:25:31 -07:00
|
|
|
}
|
|
|
|
|
2019-09-13 09:38:35 -07:00
|
|
|
auto evOffset = al::vector<ALushort>(azCount.size());
|
2018-11-09 18:08:42 -08:00
|
|
|
evOffset[0] = 0;
|
2019-05-22 10:58:18 -07:00
|
|
|
std::partial_sum(azCount.cbegin(), azCount.cend()-1, evOffset.begin()+1);
|
2019-09-13 09:38:35 -07:00
|
|
|
const auto irTotal = static_cast<ALushort>(evOffset.back() + azCount.back());
|
2019-01-22 10:27:04 -08:00
|
|
|
|
2019-12-09 20:55:54 -08:00
|
|
|
auto coeffs = al::vector<float2>(irSize*irTotal);
|
|
|
|
auto delays = al::vector<ubyte2>(irTotal);
|
2019-12-09 20:45:11 -08:00
|
|
|
if(channelType == ChanType_LeftOnly)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
2019-12-09 20:45:11 -08:00
|
|
|
if(sampleType == SampleType_S16)
|
2018-11-09 23:47:42 -08:00
|
|
|
{
|
|
|
|
for(auto &val : coeffs)
|
|
|
|
val[0] = GetLE_ALshort(data) / 32768.0f;
|
|
|
|
}
|
2019-12-09 20:45:11 -08:00
|
|
|
else if(sampleType == SampleType_S24)
|
2018-11-09 23:47:42 -08:00
|
|
|
{
|
|
|
|
for(auto &val : coeffs)
|
2019-09-14 16:55:28 -07:00
|
|
|
val[0] = static_cast<float>(GetLE_ALint24(data)) / 8388608.0f;
|
2018-11-09 23:47:42 -08:00
|
|
|
}
|
|
|
|
for(auto &val : delays)
|
|
|
|
val[0] = GetLE_ALubyte(data);
|
|
|
|
if(!data || data.eof())
|
|
|
|
{
|
|
|
|
ERR("Failed reading %s\n", filename);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-09-13 09:38:35 -07:00
|
|
|
for(size_t i{0};i < irTotal;++i)
|
2018-11-09 23:47:42 -08:00
|
|
|
{
|
|
|
|
if(delays[i][0] > MAX_HRIR_DELAY)
|
|
|
|
{
|
2019-09-13 09:38:35 -07:00
|
|
|
ERR("Invalid delays[%zu][0]: %d (%d)\n", i, delays[i][0], MAX_HRIR_DELAY);
|
2018-11-09 23:47:42 -08:00
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
2019-11-29 13:45:45 -08:00
|
|
|
delays[i][0] <<= HRIR_DELAY_FRACBITS;
|
2018-11-09 23:47:42 -08:00
|
|
|
}
|
2017-08-08 20:25:31 -07:00
|
|
|
}
|
2019-12-09 20:45:11 -08:00
|
|
|
else if(channelType == ChanType_LeftRight)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
2019-12-09 20:45:11 -08:00
|
|
|
if(sampleType == SampleType_S16)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
for(auto &val : coeffs)
|
2017-10-22 15:36:42 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
val[0] = GetLE_ALshort(data) / 32768.0f;
|
|
|
|
val[1] = GetLE_ALshort(data) / 32768.0f;
|
2017-10-22 15:36:42 -07:00
|
|
|
}
|
2017-08-08 20:25:31 -07:00
|
|
|
}
|
2019-12-09 20:45:11 -08:00
|
|
|
else if(sampleType == SampleType_S24)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
for(auto &val : coeffs)
|
|
|
|
{
|
2019-09-14 16:55:28 -07:00
|
|
|
val[0] = static_cast<float>(GetLE_ALint24(data)) / 8388608.0f;
|
|
|
|
val[1] = static_cast<float>(GetLE_ALint24(data)) / 8388608.0f;
|
2018-11-09 23:47:42 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for(auto &val : delays)
|
|
|
|
{
|
|
|
|
val[0] = GetLE_ALubyte(data);
|
|
|
|
val[1] = GetLE_ALubyte(data);
|
|
|
|
}
|
|
|
|
if(!data || data.eof())
|
|
|
|
{
|
|
|
|
ERR("Failed reading %s\n", filename);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-10-22 15:36:42 -07:00
|
|
|
|
2019-09-13 09:38:35 -07:00
|
|
|
for(size_t i{0};i < irTotal;++i)
|
2018-11-09 23:47:42 -08:00
|
|
|
{
|
|
|
|
if(delays[i][0] > MAX_HRIR_DELAY)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
2019-09-13 09:38:35 -07:00
|
|
|
ERR("Invalid delays[%zu][0]: %d (%d)\n", i, delays[i][0], MAX_HRIR_DELAY);
|
2018-11-09 23:47:42 -08:00
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
|
|
|
if(delays[i][1] > MAX_HRIR_DELAY)
|
|
|
|
{
|
2019-09-13 09:38:35 -07:00
|
|
|
ERR("Invalid delays[%zu][1]: %d (%d)\n", i, delays[i][1], MAX_HRIR_DELAY);
|
2018-11-09 23:47:42 -08:00
|
|
|
failed = AL_TRUE;
|
2017-08-08 20:25:31 -07:00
|
|
|
}
|
2019-11-29 13:45:45 -08:00
|
|
|
delays[i][0] <<= HRIR_DELAY_FRACBITS;
|
|
|
|
delays[i][1] <<= HRIR_DELAY_FRACBITS;
|
2017-08-08 20:25:31 -07:00
|
|
|
}
|
|
|
|
}
|
2018-11-09 23:47:42 -08:00
|
|
|
if(failed)
|
|
|
|
return nullptr;
|
2017-08-08 20:25:31 -07:00
|
|
|
|
2019-12-09 20:45:11 -08:00
|
|
|
if(channelType == ChanType_LeftOnly)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
/* Mirror the left ear responses to the right ear. */
|
2019-09-13 09:38:35 -07:00
|
|
|
size_t ebase{0};
|
|
|
|
for(size_t f{0};f < fdCount;f++)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
2019-09-13 09:38:35 -07:00
|
|
|
for(size_t e{0};e < evCount[f];e++)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
2019-05-24 06:47:24 -07:00
|
|
|
const ALushort evoffset{evOffset[ebase+e]};
|
|
|
|
const ALushort azcount{azCount[ebase+e]};
|
2019-09-13 09:38:35 -07:00
|
|
|
for(size_t a{0};a < azcount;a++)
|
2019-01-22 10:27:04 -08:00
|
|
|
{
|
2019-09-13 09:38:35 -07:00
|
|
|
const size_t lidx{evoffset + a};
|
|
|
|
const size_t ridx{evoffset + ((azcount-a) % azcount)};
|
2017-08-08 20:25:31 -07:00
|
|
|
|
2019-09-13 09:38:35 -07:00
|
|
|
for(size_t k{0};k < irSize;k++)
|
2019-01-22 10:27:04 -08:00
|
|
|
coeffs[ridx*irSize + k][1] = coeffs[lidx*irSize + k][0];
|
|
|
|
delays[ridx][1] = delays[lidx][0];
|
|
|
|
}
|
2017-08-08 20:25:31 -07:00
|
|
|
}
|
2019-01-22 10:27:04 -08:00
|
|
|
ebase += evCount[f];
|
2017-08-08 20:25:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 10:58:18 -07:00
|
|
|
if(fdCount > 1)
|
|
|
|
{
|
2019-10-27 15:50:59 -07:00
|
|
|
auto distance_ = al::vector<ALushort>(distance.size());
|
2019-05-22 10:58:18 -07:00
|
|
|
auto evCount_ = al::vector<ALubyte>(evCount.size());
|
|
|
|
auto azCount_ = al::vector<ALushort>(azCount.size());
|
|
|
|
auto evOffset_ = al::vector<ALushort>(evOffset.size());
|
|
|
|
auto coeffs_ = al::vector<float2>(coeffs.size());
|
2019-12-09 20:55:54 -08:00
|
|
|
auto delays_ = al::vector<ubyte2>(delays.size());
|
2019-05-22 10:58:18 -07:00
|
|
|
|
|
|
|
/* Simple reverse for the per-field elements. */
|
|
|
|
std::reverse_copy(distance.cbegin(), distance.cend(), distance_.begin());
|
|
|
|
std::reverse_copy(evCount.cbegin(), evCount.cend(), evCount_.begin());
|
|
|
|
|
|
|
|
/* Each field has a group of elevations, which each have an azimuth
|
|
|
|
* count. Reverse the order of the groups, keeping the relative order
|
|
|
|
* of per-group azimuth counts.
|
|
|
|
*/
|
|
|
|
auto azcnt_end = azCount_.end();
|
2019-09-13 09:38:35 -07:00
|
|
|
auto copy_azs = [&azCount,&azcnt_end](const ptrdiff_t ebase, const ALubyte num_evs) -> ptrdiff_t
|
2019-05-22 10:58:18 -07:00
|
|
|
{
|
|
|
|
auto azcnt_src = azCount.begin()+ebase;
|
|
|
|
azcnt_end = std::copy_backward(azcnt_src, azcnt_src+num_evs, azcnt_end);
|
|
|
|
return ebase + num_evs;
|
|
|
|
};
|
2019-09-13 09:38:35 -07:00
|
|
|
std::accumulate(evCount.cbegin(), evCount.cend(), ptrdiff_t{0}, copy_azs);
|
2019-05-22 10:58:18 -07:00
|
|
|
assert(azCount_.begin() == azcnt_end);
|
|
|
|
|
|
|
|
/* Reestablish the IR offset for each elevation index, given the new
|
|
|
|
* ordering of elevations.
|
|
|
|
*/
|
|
|
|
evOffset_[0] = 0;
|
|
|
|
std::partial_sum(azCount_.cbegin(), azCount_.cend()-1, evOffset_.begin()+1);
|
|
|
|
|
|
|
|
/* Reverse the order of each field's group of IRs. */
|
|
|
|
auto coeffs_end = coeffs_.end();
|
|
|
|
auto delays_end = delays_.end();
|
2019-09-13 09:38:35 -07:00
|
|
|
auto copy_irs = [irSize,&azCount,&coeffs,&delays,&coeffs_end,&delays_end](const ptrdiff_t ebase, const ALubyte num_evs) -> ptrdiff_t
|
2019-05-22 10:58:18 -07:00
|
|
|
{
|
|
|
|
const ALsizei abase{std::accumulate(azCount.cbegin(), azCount.cbegin()+ebase, 0)};
|
|
|
|
const ALsizei num_azs{std::accumulate(azCount.cbegin()+ebase,
|
|
|
|
azCount.cbegin() + (ebase+num_evs), 0)};
|
|
|
|
|
|
|
|
coeffs_end = std::copy_backward(coeffs.cbegin() + abase*irSize,
|
|
|
|
coeffs.cbegin() + (abase+num_azs)*irSize, coeffs_end);
|
|
|
|
delays_end = std::copy_backward(delays.cbegin() + abase,
|
|
|
|
delays.cbegin() + (abase+num_azs), delays_end);
|
|
|
|
|
|
|
|
return ebase + num_evs;
|
|
|
|
};
|
2019-09-13 09:38:35 -07:00
|
|
|
std::accumulate(evCount.cbegin(), evCount.cend(), ptrdiff_t{0}, copy_irs);
|
2019-05-22 10:58:18 -07:00
|
|
|
assert(coeffs_.begin() == coeffs_end);
|
|
|
|
assert(delays_.begin() == delays_end);
|
|
|
|
|
|
|
|
distance = std::move(distance_);
|
|
|
|
evCount = std::move(evCount_);
|
|
|
|
azCount = std::move(azCount_);
|
|
|
|
evOffset = std::move(evOffset_);
|
|
|
|
coeffs = std::move(coeffs_);
|
|
|
|
delays = std::move(delays_);
|
|
|
|
}
|
|
|
|
|
2019-02-27 23:13:40 -08:00
|
|
|
return CreateHrtfStore(rate, irSize, fdCount, evCount.data(), distance.data(), azCount.data(),
|
2019-12-09 20:55:54 -08:00
|
|
|
evOffset.data(), irTotal, coeffs.data(), delays.data(), filename);
|
2017-08-08 20:25:31 -07:00
|
|
|
}
|
|
|
|
|
2017-04-07 06:40:42 -07:00
|
|
|
|
2019-11-28 08:24:29 -08:00
|
|
|
bool checkName(const std::string &name)
|
2018-11-18 19:19:35 -08:00
|
|
|
{
|
2019-11-28 08:24:29 -08:00
|
|
|
auto match_name = [&name](const HrtfEntry &entry) -> bool { return name == entry.mDispName; };
|
|
|
|
auto &enum_names = EnumeratedHrtfs;
|
|
|
|
return std::find_if(enum_names.cbegin(), enum_names.cend(), match_name) != enum_names.cend();
|
2018-11-18 19:19:35 -08:00
|
|
|
}
|
|
|
|
|
2019-11-28 08:24:29 -08:00
|
|
|
void AddFileEntry(const std::string &filename)
|
2015-10-06 00:23:11 -07:00
|
|
|
{
|
2019-12-08 22:09:21 -08:00
|
|
|
/* Check if this file has already been enumerated. */
|
|
|
|
auto enum_iter = std::find_if(EnumeratedHrtfs.cbegin(), EnumeratedHrtfs.cend(),
|
|
|
|
[&filename](const HrtfEntry &entry) -> bool
|
|
|
|
{ return entry.mFilename == filename; });
|
|
|
|
if(enum_iter != EnumeratedHrtfs.cend())
|
2017-04-05 12:46:02 -07:00
|
|
|
{
|
2019-11-28 08:24:29 -08:00
|
|
|
TRACE("Skipping duplicate file entry %s\n", filename.c_str());
|
|
|
|
return;
|
2017-04-05 12:46:02 -07:00
|
|
|
}
|
2017-04-05 07:09:16 -07:00
|
|
|
|
2015-10-06 04:01:53 -07:00
|
|
|
/* TODO: Get a human-readable name from the HRTF data (possibly coming in a
|
2015-10-06 00:23:11 -07:00
|
|
|
* format update). */
|
2018-11-11 19:17:40 -08:00
|
|
|
size_t namepos = filename.find_last_of('/')+1;
|
|
|
|
if(!namepos) namepos = filename.find_last_of('\\')+1;
|
2016-09-01 21:05:24 -07:00
|
|
|
|
2018-11-11 19:17:40 -08:00
|
|
|
size_t extpos{filename.find_last_of('.')};
|
|
|
|
if(extpos <= namepos) extpos = std::string::npos;
|
2015-10-06 00:23:11 -07:00
|
|
|
|
2018-11-18 19:19:35 -08:00
|
|
|
const std::string basename{(extpos == std::string::npos) ?
|
|
|
|
filename.substr(namepos) : filename.substr(namepos, extpos-namepos)};
|
|
|
|
std::string newname{basename};
|
|
|
|
int count{1};
|
2019-11-28 08:24:29 -08:00
|
|
|
while(checkName(newname))
|
2018-11-18 19:19:35 -08:00
|
|
|
{
|
|
|
|
newname = basename;
|
|
|
|
newname += " #";
|
|
|
|
newname += std::to_string(++count);
|
|
|
|
}
|
2019-12-08 22:09:21 -08:00
|
|
|
EnumeratedHrtfs.emplace_back(HrtfEntry{newname, filename});
|
|
|
|
const HrtfEntry &entry = EnumeratedHrtfs.back();
|
2015-10-06 00:23:11 -07:00
|
|
|
|
2019-11-28 08:24:29 -08:00
|
|
|
TRACE("Adding file entry \"%s\"\n", entry.mFilename.c_str());
|
2015-10-06 00:23:11 -07:00
|
|
|
}
|
|
|
|
|
2016-08-31 08:16:49 -07:00
|
|
|
/* Unfortunate that we have to duplicate AddFileEntry to take a memory buffer
|
|
|
|
* for input instead of opening the given filename.
|
2016-07-12 19:02:19 -07:00
|
|
|
*/
|
2019-11-28 08:24:29 -08:00
|
|
|
void AddBuiltInEntry(const std::string &dispname, ALuint residx)
|
2016-07-12 19:02:19 -07:00
|
|
|
{
|
2019-11-28 08:24:29 -08:00
|
|
|
const std::string filename{'!'+std::to_string(residx)+'_'+dispname};
|
2016-07-12 19:02:19 -07:00
|
|
|
|
2019-12-08 22:09:21 -08:00
|
|
|
auto enum_iter = std::find_if(EnumeratedHrtfs.cbegin(), EnumeratedHrtfs.cend(),
|
|
|
|
[&filename](const HrtfEntry &entry) -> bool
|
|
|
|
{ return entry.mFilename == filename; });
|
|
|
|
if(enum_iter != EnumeratedHrtfs.cend())
|
2016-07-12 19:02:19 -07:00
|
|
|
{
|
2019-11-28 08:24:29 -08:00
|
|
|
TRACE("Skipping duplicate file entry %s\n", filename.c_str());
|
|
|
|
return;
|
2016-07-12 19:02:19 -07:00
|
|
|
}
|
|
|
|
|
2017-04-05 07:09:16 -07:00
|
|
|
/* TODO: Get a human-readable name from the HRTF data (possibly coming in a
|
|
|
|
* format update). */
|
|
|
|
|
2019-11-28 08:24:29 -08:00
|
|
|
std::string newname{dispname};
|
2018-11-18 19:19:35 -08:00
|
|
|
int count{1};
|
2019-11-28 08:24:29 -08:00
|
|
|
while(checkName(newname))
|
2018-11-18 19:19:35 -08:00
|
|
|
{
|
2019-11-28 08:24:29 -08:00
|
|
|
newname = dispname;
|
2018-11-18 19:19:35 -08:00
|
|
|
newname += " #";
|
|
|
|
newname += std::to_string(++count);
|
|
|
|
}
|
2019-12-08 22:09:21 -08:00
|
|
|
EnumeratedHrtfs.emplace_back(HrtfEntry{newname, filename});
|
|
|
|
const HrtfEntry &entry = EnumeratedHrtfs.back();
|
2016-07-12 19:02:19 -07:00
|
|
|
|
2019-11-28 08:24:29 -08:00
|
|
|
TRACE("Adding built-in entry \"%s\"\n", entry.mFilename.c_str());
|
2016-07-12 19:02:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-28 15:47:16 -08:00
|
|
|
#define IDR_DEFAULT_HRTF_MHR 1
|
2016-07-12 19:02:19 -07:00
|
|
|
|
2017-05-05 13:29:52 +03:00
|
|
|
#ifndef ALSOFT_EMBED_HRTF_DATA
|
2016-11-10 21:51:45 -08:00
|
|
|
|
2019-11-28 16:17:00 -08:00
|
|
|
al::span<const char> GetResource(int /*name*/)
|
|
|
|
{ return {}; }
|
2016-11-10 21:51:45 -08:00
|
|
|
|
2016-07-12 19:02:19 -07:00
|
|
|
#else
|
|
|
|
|
2019-11-28 15:47:16 -08:00
|
|
|
#include "hrtf_default.h"
|
2016-07-12 19:02:19 -07:00
|
|
|
|
2019-11-28 16:17:00 -08:00
|
|
|
al::span<const char> GetResource(int name)
|
2016-07-12 19:02:19 -07:00
|
|
|
{
|
2019-11-28 15:47:16 -08:00
|
|
|
if(name == IDR_DEFAULT_HRTF_MHR)
|
|
|
|
return {reinterpret_cast<const char*>(hrtf_default), sizeof(hrtf_default)};
|
2019-11-28 16:17:00 -08:00
|
|
|
return {};
|
2016-07-12 19:02:19 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
2019-11-28 08:24:29 -08:00
|
|
|
al::vector<std::string> EnumerateHrtf(const char *devname)
|
2015-10-06 00:23:11 -07:00
|
|
|
{
|
2019-11-28 08:24:29 -08:00
|
|
|
std::lock_guard<std::mutex> _{EnumeratedHrtfLock};
|
|
|
|
EnumeratedHrtfs.clear();
|
2018-11-11 19:17:40 -08:00
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
bool usedefaults{true};
|
2019-06-30 16:15:15 -07:00
|
|
|
if(auto pathopt = ConfigValueStr(devname, nullptr, "hrtf-paths"))
|
2015-10-06 00:23:11 -07:00
|
|
|
{
|
2019-06-30 16:15:15 -07:00
|
|
|
const char *pathlist{pathopt->c_str()};
|
2016-02-23 10:54:42 -08:00
|
|
|
while(pathlist && *pathlist)
|
2015-10-06 00:23:11 -07:00
|
|
|
{
|
|
|
|
const char *next, *end;
|
|
|
|
|
2016-02-23 10:54:42 -08:00
|
|
|
while(isspace(*pathlist) || *pathlist == ',')
|
|
|
|
pathlist++;
|
|
|
|
if(*pathlist == '\0')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
next = strchr(pathlist, ',');
|
|
|
|
if(next)
|
2015-10-06 00:23:11 -07:00
|
|
|
end = next++;
|
2016-02-23 10:54:42 -08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
end = pathlist + strlen(pathlist);
|
|
|
|
usedefaults = false;
|
|
|
|
}
|
2015-10-06 00:23:11 -07:00
|
|
|
|
2016-02-23 10:54:42 -08:00
|
|
|
while(end != pathlist && isspace(*(end-1)))
|
2015-10-06 00:23:11 -07:00
|
|
|
--end;
|
2016-02-23 10:54:42 -08:00
|
|
|
if(end != pathlist)
|
2015-10-06 00:23:11 -07:00
|
|
|
{
|
2018-11-11 19:17:40 -08:00
|
|
|
const std::string pname{pathlist, end};
|
|
|
|
for(const auto &fname : SearchDataFiles(".mhr", pname.c_str()))
|
2019-11-28 08:24:29 -08:00
|
|
|
AddFileEntry(fname);
|
2015-10-06 00:23:11 -07:00
|
|
|
}
|
|
|
|
|
2016-02-23 10:54:42 -08:00
|
|
|
pathlist = next;
|
2015-10-06 00:23:11 -07:00
|
|
|
}
|
|
|
|
}
|
2016-02-23 10:54:42 -08:00
|
|
|
|
|
|
|
if(usedefaults)
|
|
|
|
{
|
2018-11-11 19:17:40 -08:00
|
|
|
for(const auto &fname : SearchDataFiles(".mhr", "openal/hrtf"))
|
2019-11-28 08:24:29 -08:00
|
|
|
AddFileEntry(fname);
|
2016-07-12 19:02:19 -07:00
|
|
|
|
2019-11-28 15:47:16 -08:00
|
|
|
if(!GetResource(IDR_DEFAULT_HRTF_MHR).empty())
|
|
|
|
AddBuiltInEntry("Built-In HRTF", IDR_DEFAULT_HRTF_MHR);
|
2016-02-23 10:54:42 -08:00
|
|
|
}
|
2015-10-06 00:23:11 -07:00
|
|
|
|
2019-11-28 08:24:29 -08:00
|
|
|
al::vector<std::string> list;
|
|
|
|
list.reserve(EnumeratedHrtfs.size());
|
|
|
|
for(auto &entry : EnumeratedHrtfs)
|
|
|
|
list.emplace_back(entry.mDispName);
|
|
|
|
|
2019-10-28 23:47:30 -07:00
|
|
|
if(auto defhrtfopt = ConfigValueStr(devname, nullptr, "default-hrtf"))
|
2016-02-21 02:44:02 -08:00
|
|
|
{
|
2019-11-28 08:24:29 -08:00
|
|
|
auto iter = std::find(list.begin(), list.end(), *defhrtfopt);
|
2019-10-28 23:47:30 -07:00
|
|
|
if(iter == list.end())
|
|
|
|
WARN("Failed to find default HRTF \"%s\"\n", defhrtfopt->c_str());
|
|
|
|
else if(iter != list.begin())
|
|
|
|
std::rotate(list.begin(), iter, iter+1);
|
2016-02-21 02:44:02 -08:00
|
|
|
}
|
|
|
|
|
2015-10-06 00:23:11 -07:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2019-11-28 14:51:45 -08:00
|
|
|
HrtfStore *GetLoadedHrtf(const std::string &name, const char *devname, const ALuint devrate)
|
2017-04-05 12:27:30 -07:00
|
|
|
{
|
2019-11-28 08:24:29 -08:00
|
|
|
std::lock_guard<std::mutex> _{EnumeratedHrtfLock};
|
|
|
|
auto entry_iter = std::find_if(EnumeratedHrtfs.cbegin(), EnumeratedHrtfs.cend(),
|
|
|
|
[&name](const HrtfEntry &entry) -> bool { return entry.mDispName == name; }
|
|
|
|
);
|
|
|
|
if(entry_iter == EnumeratedHrtfs.cend())
|
|
|
|
return nullptr;
|
|
|
|
const std::string &fname = entry_iter->mFilename;
|
2017-04-05 12:27:30 -07:00
|
|
|
|
2019-11-28 08:24:29 -08:00
|
|
|
std::lock_guard<std::mutex> __{LoadedHrtfLock};
|
2019-11-29 08:41:20 -08:00
|
|
|
auto hrtf_lt_fname = [](LoadedHrtf &hrtf, const std::string &filename) -> bool
|
|
|
|
{ return hrtf.mFilename < filename; };
|
|
|
|
auto handle = std::lower_bound(LoadedHrtfs.begin(), LoadedHrtfs.end(), fname, hrtf_lt_fname);
|
2019-11-29 06:19:06 -08:00
|
|
|
while(handle != LoadedHrtfs.end() && handle->mFilename == fname)
|
2017-04-05 12:27:30 -07:00
|
|
|
{
|
2019-11-29 06:19:06 -08:00
|
|
|
HrtfStore *hrtf{handle->mEntry.get()};
|
|
|
|
if(hrtf && hrtf->sampleRate == devrate)
|
|
|
|
{
|
|
|
|
hrtf->IncRef();
|
|
|
|
return hrtf;
|
|
|
|
}
|
|
|
|
++handle;
|
2017-04-05 12:27:30 -07:00
|
|
|
}
|
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
std::unique_ptr<std::istream> stream;
|
2019-09-13 09:38:35 -07:00
|
|
|
ALint residx{};
|
2018-11-09 23:47:42 -08:00
|
|
|
char ch{};
|
2019-11-28 11:48:44 -08:00
|
|
|
if(sscanf(fname.c_str(), "!%d%c", &residx, &ch) == 2 && ch == '_')
|
2017-04-05 12:27:30 -07:00
|
|
|
{
|
2019-11-28 08:24:29 -08:00
|
|
|
TRACE("Loading %s...\n", fname.c_str());
|
2019-11-28 16:17:00 -08:00
|
|
|
al::span<const char> res{GetResource(residx)};
|
2019-05-23 08:17:05 -07:00
|
|
|
if(res.empty())
|
2017-04-06 01:35:09 -07:00
|
|
|
{
|
2019-11-28 08:24:29 -08:00
|
|
|
ERR("Could not get resource %u, %s\n", residx, name.c_str());
|
2018-11-09 23:47:42 -08:00
|
|
|
return nullptr;
|
2017-04-06 01:35:09 -07:00
|
|
|
}
|
2019-05-23 08:17:05 -07:00
|
|
|
stream = al::make_unique<idstream>(res.begin(), res.end());
|
2017-04-06 01:35:09 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-28 08:24:29 -08:00
|
|
|
TRACE("Loading %s...\n", fname.c_str());
|
|
|
|
auto fstr = al::make_unique<al::ifstream>(fname.c_str(), std::ios::binary);
|
2018-11-09 23:47:42 -08:00
|
|
|
if(!fstr->is_open())
|
2017-04-06 01:35:09 -07:00
|
|
|
{
|
2019-11-28 08:24:29 -08:00
|
|
|
ERR("Could not open %s\n", fname.c_str());
|
2018-11-09 23:47:42 -08:00
|
|
|
return nullptr;
|
2017-04-06 01:35:09 -07:00
|
|
|
}
|
2018-11-09 23:47:42 -08:00
|
|
|
stream = std::move(fstr);
|
2017-04-05 12:27:30 -07:00
|
|
|
}
|
|
|
|
|
2019-11-28 06:10:36 -08:00
|
|
|
std::unique_ptr<HrtfStore> hrtf;
|
2018-11-09 23:47:42 -08:00
|
|
|
char magic[sizeof(magicMarker02)];
|
|
|
|
stream->read(magic, sizeof(magic));
|
|
|
|
if(stream->gcount() < static_cast<std::streamsize>(sizeof(magicMarker02)))
|
2019-11-28 08:24:29 -08:00
|
|
|
ERR("%s data is too short (%zu bytes)\n", name.c_str(), stream->gcount());
|
2018-11-09 23:47:42 -08:00
|
|
|
else if(memcmp(magic, magicMarker02, sizeof(magicMarker02)) == 0)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
|
|
|
TRACE("Detected data set format v2\n");
|
2019-11-28 08:24:29 -08:00
|
|
|
hrtf = LoadHrtf02(*stream, name.c_str());
|
2017-08-08 20:25:31 -07:00
|
|
|
}
|
2018-11-09 23:47:42 -08:00
|
|
|
else if(memcmp(magic, magicMarker01, sizeof(magicMarker01)) == 0)
|
2017-04-05 12:27:30 -07:00
|
|
|
{
|
|
|
|
TRACE("Detected data set format v1\n");
|
2019-11-28 08:24:29 -08:00
|
|
|
hrtf = LoadHrtf01(*stream, name.c_str());
|
2017-04-05 12:27:30 -07:00
|
|
|
}
|
2018-11-09 23:47:42 -08:00
|
|
|
else if(memcmp(magic, magicMarker00, sizeof(magicMarker00)) == 0)
|
2017-04-05 12:27:30 -07:00
|
|
|
{
|
|
|
|
TRACE("Detected data set format v0\n");
|
2019-11-28 08:24:29 -08:00
|
|
|
hrtf = LoadHrtf00(*stream, name.c_str());
|
2017-04-05 12:27:30 -07:00
|
|
|
}
|
|
|
|
else
|
2019-11-28 08:24:29 -08:00
|
|
|
ERR("Invalid header in %s: \"%.8s\"\n", name.c_str(), magic);
|
2018-11-09 23:47:42 -08:00
|
|
|
stream.reset();
|
2017-04-05 12:27:30 -07:00
|
|
|
|
|
|
|
if(!hrtf)
|
2018-11-09 23:47:42 -08:00
|
|
|
{
|
2019-11-28 08:24:29 -08:00
|
|
|
ERR("Failed to load %s\n", name.c_str());
|
2019-02-01 20:59:31 -08:00
|
|
|
return nullptr;
|
2017-04-05 12:27:30 -07:00
|
|
|
}
|
|
|
|
|
2019-11-28 11:48:44 -08:00
|
|
|
if(hrtf->sampleRate != devrate)
|
|
|
|
{
|
|
|
|
/* Calculate the last elevation's index and get the total IR count. */
|
|
|
|
const size_t lastEv{std::accumulate(hrtf->field, hrtf->field+hrtf->fdCount, size_t{0},
|
|
|
|
[](const size_t curval, const HrtfStore::Field &field) noexcept -> size_t
|
|
|
|
{ return curval + field.evCount; }
|
|
|
|
) - 1};
|
|
|
|
const size_t irCount{size_t{hrtf->elev[lastEv].irOffset} + hrtf->elev[lastEv].azCount};
|
|
|
|
|
|
|
|
/* Resample all the IRs. */
|
|
|
|
std::array<std::array<double,HRIR_LENGTH>,2> inout;
|
|
|
|
PPhaseResampler rs;
|
|
|
|
rs.init(hrtf->sampleRate, devrate);
|
|
|
|
for(size_t i{0};i < irCount;++i)
|
|
|
|
{
|
2019-11-28 14:51:45 -08:00
|
|
|
HrirArray &coeffs = const_cast<HrirArray&>(hrtf->coeffs[i]);
|
2019-11-28 11:48:44 -08:00
|
|
|
for(size_t j{0};j < 2;++j)
|
|
|
|
{
|
|
|
|
std::transform(coeffs.cbegin(), coeffs.cend(), inout[0].begin(),
|
|
|
|
[j](const float2 &in) noexcept -> double { return in[j]; });
|
|
|
|
rs.process(HRIR_LENGTH, inout[0].data(), HRIR_LENGTH, inout[1].data());
|
|
|
|
for(size_t k{0};k < HRIR_LENGTH;++k)
|
|
|
|
coeffs[k][j] = static_cast<float>(inout[1][k]);
|
|
|
|
}
|
2019-11-29 18:19:01 -08:00
|
|
|
}
|
|
|
|
rs = {};
|
2019-11-28 14:53:13 -08:00
|
|
|
|
2019-11-29 18:19:01 -08:00
|
|
|
const ALuint srate{hrtf->sampleRate};
|
|
|
|
for(size_t i{0};i < irCount;++i)
|
|
|
|
{
|
|
|
|
for(ALubyte &delay : const_cast<ALubyte(&)[2]>(hrtf->delays[i]))
|
|
|
|
delay = static_cast<ALubyte>(minu64(MAX_HRIR_DELAY*HRIR_DELAY_FRACONE,
|
|
|
|
(uint64_t{delay}*devrate + srate/2) / srate));
|
2019-11-28 11:48:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Scale the IR size for the new sample rate and update the stored
|
|
|
|
* sample rate.
|
|
|
|
*/
|
2019-11-29 18:19:01 -08:00
|
|
|
uint64_t newIrSize{(uint64_t{hrtf->irSize}*devrate + srate-1) / srate};
|
|
|
|
newIrSize = minu64(HRIR_LENGTH, newIrSize) + (MOD_IR_SIZE-1);
|
|
|
|
hrtf->irSize = static_cast<ALuint>(newIrSize - (newIrSize%MOD_IR_SIZE));
|
2019-11-28 11:48:44 -08:00
|
|
|
hrtf->sampleRate = devrate;
|
|
|
|
}
|
|
|
|
|
2019-11-28 14:51:45 -08:00
|
|
|
if(auto hrtfsizeopt = ConfigValueUInt(devname, nullptr, "hrtf-size"))
|
|
|
|
{
|
|
|
|
if(*hrtfsizeopt > 0 && *hrtfsizeopt < hrtf->irSize)
|
|
|
|
{
|
|
|
|
hrtf->irSize = maxu(*hrtfsizeopt, MIN_IR_SIZE);
|
|
|
|
hrtf->irSize -= hrtf->irSize % MOD_IR_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:48:44 -08:00
|
|
|
TRACE("Loaded HRTF %s for sample rate %uhz, %u-sample filter\n", name.c_str(),
|
|
|
|
hrtf->sampleRate, hrtf->irSize);
|
2019-11-29 06:19:06 -08:00
|
|
|
handle = LoadedHrtfs.emplace(handle, LoadedHrtf{fname, std::move(hrtf)});
|
2019-02-01 20:59:31 -08:00
|
|
|
|
2019-11-29 06:19:06 -08:00
|
|
|
return handle->mEntry.get();
|
2017-04-05 12:27:30 -07:00
|
|
|
}
|
|
|
|
|
2015-10-06 00:23:11 -07:00
|
|
|
|
2019-11-28 06:10:36 -08:00
|
|
|
void HrtfStore::IncRef()
|
2017-04-06 13:00:29 -07:00
|
|
|
{
|
2019-09-14 18:27:57 -07:00
|
|
|
auto ref = IncrementRef(mRef);
|
2019-09-18 10:09:04 -07:00
|
|
|
TRACE("HrtfEntry %p increasing refcount to %u\n", decltype(std::declval<void*>()){this}, ref);
|
2017-04-06 13:00:29 -07:00
|
|
|
}
|
|
|
|
|
2019-11-28 06:10:36 -08:00
|
|
|
void HrtfStore::DecRef()
|
2017-04-06 13:00:29 -07:00
|
|
|
{
|
2019-09-14 18:27:57 -07:00
|
|
|
auto ref = DecrementRef(mRef);
|
2019-09-18 10:09:04 -07:00
|
|
|
TRACE("HrtfEntry %p decreasing refcount to %u\n", decltype(std::declval<void*>()){this}, ref);
|
2017-04-06 13:00:29 -07:00
|
|
|
if(ref == 0)
|
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
std::lock_guard<std::mutex> _{LoadedHrtfLock};
|
2017-04-06 13:00:29 -07:00
|
|
|
|
2019-11-28 08:24:29 -08:00
|
|
|
/* Go through and remove all unused HRTFs. */
|
|
|
|
auto remove_unused = [](LoadedHrtf &hrtf) -> bool
|
2017-04-06 13:00:29 -07:00
|
|
|
{
|
2019-11-28 08:24:29 -08:00
|
|
|
HrtfStore *entry{hrtf.mEntry.get()};
|
2019-09-14 18:27:57 -07:00
|
|
|
if(entry && ReadRef(entry->mRef) == 0)
|
2019-06-24 12:55:36 -07:00
|
|
|
{
|
2019-11-28 08:24:29 -08:00
|
|
|
TRACE("Unloading unused HRTF %s\n", hrtf.mFilename.data());
|
|
|
|
hrtf.mEntry = nullptr;
|
|
|
|
return true;
|
2019-06-24 12:55:36 -07:00
|
|
|
}
|
2019-11-28 08:24:29 -08:00
|
|
|
return false;
|
2019-06-24 12:55:36 -07:00
|
|
|
};
|
2019-11-28 08:24:29 -08:00
|
|
|
auto iter = std::remove_if(LoadedHrtfs.begin(), LoadedHrtfs.end(), remove_unused);
|
|
|
|
LoadedHrtfs.erase(iter, LoadedHrtfs.end());
|
2017-04-06 13:00:29 -07:00
|
|
|
}
|
|
|
|
}
|