JPL Spatial
Sound spatialization and propagation library
Loading...
Searching...
No Matches
SmoothedValue.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 <cstdint>
23#include <cmath>
24
25namespace JPL
26{
27 template<class T>
29 {
30 public:
31 T Current = T(0.0);
32 T Target = T(0.0);
33 T Alpha = T(1.0);
34
35 public:
37 {
38 return numOfTicks == 0
39 ? T(1.0)
40 : T(1.0 - std::exp(-1.0 / static_cast<double>(numOfTicks)));
41 }
42
44 {
45 return numOfTicks == 0
46 ? T(1.0)
47 : T(1.0 / static_cast<double>(numOfTicks));
48 }
49
54
59
61 {
62 return SmoothedValue{ .Current = current, .Target = target, .Alpha = GetExpAlpha(numOfTicks) };
63 }
64
66 {
67 return SmoothedValue{ .Current = current, .Target = target, .Alpha = GetInvAlpha(numOfTicks) };
68 }
69
71
72 // Advance smoothing to the next value
73 inline void Tick()
74 {
75 // c = a * c + (1.0f - a) * t : or a sing SUB + FMA
77 }
78
79 // Advance smoothing to the next value and return it
80 [[nodiscard]] inline T GetNext() { Tick(); return Current; }
81 };
82}
Definition AcousticMaterial.h:36
JPL_INLINE simd min(const simd &a, const simd &b) noexcept
Element-wise min.
Definition SIMD.h:1813
Definition SmoothedValue.h:29
static T GetExpAlpha(uint32_t numOfTicks)
Definition SmoothedValue.h:36
void Tick()
Definition SmoothedValue.h:73
static SmoothedValue CreateSimpleSmoothing(uint32_t numOfTicks)
Definition SmoothedValue.h:55
T GetNext()
Definition SmoothedValue.h:80
static SmoothedValue CreateExpSmoothing(T current, T target, uint32_t numOfTicks)
Definition SmoothedValue.h:60
T Current
Definition SmoothedValue.h:31
T Alpha
Definition SmoothedValue.h:33
static SmoothedValue CreateExpSmoothing(uint32_t numOfTicks)
Definition SmoothedValue.h:50
static T GetInvAlpha(uint32_t numOfTicks)
Definition SmoothedValue.h:43
T Target
Definition SmoothedValue.h:32
void SetDurationExp(uint32_t numTicks)
Definition SmoothedValue.h:70
static SmoothedValue CreateSimpleSmoothing(T current, T target, uint32_t numOfTicks)
Definition SmoothedValue.h:65