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"
|
|
|
|
|
2011-09-18 10:09:32 -07:00
|
|
|
#include <stdlib.h>
|
2011-09-18 11:10:32 -07:00
|
|
|
#include <ctype.h>
|
2011-09-18 10:09:32 -07:00
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
#include <mutex>
|
2018-11-09 18:08:42 -08:00
|
|
|
#include <array>
|
|
|
|
#include <vector>
|
2018-11-09 23:47:42 -08:00
|
|
|
#include <memory>
|
|
|
|
#include <istream>
|
2018-11-09 18:08:42 -08:00
|
|
|
#include <algorithm>
|
|
|
|
|
2011-05-01 13:59:44 -07:00
|
|
|
#include "AL/al.h"
|
|
|
|
#include "AL/alc.h"
|
|
|
|
#include "alMain.h"
|
2011-06-03 01:06:00 -07:00
|
|
|
#include "alSource.h"
|
2012-09-11 01:59:42 -07:00
|
|
|
#include "alu.h"
|
2014-02-23 21:11:01 -08:00
|
|
|
#include "hrtf.h"
|
2018-01-11 07:56:54 -08:00
|
|
|
#include "alconfig.h"
|
2019-01-06 00:23:15 -08:00
|
|
|
#include "filters/biquad.h"
|
2011-05-01 13:59:44 -07:00
|
|
|
|
2015-10-06 04:01:53 -07:00
|
|
|
#include "compat.h"
|
2016-07-07 10:31:43 -07:00
|
|
|
#include "almalloc.h"
|
2015-10-06 04:01:53 -07:00
|
|
|
|
2012-09-12 03:45:26 -07:00
|
|
|
|
2018-12-22 09:20:50 -08:00
|
|
|
struct HrtfHandle {
|
|
|
|
HrtfEntry *entry{nullptr};
|
2018-11-09 18:08:42 -08:00
|
|
|
char filename[];
|
2018-12-09 03:04:18 -08:00
|
|
|
|
|
|
|
DEF_PLACE_NEWDEL()
|
2018-11-09 18:08:42 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-12-22 09:20:50 -08:00
|
|
|
using HrtfHandlePtr = std::unique_ptr<HrtfHandle>;
|
2018-12-09 03:04:18 -08:00
|
|
|
|
2012-09-11 01:59:42 -07:00
|
|
|
/* Current data set limits defined by the makehrtf utility. */
|
|
|
|
#define MIN_IR_SIZE (8)
|
2017-07-25 16:17:46 -07:00
|
|
|
#define MAX_IR_SIZE (512)
|
2012-09-11 01:59:42 -07:00
|
|
|
#define MOD_IR_SIZE (8)
|
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)
|
|
|
|
|
|
|
|
#define MIN_FD_DISTANCE (50)
|
|
|
|
#define MAX_FD_DISTANCE (2500)
|
|
|
|
|
2012-09-11 01:59:42 -07:00
|
|
|
#define MIN_EV_COUNT (5)
|
|
|
|
#define MAX_EV_COUNT (128)
|
2011-07-16 00:22:01 -07:00
|
|
|
|
2012-09-11 01:59:42 -07:00
|
|
|
#define MIN_AZ_COUNT (1)
|
|
|
|
#define MAX_AZ_COUNT (128)
|
2011-05-01 13:59:44 -07:00
|
|
|
|
2017-10-22 15:36:42 -07:00
|
|
|
#define MAX_HRIR_DELAY (HRTF_HISTORY_LENGTH-1)
|
|
|
|
|
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;
|
2018-12-22 09:20:50 -08:00
|
|
|
al::vector<HrtfHandlePtr> LoadedHrtfs;
|
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:
|
|
|
|
databuf(const char_type *start, const char_type *end) noexcept
|
|
|
|
{
|
|
|
|
setg(const_cast<char_type*>(start), const_cast<char_type*>(start),
|
|
|
|
const_cast<char_type*>(end));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class idstream final : public std::istream {
|
|
|
|
databuf mStreamBuf;
|
|
|
|
|
|
|
|
public:
|
|
|
|
idstream(const char *start, const char *end)
|
|
|
|
: std::istream{nullptr}, mStreamBuf{start, end}
|
|
|
|
{ init(&mStreamBuf); }
|
|
|
|
};
|
2011-09-18 10:09:32 -07:00
|
|
|
|
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
|
|
|
*/
|
2018-11-09 18:08:42 -08:00
|
|
|
ALsizei CalcEvIndex(ALsizei evcount, ALfloat ev, ALfloat *mu)
|
2011-05-01 13:59:44 -07:00
|
|
|
{
|
2017-05-01 15:46:25 -07:00
|
|
|
ev = (F_PI_2+ev) * (evcount-1) / F_PI;
|
2018-11-09 23:47:42 -08:00
|
|
|
ALsizei idx{float2int(ev)};
|
2017-05-01 15:46:25 -07:00
|
|
|
|
|
|
|
*mu = ev - idx;
|
2018-05-03 21:43:53 -07:00
|
|
|
return mini(idx, evcount-1);
|
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
|
|
|
*/
|
2018-11-09 18:08:42 -08:00
|
|
|
ALsizei CalcAzIndex(ALsizei azcount, ALfloat az, ALfloat *mu)
|
2011-06-03 01:06:00 -07:00
|
|
|
{
|
2017-05-01 15:46:25 -07:00
|
|
|
az = (F_TAU+az) * azcount / F_TAU;
|
2018-11-09 23:47:42 -08:00
|
|
|
ALsizei idx{float2int(az)};
|
2017-05-01 15:46:25 -07:00
|
|
|
|
2018-05-03 21:43:53 -07:00
|
|
|
*mu = az - idx;
|
|
|
|
return idx % azcount;
|
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
|
|
|
*/
|
2018-12-22 09:20:50 -08:00
|
|
|
void GetHrtfCoeffs(const HrtfEntry *Hrtf, ALfloat elevation, ALfloat azimuth, ALfloat spread,
|
2018-10-29 11:32:50 -07:00
|
|
|
ALfloat (*RESTRICT coeffs)[2], ALsizei *delays)
|
2011-05-01 13:59:44 -07:00
|
|
|
{
|
2018-12-21 21:07:42 -08:00
|
|
|
const ALfloat dirfact{1.0f - (spread / F_TAU)};
|
2016-04-24 21:42:59 -07:00
|
|
|
|
2017-05-01 15:46:25 -07:00
|
|
|
/* Claculate the lower elevation index. */
|
2018-11-09 23:47:42 -08:00
|
|
|
ALfloat emu;
|
|
|
|
ALsizei evidx{CalcEvIndex(Hrtf->evCount, elevation, &emu)};
|
|
|
|
ALsizei evoffset{Hrtf->evOffset[evidx]};
|
2011-06-03 01:06:00 -07:00
|
|
|
|
2017-05-01 15:46:25 -07:00
|
|
|
/* Calculate lower azimuth index. */
|
2018-11-09 23:47:42 -08:00
|
|
|
ALfloat amu[2];
|
|
|
|
ALsizei azidx{CalcAzIndex(Hrtf->azCount[evidx], azimuth, &amu[0])};
|
2011-07-04 07:14:45 -07:00
|
|
|
|
2017-05-01 15:46:25 -07:00
|
|
|
/* Calculate the lower HRIR indices. */
|
2018-11-09 23:47:42 -08:00
|
|
|
ALsizei idx[4]{
|
|
|
|
evoffset + azidx,
|
|
|
|
evoffset + ((azidx+1) % Hrtf->azCount[evidx])
|
|
|
|
};
|
2017-05-01 15:46:25 -07:00
|
|
|
if(evidx < Hrtf->evCount-1)
|
|
|
|
{
|
|
|
|
/* Increment elevation to the next (upper) index. */
|
|
|
|
evidx++;
|
|
|
|
evoffset = Hrtf->evOffset[evidx];
|
2012-08-11 02:16:34 -07:00
|
|
|
|
2017-05-01 15:46:25 -07:00
|
|
|
/* Calculate upper azimuth index. */
|
|
|
|
azidx = CalcAzIndex(Hrtf->azCount[evidx], azimuth, &amu[1]);
|
2012-09-11 01:59:42 -07:00
|
|
|
|
2017-05-01 15:46:25 -07:00
|
|
|
/* Calculate the upper HRIR indices. */
|
|
|
|
idx[2] = evoffset + azidx;
|
|
|
|
idx[3] = evoffset + ((azidx+1) % Hrtf->azCount[evidx]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If the lower elevation is the top index, the upper elevation is the
|
|
|
|
* same as the lower.
|
|
|
|
*/
|
|
|
|
amu[1] = amu[0];
|
|
|
|
idx[2] = idx[0];
|
|
|
|
idx[3] = idx[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate bilinear blending weights, attenuated according to the
|
|
|
|
* directional panning factor.
|
|
|
|
*/
|
2018-12-21 21:07:42 -08:00
|
|
|
const ALfloat blend[4]{
|
2018-11-09 23:47:42 -08:00
|
|
|
(1.0f-emu) * (1.0f-amu[0]) * dirfact,
|
|
|
|
(1.0f-emu) * ( amu[0]) * dirfact,
|
|
|
|
( emu) * (1.0f-amu[1]) * dirfact,
|
|
|
|
( emu) * ( amu[1]) * dirfact
|
|
|
|
};
|
2017-05-01 15:46:25 -07:00
|
|
|
|
|
|
|
/* Calculate the blended HRIR delays. */
|
2018-05-12 21:36:23 -07:00
|
|
|
delays[0] = fastf2i(
|
2017-05-01 15:46:25 -07:00
|
|
|
Hrtf->delays[idx[0]][0]*blend[0] + Hrtf->delays[idx[1]][0]*blend[1] +
|
2018-05-12 21:36:23 -07:00
|
|
|
Hrtf->delays[idx[2]][0]*blend[2] + Hrtf->delays[idx[3]][0]*blend[3]
|
2017-05-01 15:46:25 -07:00
|
|
|
);
|
2018-05-12 21:36:23 -07:00
|
|
|
delays[1] = fastf2i(
|
2017-05-01 15:46:25 -07:00
|
|
|
Hrtf->delays[idx[0]][1]*blend[0] + Hrtf->delays[idx[1]][1]*blend[1] +
|
2018-05-12 21:36:23 -07:00
|
|
|
Hrtf->delays[idx[2]][1]*blend[2] + Hrtf->delays[idx[3]][1]*blend[3]
|
2017-05-01 15:46:25 -07:00
|
|
|
);
|
2016-10-09 00:37:47 -07:00
|
|
|
|
2018-12-21 21:07:42 -08:00
|
|
|
const ALsizei irSize{Hrtf->irSize};
|
|
|
|
ASSUME(irSize >= MIN_IR_SIZE);
|
2017-05-01 15:46:25 -07:00
|
|
|
|
2018-12-21 21:07:42 -08:00
|
|
|
/* Calculate the sample offsets for the HRIR indices. */
|
|
|
|
idx[0] *= irSize;
|
|
|
|
idx[1] *= irSize;
|
|
|
|
idx[2] *= irSize;
|
|
|
|
idx[3] *= irSize;
|
2018-11-09 18:08:42 -08:00
|
|
|
|
2017-05-01 15:46:25 -07:00
|
|
|
/* Calculate the blended HRIR coefficients. */
|
2018-12-21 21:07:42 -08:00
|
|
|
ALfloat *coeffout{al::assume_aligned<16>(coeffs[0])};
|
|
|
|
coeffout[0] = PassthruCoeff * (1.0f-dirfact);
|
|
|
|
coeffout[1] = PassthruCoeff * (1.0f-dirfact);
|
|
|
|
std::fill(coeffout+2, coeffout + irSize*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
|
|
|
{
|
2018-12-21 21:07:42 -08:00
|
|
|
const ALfloat *srccoeffs{al::assume_aligned<16>(Hrtf->coeffs[idx[c]])};
|
|
|
|
const ALfloat mult{blend[c]};
|
|
|
|
auto blend_coeffs = [mult](const ALfloat src, const ALfloat coeff) noexcept -> ALfloat
|
|
|
|
{ return src*mult + coeff; };
|
|
|
|
std::transform<const ALfloat*RESTRICT>(srccoeffs, srccoeffs + irSize*2, coeffout,
|
|
|
|
coeffout, blend_coeffs);
|
2014-11-23 10:49:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-09 15:59:29 -08:00
|
|
|
|
2018-12-22 09:20:50 -08:00
|
|
|
void BuildBFormatHrtf(const HrtfEntry *Hrtf, DirectHrtfState *state, const ALsizei NumChannels, const AngularPoint *AmbiPoints, const ALfloat (*RESTRICT AmbiMatrix)[MAX_AMBI_COEFFS], const ALsizei AmbiCount, const ALfloat *RESTRICT 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
|
|
|
{
|
2018-12-21 18:17:59 -08:00
|
|
|
static constexpr int OrderFromChan[MAX_AMBI_COEFFS]{
|
|
|
|
0, 1,1,1, 2,2,2,2,2, 3,3,3,3,3,3,3,
|
|
|
|
};
|
2019-01-06 00:23:15 -08:00
|
|
|
/* Set this to true for dual-band HRTF processing. May require a higher
|
|
|
|
* quality filter, or better calculation of the new IR length to deal with
|
|
|
|
* the tail generated by the filter.
|
|
|
|
*/
|
|
|
|
static constexpr bool DualBand{true};
|
2018-12-21 18:17:59 -08:00
|
|
|
|
|
|
|
ASSUME(NumChannels > 0);
|
|
|
|
ASSUME(AmbiCount > 0);
|
|
|
|
|
2018-11-09 18:08:42 -08:00
|
|
|
ALsizei min_delay{HRTF_HISTORY_LENGTH};
|
|
|
|
ALsizei max_delay{0};
|
2018-11-24 16:58:49 -08:00
|
|
|
al::vector<ALsizei> idx(AmbiCount);
|
2018-12-21 18:17:59 -08:00
|
|
|
auto calc_idxs = [Hrtf,&max_delay,&min_delay](const AngularPoint &pt) noexcept -> ALsizei
|
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
|
|
|
{
|
2016-08-17 05:34:09 -07:00
|
|
|
/* Calculate elevation index. */
|
2018-12-21 08:55:22 -08:00
|
|
|
const auto evidx = clampi(
|
2018-12-21 18:17:59 -08:00
|
|
|
static_cast<ALsizei>((90.0f+pt.Elev)*(Hrtf->evCount-1)/180.0f + 0.5f),
|
2018-12-21 08:55:22 -08:00
|
|
|
0, Hrtf->evCount-1);
|
2016-08-17 05:34:09 -07:00
|
|
|
|
2018-12-21 08:55:22 -08:00
|
|
|
const ALsizei azcount{Hrtf->azCount[evidx]};
|
|
|
|
const ALsizei evoffset{Hrtf->evOffset[evidx]};
|
2016-08-17 05:34:09 -07:00
|
|
|
|
|
|
|
/* Calculate azimuth index for this elevation. */
|
2018-12-21 18:17:59 -08:00
|
|
|
const auto azidx = static_cast<ALsizei>((360.0f+pt.Azim)*azcount/360.0f + 0.5f) % azcount;
|
2016-08-17 05:34:09 -07:00
|
|
|
|
2018-12-21 18:17:59 -08:00
|
|
|
/* Calculate the index for the impulse response. */
|
|
|
|
ALsizei idx{evoffset + azidx};
|
2018-04-16 18:51:01 -07:00
|
|
|
|
2018-12-21 18:17:59 -08:00
|
|
|
min_delay = mini(min_delay, mini(Hrtf->delays[idx][0], Hrtf->delays[idx][1]));
|
|
|
|
max_delay = maxi(max_delay, maxi(Hrtf->delays[idx][0], Hrtf->delays[idx][1]));
|
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-12-21 18:17:59 -08:00
|
|
|
return idx;
|
|
|
|
};
|
|
|
|
std::transform(AmbiPoints, AmbiPoints+AmbiCount, idx.begin(), calc_idxs);
|
2018-04-17 21:27:47 -07:00
|
|
|
|
2019-01-06 00:54:39 -08:00
|
|
|
const ALdouble xover_norm{400.0 / Hrtf->sampleRate};
|
2019-01-06 00:23:15 -08:00
|
|
|
const ALsizei order_limit{OrderFromChan[NumChannels-1] + 1};
|
2019-01-06 00:54:39 -08:00
|
|
|
BiquadFilterR<double> shelf[MAX_AMBI_ORDER+1];
|
2019-01-06 00:23:15 -08:00
|
|
|
for(ALsizei o{0};o < order_limit;++o)
|
|
|
|
{
|
2019-01-06 00:54:39 -08:00
|
|
|
const auto g = std::sqrt(double{AmbiOrderHFGain[o]});
|
|
|
|
shelf[o].setParams(BiquadType::HighShelf, g, xover_norm, calc_rcpQ_from_slope(g, 1.0));
|
2019-01-06 00:23:15 -08:00
|
|
|
}
|
|
|
|
|
2018-12-21 18:17:59 -08:00
|
|
|
al::vector<std::array<std::array<ALdouble,2>,HRIR_LENGTH>> tmpres(NumChannels);
|
2019-01-06 00:54:39 -08:00
|
|
|
al::vector<std::array<ALdouble,HRIR_LENGTH>> tmpfilt(order_limit+1);
|
2018-11-09 18:08:42 -08:00
|
|
|
for(ALsizei c{0};c < AmbiCount;++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
|
|
|
{
|
2018-12-21 18:17:59 -08:00
|
|
|
const ALfloat (*fir)[2]{&Hrtf->coeffs[idx[c] * Hrtf->irSize]};
|
2019-01-06 00:23:15 -08:00
|
|
|
const ALsizei ldelay{Hrtf->delays[idx[c]][0] - min_delay};
|
|
|
|
const ALsizei rdelay{Hrtf->delays[idx[c]][1] - min_delay};
|
2017-04-07 11:49:24 -07:00
|
|
|
|
2019-01-06 00:23:15 -08:00
|
|
|
if(!DualBand)
|
2016-08-30 22:33:33 -07:00
|
|
|
{
|
2018-11-09 18:08:42 -08:00
|
|
|
for(ALsizei i{0};i < NumChannels;++i)
|
2017-03-31 04:25:22 -07:00
|
|
|
{
|
2018-12-21 18:17:59 -08:00
|
|
|
const ALdouble mult{(ALdouble)AmbiOrderHFGain[OrderFromChan[i]] * AmbiMatrix[c][i]};
|
|
|
|
const ALsizei numirs{mini(Hrtf->irSize, HRIR_LENGTH-maxi(ldelay, rdelay))};
|
|
|
|
ALsizei lidx{ldelay}, ridx{rdelay};
|
|
|
|
for(ALsizei j{0};j < numirs;++j)
|
2017-04-07 11:49:24 -07:00
|
|
|
{
|
2018-04-17 21:27:47 -07:00
|
|
|
tmpres[i][lidx++][0] += fir[j][0] * mult;
|
|
|
|
tmpres[i][ridx++][1] += fir[j][1] * mult;
|
2017-04-07 11:49:24 -07:00
|
|
|
}
|
2017-03-31 04:25:22 -07:00
|
|
|
}
|
2016-08-30 22:33:33 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-06 00:23:15 -08:00
|
|
|
/* Extract the left HRIR and increase its per-order high-frequency
|
|
|
|
* response.
|
|
|
|
*/
|
|
|
|
auto tmpfilt_iter = std::transform(fir, fir+Hrtf->irSize, tmpfilt.back().begin(),
|
2018-12-21 18:17:59 -08:00
|
|
|
[](const ALfloat (&ir)[2]) noexcept { return ir[0]; });
|
2019-01-06 00:54:39 -08:00
|
|
|
std::fill(tmpfilt_iter, tmpfilt.back().end(), 0.0);
|
2019-01-06 00:23:15 -08:00
|
|
|
for(ALsizei o{0};o < order_limit;++o)
|
|
|
|
{
|
|
|
|
shelf[o].clear();
|
|
|
|
shelf[o].process(tmpfilt[o].data(), tmpfilt.back().data(), HRIR_LENGTH);
|
|
|
|
}
|
2016-08-21 03:05:42 -07:00
|
|
|
|
2017-04-07 11:49:24 -07:00
|
|
|
/* Apply left ear response with delay. */
|
2018-11-09 18:08:42 -08:00
|
|
|
for(ALsizei i{0};i < NumChannels;++i)
|
2016-08-21 03:05:42 -07:00
|
|
|
{
|
2019-01-06 00:23:15 -08:00
|
|
|
const ALsizei order{OrderFromChan[i]};
|
|
|
|
const ALdouble mult{AmbiMatrix[c][i]};
|
|
|
|
for(ALsizei lidx{ldelay},j{0};lidx < HRIR_LENGTH;++lidx,++j)
|
|
|
|
tmpres[i][lidx][0] += tmpfilt[order][j] * mult;
|
2016-08-21 03:05:42 -07:00
|
|
|
}
|
2016-08-17 05:34:09 -07:00
|
|
|
|
2019-01-06 00:23:15 -08:00
|
|
|
/* Extract the right HRIR and increase its per-order high-frequency
|
|
|
|
* response.
|
|
|
|
*/
|
|
|
|
tmpfilt_iter = std::transform(fir, fir+Hrtf->irSize, tmpfilt.back().begin(),
|
2018-12-21 18:17:59 -08:00
|
|
|
[](const ALfloat (&ir)[2]) noexcept { return ir[1]; });
|
2019-01-06 00:54:39 -08:00
|
|
|
std::fill(tmpfilt_iter, tmpfilt.back().end(), 0.0);
|
2019-01-06 00:23:15 -08:00
|
|
|
for(ALsizei o{0};o < order_limit;++o)
|
|
|
|
{
|
|
|
|
shelf[o].clear();
|
|
|
|
shelf[o].process(tmpfilt[o].data(), tmpfilt.back().data(), HRIR_LENGTH);
|
|
|
|
}
|
2016-08-21 03:05:42 -07:00
|
|
|
|
2017-04-07 11:49:24 -07:00
|
|
|
/* Apply right ear response with delay. */
|
2018-11-09 18:08:42 -08:00
|
|
|
for(ALsizei i{0};i < NumChannels;++i)
|
2016-08-21 03:05:42 -07:00
|
|
|
{
|
2019-01-06 00:23:15 -08:00
|
|
|
const ALsizei order{OrderFromChan[i]};
|
|
|
|
const ALdouble mult{AmbiMatrix[c][i]};
|
|
|
|
for(ALsizei ridx{rdelay},j{0};ridx < HRIR_LENGTH;++ridx,++j)
|
|
|
|
tmpres[i][ridx][1] += tmpfilt[order][j] * mult;
|
2016-08-21 03:05:42 -07:00
|
|
|
}
|
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-01-06 00:23:15 -08:00
|
|
|
tmpfilt.clear();
|
2016-08-17 05:34:09 -07:00
|
|
|
|
2018-11-09 18:08:42 -08:00
|
|
|
for(ALsizei i{0};i < NumChannels;++i)
|
2018-04-17 21:27:47 -07:00
|
|
|
{
|
2018-11-09 18:08:42 -08:00
|
|
|
for(ALsizei idx{0};idx < HRIR_LENGTH;idx++)
|
2018-04-17 21:27:47 -07:00
|
|
|
{
|
|
|
|
state->Chan[i].Coeffs[idx][0] = (ALfloat)tmpres[i][idx][0];
|
|
|
|
state->Chan[i].Coeffs[idx][1] = (ALfloat)tmpres[i][idx][1];
|
|
|
|
}
|
|
|
|
}
|
2018-11-09 18:08:42 -08:00
|
|
|
tmpres.clear();
|
|
|
|
idx.clear();
|
2018-04-17 21:27:47 -07:00
|
|
|
|
2018-11-09 18:08:42 -08:00
|
|
|
ALsizei max_length;
|
2019-01-06 00:23:15 -08:00
|
|
|
if(!DualBand)
|
2018-05-04 06:48:20 -07:00
|
|
|
max_length = mini(max_delay-min_delay + Hrtf->irSize, HRIR_LENGTH);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Increase the IR size by 2/3rds to account for the tail generated by
|
2019-01-06 00:23:15 -08:00
|
|
|
* the filter.
|
2018-05-04 06:48:20 -07:00
|
|
|
*/
|
|
|
|
const ALsizei irsize = mini(Hrtf->irSize*5/3, HRIR_LENGTH);
|
|
|
|
max_length = mini(max_delay-min_delay + irsize, HRIR_LENGTH);
|
|
|
|
}
|
|
|
|
/* Round up to the next IR size multiple. */
|
|
|
|
max_length += MOD_IR_SIZE-1;
|
|
|
|
max_length -= max_length%MOD_IR_SIZE;
|
|
|
|
|
|
|
|
TRACE("Skipped delay: %d, max delay: %d, new FIR length: %d\n",
|
|
|
|
min_delay, max_delay-min_delay, 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 {
|
|
|
|
|
2018-12-22 09:20:50 -08:00
|
|
|
HrtfEntry *CreateHrtfStore(ALuint rate, ALsizei irSize, ALfloat distance, ALsizei evCount,
|
2018-11-09 23:47:42 -08:00
|
|
|
ALsizei irCount, const ALubyte *azCount, const ALushort *evOffset, const ALfloat (*coeffs)[2],
|
|
|
|
const ALubyte (*delays)[2], const char *filename)
|
2017-03-31 03:45:26 -07:00
|
|
|
{
|
2018-12-22 09:20:50 -08:00
|
|
|
HrtfEntry *Hrtf;
|
2017-03-31 03:45:26 -07:00
|
|
|
size_t total;
|
|
|
|
|
2018-12-22 09:20:50 -08:00
|
|
|
total = sizeof(HrtfEntry);
|
2017-03-31 03:45:26 -07:00
|
|
|
total += sizeof(Hrtf->azCount[0])*evCount;
|
2017-03-31 04:25:22 -07:00
|
|
|
total = RoundUp(total, sizeof(ALushort)); /* Align for ushort fields */
|
2017-03-31 03:45:26 -07:00
|
|
|
total += sizeof(Hrtf->evOffset[0])*evCount;
|
2017-04-07 08:46:50 -07:00
|
|
|
total = RoundUp(total, 16); /* Align for coefficients using SIMD */
|
2017-03-31 03:45:26 -07:00
|
|
|
total += sizeof(Hrtf->coeffs[0])*irSize*irCount;
|
|
|
|
total += sizeof(Hrtf->delays[0])*irCount;
|
|
|
|
|
2018-12-22 09:20:50 -08:00
|
|
|
Hrtf = static_cast<HrtfEntry*>(al_calloc(16, total));
|
2018-11-09 23:47:42 -08:00
|
|
|
if(Hrtf == nullptr)
|
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
|
|
|
|
{
|
2018-12-22 09:20:50 -08:00
|
|
|
uintptr_t offset = sizeof(HrtfEntry);
|
2017-03-31 03:45:26 -07:00
|
|
|
char *base = (char*)Hrtf;
|
|
|
|
ALushort *_evOffset;
|
|
|
|
ALubyte *_azCount;
|
2017-04-07 08:46:50 -07:00
|
|
|
ALubyte (*_delays)[2];
|
|
|
|
ALfloat (*_coeffs)[2];
|
2017-03-31 03:45:26 -07:00
|
|
|
ALsizei i;
|
|
|
|
|
2017-04-06 13:00:29 -07:00
|
|
|
InitRef(&Hrtf->ref, 0);
|
2017-03-31 03:45:26 -07:00
|
|
|
Hrtf->sampleRate = rate;
|
|
|
|
Hrtf->irSize = irSize;
|
2017-10-23 13:26:35 -07:00
|
|
|
Hrtf->distance = distance;
|
2017-03-31 03:45:26 -07:00
|
|
|
Hrtf->evCount = evCount;
|
|
|
|
|
|
|
|
/* Set up pointers to storage following the main HRTF struct. */
|
2018-11-09 18:08:42 -08:00
|
|
|
_azCount = reinterpret_cast<ALubyte*>(base + offset);
|
2017-03-31 03:45:26 -07:00
|
|
|
offset += sizeof(_azCount[0])*evCount;
|
|
|
|
|
2017-03-31 04:25:22 -07:00
|
|
|
offset = RoundUp(offset, sizeof(ALushort)); /* Align for ushort fields */
|
2018-11-09 18:08:42 -08:00
|
|
|
_evOffset = reinterpret_cast<ALushort*>(base + offset);
|
2017-03-31 03:45:26 -07:00
|
|
|
offset += sizeof(_evOffset[0])*evCount;
|
|
|
|
|
2017-04-07 08:46:50 -07:00
|
|
|
offset = RoundUp(offset, 16); /* Align for coefficients using SIMD */
|
2018-11-09 18:08:42 -08:00
|
|
|
_coeffs = reinterpret_cast<ALfloat(*)[2]>(base + offset);
|
2017-03-31 03:45:26 -07:00
|
|
|
offset += sizeof(_coeffs[0])*irSize*irCount;
|
|
|
|
|
2018-11-09 18:08:42 -08:00
|
|
|
_delays = reinterpret_cast<ALubyte(*)[2]>(base + offset);
|
2017-03-31 03:45:26 -07:00
|
|
|
offset += sizeof(_delays[0])*irCount;
|
|
|
|
|
2018-01-15 08:38:25 -08:00
|
|
|
assert(offset == total);
|
|
|
|
|
2017-03-31 03:45:26 -07:00
|
|
|
/* Copy input data to storage. */
|
|
|
|
for(i = 0;i < evCount;i++) _azCount[i] = azCount[i];
|
|
|
|
for(i = 0;i < evCount;i++) _evOffset[i] = evOffset[i];
|
2017-03-31 04:25:22 -07:00
|
|
|
for(i = 0;i < irSize*irCount;i++)
|
2017-04-07 08:46:50 -07:00
|
|
|
{
|
|
|
|
_coeffs[i][0] = coeffs[i][0];
|
|
|
|
_coeffs[i][1] = coeffs[i][1];
|
|
|
|
}
|
|
|
|
for(i = 0;i < irCount;i++)
|
|
|
|
{
|
|
|
|
_delays[i][0] = delays[i][0];
|
|
|
|
_delays[i][1] = delays[i][1];
|
|
|
|
}
|
2017-03-31 03:45:26 -07:00
|
|
|
|
2018-01-15 08:38:25 -08:00
|
|
|
/* Finally, assign the storage pointers. */
|
|
|
|
Hrtf->azCount = _azCount;
|
|
|
|
Hrtf->evOffset = _evOffset;
|
|
|
|
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;
|
2017-06-16 22:58:13 -07:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-12-22 09:20:50 -08:00
|
|
|
HrtfEntry *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};
|
2012-09-12 07:25:05 -07:00
|
|
|
if(irSize < MIN_IR_SIZE || irSize > MAX_IR_SIZE || (irSize%MOD_IR_SIZE))
|
|
|
|
{
|
|
|
|
ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
|
|
|
|
irSize, MIN_IR_SIZE, MAX_IR_SIZE, MOD_IR_SIZE);
|
|
|
|
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
|
|
|
|
2018-11-24 16:58:49 -08:00
|
|
|
al::vector<ALushort> evOffset(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
|
|
|
}
|
2018-11-09 23:47:42 -08:00
|
|
|
for(ALsizei 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
|
|
|
{
|
2018-11-09 18:08:42 -08:00
|
|
|
ERR("Invalid evOffset: evOffset[%d]=%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())
|
|
|
|
{
|
|
|
|
ERR("Invalid evOffset: evOffset[" SZFMT "]=%d (irCount=%d)\n",
|
|
|
|
evOffset.size()-1, evOffset.back(), irCount);
|
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
|
|
|
if(failed)
|
|
|
|
return nullptr;
|
2012-09-12 07:25:05 -07:00
|
|
|
|
2018-11-24 16:58:49 -08:00
|
|
|
al::vector<ALubyte> azCount(evCount);
|
2018-11-09 23:47:42 -08:00
|
|
|
for(ALsizei i{1};i < evCount;i++)
|
|
|
|
{
|
2018-11-09 18:08:42 -08:00
|
|
|
azCount[i-1] = 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)
|
|
|
|
{
|
|
|
|
ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
|
|
|
|
i-1, azCount[i-1], MIN_AZ_COUNT, MAX_AZ_COUNT);
|
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
|
|
|
}
|
2018-11-09 23:47:42 -08:00
|
|
|
azCount.back() = irCount - evOffset.back();
|
|
|
|
if(azCount.back() < MIN_AZ_COUNT || azCount.back() > MAX_AZ_COUNT)
|
2018-11-09 18:08:42 -08:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
ERR("Unsupported azimuth count: azCount[" SZFMT "]=%d (%d to %d)\n",
|
|
|
|
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;
|
|
|
|
|
2018-11-24 16:58:49 -08:00
|
|
|
al::vector<std::array<ALfloat,2>> coeffs(irSize*irCount);
|
|
|
|
al::vector<std::array<ALubyte,2>> delays(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
|
|
|
}
|
2018-11-09 23:47:42 -08:00
|
|
|
for(ALsizei 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
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
ERR("Invalid delays[%d]: %d (%d)\n", i, delays[i][0], MAX_HRIR_DELAY);
|
2016-08-31 08:16:49 -07:00
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
|
|
|
}
|
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. */
|
|
|
|
for(ALsizei i{0};i < evCount;i++)
|
2017-04-07 08:46:50 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
ALushort evoffset = evOffset[i];
|
|
|
|
ALubyte azcount = azCount[i];
|
|
|
|
for(ALsizei j{0};j < azcount;j++)
|
2017-04-07 08:46:50 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
ALsizei lidx = evoffset + j;
|
|
|
|
ALsizei ridx = evoffset + ((azcount-j) % azcount);
|
2017-04-07 08:46:50 -07:00
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
for(ALsizei k{0};k < irSize;k++)
|
|
|
|
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
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
return CreateHrtfStore(rate, irSize, 0.0f, evCount, irCount, azCount.data(),
|
|
|
|
evOffset.data(), &reinterpret_cast<ALfloat(&)[2]>(coeffs[0]),
|
|
|
|
&reinterpret_cast<ALubyte(&)[2]>(delays[0]), filename);
|
2012-09-12 07:25:05 -07:00
|
|
|
}
|
|
|
|
|
2018-12-22 09:20:50 -08:00
|
|
|
HrtfEntry *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};
|
2012-09-12 07:25:05 -07:00
|
|
|
if(irSize < MIN_IR_SIZE || irSize > MAX_IR_SIZE || (irSize%MOD_IR_SIZE))
|
|
|
|
{
|
|
|
|
ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
|
|
|
|
irSize, MIN_IR_SIZE, MAX_IR_SIZE, MOD_IR_SIZE);
|
|
|
|
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
|
|
|
|
2018-11-24 16:58:49 -08:00
|
|
|
al::vector<ALubyte> azCount(evCount);
|
2018-11-09 23:47:42 -08:00
|
|
|
data.read(reinterpret_cast<char*>(azCount.data()), evCount);
|
|
|
|
if(!data || data.eof() || data.gcount() < evCount)
|
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
|
|
|
for(ALsizei 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
|
|
|
{
|
2018-11-09 18:08:42 -08:00
|
|
|
ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
|
|
|
|
i, azCount[i], MIN_AZ_COUNT, MAX_AZ_COUNT);
|
|
|
|
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
|
|
|
|
2018-11-24 16:58:49 -08:00
|
|
|
al::vector<ALushort> evOffset(evCount);
|
2018-11-09 23:47:42 -08:00
|
|
|
evOffset[0] = 0;
|
|
|
|
ALushort irCount{azCount[0]};
|
|
|
|
for(ALsizei i{1};i < evCount;i++)
|
2012-09-12 07:25:05 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
evOffset[i] = evOffset[i-1] + azCount[i-1];
|
|
|
|
irCount += azCount[i];
|
2016-08-31 08:16:49 -07:00
|
|
|
}
|
|
|
|
|
2018-11-24 16:58:49 -08:00
|
|
|
al::vector<std::array<ALfloat,2>> coeffs(irSize*irCount);
|
|
|
|
al::vector<std::array<ALubyte,2>> delays(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;
|
|
|
|
}
|
|
|
|
for(ALsizei i{0};i < irCount;i++)
|
|
|
|
{
|
|
|
|
if(delays[i][0] > MAX_HRIR_DELAY)
|
2012-09-12 07:25:05 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
ERR("Invalid delays[%d]: %d (%d)\n", i, delays[i][0], MAX_HRIR_DELAY);
|
|
|
|
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
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
/* Mirror the left ear responses to the right ear. */
|
|
|
|
for(ALsizei i{0};i < evCount;i++)
|
2017-04-07 08:46:50 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
ALushort evoffset = evOffset[i];
|
|
|
|
ALubyte azcount = azCount[i];
|
|
|
|
for(ALsizei j{0};j < azcount;j++)
|
2017-04-07 08:46:50 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
ALsizei lidx = evoffset + j;
|
|
|
|
ALsizei ridx = evoffset + ((azcount-j) % azcount);
|
2017-04-07 08:46:50 -07:00
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
for(ALsizei k{0};k < irSize;k++)
|
|
|
|
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
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
return CreateHrtfStore(rate, irSize, 0.0f, evCount, irCount, azCount.data(),
|
|
|
|
evOffset.data(), &reinterpret_cast<ALfloat(&)[2]>(coeffs[0]),
|
|
|
|
&reinterpret_cast<ALubyte(&)[2]>(delays[0]), filename);
|
2012-09-12 07:25:05 -07:00
|
|
|
}
|
|
|
|
|
2017-08-08 20:25:31 -07:00
|
|
|
#define SAMPLETYPE_S16 0
|
|
|
|
#define SAMPLETYPE_S24 1
|
|
|
|
|
|
|
|
#define CHANTYPE_LEFTONLY 0
|
|
|
|
#define CHANTYPE_LEFTRIGHT 1
|
|
|
|
|
2018-12-22 09:20:50 -08:00
|
|
|
HrtfEntry *LoadHrtf02(std::istream &data, const char *filename)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
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};
|
2017-08-08 20:25:31 -07:00
|
|
|
if(sampleType > SAMPLETYPE_S24)
|
|
|
|
{
|
|
|
|
ERR("Unsupported sample type: %d\n", sampleType);
|
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
|
|
|
if(channelType > CHANTYPE_LEFTRIGHT)
|
|
|
|
{
|
|
|
|
ERR("Unsupported channel type: %d\n", channelType);
|
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(irSize < MIN_IR_SIZE || irSize > MAX_IR_SIZE || (irSize%MOD_IR_SIZE))
|
|
|
|
{
|
|
|
|
ERR("Unsupported HRIR size: irSize=%d (%d to %d by %d)\n",
|
|
|
|
irSize, MIN_IR_SIZE, MAX_IR_SIZE, MOD_IR_SIZE);
|
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
2017-10-22 15:36:42 -07:00
|
|
|
if(fdCount != 1)
|
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
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
ALushort distance{};
|
|
|
|
ALubyte evCount{};
|
2018-11-24 16:58:49 -08:00
|
|
|
al::vector<ALubyte> azCount;
|
2018-11-09 23:47:42 -08:00
|
|
|
for(ALsizei i{0};i < fdCount;i++)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
distance = GetLE_ALushort(data);
|
|
|
|
evCount = GetLE_ALubyte(data);
|
|
|
|
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
|
|
|
|
2017-10-22 15:36:42 -07:00
|
|
|
if(distance < MIN_FD_DISTANCE || distance > MAX_FD_DISTANCE)
|
|
|
|
{
|
|
|
|
ERR("Unsupported field distance: distance=%d (%dmm to %dmm)\n",
|
|
|
|
distance, MIN_FD_DISTANCE, MAX_FD_DISTANCE);
|
|
|
|
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;
|
2017-08-08 20:25:31 -07:00
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
azCount.resize(evCount);
|
|
|
|
data.read(reinterpret_cast<char*>(azCount.data()), evCount);
|
|
|
|
if(!data || data.eof() || data.gcount() < evCount)
|
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
|
|
|
}
|
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
for(ALsizei j{0};j < evCount;j++)
|
2017-10-22 15:36:42 -07:00
|
|
|
{
|
|
|
|
if(azCount[j] < MIN_AZ_COUNT || azCount[j] > MAX_AZ_COUNT)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
|
|
|
ERR("Unsupported azimuth count: azCount[%d]=%d (%d to %d)\n",
|
2017-10-22 15:36:42 -07:00
|
|
|
j, azCount[j], 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
|
|
|
}
|
|
|
|
|
2018-11-24 16:58:49 -08:00
|
|
|
al::vector<ALushort> evOffset(evCount);
|
2018-11-09 18:08:42 -08:00
|
|
|
evOffset[0] = 0;
|
2018-11-09 23:47:42 -08:00
|
|
|
ALushort irCount{azCount[0]};
|
|
|
|
for(ALsizei i{1};i < evCount;++i)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
2018-11-09 18:08:42 -08:00
|
|
|
evOffset[i] = evOffset[i-1] + azCount[i-1];
|
|
|
|
irCount += azCount[i];
|
2017-08-08 20:25:31 -07:00
|
|
|
}
|
|
|
|
|
2018-11-24 16:58:49 -08:00
|
|
|
al::vector<std::array<ALfloat,2>> coeffs(irSize*irCount);
|
|
|
|
al::vector<std::array<ALubyte,2>> delays(irCount);
|
2018-11-09 23:47:42 -08:00
|
|
|
if(channelType == CHANTYPE_LEFTONLY)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
if(sampleType == SAMPLETYPE_S16)
|
|
|
|
{
|
|
|
|
for(auto &val : coeffs)
|
|
|
|
val[0] = GetLE_ALshort(data) / 32768.0f;
|
|
|
|
}
|
|
|
|
else if(sampleType == SAMPLETYPE_S24)
|
|
|
|
{
|
|
|
|
for(auto &val : coeffs)
|
|
|
|
val[0] = GetLE_ALint24(data) / 8388608.0f;
|
|
|
|
}
|
|
|
|
for(auto &val : delays)
|
|
|
|
val[0] = GetLE_ALubyte(data);
|
|
|
|
if(!data || data.eof())
|
|
|
|
{
|
|
|
|
ERR("Failed reading %s\n", filename);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
for(ALsizei i{0};i < irCount;++i)
|
|
|
|
{
|
|
|
|
if(delays[i][0] > MAX_HRIR_DELAY)
|
|
|
|
{
|
|
|
|
ERR("Invalid delays[%d][0]: %d (%d)\n", i, delays[i][0], MAX_HRIR_DELAY);
|
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
|
|
|
}
|
2017-08-08 20:25:31 -07:00
|
|
|
}
|
2018-11-09 23:47:42 -08:00
|
|
|
else if(channelType == CHANTYPE_LEFTRIGHT)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -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
|
|
|
}
|
2018-11-09 23:47:42 -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)
|
|
|
|
{
|
|
|
|
val[0] = GetLE_ALint24(data) / 8388608.0f;
|
|
|
|
val[1] = GetLE_ALint24(data) / 8388608.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
for(ALsizei i{0};i < irCount;++i)
|
|
|
|
{
|
|
|
|
if(delays[i][0] > MAX_HRIR_DELAY)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
ERR("Invalid delays[%d][0]: %d (%d)\n", i, delays[i][0], MAX_HRIR_DELAY);
|
|
|
|
failed = AL_TRUE;
|
|
|
|
}
|
|
|
|
if(delays[i][1] > MAX_HRIR_DELAY)
|
|
|
|
{
|
|
|
|
ERR("Invalid delays[%d][1]: %d (%d)\n", i, delays[i][1], MAX_HRIR_DELAY);
|
|
|
|
failed = AL_TRUE;
|
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
|
|
|
|
2018-11-09 23:47:42 -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. */
|
|
|
|
for(ALsizei i{0};i < evCount;i++)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
ALushort evoffset = evOffset[i];
|
|
|
|
ALubyte azcount = azCount[i];
|
|
|
|
for(ALsizei j{0};j < azcount;j++)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
ALsizei lidx = evoffset + j;
|
|
|
|
ALsizei ridx = evoffset + ((azcount-j) % azcount);
|
2017-08-08 20:25:31 -07:00
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
for(ALsizei k{0};k < irSize;k++)
|
|
|
|
coeffs[ridx*irSize + k][1] = coeffs[lidx*irSize + k][0];
|
|
|
|
delays[ridx][1] = delays[lidx][0];
|
2017-08-08 20:25:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
return CreateHrtfStore(rate, irSize,
|
|
|
|
(ALfloat)distance / 1000.0f, evCount, irCount, azCount.data(), evOffset.data(),
|
|
|
|
&reinterpret_cast<ALfloat(&)[2]>(coeffs[0]),
|
|
|
|
&reinterpret_cast<ALubyte(&)[2]>(delays[0]), filename
|
|
|
|
);
|
2017-08-08 20:25:31 -07:00
|
|
|
}
|
|
|
|
|
2017-04-07 06:40:42 -07:00
|
|
|
|
2018-11-18 22:14:44 -08:00
|
|
|
bool checkName(al::vector<EnumeratedHrtf> &list, const std::string &name)
|
2018-11-18 19:19:35 -08:00
|
|
|
{
|
|
|
|
return std::find_if(list.cbegin(), list.cend(),
|
|
|
|
[&name](const EnumeratedHrtf &entry)
|
|
|
|
{ return name == entry.name; }
|
|
|
|
) != list.cend();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddFileEntry(al::vector<EnumeratedHrtf> &list, const std::string &filename)
|
2015-10-06 00:23:11 -07:00
|
|
|
{
|
2017-04-05 07:09:16 -07:00
|
|
|
/* Check if this file has already been loaded globally. */
|
2018-12-09 03:04:18 -08:00
|
|
|
auto loaded_entry = LoadedHrtfs.begin();
|
|
|
|
for(;loaded_entry != LoadedHrtfs.end();++loaded_entry)
|
2016-07-24 21:59:02 -07:00
|
|
|
{
|
2018-12-09 03:04:18 -08:00
|
|
|
if(filename != (*loaded_entry)->filename)
|
|
|
|
continue;
|
2016-07-24 21:59:02 -07:00
|
|
|
|
2018-12-09 03:04:18 -08:00
|
|
|
/* Check if this entry has already been added to the list. */
|
|
|
|
auto iter = std::find_if(list.cbegin(), list.cend(),
|
|
|
|
[loaded_entry](const EnumeratedHrtf &entry) -> bool
|
|
|
|
{ return loaded_entry->get() == entry.hrtf; }
|
|
|
|
);
|
|
|
|
if(iter != list.cend())
|
|
|
|
{
|
|
|
|
TRACE("Skipping duplicate file entry %s\n", filename.c_str());
|
|
|
|
return;
|
2016-02-21 04:46:14 -08:00
|
|
|
}
|
2018-12-09 03:04:18 -08:00
|
|
|
|
|
|
|
break;
|
2015-10-06 04:01:53 -07:00
|
|
|
}
|
|
|
|
|
2018-12-09 03:04:18 -08:00
|
|
|
if(loaded_entry == LoadedHrtfs.end())
|
2017-04-05 12:46:02 -07:00
|
|
|
{
|
2018-11-11 19:17:40 -08:00
|
|
|
TRACE("Got new file \"%s\"\n", filename.c_str());
|
2017-04-05 12:46:02 -07:00
|
|
|
|
2018-12-22 09:20:50 -08:00
|
|
|
LoadedHrtfs.emplace_back(HrtfHandlePtr{new
|
|
|
|
(al_calloc(DEF_ALIGN, FAM_SIZE(HrtfHandle, filename, filename.length()+1)))
|
|
|
|
HrtfHandle{}});
|
2018-12-09 03:04:18 -08:00
|
|
|
loaded_entry = LoadedHrtfs.end()-1;
|
|
|
|
strcpy((*loaded_entry)->filename, filename.c_str());
|
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};
|
|
|
|
while(checkName(list, newname))
|
|
|
|
{
|
|
|
|
newname = basename;
|
|
|
|
newname += " #";
|
|
|
|
newname += std::to_string(++count);
|
|
|
|
}
|
2018-12-09 03:04:18 -08:00
|
|
|
list.emplace_back(EnumeratedHrtf{newname, loaded_entry->get()});
|
2018-11-18 19:19:35 -08:00
|
|
|
const EnumeratedHrtf &entry = list.back();
|
2015-10-06 00:23:11 -07:00
|
|
|
|
2018-11-18 21:18:19 -08:00
|
|
|
TRACE("Adding file entry \"%s\"\n", entry.name.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
|
|
|
*/
|
2018-11-18 19:19:35 -08:00
|
|
|
void AddBuiltInEntry(al::vector<EnumeratedHrtf> &list, const std::string &filename, ALuint residx)
|
2016-07-12 19:02:19 -07:00
|
|
|
{
|
2018-12-09 03:04:18 -08:00
|
|
|
auto loaded_entry = LoadedHrtfs.begin();
|
|
|
|
for(;loaded_entry != LoadedHrtfs.end();++loaded_entry)
|
2016-07-24 21:59:02 -07:00
|
|
|
{
|
2018-12-09 03:04:18 -08:00
|
|
|
if(filename != (*loaded_entry)->filename)
|
|
|
|
continue;
|
2016-07-24 21:59:02 -07:00
|
|
|
|
2018-12-09 03:04:18 -08:00
|
|
|
/* Check if this entry has already been added to the list. */
|
|
|
|
auto iter = std::find_if(list.cbegin(), list.cend(),
|
|
|
|
[loaded_entry](const EnumeratedHrtf &entry) -> bool
|
|
|
|
{ return loaded_entry->get() == entry.hrtf; }
|
|
|
|
);
|
|
|
|
if(iter != list.cend())
|
|
|
|
{
|
|
|
|
TRACE("Skipping duplicate file entry %s\n", filename.c_str());
|
|
|
|
return;
|
2016-07-12 19:02:19 -07:00
|
|
|
}
|
2018-12-09 03:04:18 -08:00
|
|
|
|
|
|
|
break;
|
2016-07-12 19:02:19 -07:00
|
|
|
}
|
|
|
|
|
2018-12-09 03:04:18 -08:00
|
|
|
if(loaded_entry == LoadedHrtfs.end())
|
2016-07-12 19:02:19 -07:00
|
|
|
{
|
2018-12-09 03:04:18 -08:00
|
|
|
const size_t namelen{filename.length()+32};
|
2016-07-12 19:02:19 -07:00
|
|
|
|
2018-11-11 19:17:40 -08:00
|
|
|
TRACE("Got new file \"%s\"\n", filename.c_str());
|
2016-07-12 19:02:19 -07:00
|
|
|
|
2018-12-22 09:20:50 -08:00
|
|
|
LoadedHrtfs.emplace_back(HrtfHandlePtr{new
|
|
|
|
(al_calloc(DEF_ALIGN, FAM_SIZE(HrtfHandle, filename, namelen)))
|
|
|
|
HrtfHandle{}});
|
2018-12-09 03:04:18 -08:00
|
|
|
loaded_entry = LoadedHrtfs.end()-1;
|
|
|
|
snprintf((*loaded_entry)->filename, namelen, "!%u_%s",
|
2018-11-11 19:17:40 -08:00
|
|
|
residx, filename.c_str());
|
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). */
|
|
|
|
|
2018-11-18 19:19:35 -08:00
|
|
|
std::string newname{filename};
|
|
|
|
int count{1};
|
|
|
|
while(checkName(list, newname))
|
|
|
|
{
|
2018-11-12 22:26:12 -08:00
|
|
|
newname = filename;
|
2018-11-18 19:19:35 -08:00
|
|
|
newname += " #";
|
|
|
|
newname += std::to_string(++count);
|
|
|
|
}
|
2018-12-09 03:04:18 -08:00
|
|
|
list.emplace_back(EnumeratedHrtf{newname, loaded_entry->get()});
|
2018-11-18 19:19:35 -08:00
|
|
|
const EnumeratedHrtf &entry = list.back();
|
2016-07-12 19:02:19 -07:00
|
|
|
|
2018-11-18 21:18:19 -08:00
|
|
|
TRACE("Adding built-in entry \"%s\"\n", entry.name.c_str());
|
2016-07-12 19:02:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-10 12:37:07 -08:00
|
|
|
#define IDR_DEFAULT_44100_MHR 1
|
|
|
|
#define IDR_DEFAULT_48000_MHR 2
|
2016-07-12 19:02:19 -07:00
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
struct ResData { const char *data; size_t size; };
|
2017-05-05 13:29:52 +03:00
|
|
|
#ifndef ALSOFT_EMBED_HRTF_DATA
|
2016-11-10 21:51:45 -08:00
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
ResData GetResource(int UNUSED(name))
|
|
|
|
{ return {nullptr, 0u}; }
|
2016-11-10 21:51:45 -08:00
|
|
|
|
2016-07-12 19:02:19 -07:00
|
|
|
#else
|
|
|
|
|
2017-05-05 13:29:52 +03:00
|
|
|
#include "default-44100.mhr.h"
|
|
|
|
#include "default-48000.mhr.h"
|
2016-07-12 19:02:19 -07:00
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
ResData GetResource(int name)
|
2016-07-12 19:02:19 -07:00
|
|
|
{
|
|
|
|
if(name == IDR_DEFAULT_44100_MHR)
|
2018-11-09 23:47:42 -08:00
|
|
|
return {reinterpret_cast<const char*>(hrtf_default_44100), sizeof(hrtf_default_44100)};
|
2016-07-12 19:02:19 -07:00
|
|
|
if(name == IDR_DEFAULT_48000_MHR)
|
2018-11-09 23:47:42 -08:00
|
|
|
return {reinterpret_cast<const char*>(hrtf_default_48000), sizeof(hrtf_default_48000)};
|
|
|
|
return {nullptr, 0u};
|
2016-07-12 19:02:19 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
2018-11-18 19:19:35 -08:00
|
|
|
al::vector<EnumeratedHrtf> EnumerateHrtf(const char *devname)
|
2015-10-06 00:23:11 -07:00
|
|
|
{
|
2018-11-18 19:19:35 -08:00
|
|
|
al::vector<EnumeratedHrtf> list;
|
2018-11-11 19:17:40 -08:00
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
bool usedefaults{true};
|
|
|
|
const char *pathlist{""};
|
2018-11-15 05:38:27 -08:00
|
|
|
if(ConfigValueStr(devname, nullptr, "hrtf-paths", &pathlist))
|
2015-10-06 00:23:11 -07:00
|
|
|
{
|
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()))
|
2018-11-18 19:19:35 -08:00
|
|
|
AddFileEntry(list, 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
|
|
|
}
|
|
|
|
}
|
2018-11-15 05:38:27 -08:00
|
|
|
else if(ConfigValueExists(devname, nullptr, "hrtf_tables"))
|
2016-02-23 10:54:42 -08:00
|
|
|
ERR("The hrtf_tables option is deprecated, please use hrtf-paths instead.\n");
|
|
|
|
|
|
|
|
if(usedefaults)
|
|
|
|
{
|
2018-11-11 19:17:40 -08:00
|
|
|
for(const auto &fname : SearchDataFiles(".mhr", "openal/hrtf"))
|
2018-11-18 19:19:35 -08:00
|
|
|
AddFileEntry(list, fname);
|
2016-07-12 19:02:19 -07:00
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
ResData res{GetResource(IDR_DEFAULT_44100_MHR)};
|
|
|
|
if(res.data != nullptr && res.size > 0)
|
2018-11-18 19:19:35 -08:00
|
|
|
AddBuiltInEntry(list, "Built-In 44100hz", IDR_DEFAULT_44100_MHR);
|
2016-07-12 19:02:19 -07:00
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
res = GetResource(IDR_DEFAULT_48000_MHR);
|
|
|
|
if(res.data != nullptr && res.size > 0)
|
2018-11-18 19:19:35 -08:00
|
|
|
AddBuiltInEntry(list, "Built-In 48000hz", IDR_DEFAULT_48000_MHR);
|
2016-02-23 10:54:42 -08:00
|
|
|
}
|
2015-10-06 00:23:11 -07:00
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
const char *defaulthrtf{""};
|
2018-11-18 19:19:35 -08:00
|
|
|
if(!list.empty() && ConfigValueStr(devname, nullptr, "default-hrtf", &defaulthrtf))
|
2016-02-21 02:44:02 -08:00
|
|
|
{
|
2018-11-18 19:19:35 -08:00
|
|
|
auto iter = std::find_if(list.begin(), list.end(),
|
|
|
|
[defaulthrtf](const EnumeratedHrtf &entry) -> bool
|
2018-11-18 21:18:19 -08:00
|
|
|
{ return entry.name == defaulthrtf; }
|
2018-11-18 19:19:35 -08:00
|
|
|
);
|
|
|
|
if(iter == list.end())
|
2016-09-10 07:55:33 -07:00
|
|
|
WARN("Failed to find default HRTF \"%s\"\n", defaulthrtf);
|
2018-11-18 19:19:35 -08:00
|
|
|
else if(iter != list.begin())
|
2016-02-21 02:44:02 -08:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
EnumeratedHrtf entry{*iter};
|
2018-11-18 19:19:35 -08:00
|
|
|
list.erase(iter);
|
|
|
|
list.insert(list.begin(), entry);
|
2016-02-21 02:44:02 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-06 00:23:11 -07:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2018-12-22 09:20:50 -08:00
|
|
|
HrtfEntry *GetLoadedHrtf(HrtfHandle *handle)
|
2017-04-05 12:27:30 -07:00
|
|
|
{
|
2018-11-09 23:47:42 -08:00
|
|
|
std::lock_guard<std::mutex> _{LoadedHrtfLock};
|
2017-04-05 12:27:30 -07:00
|
|
|
|
2018-12-22 09:20:50 -08:00
|
|
|
if(handle->entry)
|
2017-04-05 12:27:30 -07:00
|
|
|
{
|
2018-12-22 09:20:50 -08:00
|
|
|
HrtfEntry *hrtf{handle->entry};
|
2017-04-06 13:00:29 -07:00
|
|
|
Hrtf_IncRef(hrtf);
|
2018-11-09 23:47:42 -08:00
|
|
|
return hrtf;
|
2017-04-05 12:27:30 -07:00
|
|
|
}
|
|
|
|
|
2018-11-09 23:47:42 -08:00
|
|
|
std::unique_ptr<std::istream> stream;
|
|
|
|
const char *name{""};
|
|
|
|
ALuint residx{};
|
|
|
|
char ch{};
|
2018-12-22 09:20:50 -08:00
|
|
|
if(sscanf(handle->filename, "!%u%c", &residx, &ch) == 2 && ch == '_')
|
2017-04-05 12:27:30 -07:00
|
|
|
{
|
2018-12-22 09:20:50 -08:00
|
|
|
name = strchr(handle->filename, ch)+1;
|
2017-04-06 01:35:09 -07:00
|
|
|
|
|
|
|
TRACE("Loading %s...\n", name);
|
2018-11-09 23:47:42 -08:00
|
|
|
ResData res{GetResource(residx)};
|
|
|
|
if(!res.data || res.size == 0)
|
2017-04-06 01:35:09 -07:00
|
|
|
{
|
2017-12-17 15:56:30 -08:00
|
|
|
ERR("Could not get resource %u, %s\n", residx, name);
|
2018-11-09 23:47:42 -08:00
|
|
|
return nullptr;
|
2017-04-06 01:35:09 -07:00
|
|
|
}
|
2019-01-01 14:33:01 -08:00
|
|
|
stream = al::make_unique<idstream>(res.data, res.data+res.size);
|
2017-04-06 01:35:09 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-22 09:20:50 -08:00
|
|
|
name = handle->filename;
|
2017-04-06 01:35:09 -07:00
|
|
|
|
2018-12-22 09:20:50 -08:00
|
|
|
TRACE("Loading %s...\n", handle->filename);
|
2019-01-01 14:33:01 -08:00
|
|
|
auto fstr = al::make_unique<al::ifstream>(handle->filename, std::ios::binary);
|
2018-11-09 23:47:42 -08:00
|
|
|
if(!fstr->is_open())
|
2017-04-06 01:35:09 -07:00
|
|
|
{
|
2018-12-22 09:20:50 -08:00
|
|
|
ERR("Could not open %s\n", handle->filename);
|
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
|
|
|
}
|
|
|
|
|
2018-12-22 09:20:50 -08:00
|
|
|
HrtfEntry *hrtf{nullptr};
|
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)))
|
|
|
|
ERR("%s data is too short (" SZFMT " bytes)\n", name, stream->gcount());
|
|
|
|
else if(memcmp(magic, magicMarker02, sizeof(magicMarker02)) == 0)
|
2017-08-08 20:25:31 -07:00
|
|
|
{
|
|
|
|
TRACE("Detected data set format v2\n");
|
2018-11-09 23:47:42 -08:00
|
|
|
hrtf = LoadHrtf02(*stream, name);
|
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");
|
2018-11-09 23:47:42 -08:00
|
|
|
hrtf = LoadHrtf01(*stream, name);
|
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");
|
2018-11-09 23:47:42 -08:00
|
|
|
hrtf = LoadHrtf00(*stream, name);
|
2017-04-05 12:27:30 -07:00
|
|
|
}
|
|
|
|
else
|
2018-11-09 23:47:42 -08:00
|
|
|
ERR("Invalid header in %s: \"%.8s\"\n", name, magic);
|
|
|
|
stream.reset();
|
2017-04-05 12:27:30 -07:00
|
|
|
|
|
|
|
if(!hrtf)
|
2017-04-06 01:35:09 -07:00
|
|
|
ERR("Failed to load %s\n", name);
|
2018-11-09 23:47:42 -08:00
|
|
|
else
|
|
|
|
{
|
2018-12-22 09:20:50 -08:00
|
|
|
handle->entry = hrtf;
|
2018-11-09 23:47:42 -08:00
|
|
|
Hrtf_IncRef(hrtf);
|
|
|
|
TRACE("Loaded HRTF support for format: %s %uhz\n",
|
|
|
|
DevFmtChannelsString(DevFmtStereo), hrtf->sampleRate);
|
2017-04-05 12:27:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return hrtf;
|
|
|
|
}
|
|
|
|
|
2015-10-06 00:23:11 -07:00
|
|
|
|
2018-12-22 09:20:50 -08:00
|
|
|
void Hrtf_IncRef(HrtfEntry *hrtf)
|
2017-04-06 13:00:29 -07:00
|
|
|
{
|
2018-11-19 02:17:06 -08:00
|
|
|
auto ref = IncrementRef(&hrtf->ref);
|
2017-04-06 13:00:29 -07:00
|
|
|
TRACEREF("%p increasing refcount to %u\n", hrtf, ref);
|
|
|
|
}
|
|
|
|
|
2018-12-22 09:20:50 -08:00
|
|
|
void Hrtf_DecRef(HrtfEntry *hrtf)
|
2017-04-06 13:00:29 -07:00
|
|
|
{
|
2018-11-19 02:17:06 -08:00
|
|
|
auto ref = DecrementRef(&hrtf->ref);
|
2017-04-06 13:00:29 -07:00
|
|
|
TRACEREF("%p decreasing refcount to %u\n", hrtf, ref);
|
|
|
|
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
|
|
|
|
2018-12-09 03:04:18 -08:00
|
|
|
/* Need to double-check that it's still unused, as another device
|
|
|
|
* could've reacquired this HRTF after its reference went to 0 and
|
|
|
|
* before the lock was taken.
|
|
|
|
*/
|
|
|
|
auto iter = std::find_if(LoadedHrtfs.begin(), LoadedHrtfs.end(),
|
2018-12-22 09:20:50 -08:00
|
|
|
[hrtf](const HrtfHandlePtr &entry) noexcept -> bool
|
|
|
|
{ return hrtf == entry->entry; }
|
2018-12-09 03:04:18 -08:00
|
|
|
);
|
|
|
|
if(iter != LoadedHrtfs.end() && ReadRef(&hrtf->ref) == 0)
|
2017-04-06 13:00:29 -07:00
|
|
|
{
|
2018-12-22 09:20:50 -08:00
|
|
|
al_free((*iter)->entry);
|
|
|
|
(*iter)->entry = nullptr;
|
2018-12-09 03:04:18 -08:00
|
|
|
TRACE("Unloaded unused HRTF %s\n", (*iter)->filename);
|
2017-04-06 13:00:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|