Skip to content

Commit

Permalink
Check the sample rate limit for loading HRTF
Browse files Browse the repository at this point in the history
  • Loading branch information
kcat committed Feb 2, 2024
1 parent 1b6f5fb commit cda0a51
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions core/hrtf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ constexpr uint HrirDelayFracBits{2};
constexpr uint HrirDelayFracOne{1 << HrirDelayFracBits};
constexpr uint HrirDelayFracHalf{HrirDelayFracOne >> 1};

/* The sample rate is stored as a 24-bit integer, so 16MHz is the largest
* supported.
*/
constexpr uint MaxSampleRate{0xff'ff'ff};

static_assert(MaxHrirDelay*HrirDelayFracOne < 256, "MAX_HRIR_DELAY or DELAY_FRAC too large");


Expand Down Expand Up @@ -399,6 +404,9 @@ std::unique_ptr<HrtfStore> CreateHrtfStore(uint rate, uint8_t irSize,
static_assert(alignof(HrtfStore::Elevation) <= alignof(HrtfStore));
static_assert(16 <= alignof(HrtfStore));

if(rate > MaxSampleRate)
throw std::runtime_error{"Sample rate is too large (max: "+std::to_string(MaxSampleRate)+"hz)"};

const size_t irCount{size_t{elevs.back().azCount} + elevs.back().irOffset};
size_t total{sizeof(HrtfStore)};
total = RoundUp(total, alignof(HrtfStore::Field)); /* Align for field infos */
Expand Down Expand Up @@ -1247,6 +1255,11 @@ std::vector<std::string> EnumerateHrtf(std::optional<std::string> pathopt)

HrtfStorePtr GetLoadedHrtf(const std::string_view name, const uint devrate)
try {
if(devrate > MaxSampleRate)
{
WARN("Device sample rate too large for HRTF (%uhz > %uhz)\n", devrate, MaxSampleRate);
return nullptr;
}
std::lock_guard<std::mutex> enumlock{EnumeratedHrtfLock};
auto entry_iter = std::find_if(EnumeratedHrtfs.cbegin(), EnumeratedHrtfs.cend(),
[name](const HrtfEntry &entry) -> bool { return entry.mDispName == name; });
Expand Down

0 comments on commit cda0a51

Please sign in to comment.