Jamba C++ API 8.0.0
Loading...
Searching...
No Matches
AudioUtils.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 pongasoft
3 *
4 * Licensed under the Apache License, Version 2.0 or the MIT license,
5 * at your option. You may not use this file except in compliance with
6 * one of these licenses. You may obtain copies of the licenses at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 * https://opensource.org/licenses/MIT
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 *
17 * @author Yan Pujante
18 */
19#pragma once
20
21#include <pluginterfaces/vst/ivstaudioprocessor.h>
22
23#include <cmath>
24#include <concepts>
25#include <type_traits>
26
27namespace pongasoft {
28namespace VST {
29
30using namespace Steinberg;
31using namespace Steinberg::Vst;
32
33#define BIT_SET(a,b) ((a) |= (static_cast<std::make_unsigned_t<decltype(a)>>(1)<<(b)))
34#define BIT_CLEAR(a,b) ((a) &= ~(static_cast<std::make_unsigned_t<decltype(a)>>(1)<<(b)))
35#define BIT_TEST(a,b) (((a) & (static_cast<std::make_unsigned_t<decltype(a)>>(1)<<(b))) != 0)
36
38template<typename T>
39constexpr T bitSet(T a, int bit) { return a | (static_cast<std::make_unsigned_t<T>>(1) << bit); }
40
42template<typename T>
43constexpr T bitClear(T a, int bit) { return a & ~(static_cast<std::make_unsigned_t<T>>(1) << bit); }
44
46template<typename T>
47constexpr bool bitTest(T a, int bit) { return (a & (static_cast<std::make_unsigned_t<T>>(1) << bit)) != 0; }
48
49// defines the threshold of silence as constants
50constexpr Sample32 Sample32SilentThreshold = ((Sample32)2.0e-8);
51constexpr Sample64 Sample64SilentThreshold = ((Sample64)2.0e-8);
52
53// defines the threshold of silence as a templated method
54template<std::floating_point SampleType>
55SampleType getSampleSilentThreshold() noexcept;
56template<>
57constexpr Sample32 getSampleSilentThreshold<Sample32>() noexcept { return Sample32SilentThreshold; }
58template<>
59constexpr Sample64 getSampleSilentThreshold<Sample64>() noexcept { return Sample64SilentThreshold; }
60
61//------------------------------------------------------------------------
62// check if sample is silent (lower than threshold) Sample32 version
63//------------------------------------------------------------------------
64inline bool isSilent(Sample32 value)
65{
66 if(value < 0)
67 value = -value;
68 return value <= Sample32SilentThreshold;
69}
70
71//------------------------------------------------------------------------
72// check if sample is silent (lower than threshold) Sample64 version
73//------------------------------------------------------------------------
74inline bool isSilent(Sample64 value)
75{
76 if(value < 0)
77 value = -value;
78 return value <= Sample64SilentThreshold;
79}
80
81//------------------------------------------------------------------------
82// dbToSample
83//------------------------------------------------------------------------
84template<std::floating_point SampleType>
85SampleType dbToSample(double valueInDb)
86{
87 return static_cast<SampleType>(std::pow(10.0, valueInDb / 20.0));
88}
89
90//------------------------------------------------------------------------
91// sampleToDb
92//------------------------------------------------------------------------
93template<std::floating_point SampleType>
94double sampleToDb(SampleType valueInSample)
95{
96 return std::log10(valueInSample) * 20.0;
97}
98
99}
100}
Definition Clock.h:24
SampleType getSampleSilentThreshold() noexcept
constexpr bool bitTest(T a, int bit)
Test if bit bit is set in a.
Definition AudioUtils.h:47
bool isSilent(Sample32 value)
Definition AudioUtils.h:64
constexpr Sample64 getSampleSilentThreshold< Sample64 >() noexcept
Definition AudioUtils.h:59
SampleType dbToSample(double valueInDb)
Definition AudioUtils.h:85
constexpr Sample32 Sample32SilentThreshold
Definition AudioUtils.h:50
constexpr Sample64 Sample64SilentThreshold
Definition AudioUtils.h:51
double sampleToDb(SampleType valueInSample)
Definition AudioUtils.h:94
constexpr T bitClear(T a, int bit)
Clears bit bit in a.
Definition AudioUtils.h:43
constexpr T bitSet(T a, int bit)
Sets bit bit in a.
Definition AudioUtils.h:39
Definition Clock.h:23