JPL Spatial
Sound spatialization and propagation library
Loading...
Searching...
No Matches
DecibelsAndGain.h
Go to the documentation of this file.
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"
25
26#include <cmath>
27#include <concepts>
28#include <numbers>
29#include <type_traits>
30
31namespace JPL
32{
33 template<class T>
34 concept CFloatOrSIMD =
35 std::same_as<float, std::remove_cvref_t<T>> ||
36 std::same_as<double, std::remove_cvref_t<T>> ||
37 std::same_as<simd, std::remove_cvref_t<T>>;
38
39 template<CFloatOrSIMD T>
40 JPL_INLINE T dBToGain(const T& dB) noexcept
41 {
42 // Our base of `pow` is always 10,
43 // so we don't need half the error handlign checks of `pow`.
44
45 // This is effectively a cheaper version of pow(10.0f, dB / 20.0f)
46 if constexpr (std::same_as<T, simd>)
47 {
48 static const T ln10div20(std::numbers::ln10_v<float> * 0.05f);
49 return exp(ln10div20 * dB);
50 }
51 else
52 {
53 static constexpr T ln10div20 = std::numbers::ln10_v<T> * T(0.05);
54 return std::exp(ln10div20 * dB);
55 }
56 }
57
58 template<CFloatOrSIMD T>
59 JPL_INLINE T dBToIntencity(const T& dB) noexcept
60 {
61 // A cheaper version of pow(10.0f, dB / 10.0f)
62 if constexpr (std::same_as<T, simd>)
63 {
64 static const T ln10div10 = std::numbers::ln10_v<float> * 0.1f;
65 return exp(ln10div10 * dB);
66 }
67 else
68 {
69 static const T ln10div10 = std::numbers::ln10_v<T> * T(0.1);
70 return std::exp(ln10div10 * dB);
71 }
72 }
73
74 template<CFloatOrSIMD T>
75 JPL_INLINE T GainTodB(const T& gainFactor) noexcept
76 {
77 if constexpr (std::same_as<T, simd>)
78 return -20.0f * log10(gainFactor);
79 else
80 return static_cast<T>(-20.0) * std::log10(gainFactor);
81 }
82
83 template<CFloatOrSIMD T>
84 JPL_INLINE constexpr T IntencityTodB(const T& intencityFactor) noexcept
85 {
86 if constexpr (std::same_as<T, simd>)
87 return -10.0f * log10(intencityFactor);
88 else
89 return static_cast<T>(-10.0) * std::log10(intencityFactor);
90 }
91} // namespace JPL
Definition DecibelsAndGain.h:34
Definition AcousticMaterial.h:36
simd exp(simd x) noexcept
Exponent for 4-wide 32-bit float vector.
Definition SIMDMath.h:298
JPL_INLINE T dBToGain(const T &dB) noexcept
Definition DecibelsAndGain.h:40
JPL_INLINE constexpr T IntencityTodB(const T &intencityFactor) noexcept
Definition DecibelsAndGain.h:84
JPL_INLINE T dBToIntencity(const T &dB) noexcept
Definition DecibelsAndGain.h:59
JPL_INLINE T GainTodB(const T &gainFactor) noexcept
Definition DecibelsAndGain.h:75
JPL_INLINE simd min(const simd &a, const simd &b) noexcept
Element-wise min.
Definition SIMD.h:1813
JPL_INLINE simd log10(const simd &vec) noexcept
log10 for 4-wide 32-bit float vector
Definition SIMDMath.h:241