JPL Spatial
Sound spatialization and propagation library
Loading...
Searching...
No Matches
AcousticMaterial.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"
26
27#include <algorithm>
28#include <cmath>
29#include <concepts>
30#include <ostream>
31#include <string_view>
32#include <unordered_map>
33#include <utility>
34
35namespace JPL
36{
37 template<CFloatOrSIMD T>
38 JPL_INLINE T ReflectionLoss_dB(const T& alpha)
39 {
40 if constexpr (std::same_as<T, simd>)
41 {
42 const T reflectance = max(T(1e-6f), T(1.0f) - alpha);
43 return IntencityTodB(reflectance);
44 }
45 else
46 {
47 const T reflectance = std::max(T(1e-6f), T(1.0f) - alpha);
48 return IntencityTodB(reflectance);
49 }
50 }
51
54 {
55 using List = std::unordered_map<uint32, AcousticMaterial>;
56
59
60 // The bands are 0-176 Hz, 176-775 Hz, 775-3408 Hz, and 3408-22050 Hz
62 // TODO: user-provided frequency bands
63
64 std::string_view Name;
66
67 // TODO: recompute these when user defined Acoustic Material coefficients changed
68
69 // Crude average in dB scalse
71
72 // Crude average
73 float AbsorptionAverage = 0.0f;
74
75 // 1.0 - AbsorptionAverage
77
78 AbsorptionCoeffs Coeffs; // alpha coefficient
81
82 static const AcousticMaterial* Get(uint32 materialID);
83 static const AcousticMaterial* Get(std::string_view materialName);
85 static const List& GetListOfMaterials() { return sList; }
86
87 JPL_INLINE void Accumulate(AbsorptionCoeffs& inOutCoefMultiplier) const
88 {
89 inOutCoefMultiplier *= simd(1.0f) - Coeffs;
90 }
91
94 static void SetMaterial(std::string_view name, const AbsorptionCoeffs& coeffs);
95
97 static std::pair<uint32, AcousticMaterial> MakeMaterial(std::string_view name, const AbsorptionCoeffs& coeffs);
98
99 private:
100 // Make sure the List is initialized anyone tries to use it
101 static List sList;
102 };
103
104 inline std::ostream& operator<<(std::ostream& os, const AcousticMaterial& v)
105 {
106 os << v.Name << ": ";
107 os << v.Coeffs;
108 return os;
109 }
110} // namespace JPL
Definition AcousticMaterial.h:36
const FreqBandCenters cBandCenters
Definition FrequencyBands.h:89
JPL_INLINE constexpr T IntencityTodB(const T &intencityFactor) noexcept
Definition DecibelsAndGain.h:84
std::uint32_t uint32
Definition Core.h:311
simd AbsorptionCoeffs
Definition FrequencyBands.h:29
simd FreqBandCenters
Definition FrequencyBands.h:30
JPL_INLINE simd max(const simd &a, const simd &b) noexcept
Element-wise max.
Definition SIMD.h:1799
std::ostream & operator<<(std::ostream &os, const AcousticMaterial &v)
Definition AcousticMaterial.h:104
JPL_INLINE T ReflectionLoss_dB(const T &alpha)
Definition AcousticMaterial.h:38
Acoustical properties of a material.
Definition AcousticMaterial.h:54
static std::pair< uint32, AcousticMaterial > MakeMaterial(std::string_view name, const AbsorptionCoeffs &coeffs)
Make material without adding it to global static list.
static const AcousticMaterial & GetDefault()
AbsorptionCoeffs AmplitudeFactors
Definition AcousticMaterial.h:80
uint32 ID
Definition AcousticMaterial.h:65
AbsorptionCoeffs Absorption_dB
Definition AcousticMaterial.h:79
static const FreqBandCenters cBandCenters
Definition AcousticMaterial.h:61
float AbsorptionAverage_dB
Definition AcousticMaterial.h:70
static const AcousticMaterial * Get(std::string_view materialName)
static void SetMaterial(std::string_view name, const AbsorptionCoeffs &coeffs)
static const AcousticMaterial * Get(uint32 materialID)
JPL_INLINE void Accumulate(AbsorptionCoeffs &inOutCoefMultiplier) const
Definition AcousticMaterial.h:87
std::string_view Name
Definition AcousticMaterial.h:64
AbsorptionCoeffs Coeffs
Definition AcousticMaterial.h:78
static const List & GetListOfMaterials()
Definition AcousticMaterial.h:85
float AbsorptionAverage
Definition AcousticMaterial.h:73
float AbsorptionAverageOneMinus
Definition AcousticMaterial.h:76
std::unordered_map< uint32, AcousticMaterial > List
Definition AcousticMaterial.h:55
Minimal 4-wide 32-bit float vector implementation for SIMD.
Definition SIMD.h:60