Jamba C++ API 7.5.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 <type_traits>
25
26namespace pongasoft {
27namespace VST {
28
29using namespace Steinberg;
30using namespace Steinberg::Vst;
31
32#define BIT_SET(a,b) ((a) |= (static_cast<std::make_unsigned_t<decltype(a)>>(1)<<(b)))
33#define BIT_CLEAR(a,b) ((a) &= ~(static_cast<std::make_unsigned_t<decltype(a)>>(1)<<(b)))
34#define BIT_TEST(a,b) (((a) & (static_cast<std::make_unsigned_t<decltype(a)>>(1)<<(b))) != 0)
35
37template<typename T>
38constexpr T bitSet(T a, int bit) { return a | (static_cast<std::make_unsigned_t<T>>(1) << bit); }
39
41template<typename T>
42constexpr T bitClear(T a, int bit) { return a & ~(static_cast<std::make_unsigned_t<T>>(1) << bit); }
43
45template<typename T>
46constexpr bool bitTest(T a, int bit) { return (a & (static_cast<std::make_unsigned_t<T>>(1) << bit)) != 0; }
47
48// defines the threshold of silence as constants
49constexpr Sample32 Sample32SilentThreshold = ((Sample32)2.0e-8);
50constexpr Sample64 Sample64SilentThreshold = ((Sample64)2.0e-8);
51
52// defines the threshold of silence as a templated method
53template<typename SampleType>
54SampleType getSampleSilentThreshold() noexcept;
55template<>
56constexpr Sample32 getSampleSilentThreshold<Sample32>() noexcept { return Sample32SilentThreshold; }
57template<>
58constexpr Sample64 getSampleSilentThreshold<Sample64>() noexcept { return Sample64SilentThreshold; }
59
60//------------------------------------------------------------------------
61// check if sample is silent (lower than threshold) Sample32 version
62//------------------------------------------------------------------------
63inline bool isSilent(Sample32 value)
64{
65 if(value < 0)
66 value = -value;
67 return value <= Sample32SilentThreshold;
68}
69
70//------------------------------------------------------------------------
71// check if sample is silent (lower than threshold) Sample64 version
72//------------------------------------------------------------------------
73inline bool isSilent(Sample64 value)
74{
75 if(value < 0)
76 value = -value;
77 return value <= Sample64SilentThreshold;
78}
79
80//------------------------------------------------------------------------
81// dbToSample
82//------------------------------------------------------------------------
83template<typename SampleType>
84inline SampleType dbToSample(double valueInDb)
85{
86 return static_cast<SampleType>(std::pow(10.0, valueInDb / 20.0));
87}
88
89//------------------------------------------------------------------------
90// sampleToDb
91//------------------------------------------------------------------------
92template<typename SampleType>
93inline double sampleToDb(SampleType valueInSample)
94{
95 return std::log10(valueInSample) * 20.0;
96}
97
98}
99}
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:46
bool isSilent(Sample32 value)
Definition AudioUtils.h:63
constexpr Sample64 getSampleSilentThreshold< Sample64 >() noexcept
Definition AudioUtils.h:58
SampleType dbToSample(double valueInDb)
Definition AudioUtils.h:84
constexpr Sample32 Sample32SilentThreshold
Definition AudioUtils.h:49
constexpr Sample64 Sample64SilentThreshold
Definition AudioUtils.h:50
double sampleToDb(SampleType valueInSample)
Definition AudioUtils.h:93
constexpr T bitClear(T a, int bit)
Clears bit bit in a.
Definition AudioUtils.h:42
constexpr T bitSet(T a, int bit)
Sets bit bit in a.
Definition AudioUtils.h:38
Definition Clock.h:23