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