Skip to content

Commit 05058d8

Browse files
committed
Decibels & Gain utilities
1 parent 900cead commit 05058d8

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//
2+
// ██╗██████╗ ██╗ ██╗██████╗ ███████╗
3+
// ██║██╔══██╗ ██║ ██║██╔══██╗██╔════╝ ** JPLSpatial **
4+
// ██║██████╔╝ ██║ ██║██████╔╝███████╗
5+
// ██ ██║██╔═══╝ ██║ ██║██╔══██╗╚════██║ https://github.com/Jaytheway/JPLSpatial
6+
// ╚█████╔╝██║ ███████╗██║██████╔╝███████║
7+
// ╚════╝ ╚═╝ ╚══════╝╚═╝╚═════╝ ╚══════╝
8+
//
9+
// Copyright Jaroslav Pevno, JPLSpatial is offered under the terms of the ISC license:
10+
//
11+
// Permission to use, copy, modify, and/or distribute this software for any purpose with or
12+
// without fee is hereby granted, provided that the above copyright notice and this permission
13+
// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
14+
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15+
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
16+
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
17+
// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
18+
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19+
20+
#pragma once
21+
22+
#include "JPLSpatial/Core.h"
23+
#include "JPLSpatial/Math/SIMD.h"
24+
#include "JPLSpatial/Math/SIMDMath.h"
25+
26+
#include <cmath>
27+
#include <concepts>
28+
#include <numbers>
29+
#include <type_traits>
30+
31+
namespace JPL
32+
{
33+
template<class T>
34+
concept CFloatOrSIMD = std::same_as<float, std::remove_cvref_t<T>> || std::same_as<simd, std::remove_cvref_t<T>>;
35+
36+
template<CFloatOrSIMD T>
37+
JPL_INLINE T dBToGain(const T& dB) noexcept
38+
{
39+
// Our base of `pow` is always 10,
40+
// so we don't need half the error handlign checks of `pow`.
41+
42+
// This is effectively a cheaper version of pow(10.0f, dB / 20.0f)
43+
static const T ln10div20 = std::numbers::ln10_v<float> * 0.05f;
44+
if constexpr (std::same_as<T, simd>)
45+
return exp(ln10div20 * dB);
46+
else
47+
return std::exp(ln10div20 * dB);
48+
}
49+
50+
template<CFloatOrSIMD T>
51+
JPL_INLINE T dBToIntencity(const T& dB) noexcept
52+
{
53+
// A cheaper version of pow(10.0f, dB / 10.0f)
54+
static const T ln10div10 = std::numbers::ln10_v<float> * 0.1f;
55+
if constexpr (std::same_as<T, simd>)
56+
return exp(ln10div10 * dB);
57+
else
58+
return std::exp(ln10div10 * dB);
59+
}
60+
61+
template<CFloatOrSIMD T>
62+
JPL_INLINE T GainTodB(const T& gainFactor) noexcept
63+
{
64+
if constexpr (std::same_as<T, simd>)
65+
return -20.0f * log10(gainFactor);
66+
else
67+
return -20.0f * std::log10(gainFactor);
68+
}
69+
70+
template<CFloatOrSIMD T>
71+
JPL_INLINE T IntencityTodB(const T& intencityFactor) noexcept
72+
{
73+
if constexpr (std::same_as<T, simd>)
74+
return -10.0f * log10(intencityFactor);
75+
else
76+
return -10.0f * std::log10(intencityFactor);
77+
}
78+
} // namespace JPL

0 commit comments

Comments
 (0)